[Home]
[Search]
[Contents]
This class implements complex numbers. Using overloaded functions and operators, you can treat complex numbers like you treat reals or integers. You can use the following:
#include <oldcomplex.h>You can declare a complex variable three ways: uninitialized, initialized with two numbers, or initialized with another complex number. For example:
complex z1; // Uninitialized complex z2(1.0,2.0); // Initialized with two numbers complex z3 = z2; // Initialized with a complex numberTo access components of a complex number, use member functions complex::real() and complex::imag(). For example:
complex z1, z2(5.0,6.0); z1.real() = z2.imag(); z1.imag() = z2.real();To print or read a complex number, use stream operators operator>> and operator<<. For example, this program:
#include <complex.h>
#include <iostream.h>
main()
{
complex z1(1.1, 2.0), z2;
cout << "Enter z2: ";
cin >> z2;
cout << "z1 = " << z1 << '\n';
cout << "z2 = " << z2 << '\n';
return 0;
}
prints this:
Enter z2: 10 9.8 z1 = (1.1, 2) z2 = (10,9.8)You can also use a complex number in the middle of a statement. For example, this line prints out the sine of (1, 1):
cout << sin(complex(1,1) );Note: To invoke the complex version of a function, one of its arguments must be complex. For example, to figure the square root of -1, you must use sqrt(complex(-1,0)).
#include <oldcomplex.h>
complex complexFunc()
{
// Invokes complex(double, double)
complex z1(2.0,4.0);
// Invokes complex(const complex&)
complex z2 = z1;
// Invokes complex(const complex&) to
// create a temporary variable and adds
// it to z1.
return complex(1.1, 2.2) + z1;
}
The arg function is equivalent to:
atan2( imag( x), real( x))
complex::imag, complex::real
Header
oldcomplex.h
Prototype
double& complex::imag();
double& complex::real();
Description
complex::imag returns the imaginary portion of a complex
number; complex::real returns the real portion. Either function
can be on the left side or right side of an assignment statement.
Example
For complex::imag For complex::real
#include <oldcomplex.h> #include <oldcomplex.h>
complex z1(1.0,2.0), z2; complex z1(1.0,2.0), z2;
z1.imag() = z2.imag(); z1.real() = z2.real();
conj
Header
oldcomplex.h
Prototype
complex conj(const complex& z);
Description
Returns the complex conjugate of a complex number. If the complex
number z is x+ iy, then conj(z) is x-iy.
imag
Header
oldcomplex.h
Prototype
double imag(const complex& z );
Description
Returns the imaginary portion of a complex number. It can be on
only the right side of an assignment statement.
modulus
Header
oldcomplex.h
Prototype
double modulus(const complex& z );
Description
Returns the absolute value of a complex number. The absolute value
of a complex number is also called the modulus. modulus(z) is
equal to abs(z), which is sqrt (x * x + y * y).
norm
Header
oldcomplex.h
Prototype
double norm(const complex& z);
Description
Returns the square of the absolute value of a complex number. For
z, of form (x + iy), conj(z) = (x * x + y * y).
polar
Header
oldcomplex.h
Prototype
complex polar(double range, double theta );
Description
Returns the complex number that is the result of converting polar
coordinates range and theta to x and y coordinates. This is equivalent to
using the formula:
complex(range * cos(theta), range * sin(theta));
real
Header
oldcomplex.h
Prototype
double real(const complex& z );
Description
Returns the real portion of a complex number. It can be on only the
right side of an assignment statement.
Trigonometric and hyperbolic functions
The trigonometric and hyperbolic functions overload the names of
ANSI C library floating-point trigonometric and hyperbolic functions.
acos
Header
oldcomplex.h
Prototype
complex acos(const complex& z);
Description
Returns the arccosine of a complex number.
asin, asinh
Header
oldcomplex.h
Prototype
complex asin(const complex& z); complex asinh(const complex& z);
Description
asin returns the arcsine of a complex number. asinh returns the
hyperbolic arcsine of a complex number.
atan, atanh
Header
oldcomplex.h
Prototype
complex atan(const complex& z); complex atanh(const complex& z);
Description
atan returns the arctangent of a complex number. atanh returns
the hyperbolic arctangent of a complex number.
cos, cosh
Header
oldcomplex.h
Prototype
complex cos(const complex& z); complex cosh(const complex& z);
Description
cos returns the cosine of a complex number. cosh returns the
hyperbolic cosine of a complex number.
sin, sinh
Header
oldcomplex.h
Prototype
complex sin (const complex& z); complex sinh(const complex& z);
Description
sin returns the sine of a complex number. sinh returns the
hyperbolic sine of a complex number.
tan, tanh
Header
oldcomplex.h
Prototype
complex tan(const complex& z); complex tanh(const complex& z);
Description
tan returns the tangent of a complex number. tanh returns the
hyperbolic tangent of a complex number.
Mathematical functions
The mathematical functions overload functions contained in the
ANSI C math library to provide consistent complex support.
abs
Header
oldcomplex.h
Prototype
double abs(const complex&);
Description
Returns the absolute value of a complex number.
exp
Header
oldcomplex.h
Prototype
complex exp(const complex&);
Description
Returns the value of e raised to a complex number.
log, log10
Header
oldcomplex.h
Prototype
complex log(const complex&); complex log10(const complex&);
Description
log returns the logarithm base e of a complex number. log10
returns the logarithm base 10 of a complex number.
pow
Header
oldcomplex.h
Prototype
complex pow(const complex&, double);
complex pow(const complex&, const complex &);
complex pow(double, const complex&);
complex pow(const complex&, int);
Description
Returns any number raised to a complex number or vice versa.
sqrt
Header
oldcomplex.h
Prototype
complex sqrt(const complex&);
Description
Returns the square root of a complex number.