- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/include/Complex.h

Go to the documentation of this file.

    00001 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:52 $ $Author: dgl $ $Name:  $ $Id: _complex_8h-source.html,v 1.2 2006/09/09 06:22:52 dgl Exp $ */
    00002 #ifndef COMPLEX_H
    00003 #define COMPLEX_H
    00004
    00005 // The Musimat Tutorial © 2006 Gareth Loy
    00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00007 // and published exclusively by The MIT Press.
    00008 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00014 // The Musimat website:                 http://www.musimat.com/
    00015 // This program is released under the terms of the GNU General Public License
    00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00017
    00020
    00021 #include <iostream>
    00022 #include <math.h>
    00023 using namespace std;
    00024 #include "Musimat.h"
    00025
    00026
    00028 class Complex
    00029 {
    00032         friend ostream& operator<<( ostream& os, const Complex& c ) {
    00033                 cout << "(";
    00034                 // cout.precision(2); // Use this to set precision, if desired
    00035                 cout << c.m_r << ", ";
    00036                 cout << c.m_i;
    00037                 cout << ")";
    00038                 Return os;
    00039         }
    00040
    00044         friend istream& operator>>( istream& is, Complex& c ) {
    00045                 char ch;
    00046                 is >> ch && (ch == '{' || ch == '(')
    00047                         && is >> c.m_r >> ch && ch == ','
    00048                         && is >> c.m_i >> ch && (ch == '}' || ch == '(');
    00049                 Return is;
    00050         }
    00051
    00052 public:
    00053
    00055         Complex() :
    00056           m_r(0),
    00057           m_i(0) {
    00058           }
    00059
    00062         Complex(Real i) :
    00063           m_r(i),
    00064           m_i(i) {
    00065           }
    00066
    00070         Complex(Real r, Real i) :
    00071           m_r(r),
    00072           m_i(i) {
    00073           }
    00074
    00077         Real r(Void) {
    00078                 Return m_r;
    00079         }
    00080
    00083         Real i(Void) {
    00084                 Return m_i;
    00085         }
    00086
    00089         Void r(Real r) { m_r = r; }
    00090
    00093         Void i(Real i) { m_i = i; }
    00094
    00096         Complex& operator=(Const Real& x) {
    00097                 m_r = x;
    00098                 m_i = 0;
    00099                 Return *this;
    00100         }
    00101
    00103         Bool operator==(Const Complex& x) Const {
    00104                 Return m_r == x.m_r && m_i == x.m_i;
    00105         }
    00106
    00108         Bool operator!=(Const Complex& x) Const {
    00109                 Return !(m_r == x.m_r && m_i == x.m_i);
    00110         }
    00111
    00113         Bool operator<(Const Complex& x) Const {
    00114                 Return m_r < x.m_r && m_i < x.m_i;
    00115         }
    00116
    00118         Bool operator<=(Const Complex& x) Const {
    00119                 Return m_r <= x.m_r && m_i <= x.m_i;
    00120         }
    00121
    00123         Bool operator>=(Const Complex& x) Const {
    00124                 Return m_r >= x.m_r && m_i >= x.m_i;
    00125         }
    00126
    00128         Bool operator>(Const Complex& x) Const {
    00129                 Return m_r > x.m_r && m_i > x.m_i;
    00130         }
    00131
    00133         Complex operator+(Const Complex& c){
    00134                 Return Complex(m_r + c.m_r, m_i + c.m_i);
    00135         }
    00136
    00138         Complex operator-(Const Complex& c){
    00139                 Return Complex(m_r - c.m_r, m_i - c.m_i);
    00140         }
    00141
    00144         Complex operator-(){
    00145                 Return Complex(-m_r, -m_i);
    00146         }
    00147
    00150         Complex operator~(){
    00151                 Return Complex(m_r, -m_i);
    00152         }
    00153
    00155         Complex operator*(Const Complex& c){
    00156                 Return Complex(m_r * c.m_r - m_i * c.m_i,
    00157                                            m_r * c.m_i + m_i * c.m_r);
    00158         }
    00159
    00161         Complex operator*(Const Real& r){
    00162                 Complex c(r, 0.0);
    00163                 Return *this * c;
    00164         }
    00165
    00167         Complex operator/(Const Complex& c){
    00168                 Real modulus = c.m_r * c.m_r + c.m_i * c.m_i;
    00169                 Real r = m_i * c.m_i + m_r * c.m_r;
    00170                 Real i = m_i * c.m_r - m_r * c.m_i;
    00171
    00172                 Return Complex(r / modulus, i / modulus);
    00173         }
    00174
    00176         Complex operator/(Const Real& r){
    00177                 Complex c(r, 0.0);
    00178                 Return *this / c;
    00179         }
    00180
    00182         Complex& operator+=(const Complex x) {
    00183                 Return( *this = *this + x );
    00184         }
    00185
    00187         Complex& operator-=(const Complex x) {
    00188                 Return( *this = *this - x );
    00189         }
    00190
    00192         Complex& operator*=(const Complex x) {
    00193                 Return( *this = *this * x );
    00194         }
    00195
    00197         Complex& operator/=(const Complex x) {
    00198                 Return( *this = *this / x );
    00199         }
    00200
    00202         Complex& operator/=(const Real r) {
    00203                 Complex x(r, 0.0);
    00204                 Return( *this = *this / x );
    00205         }
    00206
    00209         Complex Abs() {
    00210                 Complex t(m_r < 0.0 ? -m_r : m_r, m_i < 0.0 ? -m_i : m_i);
    00211                 Return(t);
    00212         }
    00213
    00216         Complex Conjugate() {
    00217                 Complex t(m_r, -m_i);
    00218                 Return(t);
    00219         }
    00220
    00222         Complex Exp(Complex e) {
    00223                 Real r = e.r();
    00224                 r = exp(r);
    00225                 Real theta = e.i();
    00226                 Complex t(r * Cos(theta), r * Sin(theta));
    00227                 Return t;
    00228         }
    00229
    00233         Void Complex::print(String s = 0) {
    00234                 If (s)
    00235                         cout << s;
    00236                 cout << "{ ";
    00237                 cout.precision(2);
    00238                 cout << m_r << ", ";
    00239                 cout << m_i;
    00240                 cout << " }" << endl;
    00241         }
    00242
    00243
    00244 private:
    00245         Real m_r;
    00246         Real m_i;
    00247 };
    00248
    00251 inline Real RealPart( Complex c ) { Return c.r(); }
    00252
    00255 inline Real ImagPart( Complex c ) { Return c.i(); }
    00256
    00258 inline Void RealPart( Complex& c, Real r ) { c.r(r); }
    00259
    00261 inline Void ImagPart( Complex& c, Real r ) { c.i(r); }
    00262
    00264 inline Complex Abs( Complex c ) { Return(c.Abs()); }
    00265
    00267 inline Complex Conjugate( Complex c ) { Return(~c); }
    00268
    00272 inline Complex Exp( Complex a, Complex b ) {
    00273         Complex x = a.Exp( b );
    00274         Return x;
    00275 }
    00276
    00280 inline Complex Exp(Real r, Real theta) {
    00281         Complex t(r * Cos(theta), r * Sin(theta));
    00282         Return t;
    00283 }
    00284
    00285 #endif // COMPLEX_H