1 
2 /*
3  *  Complex - Implements a complex number
4  *  Copyright (c) 2003-2006 by Mattias Hultgren <mattias_hultgren@tele2.se>
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; version 2 of the License.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public
17  *   License along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20 
21 #ifndef COMPLEX_H_
22 #define COMPLEX_H_
23 
24 #include "vartypes.h"
25 #include "integer.h"
26 #include "real.h"
27 #include "utf8_string.h"
28 
29 
30 #define COMPLEX_H_VERSION "v3"
31 #define COMPLEX_H_DATE    "2005-09 - 2006-07"
32 
33 
34 
35 class Complex
36 {
37 public:
Complex()38 	Complex() { real = 0.0; imaginary = 0.0; }
throw(error_obj)39 	Complex(const Real &re) throw(error_obj) { real = re; imaginary = Real(0); }
Complex(const Real & re,const Real & im)40 	Complex(const Real &re,const Real &im) throw(error_obj) { real = re; imaginary = im; }
throw(error_obj)41 	Complex(const Complex &src) throw(error_obj) { real = src.real; imaginary = src.imaginary; }
42 
43 	Real real,imaginary;
44 
45 	inline Complex operator+(const Real &val) const { Complex tmp(real, imaginary); tmp.real.add( val ); return tmp; }
46 	inline Complex operator+(const Complex &val) const { Complex tmp(real, imaginary); tmp.real.add( val.real ); tmp.imaginary.add( val.imaginary ); return tmp; }
47 	inline Complex & operator+=(const Real &val) { real.add( val ); return *this; }
48 	inline Complex & operator+=(const Complex &val) { real.add( val.real ); imaginary.add( val.imaginary ); return *this; }
49 	inline Complex operator-(const Real &val) const { return Complex(real - val, imaginary); }
50 	inline Complex operator-(const Complex &var) const { return Complex(real - var.real, imaginary - var.imaginary); }
51 	inline Complex & operator-=(const Real &val) { real.sub( val ); return *this; }
52 	inline Complex & operator-=(const Complex &var) { real.sub( var.real ); imaginary.sub( var.imaginary ); return *this; }
53 	inline Complex operator*(const Real &val) const { return Complex(real * val, imaginary * val); }
54 	Complex operator*(const Complex &var) const;
55 	inline Complex operator/(const Real &val) const { return Complex(real / val, imaginary / val); }
56 	Complex operator/(const Complex &var) const throw(error_obj);
57 
58 	inline bool operator==(const Complex &var) const { return (real == var.real  &&  imaginary == var.imaginary); }
59 	inline bool operator!=(const Complex &var) const { return !(*this == var); }
60 	bool operator<(const Complex &var) const throw(error_obj);
61 	bool operator>(const Complex &var) const throw(error_obj);
62 	inline bool operator<=(const Complex &var) const throw(error_obj) { return !(*this > var); }
throw(error_obj)63 	inline bool operator>=(const Complex &var) const throw(error_obj) { return !(*this < var); }
64 
65 	void append_to_string( utf8_string &str, const Format &fmt ) const throw(error_obj);
66 
throw(error_obj)67 	inline Complex & operator=(const Complex &src) throw(error_obj) { real = src.real; imaginary = src.imaginary; return *this; }
68 };
69 bool complex_almost_equal(const Complex &l,const Complex &r);
70 Complex power(const Complex &left,const Complex &right) throw(error_obj);
71 Complex sqroot(const Complex &var) throw(error_obj);
72 Complex abs(const Complex &var) throw(error_obj);
73 // arg's return value is in radians
arg(const Complex & var)74 inline Complex arg(const Complex &var) {   return Complex(atan2x(floatx(var.imaginary),floatx(var.real))); }
ipart(const Complex & var)75 inline Complex ipart(const Complex &var) { return Complex( ipart(var.real), ipart(var.imaginary) ); }
fpart(const Complex & var)76 inline Complex fpart(const Complex &var) { return Complex( fpart(var.real), fpart(var.imaginary) ); }
77 inline Complex operator+(const Real &val,const Complex &com) { return Complex(com.real + val, com.imaginary); }
78 inline Complex operator-(const Real &val,const Complex &com) { return Complex(com.real - val, com.imaginary); }
79 inline Complex operator-(const Complex &com) { return Complex(-com.real, -com.imaginary); }
80 inline Complex operator*(const Real &val,const Complex &com) { return Complex(com.real * val, com.imaginary * val); }
81 Complex operator/(const Real &val,const Complex &com);
82 
83 
84 Complex expc(const Complex &val);
85 Complex logc(const Complex &val);
86 Complex log2c(const Complex &val);
87 Complex log10c(const Complex &val);
88 Complex sinc(const Complex &val);
89 Complex cosc(const Complex &val);
90 Complex tanc(const Complex &val);
91 Complex asinc(const Complex &val);
92 Complex acosc(const Complex &val);
93 Complex atanc(const Complex &val);
94 Complex sinhc(const Complex &val);
95 Complex coshc(const Complex &val);
96 Complex tanhc(const Complex &val);
97 Complex asinhc(const Complex &val);
98 Complex acoshc(const Complex &val);
99 Complex atanhc(const Complex &val);
100 
101 
102 
103 
104 #endif // COMPLEX_H_
105