Serving the Quantitative Finance Community

 
User avatar
AppleNY
Topic Author
Posts: 0
Joined: May 26th, 2006, 4:14 pm

Complex numbers with VBA

June 17th, 2006, 3:12 pm

Hello, Could somebody pls help me. I need to write a program in VBA which includes complex numbers. How to use complex numbers in VBA? and in Excel?Thanks
 
User avatar
PaperCut
Posts: 0
Joined: May 14th, 2004, 6:45 pm

Complex numbers with VBA

June 18th, 2006, 2:23 am

I found a code snippet here.Seems to do the trick.If you are doing anything crazy like Heston, you are better off using some industrial strength tool like MATLAB, Mathematica, or even writing stuff in FORTRAN. Then, port your solution back to VB afterwards.
 
User avatar
PaperCut
Posts: 0
Joined: May 14th, 2004, 6:45 pm

Complex numbers with VBA

June 18th, 2006, 2:47 am

For posterity:Option ExplicitConst pi = 3.14159265358979Type Complex re As Double im As DoubleEnd TypePublic Function AddComplex(a As Complex, b As Complex) As ComplexAddComplex.re = a.re + b.reAddComplex.im = a.im + b.imEnd FunctionPublic Function MultiplyComplex(a As Complex, b As Complex) As ComplexMultiplyComplex.re = a.re * b.re - a.im * b.imMultiplyComplex.im = a.re * b.im + a.im * b.reEnd FunctionPublic Function Conjugate(a As Complex) As ComplexConjugate.re = a.reConjugate.im = -a.imEnd FunctionPublic Function Modulo(a As Complex) As DoubleModulo = Sqr(a.re ^ 2 + a.im ^ 2)End FunctionPublic Function Argument(a As Complex) As DoubleDim i As IntegerDim v(1 To 4) As DoubleIf (z.re = 0) And (z.im = 0) Then Argument = -10 End FunctionEnd Ifv(1) = ArcSin(z.im / Modulo(z))v(2) = mcPI - v(1)v(3) = ArcCos(z.re / Modulo(z))v(4) = -1 * v(3)For i = 1 To 4 While v(i) > mcPI v(i) = v(i) - 2 * mcPI Wend While v(i) < mcPI v(i) = v(i) + 2 * mcPI WendNext iIf v(1) = v(3) Then Argument = v(1)If v(2) = v(3) Then Argument = v(2)If v(1) = v(4) Then Argument = v(1)If v(2) = v(4) Then Argument = v(2)End Function' Code by : Steven Roland Bazinet (ArcSin function only)Private Function ArcSin(vntSine As Variant) As DoubleOn Error GoTo ERROR_ArcSineConst cOVERFLOW = 6Dim blnEditPassed As BooleanDim dblTemp As Double blnEditPassed = False If IsNumeric(vntSine) Then If vntSine >= -1 And vntSine <= 1 Then blnEditPassed = True dblTemp = Sqr(-vntSine * vntSine + 1) If dblTemp = 0 Then ArcSin = Sgn(vntSine) * pi / 2 Else ArcSin = Atn(vntSine / dblTemp) End If End If End IfEXIT__ArcSine: If Not blnEditPassed Then Err.Raise cOVERFLOW Exit FunctionERROR_ArcSine: On Error GoTo 0 blnEditPassed = False Resume EXIT__ArcSineEnd Function' Code by : Me, based (very much!) on Steven R Bazinet's codePrivate Function ArcCos(vntcos As Variant) As DoubleOn Error GoTo ERROR_ArcSineConst cOVERFLOW = 6Dim blnEditPassed As BooleanDim dblTemp As Double blnEditPassed = False If IsNumeric(vntcos) Then If vntcos >= -1 And vntcos <= 1 Then blnEditPassed = True dblTemp = Sqr(-vntcos * vntcos + 1) If dblTemp = 0 Then ArcCos = Sgn(vntcos) * pi / 2 Else ArcCos = Atn(dblTemp / vntcos) End If End If End IfEXIT__ArcCos: If Not blnEditPassed Then Err.Raise cOVERFLOW Exit FunctionERROR_ArcCos: On Error GoTo 0 blnEditPassed = False Resume EXIT__ArcSineEnd Function