Page 1 of 1

[Solved] Use complex numbers in VBA

Posted: Sun Dec 24, 2023 12:06 am
by Ricky
anyone has seen any VBA macro to do some complex numbers operations
+ 1 X / in polar or rectangular form ?


thanks for any feedback

Re: VBA complex numbers ?

Posted: Sun Dec 24, 2023 1:23 am
by Lupp
 Edit: The topic was meanwhile moved to the branch "Macros and UNO API" by an administrator.
Lupp about 2023-12-24 13:10 UTC 
Sorry. This is the Calc branch of a forum about successors of OO.o
There is no VBA in OO.o, AOO, LibO, and there is no VBA in Calc.
The representation of complex numbers in Calc is queer, and the ODF specification doesn't make any hope.
You may, of course, create UserDefinedFunctions starting from scratch or using the few Calc standard functions representing complex numbers as strings.
Concerning a polar representation there is no specification at all. You would need to use strings again, and to start with a syntax definition.

First of all you should probably describe to more detail what you actually want/need.

Re: VBA complex numbers

Posted: Sun Dec 24, 2023 9:06 am
by MrProgrammer
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
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.

Re: VBA complex numbers

Posted: Sun Dec 24, 2023 11:27 am
by Hagar Delest
Moved to the Macro forum.

Re: VBA complex numbers

Posted: Sun Dec 24, 2023 2:52 pm
by Lupp
Let me also remind you of the fact that Calc functions are availabe in AOO Basic via the service

Code: Select all

com.sun.star.sheet.FunctionAccess
This is common heritage of all the descendants of StarOffice / OO.o.
You may also want to throw a glance on the "Code Snippet" I published in viewtopic.php?t=109481 (It does not include enhancements only available in LibO.)
Calc comes with a lot of (25?) functions with names starting with "IM", and with the function COMPLEX(). All these are made for returning or working with "Complex Numbers" in the strange textual representation (Gaussean-based: Re + Im i - no spaces).
See
aoo111006ComplexNumberFunctionsDemo.ods
(16.31 KiB) Downloaded 427 times
However, these are the functions also easily implemented by user code. The actually needed functions for complex matrices do not exist there. If you are specifically interested in solving systems of equations with complex coefficients (as occurring in electrical engineering), please ask a new question. (Like "How to invert a square matrix with complex elements in Calc?")