Thanks for the two suggestions from Neuro Fuzzy. If there are not alot of complex number calcs then simply multiplying out the calculation would certainly be more efficient and another global variable may not be needed. It would also be nice to have a general equation evaluator library using a string argument for the equation and value arguments. It would come in handy for calculator applications.
Here is the code with all local variables and no functions.
REM *** Library Main Test ***
x as Complex
y as Complex
z as Complex
x.Re = 1.0: x.Im = 2.0
y.Re = 3.0: y.Im = 4.0
Print "Complex Number Math"
PrintCNum(x,"x")
PrintCNum(y,"y")
'Complex addition
z.Re = x.Re + y.Re
z.Im = x.Im + y.Im
PrintCNum(z,"x + y")
'Complex subtraction
z.Re = x.Re - y.Re
z.Im = x.Im - y.Im
PrintCNum(z,"x - y")
'Complex multiplication
z.re = x.re*y.re - x.im*y.im
z.im = x.re*y.im + x.im*y.re
PrintCNum(z,"x * y")
'Complex Division
'1 - Find the denominator
CDen as Double Float
CDen = y.re^2 + y.im^2
'2 - Find the numerator
z.re = x.re*y.re - x.im*(-y.im)
z.im = x.re*(-y.im) + x.im*y.re
'3 - Divide the numerator by the denominator
z.re = z.re/CDen
z.im = z.im/Cden
PrintCNum(z,"x / y")
'Complex Matrix Math
'Init two complex matrices (2x2)
Dim a(2,2) as Complex
Dim b(2,2) as Complex
Dim c(2,2) as Complex
Print
Print "Complex Matrix Math"
Print "a(1,1)=a(1,2)=a(2,1)=a(2,2)=1+j2"
Print "b(1,1)=b(1,2)=b(2,1)=b(2,2)=3+j4"
For i=1 to 2
For j = 1 to 2
a(i,j).Re = 1.0
a(i,j).Im = 2.0
b(i,j).Re = 3.0
b(i,j).Im = 4.0
Next j
Next i
'Complex Matrix Addition
Print "a + b ="
For i=1 to 2
For j=1 to 2
c(i,j).Re = a(i,j).Re + b(i,j).Re
c(i,j).Im = a(i,j).Im + b(i,j).Im
print c(i,j).Re," +j ",c(i,j).Im," ";
Next j
print
Next i
'Complex Matrix Subtraction
Print "a - b ="
For i=1 to 2
For j=1 to 2
c(i,j).Re = a(i,j).Re - b(i,j).Re
c(i,j).Im = a(i,j).Im - b(i,j).Im
print c(i,j).Re," +j ",c(i,j).Im," ";
Next j
Print
Next i
'Complex Matrix Multiplication
Print "a * b ="
For i=1 to 2
For j=1 to 2
c(i,j).Re = a(i,j).Re*b(i,j).Re - a(i,j).Im*b(i,j).Im
c(i,j).Im = a(i,j).Im*b(i,j).Re + a(i,j).Re*b(i,j).Im
print c(i,j).Re," +j ",c(i,j).Im," ";
Next j
Print
Next i
Wait Key
Wait Key
End
Type Complex
Re as Double Float
Im as Double Float
EndType 'Complex
Function PrintCNum(CNum as Complex, CNumName$)
If CNum.Im < 0
print CNumName$, " = ", CNum.Re, "-j ", -CNum.Im
Else
print CNumName$, " = ", CNum.Re, "+j ", CNum.Im
EndIf
EndFunction
Thanks also to Sven B for pointing out that vector functions are available that can be used to perform complex number math so a new library may not be necessary. One question: How do you make an array of vectors to create a complex number matrix?
Rem ***** Main Source File *****
REM *** Library Main Test ***
#Constant CNum1 1
#Constant CNum2 2
#Constant CNum3 3
result = Make Vector2(CNum1)
result = Make Vector2(CNum2)
result = Make Vector2(CNum3)
Set Vector2 CNum1,1.0,2.0
Set Vector2 CNum2,3.0,4.0
PrintVector2(CNum1,"CNum1")
PrintVector2(CNum2,"CNum2")
'Complex addition
Add Vector2 CNum3,CNum1,CNum2
PrintVector2(CNum3,"CNum1 + CNum2 = ")
'Complex subtraction
Subtract Vector2 CNum3,CNum2,CNum1
PrintVector2(CNum3,"CNum2 - CNum1 = ")
'Complex multiplication
Set Vector2 CNum3, X Vector2(CNum1)*X Vector2(CNum2)-Y Vector2(CNum1)*Y Vector2(CNum2),_
X Vector2(CNum1)*Y Vector2(CNum2)+Y Vector2(CNum1)*X Vector2(CNum2)
PrintVector2(CNum3,"CNum1 * CNum2 = ")
'Complex division (CNum1/CNum2)
'1 - Find the denominator
CDen = Squared Length Vector2(CNum2)
'2 - Find the numerator (CNum1 * Conj(CNum2))
Set Vector2 CNum3, X Vector2(CNum1)*X Vector2(CNum2)-Y Vector2(CNum1)*(-(Y Vector2(CNum2))),_
X Vector2(CNum1)*(-(Y Vector2(CNum2)))+Y Vector2(CNum1)*X Vector2(CNum2)
'3 - Divide the numerator by the denominator
Divide Vector2 CNum3,CDen
PrintVector2(CNum3,"CNum1 / CNum2 = ")
Wait Key
End
Function PrintVector2(VectorNum, VectorName$)
print VectorName$, " = ", X Vector2(VectorNum), "+j ", Y Vector2(VectorNum)
EndFunction
Life is not a dream