Specifications

Appendix E 603
Complex Operation Programs
Complex Operation Programs
E. Complex Operation
Program
`
` Dividing Complex
`
Public Function complex_div(a As Complex, b As Complex) As Complex
Dim c As Complex
Dim de As Double
de = b.Re ^ 2 - b.Im ^ 2
c.Re = (a.Re * b.Re + a.Im * b.Im) / de
c.Im = (a.Im * b.Re - a.Re * b.Im) / de
complex_div = c
End Function
Sample Implementation in HTBasic
The following is a sample program in HTBasic that performs addition, subtraction,
multiplication, and division operations on complexes. The variables used in the program
are as follows.
A(*), B(*) Array to be operated. The index of this array starts with 1.
C(*) Array in which operation results are stored. The index of this array
starts with 1.
Nop The upper limit of the index value into the array.
Example E-2 Example of Complex Operation Program in HTBasic
10 !
20 ! Adding Complex Arrays
30 !
40 SUB Complex_add(A(*),B(*),C(*),Nop)
50 INTEGER I,J
60 FOR I=1 TO Nop
70 FOR J=1 TO 2
80 C(I,J)=A(I,J)+B(I,J)
90 NEXT J
100 NEXT I
110 SUBEND
120 !
130 ! Substracting Complex Arrays
140 !
150 SUB Complex_sub(A(*),B(*),C(*),Nop)
160 INTEGER I,J
170 FOR I=1 TO Nop
180 FOR J=1 TO 2
190 C(I,J)=A(I,J)-B(I,J)
200 NEXT J
210 NEXT I
220 SUBEND
230 !
240 ! Multiplying Complex Arrays
250 !
260 SUB Complex_mul(A(*),B(*),C(*),Nop)
270 INTEGER I
280 FOR I=1 TO Nop
290 C(I,1)=A(I,1)*B(I,1)-A(I,2)*B(I,2)
300 C(I,2)=A(I,1)*B(I,2)+A(I,2)*B(I,1)
310 NEXT I