📄 cx.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="en-nz">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Complex library</title>
<link rel="stylesheet" type="text/css" href="rbd.css">
<link REL="SHORTCUT ICON" HREF="favicon.ico">
</head>
<body>
<h1><a name="top"></a>Complex number library</h1>
<p><i>Version of June, 2005</i></p>
<ul>
<li><a href="#introduction">Introduction</a>
<ul>
<li><a href="#author">Author and site details</a></li>
<li><a href="#files">Files in this package</a></li>
<li><a href="#testing">Testing and getting started</a></li>
<li><a href="#change">Change history</a></li>
</ul>
</li>
<li><a href="#Classes">Classes</a></li>
<li><a href="#Constructors">Constructors</a>
<ul>
<li><a href="#Complex">Complex</a></li>
<li><a href="#Imaginary">Imaginary</a></li>
<li><a href="#Polar">Polar</a></li>
</ul>
</li>
<li><a href="#Member">Member functions</a></li>
<li><a href="#Global">Global functions</a></li>
<li><a href="#Conversions">Conversions</a></li>
<li><a href="#Binary">Binary functions</a>
<ul>
<li><a href="#Additive">Additive</a></li>
<li><a href="#Multiplicative">Multiplicative</a></li>
<li><a href="#AdditiveSelf">Additive to self</a></li>
<li><a href="#MultiplicativeSelf">Multiplicative to self</a></li>
<li><a href="#Equality">Equality</a></li>
<li><a href="#Power">Power</a></li>
</ul>
</li>
<li><a href="#input">Input and output</a></li>
<li><a href="#GlobVarFn">Global variables, functions</a></li>
<li><a href="#example">Mandelbrot set example</a></li>
</ul>
<ul>
<li><a HREF="ol_doc.htm">To online documentation page</a></li>
</ul>
<h2><a name="introduction"></a>Introduction</h2>
<p>This is a rather more extensive complex number library than the one provided
in the standard library. It provides complex, imaginary and polar classes and a
representation of the <i>i</i> or <i>j</i> of complex analysis. It does not use
templates. As in my other libraries, this library is based on a floating point
type <i>Real</i>. This must be <i>typedef</i>ed to be <i>double</i>, <i>float</i>
or a class defined by the user (this is done in the file include.h).</p>
<p><b>This library is not going to replace the one in the standard library
- so use this one only if you need its additional facilities or you
really don't want to use the standard one.</b></p>
<p>As usual with my libraries, there are no restrictions on the use of this
library. If you distribute it, you should note that it is available essentially
for free on the Internet. </p>
<p>Use is at your own risk. I take no responsibility for errors and problems or
losses you may incur through the use of this library. But please report errors.</p>
<p>See the documentation for <a href="nm11.htm">newmat11</a>
for setting options in the file <i>include.h</i> .</p>
<p>Go to the <a href="download.html">download</a> page for the library.</p>
<h3><a name="author"></a>Author and site details</h3>
<p>Author: Robert Davies; robert<b> at </b>statsresearch.co.nz.</p>
<p>Available from <a href="http://www.robertnz.net">http://www.robertnz.net</a></p>
<h3><a name="files"></a>Files in this package</h3>
<p>The following files are included in this package </p>
<div align="left">
<table border="0" width="80%" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0">
<tr>
<td width="33%" valign="top">cx.h</td>
<td width="67%">header file for the complex number library</td>
</tr>
<tr>
<td width="33%" valign="top">cx.cpp</td>
<td width="67%">function bodies</td>
</tr>
<tr>
<td width="33%" valign="top">cx_polar.cpp</td>
<td width="67%">function bodies for polar class</td>
</tr>
<tr>
<td width="33%" valign="top">boolean.h</td>
<td width="67%">simulation of the standard Boolean type</td>
</tr>
<tr>
<td width="33%" valign="top">myexcept.h</td>
<td width="67%">header for the exceptions simulator</td>
</tr>
<tr>
<td width="33%" valign="top">myexcept.cpp</td>
<td width="67%">bodies for the exceptions simulator</td>
</tr>
<tr>
<td width="33%" valign="top">include.h</td>
<td width="67%">options header file (see documentation in newmat10)</td>
</tr>
<tr>
<td width="33%" valign="top">cxtest.h</td>
<td width="67%">test program header file</td>
</tr>
<tr>
<td width="33%" valign="top">cxtest.cpp</td>
<td width="67%">test program main function</td>
</tr>
<tr>
<td width="33%" valign="top">cxtest1.cpp</td>
<td width="67%">test unary functions</td>
</tr>
<tr>
<td width="33%" valign="top">cxtest2.cpp</td>
<td width="67%">test binary functions </td>
</tr>
<tr>
<td width="33%" valign="top">cxtest.txt</td>
<td width="67%">output from the test program</td>
</tr>
<tr>
<td width="33%" valign="top">array1.h</td>
<td width="67%">simple array class used by test program</td>
</tr>
<tr>
<td width="33%" valign="top">mandel.cpp</td>
<td width="67%">Mandelbrot set example</td>
</tr>
<tr>
<td width="33%" valign="top">mandel.txt</td>
<td width="67%">Output from mandel.cpp</td>
</tr>
<tr>
<td width="33%" valign="top">cx_targ.txt</td>
<td width="67%">target file for use with genmake</td>
</tr>
<tr>
<td width="33%" valign="top">cx_b55.mak</td>
<td width="67%">make file for Borland 5.5 compiler</td>
</tr>
<tr>
<td width="33%" valign="top">cx_b56.mak</td>
<td width="67%">make file for Borland 5.6 compiler</td>
</tr>
<tr>
<td width="33%" valign="top">cx_m6.mak</td>
<td width="67%">make file for VC++ 6, 7 compilers</td>
</tr>
<tr>
<td width="33%" valign="top">cx_ow.mak</td>
<td width="67%">make file for Open Watcom compiler</td>
</tr>
<tr>
<td width="33%" valign="top">cx_gnu.mak</td>
<td width="67%">make file for Gnu compiler</td>
</tr>
<tr>
<td width="33%" valign="top">cx_cc.mak</td>
<td width="67%">make file for CC compiler</td>
</tr>
<tr>
<td width="33%" valign="top">cx_i8.mak</td>
<td width="67%">make file for Intel 8.1 compiler for Windows</td>
</tr>
<tr>
<td width="33%" valign="top">cx_il8.mak</td>
<td width="67%">make file for Intel 8.1 compiler for Linux</td>
</tr>
<tr>
<td width="33%" valign="top">cx.htm</td>
<td width="67%">this file</td>
</tr>
<tr>
<td width="33%" valign="top">rbd.css</td>
<td width="67%">Style sheet for cx.htm</td>
</tr>
<tr>
<td width="33%" valign="top">polar.gif</td>
<td width="67%">diagram of polar coordinates</td>
</tr>
<tr>
<td width="33%" valign="top">mandel.gif</td>
<td width="67%">Mandelbrot set example</td>
</tr>
</table>
</div>
<h3><a name="testing"></a>Testing and getting started</h3>
<p>I have tested this program on the Borland 5.5,5.6; MS VC++ 6,7; Gnu 3.3, 3.4;
Intel 8. The <i>test</i> program uses <i>templates</i> so you will not be able
to run the test program on compilers that don't support templates. I
couldn't get the templates in the test program to compile with the Sun CC
compiler. The program didn't compile with the Open Watcom (1.3) compiler.</p>
<p>I include make files for the test programs for several compilers - see the
<a href="#files">file list</a>. Make files for
some other compilers can be generated with my <a href="genmake.htm">genmake</a>
utility.</p>
<p>If the program is working correctly the output from the test program will end
with two lines similar to</p>
<pre> maximum from cxtest1: 3.10082e-14
maximum from cxtest2: 7.10543e-15</pre>
The exact values will, most likely, differ. These values are from the Borland 5.02 compiler.
<p>For older compilers that don't support <i>bool</i> variables you need to edit <i> include.h</i> to
enable my simulated Booleans (comment out the statement <i>#define bool_LIB 0</i>).</p>
<p>You will need to #include files <i> include.h</i> and <i> cx.h</i> in your programs that use this
package. Edit <i> include.h</i> to determine whether exceptions are to be used,
simulated or disabled. If you use the simulated exceptions you should turn off
the exception capability of a compiler that does support exceptions. If you want to use namespace activate the <i>use_namespace</i> definition. This
library uses the namespace name RBD_COMPLEX.</p>
<h3><a name="change"></a>Change history</h3>
<table border="1" cellpadding="2" cellspacing="0" width="100%">
<tr>
<td width="20%">May, 2000</td>
<td width="80%">Initial release</td>
</tr>
<tr>
<td width="20%">July, 2001</td>
<td width="80%">Improve compatibility with my other libraries, include
sum_square() function</td>
</tr>
</table>
<h2><a name="Classes"></a>Classes</h2>
<table border="1" cellspacing="0" width="100%" cellpadding="2">
<tr>
<td width="40%"><b>class</b></td>
<td width="50%"><b>description</b></td>
<td width="10%"><b>user</b></td>
</tr>
<tr>
<td width="40%">class CX</td>
<td width="50%">complex</td>
<td width="10%">yes</td>
</tr>
<tr>
<td width="40%">class Imag</td>
<td width="50%">imaginary</td>
<td width="10%">yes</td>
</tr>
<tr>
<td width="40%">class ImaginaryUnit</td>
<td width="50%">the <i>i</i> or <i>j</i> of complex analysis</td>
<td width="10%">yes</td>
</tr>
<tr>
<td width="40%">class Polar</td>
<td width="50%">polar</td>
<td width="10%">yes</td>
</tr>
<tr>
<td width="40%">class ConvertFromReal</td>
<td width="50%">prevent implicit conversion</td>
<td width="10%">no</td>
</tr>
<tr>
<td width="40%">class Quadrant</td>
<td width="50%">mod 4 arithmetic</td>
<td width="10%">no</td>
</tr>
</table>
<p>Class <i>CX</i> is the main complex class. I have chosen the name <i>CX</i>
so as not to conflict with other complex classes. For example, using <i>complex</i>
will clash with the standard library.</p>
<p>Class <i>Imag</i> defines imaginary variables. If you have a complex variable
that will always be imaginary (the real part is always zero), then use the Imag
class to save computer time and make the program clearer.</p>
<p>Class <i>ImaginaryUnit</i> is used to represent <i>i</i> = sqrt(-1). I use
this to define <i> _I_</i> as a global variable to represent <i>i</i>.</p>
<p>Class <i>Polar</i> represents a complex number in polar coordinates.</p>
<p>I use class <i>ConvertFromReal</i> is used to prevent implicit conversions from <i>Real</i>
to <i>Imag</i>. Modern C++ compilers use the keyword <i>explicit</i> to prevent
implicit conversion, but, at present I don't want to assume all compilers
implement <i>explicit</i>.</p>
<p>Class <i>Quadrant</i> is used by class <i>Polar</i> in the representation of
the angle part of the polar coordinate.</p>
<p>The angle in the polar coordinates is represented by <i>theta</i> which is no
greater than <i>pi</i>/4 in absolute value and represents the angle from one of
the axes. Quadrant shows which axis. A <i>char</i> value in a Quadrant object
can take on one of the values 0, 1, 2 or 3 corresponding to the positive real
axis, the positive imaginary axis, the negative real axis or the negative
imaginary axis respectively. This representation means that one doesn't lose
accuracy when converting from complex coordinates to polar coordinates for
complex numbers very close to the real or imaginary axes.</p>
<p align="center">
<img border="0" src="polar.gif" width="252" height="225" alt="Polar coordinate diagram"></p>
<h2><a name="Constructors"></a>Constructors</h2>
<h3><a name="Complex"></a>Complex</h3>
<table border="1" cellspacing="0" width="100%" cellpadding="2">
<tr>
<td width="30%">
CX()</td>
<td width="70%">constructor, don't set values</td>
</tr>
<tr>
<td width="30%">
CX(Real x, Real y)</td>
<td width="70%">set real part to <i>x</i>, imaginary part to <i>y</i></td>
</tr>
<tr>
<td width="30%">
CX(Real x)</td>
<td width="70%">set real part to <i>x</i>, imaginary part to 0</td>
</tr>
<tr>
<td width="30%">
CX(Imag y)</td>
<td width="70%">set real part to 0, imaginary part to <i>y</i></td>
</tr>
<tr>
<td width="30%">
CX(ImaginaryUnit)</td>
<td width="70%">set real part to 0, imaginary part to 1</td>
</tr>
<tr>
<td width="30%">
CX(const CX& z)</td>
<td width="70%">copy constructor</td>
</tr>
<tr>
<td width="30%">
CX(const Polar& p)</td>
<td width="70%">convert polar value <i>p</i> to complex</td>
</tr>
</table>
<h3><a name="Imaginary"></a>Imaginary</h3>
<table border="1" cellspacing="0" width="100%" cellpadding="2">
<tr>
<td width="30%">
Imag()</td>
<td width="70%">constructor, don't set values</td>
</tr>
<tr>
<td width="30%">
Imag(ConvertFromReal x)</td>
<td width="70%">set real part to <i>x</i>. ConvertFromReal stops implicit
conversions</td>
</tr>
<tr>
<td width="30%">
Imag(const Imag& z)</td>
<td width="70%">copy constructor</td>
</tr>
<tr>
<td width="30%">
Imag(ImaginaryUnit)</td>
<td width="70%">set value to <i>i</i></td>
</tr>
</table>
<h3><a name="Polar"></a>Polar</h3>
<table border="1" cellspacing="0" width="100%" cellpadding="2">
<tr>
<td width="30%"> Polar()</td>
<td width="70%">constructor, don't set values</td>
</tr>
<tr>
<td width="30%">
Polar(Real r, Real theta)</td>
<td width="70%">set polar coordinates to <i>r</i>, <i>theta</i> after
normalising</td>
</tr>
<tr>
<td width="30%">
Polar(Real x)</td>
<td width="70%">set polar coordinates to <i>x</i>, 0 or -<i>x</i>, <i>pi</i></td>
</tr>
<tr>
<td width="30%">
Polar(Imag y)</td>
<td width="70%">set polar coordinates to <i>y</i>, <i>pi</i>/2 or -<i>y</i>,-<i>pi</i>/2</td>
</tr>
<tr>
<td width="30%">
Polar(ImaginaryUnit)</td>
<td width="70%">set polar coordinates to 1, <i>pi</i>/2</td>
</tr>
<tr>
<td width="30%">
Polar(const CX& z)</td>
<td width="70%">convert complex <i>z</i> to polar </td>
</tr>
<tr>
<td width="30%">
Polar(const Polar& p)</td>
<td width="70%">copy constructor</td>
</tr>
</table>
<h2><a name="Member"></a>Member functions</h2>
<table border="1" cellspacing="0" width="100%" cellpadding="2">
<tr>
<td width="26%"><b>Function</b></td>
<td width="26%"><b>Description</b></td>
<td width="48%" colspan="4" align="center">
<b>Return types with this class</b></td>
</tr>
<tr>
<td width="26%"> </td>
<td width="26%"> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -