anyone has seen any VBA macro to do some complex numbers operations
+ 1 X / in polar or rectangular form ?
thanks for any feedback
[Solved] Use complex numbers in VBA
[Solved] Use complex numbers in VBA
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
Reason: Tagged ✓ [Solved] Q: Complex numbers in macro? A: Examples provided -- MrProgrammer, forum moderator
Openoffice 3.1 Win 10 64 bits
Re: VBA complex numbers ?
Edit: The topic was meanwhile moved to the branch "Macros and UNO API" by an administrator. Lupp about 2023-12-24 13:10 UTC |
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
---
Lupp from München
- MrProgrammer
- Moderator
- Posts: 4927
- Joined: Fri Jun 04, 2010 7:57 pm
- Location: Wisconsin, USA
Re: VBA complex numbers
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
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).
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).
- Hagar Delest
- Moderator
- Posts: 32696
- Joined: Sun Oct 07, 2007 9:07 pm
- Location: France
Re: VBA complex numbers
Moved to the Macro forum.
LibreOffice 24.2 on Xubuntu 24.04 and 7.6.4.1 portable on Windows 10
Re: VBA complex numbers
Let me also remind you of the fact that Calc functions are availabe in AOO Basic via the service
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 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?")
Code: Select all
com.sun.star.sheet.FunctionAccess
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 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
---
Lupp from München