The procedure for requesting AWR support has changed. Please read all about the new AWR product support process.
##### Page tree
Skip to end of banner
Go to start of banner

# Scripting How-To: Performing Complex Arithmetic in AWR Scripting

Skip to end of metadata
Go to start of metadata

### Summary

This document explains how to perform complex arithmetic in AWR Scripting (VB).

VB does not handle complex arithmetic by default so we have to write our own functions. This becomes much more intuitive if we also use the VB "Type" definition capability to create a new data type, "Complex". Below are functions defined for many different complex number operations including some matrix operations with complex numbers. In addition to the functions there is code that tests the functions. The functions include:

• Addition of complex numers

• Subtraction of complex numbers

• Multiplication of complex numbers

• Division of complex numbers

• Complex absolute value

• Complex inversion

• Real of a complex number

• Imaginary of a complex number

• Complex Matrix Addition

• Complex Matrix Subtraction

• Complex Matrix Inversion

### Code Snippets

``` Public Type Complex
re As Double
im As Double
End Type
' Complex Number Addition function
Function Sum(z1 As Complex, z2 As Complex) As
Complex
Dim z As Complex
z.re = z1.re + z2.re
z.im = z1.im + z2.im
Sum = z
End Function

' Complex Number Substraction function
Function Dif(z1 As Complex, z2 As Complex) As
Complex
Dim z As Complex
z.re = z1.re - z2.re
z.im = z1.im - z2.im
Dif = z
End Function

' Complex Number Multiplication function
Function Mul(sign As Integer, z1 As Complex, z2 As Complex) As
Complex
Dim z As Complex
z.re = sign*(z1.re*z2.re - z1.im*z2.im)
z.im = sign*(z1.re*z2.im + z2.re*z1.im)
Mul = z
End Function

' Complex Number Division function
Function Div(z1 As Complex, z2 As Complex) As
Complex
Dim z As Complex
Dim r As Double
r = z2.re^2+z2.im^2
z.re = (z1.re*z2.re + z1.im*z2.im)/r
z.im = (z2.re*z1.im - z1.re*z2.im)/r
Div = z
End Function

' Complex Number norm function
Function Cabs(z1 As Complex) As Double
Cabs = (z1.re*z1.re + z1.im*z1.im)^0.5
End Function

' Complex Number Inversion function
Function Inv(z1 As Complex) As Complex
Dim z As Complex
Dim Ident As Complex
Ident.re = 1
Ident.im = 0
z = Div(Ident, z1)
Inv = z
End Function

' Complex Real function
Function Real(z1 As Complex) As Double
Dim z As Complex
Real = z.re
End Function

' Complex Imag function
Function Imag(z1 As Complex) As Double
Dim z As Complex
Imag = z.im
End Function

' Complex Number Matrix Addition function
Function MSum(m1() As Complex, m2() As Complex) As
Complex()
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim M() As Complex
n = UBound(m1,1)
ReDim M(n,n)
For i=1 To n
For j=1 To n
M(i,j).re = m1(i,j).re + m2(i,j).re
M(i,j).im = m1(i,j).im + m2(i,j).im
Debug.Print "" & M(i,j).re & " | " & M(i,j).im
& "j"
Next j
Next i
MSum=M
End Function

' Complex Number Matrix Substraction
function
Function MDif(m1() As Complex, m2() As Complex) As
Complex()
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim M() As Complex
n = UBound(m1,1)
ReDim M(n,n)
For i=1 To n
For j=1 To n
M(i,j).re = m1(i,j).re - m2(i,j).re
M(i,j).im = m1(i,j).im - m2(i,j).im
Debug.Print "" & M(i,j).re & " | " & M(i,j).im
& "j"
Next j
Next i
MDif=M
End Function

' Complex Number Matrix Inversion function
Function MInv(m1() As Complex) As Complex()
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim M() As Complex
n = UBound(m1,1)
ReDim M(n,n)
Dim det As Complex
Dim Ident As Complex
Ident.re=1
Ident.im=0
det =
Dif(Mul(1,m1(1,1),m1(2,2)),Mul(1,m1(1,2),m1(2,1)))
M(1,1) = Div(m1(2,2), det)
M(1,2) = Div(Mul(-1,Ident,m1(1,2)),det)
M(2,1) = Div(Mul(-1,Ident,m1(2,1)),det)
M(2,2) = Div(m1(1,1), det)
For i=1 To n
For j=1 To n
Debug.Print "" & Format(M(i,j).re, "Scientific") & "
| " & Format(M(i,j).im, "Scientific") & "j"
Next j
Next i
MInv=M
End Function
Sub Main()

'TEST OF THE FUNCTIONS
Debug.Clear
'Complex Number Decleration
Dim Result As Complex
Dim z1 As Complex
Dim z2 As Complex

z1.re=1
z1.im=2
z2.re=1
z2.im=3

' Test Sum of Complex Number
Result = Sum(z1, z2)
Debug.Print "Sum " & Result.re & " | " &
Result.im & "j"

' Test Dif of Complex Number
Result = Dif(z1, z2)
Debug.Print "Dif " & Result.re & " | " &
Result.im & "j"

' Test Mul of Complex Number
Result = Mul(1, z1, z2)
Debug.Print "Mul " & Result.re & " | " &
Result.im & "j"

' Test Div of Complex Number
Result = Div(z1, z2)
Debug.Print "Div " & Result.re & " | " &
Result.im & "j"

' Test Inv of Complex Number
Result = Inv(z1)
Debug.Print "Inv " & Result.re & " | " &
Result.im & "j"

' Complex Number Matrix Declaration
Dim SResult() As Complex
Dim n1(2,2) As Complex
Dim n2(2,2) As Complex

n1(1,1).re=3
n1(1,1).im=2
n1(1,2).re=1
n1(1,2).im=2
n1(2,1).re=2
n1(2,1).im=1
n1(2,2).re=1
n1(2,2).im=3

n2(1,1).re=1
n2(1,1).im=1
n2(1,2).re=2
n2(1,2).im=2
n2(2,1).re=3
n2(2,1).im=3
n2(2,2).re=4
n2(2,2).im=4

' Test Sum Matrix
SResult = MSum(n1,n2)

' Test Inv Matrix
SResult = MInv(n1)

End Sub```

` `