unit complexMath; { complexMath } { Copyright © 1991 by Michael J. Gibbs, all rights reserved. } { } { This unit provides basic arithmetic functions for complex numbers and a } { type declaration for them. } { } { 9109091318 M. Gibbs: initial release } { } { Complex math functions ------------------------------------------------- } { } { addComplex - Adds the complex numbers 'a' and 'b', returning the sum } { as the function result. } { subComplex - Subtracts the complex number 'b' from 'a', returning the } { difference as the function result. } { mulComplex - Multiplies the complex numbers 'a' and 'b', returning the } { product as the function result. } { divComplex - Divides the complex number 'a' by 'b', returning the } { quotient as the function result. } { } { Complex number conversions --------------------------------------------- } { } { imag2Polar - Converts the complex number 'value' from real+imaginary } { to polar coordinates as the function result. } { polar2Imag - Converts the polar coordinate number 'value' to real+ } { imaginary form as the function result. } interface type complex = record realPart, imagPart: extended; end; { complex math } function addComplex (a, b: complex): complex; function subComplex (a, b: complex): complex; function mulComplex (a, b: complex): complex; function divComplex (a, b: complex): complex; { complex conversions } function imag2Polar (value: complex): complex; function polar2Imag (value: complex): complex; implementation function addComplex (a, b: complex): complex; begin addComplex.realPart := a.realPart + b.realPart; addComplex.imagPart := a.imagPart + b.imagPart; end; function subComplex (a, b: complex): complex; begin subComplex.realPart := a.realPart - b.realPart; subComplex.imagPart := a.imagPart - b.imagPart; end; function mulComplex (a, b: complex): complex; begin mulComplex.realPart := a.realPart * b.realPart - a.imagPart * b.imagPart; mulComplex.imagPart := a.realPart * b.imagPart + a.imagPart * b.realPart; end; function divComplex (a, b: complex): complex; var divisor: extended; conjugate, temp: complex; begin conjugate := b; conjugate.imagPart := -1 * conjugate.imagPart; temp := mulComplex(b, conjugate); divisor := temp.realPart; temp := mulComplex(a, conjugate); divComplex.realPart := temp.realPart / divisor; divComplex.imagPart := temp.imagPart / divisor; end; function imag2Polar (value: complex): complex; begin imag2Polar.imagPart := arctan(value.imagPart / value.realPart); imag2Polar.realPart := value.realPart / cos(value.imagPart); end; function polar2Imag (value: complex): complex; begin polar2Imag.realPart := value.realPart * cos(value.imagPart); polar2Imag.imagPart := value.realPart * sin(value.imagPart); end; end.