Ricky wrote: ↑Sun Dec 24, 2023 12:06 am
macro to do some complex numbers operations
OpenOffice Basic does not provide support for complex numbers, but you can create it yourself. This program prints the sum, difference, product, and quotient of 3+2i and 1-2i. Details about
complex number calculations can be found with a web search. An OpenOffice forum is not the right place to ask questions about the mathematics of complex number arithmetic like
How do I calculate the cosine of a complex number.
Type Complex
Real As Double
Imag As Double
End Type
Sub ComplexTest
Dim D As Complex : Dim E As Complex : Dim Z As Complex
Dim M As String : Const F = "0.00"
D.Real = 3 : D.Imag = 2 : E.Real = 1 : E.Imag = -2
Z = ComplexAdd(D,E)
M = ComplexFormat(D,F) & " + " & ComplexFormat(E,F) _
& " = " & ComplexFormat(Z,F) & CHR(10)
Z = ComplexSub(D,E)
M = M & ComplexFormat(D,F) & " - " & ComplexFormat(E,F) _
& " = " & ComplexFormat(Z,F) & CHR(10)
Z = ComplexMul(D,E)
M = M & ComplexFormat(D,F) & " * " & ComplexFormat(E,F) _
& " = " & ComplexFormat(Z,F) & CHR(10)
Z = ComplexDiv(D,E)
M = M & ComplexFormat(D,F) & " / " & ComplexFormat(E,F) _
& " = " & ComplexFormat(Z,F)
MSGBOX(M)
End Sub
Function ComplexAdd(P As Complex,Q As Complex) As Complex
Dim Z As Complex
Z.Real = P.Real+Q.Real
Z.Imag = P.Imag+Q.Imag
ComplexAdd = Z
End Function
Function ComplexSub(P As Complex,Q As Complex) As Complex
Dim Z As Complex
Z.Real = P.Real-Q.Real
Z.Imag = P.Imag-Q.Imag
ComplexSub = Z
End Function
Function ComplexMul(P As Complex,Q As Complex) As Complex
Dim Z As Complex
Z.Real = P.Real*Q.Real-P.Imag*Q.Imag
Z.Imag = P.Imag*Q.Real+P.Real*Q.Imag
ComplexMul = Z
End Function
Function ComplexDiv(P As Complex,Q As Complex) As Complex
Dim Z As Complex : Dim Y As Double
Y = Q.Real*Q.Real+Q.Imag*Q.Imag
Z.Real = (P.Real*Q.Real+P.Imag*Q.Imag)/Y
Z.Imag = (P.Imag*Q.Real-P.Real*Q.Imag)/Y
ComplexDiv = Z
End Function
Function ComplexFormat(Number As Complex,Pattern As String) As String
ComplexFormat = IIF(Number.Real>=0,"+","") & FORMAT(Number.Real,Pattern) _
& IIF(Number.Imag>=0,"+","") & FORMAT(Number.Imag,Pattern) & "i"
End Function

- Output from MSGBOX
- 202312240052.jpg (68.93 KiB) Viewed 3191 times
Ricky wrote: ↑Sun Dec 24, 2023 12:06 am
… in polar form …
You can also write Basic programs to work with complex numbers in polar coordinates.
Type Polar
Magnitude As Double
Argument As Double
End Type
Function PolarMul(P As Polar,Q As Polar) As Polar
…
End Function
If this solved your problem please go to your first post use the Edit ☐ button and add [Solved] to the start of the Subject field. Select the green checkmark icon at the same time.