[Solved] Use complex numbers in VBA

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
Ricky
Posts: 14
Joined: Sun Nov 12, 2017 11:39 pm

[Solved] Use complex numbers in VBA

Post 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
Last edited by MrProgrammer on Mon Jan 01, 2024 8:19 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] Q: Complex numbers in macro? A: Examples provided -- MrProgrammer, forum moderator
Openoffice 3.1 Win 10 64 bits
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: VBA complex numbers ?

Post 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.
Last edited by Lupp on Sun Dec 24, 2023 3:13 pm, edited 3 times in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
MrProgrammer
Moderator
Posts: 4909
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: VBA complex numbers

Post 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 2373 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.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: VBA complex numbers

Post by Hagar Delest »

Moved to the Macro forum.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: VBA complex numbers

Post 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 333 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?")
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Locked