complex number

J

John Vinson

Hi,
How can I add complex number in forms.

Access doesn't support complex numbers (e.g. x + iy) natively; you'll
need to store the value in two number fields, and write your own VBA
code to add, subtract, multiply and divide them.

If you have some other meaning for the term "complex number" please
explain.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
S

StCyrM

Hi Alain

Here's a group of functions that should be useful to you.

Best regards

Maurice St-Cyr




------------------- Code Start -----
Option Compare Database 'Use database order for string comparisons
Option Explicit

Type Complex
i As Double
r As Double
End Type

Type Polar
z As Double ' Magnitude
Th As Double ' Theta - angle
End Type

Sub Complex2Polar(a As Complex, Result As Polar)
'
' Converts a Complex number into Polar form
'
Result.z = IMMag(a)
Result.Th = ATan2(a.r, a.i) ' From Trig module
End Sub

Sub IMAdd(a As Complex, b As Complex, Result As Complex)
'
' Adds 2 complex numbers
'
Result.i = a.i + b.i
Result.r = a.r + b.r
End Sub

Sub IMConjugate(a As Complex, Result As Complex)
'
' Returns the Conjugate of A
'
Result.r = a.r
Result.i = -a.i
End Sub

Sub IMDiv(a As Complex, b As Complex, Result As Complex)
'
' Divides A by B
'
Dim B1 As Complex
IMInverse b, B1
IMProduct a, B1, Result
End Sub

Sub IMExp(a As Complex, Result As Complex)
'
' Does e^A where A is a complex number
'
Dim A1 As Complex, z As Double
A1 = a
z = Exp(A1.r)
Result.r = z * Cos(A1.i)
Result.i = z * Sin(A1.i)
End Sub

Function IMFormat(a As Complex) As String
'
' Formats a Complex number into a string
'
If a.i >= 0 Then
IMFormat = a.r & " + " & a.i & "i"
Else
IMFormat = a.r & " - " & Abs(a.i) & "i"
End If
End Function

Sub IMInverse(a As Complex, Result As Complex)
'
' Finds the inverse value of A, such that A * Result = 1
'
Dim Temp As Double
Temp = a.r * a.r + a.i * a.i
Result.r = a.r / Temp
Result.i = -a.i / Temp
End Sub

Sub IMln(a As Complex, Result As Complex)
'
' Returns natural Logarithm of A
'
Dim A1 As Complex
A1 = a
Result.r = Log(IMMag(A1))
Result.i = ATan2(A1.r, A1.i)
End Sub

Sub IMLog10(a As Complex, Result As Complex)
'
' Takes Log A base 10
'
IMln a, Result
IMRProduct Result, Log10(Exp(1)), Result
End Sub

Sub IMLog2(a As Complex, Result As Complex)
'
' Takes Log A base 2
'
IMln a, Result
IMRProduct Result, Log2(Exp(1)), Result
End Sub

Function IMMag(a As Complex) As Double
'
' Returns Magnitude of Complex number
'
IMMag = Sqr(a.r * a.r + a.i * a.i)
End Function

Function IMPFormat(a As Polar) As String
'
' Formats a Polar number into a string
'
If a.Th >= 0 Then
IMPFormat = a.z & " + " & a.Th & "Th"
Else
IMPFormat = a.z & " - " & Abs(a.Th) & "Th"
End If
End Function

Sub IMPower(a As Complex, b As Complex, Result As Complex)
'
' A^B where both are complex numbers
'
Dim Temp1 As Complex, Temp2 As Complex
IMln a, Temp1
IMProduct Temp1, b, Temp2
IMExp Temp2, Result
End Sub

Sub IMProduct(a As Complex, b As Complex, Result As Complex)
'
' Multiples 2 complex numbers
'
Dim A1 As Complex, B1 As Complex ' Prevents problems if do IMult A, A,
A
A1 = a
B1 = b
Result.i = A1.i * B1.r + A1.r * B1.i
Result.r = A1.r * B1.r - A1.i * B1.i
End Sub

Sub IMRDiv(a As Complex, b As Double, Result As Complex)
'
' Divides A by the scalar value B
'
Result.r = a.r / b
Result.i = a.i / b
End Sub

Sub IMRPower(a As Complex, b As Double, Result As Complex)
'
' Does A^B where A in a Complex number and B is Real number
'
Dim APolar As Polar
Complex2Polar a, APolar
APolar.z = APolar.z ^ b
APolar.Th = APolar.Th * b
Polar2Complex APolar, Result
End Sub

Sub IMRProduct(a As Complex, b As Double, Result As Complex)
'
' Multiplies A by the scalar value B
'
Result.r = a.r * b
Result.i = a.i * b
End Sub

Sub IMSqr(a As Complex, Result As Complex)
'
' Takes the square root of a complex number
'
Dim A1 As Polar
Complex2Polar a, A1
A1.z = Sqr(A1.z)
A1.Th = A1.Th / 2
Polar2Complex A1, Result
End Sub

Sub IMSub(a As Complex, b As Complex, Result As Complex)
'
' Subtracts 2 complex numbers
'
Result.i = a.i - b.i
Result.r = a.r - b.r
End Sub

Sub IMTest()
'
' This procedure tests all the Complex Math functions and outputs results to
the Debug Window.
'
Dim a As Complex, b As Complex, C As Complex, D As Complex, E As Complex, P As
Polar
a.r = 3: a.i = 4
b.r = 5: b.i = -3
Debug.Print "A", IMFormat(a)
Debug.Print "B", IMFormat(b)
Complex2Polar a, P
Polar2Complex P, D
Debug.Print "A -> P -> A", IMPFormat(P), IMFormat(D)
IMInverse a, C
IMProduct a, C, D
Debug.Print "Inverse A", IMFormat(C), IMFormat(D)
IMConjugate a, C
Debug.Print "Conjugate", IMFormat(C)
Debug.Print "Magnitude", IMMag(a)
IMAdd a, b, C
IMSub C, b, D
Debug.Print "A+B,-B", IMFormat(C), IMFormat(D)
IMProduct a, b, C
IMDiv C, b, D
Debug.Print "A*B,/B", IMFormat(C), IMFormat(D)
IMRProduct a, 5, C
IMRDiv C, 5, D
Debug.Print "A*5,/5", IMFormat(C), IMFormat(D)
IMSqr a, C
IMRPower C, 2, D
Debug.Print "Sqr(A),^2", IMFormat(C), IMFormat(D)
IMln a, C
IMExp C, D
Debug.Print "Ln A,e^", IMFormat(C), IMFormat(D)
IMLog10 a, C
D.r = 10: D.i = 0
IMPower D, C, E
Debug.Print "Log10 A,10^", IMFormat(C), IMFormat(E)
IMLog2 a, C
D.r = 2: D.i = 0
IMPower D, C, E
Debug.Print "Log2 A,2^", IMFormat(C), IMFormat(E)
IMPower a, b, C
IMInverse b, D
IMPower C, D, E
Debug.Print "A^B,^(1/B)", IMFormat(C), IMFormat(E)
End Sub

Sub Polar2Complex(a As Polar, Result As Complex)
'
' Converts a Polar form into Complex number
'
Result.r = a.z * Cos(a.Th)
Result.i = a.z * Sin(a.Th)
End Sub

----------------- Code end ------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top