home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
pas_nl
/
shcmplx
/
shcmplx.doc
next >
Wrap
Text File
|
1991-03-08
|
13KB
|
597 lines
ShCmplx
A Complex Arithmetic Unit for Turbo Pascal 5.0 and above.
by
W. G. Madison and Associates, Ltd.
8425 Greenbelt Road
P.O. Box 898
Greenbelt, MD 20770
(301)552-7234
CIS 73240,342
Copyright 1991 Madison & Associates
All Rights Reserved
This unit and the associated .DOC and TEST*.* files may be
freely copied and distributed, provided only that no fee is
charged for the package beyond a nominal copying charge, and
provided that the package is distributed IN UNALTERED FORM.
The sole exception to this latter restriction is that bona-
fide clubs and user groups may append text material to the
documentation file, provided that any material appended is
clearly identified as to its source, its beginning, and its
end.
INTRODUCTION
ShCmplx is a complex arithmetic unit for Turbo Pascal, version 5
or higher. It provides for the basic binary arithmetic functions
(addition, subtraction, multiplication, division), plus the raising of
a complex number to a real power. Conversion functions are provided to
convert between a complex number represented in Cartesian form and one
represented in polar form. Finally, provision is made to convert a
complex number in Cartesian form to a string; or a complex in polar
form to a string expressing the angle either in degrees or in radians.
All of the described functionality is available as a suite of
Pascal functions. All except the string conversion routines are also
available as Pascal procedures.
"HEY, WAIT A MINUTE!" say you, sagely. "Complex numbers are
composed of two real numbers. Thus, a complex smells like it is
inherently a record structure. And Pascal functions can only return
simple data types - no structured types."
"You get a gold star on your forehead," say I. "But we're gonna
make use of the fact that Pascal functions can also return pointer
values."
What this means in practice is that type complex, in this
package, is not defined as
type
Complex = record
Re,
Im : ComplexElement;
end;
but, rather, as
type
ComplexBaseType = record
Re,
Im : ComplexElement;
end;
Complex = ^ComplexBaseType;
Thus, if we had an expression which we wished to evaluate - for
example:
A = A + B + C + D
this is able to be evaluated (where CAddF is the function name for
complex addition) as:
A^ := CAddF(CAddF(A, B), CAddF(C, D))^;
which is the equivalent of (with the pointers de-referenced)
A := ((A + B) + (C + D));
or
A^ := CAddF(A, CAddF(B, CAddF(C, D)))^;
which is the equivalent of (again, with the pointers de-referenced)
A := (A + (B + (C + D)));
and the result of this calculation can be displayed using either
WriteLn(Cmp2Str(A, 8, 4));
or
WriteLn(Cmp2Str(CAddF(CAddF(A, B), CAddF(C, D)), 8, 4));
where the "8, 4" in each case carry the same significance as the
parameters used in printing real quantities; specifically, the total
field width and number of places to the right of the decimal point (in
this case, for each numeric component).
Note that
A := CAddF(B, C);
- 2 -
is normally not what you would want. This would assign the returned
pointer value to the pointer variable 'A', rather than assigning the
complex value pointed to by the returned pointer to the element
pointed to by 'A', which you would get by writing
A^ := CAddF(B, C)^;
This type of error is, unfortunately, not detectable by the
Pascal run-time system. Also, I can't detect it (even at my diabolical
cleverest.) So you need to be careful that you don't do it to
yourself.
A test program (TESTCMPX) is included with the distribution set,
in both source and executable form. Study it for ideas and specifics
of how to use the various functions.
{===========================================================}
TYPES AND CONSTANTS
type
ComplexElement = extended;
ComplexBaseType = record
Re,
Im : ComplexElement;
end;
Complex = ^ComplexBaseType;
- 3 -
PROCEDURE AND FUNCTION CALLS
In the following, where EXAMPLES are not shown, they may be found
in the file TestCmpx.PAS.
_____________________________________
C2P
Declarations:
procedure C2P(A : Complex; var Result : Complex);
function C2PF(A : Complex) : Complex;
Purpose:
Transforms a complex in Cartesian form into polar form.
Examples:
CpPwrR(C2PF(MyCmpx), 3.0, MyCmpxCubedPolar);
Comments:
The magnitude will be stored in Result^.Re and the angle
(expressed in radians) in Result^.Im
See Also:
P2C
_____________________________________
CABS
Declarations:
procedure CAbs(A : Complex; var Result : ComplexElement);
function CAbsF(A : Complex) : ComplexElement;
Purpose:
Returns the absolute value of a complex number.
Examples:
∙∙∙
MyCmpx^.Re := 3.0;
MyCmpx^.Im := 4.0;
∙∙∙
WriteLn(CAbs(MyCmpx):4:1); {will display 5.0}
Comments:
See Also:
- 4 -
_____________________________________
CADD
Declarations:
procedure CAdd(A, B : Complex; var Result : Complex);
function CAddF(A, B : Complex) : Complex;
Purpose:
Returns the sum of two complex numbers A + B.
Examples:
Comments:
See Also:
CSub CMul CDiv
_____________________________________
CCONJ
Declarations:
procedure CConj(A : Complex; var Result : Complex);
function CConjF(A : Complex) : Complex;
Purpose:
Returns the complex conjugate of a complex number.
Examples:
Comments:
Recall that if a complex number A = R + Ii, then the complex
conjugate of A is R - Ii.
See Also:
- 5 -
_____________________________________
CDIV
Declarations:
procedure CDiv(A, B : Complex; var Result : Complex);
function CDivF(A, B : Complex) : Complex;
Purpose:
Returns the quotient of two complex numbers A / B.
Examples:
Comments:
CAbsF(B) must be > 0.0. If CAbsF(B) <= 0.0, CmplxError will
return -1.
See Also:
CAdd CMul CSub
_____________________________________
CMP2STR
Declarations:
function Cmp2Str(A : Complex; Width, Places : byte) : string;
Purpose:
Converts a complex value to a string of the form (Re + Im i).
Examples:
Comments:
The Width and Places arguments have the same significance that
they do in the system procedure STR, and are applied individually to
both the REAL and IMAGINARY parts.
See Also:
CmpP2Str CmpP2StrD
- 6 -
_____________________________________
CMPLXERROR
Declarations:
function CmplxError : integer;
Purpose:
Returns execution error conditions for the package.
Examples:
Comments:
Return = 0 OK
-1 Attempt to divide by zero (CDiv)
-2 Real part of complex is zero (CpPwrR)
See Also:
_____________________________________
CMPP2STR
Declarations:
function CmpP2Str(A : Complex; Width, Places : byte) : string;
Purpose:
Converts a complex in polar form to a string with the angle
expressed in radians.
Examples:
Comments:
The Width and Places arguments have the same significance that
they do in the system procedure STR, and are applied individually to
both the MAGNITUDE and ANGLE parts.
See Also:
Cmp2Str CmpP2StrD
- 7 -
_____________________________________
CMPP2STRD
Declarations:
function CmpP2StrD(A : Complex; Width, Places : byte) : string;
Purpose:
Converts a complex in polar form to a string with the angle in
degrees.
Examples:
Comments:
The Width and Places arguments have the same significance that
they do in the system procedure STR, and are applied individually to
both the MAGNITUDE and ANGLE parts.
See Also:
Cmp2Str CmpP2Str
_____________________________________
CMUL
Declarations:
procedure CMul(A, B : Complex; var Result : Complex);
function CMulF(A, B : Complex) : Complex;
Purpose:
Returns the product of two complex numbers A * B.
Example:
Comments:
See Also:
CAdd CDiv CSub RxC
- 8 -
_____________________________________
CPPWRR
Declarations:
procedure CpPwrR(A : Complex; R : ComplexElement;
var Result : Complex);
function CpPwrRF(A : Complex; R : ComplexElement) : Complex;
Purpose:
Raises a complex (in polar form) to a real power.
Examples:
Comments:
If A^.Re = 0, CmplxError will return -2 and Result will be 0 + 0i
See Also:
_____________________________________
CSUB
Declarations:
procedure CSub(A, B : Complex; var Result : Complex);
function CSubF(A, B : Complex) : Complex;
Purpose:
Returns the difference between two complex numbers A - B.
Examples:
Comments:
See Also:
CAdd CDiv CMul
- 9 -
_____________________________________
P2C
Declarations:
procedure P2C(A : Complex; var Result : Complex);
function P2CF(A : Complex) : Complex;
Purpose:
Transforms a complex in polar form into Cartesian form. The
magnitude is stored in A^.Re and the angle (expressed in radians) in
A^.Im.
Examples:
Comments:
See Also:
C2P
_____________________________________
RXC
Declarations:
procedure RxC(A : ComplexElement; B : Complex;
var Result : Complex);
function RxCF(A : ComplexElement; B : Complex) : Complex;
Purpose:
Returns the complex product of a real and a complex.
Examples:
Comments:
This is simply a special case of CMul, in which the imaginary
part of one of the factors is zero. It occurrs with sufficient
frequency, however, to warrent separate treatment.
See Also:
CMul
_____________________________________
- 10 -