Download code

Jump to: navigation, search

Back to Complex_numbers_(C_Plus_Plus)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

complex.cpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Complex_numbers_(C_Plus_Plus)?oldid=19030
14 */
15 
16 #include "complex.h"
17 
18 complex exp(complex c) {
19     double e_to_a = exp(c.getRe());
20     double b = c.getIm();
21     return complex(e_to_a*cos(b), e_to_a*sin(b));
22 }
23 


hijacker
hijacker
hijacker
hijacker

complex.h

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Complex_numbers_(C_Plus_Plus)?oldid=19030
 14 */
 15 
 16 #ifndef _COMPLEX_H_
 17 #define _COMPLEX_H_
 18 
 19 #include <math.h>
 20 
 21 class complex {
 22 public:
 23     double getRe();
 24     double getIm();
 25     void setRe(double value);
 26     void setIm(double value);
 27 
 28     complex(double re, double im);
 29     complex();
 30     explicit complex(double re);
 31     static complex fromPolar(double radius, double angle);
 32     complex operator+(complex rhs);
 33     complex operator-(complex rhs);
 34     complex operator*(complex rhs);
 35     complex operator+(double rhs);
 36     complex operator-(double rhs);
 37     complex operator*(double rhs);
 38     complex conjugate();
 39     double norm();
 40     complex operator/(double rhs);
 41     complex operator/(complex rhs);
 42 private:
 43     double real;
 44     double imag;
 45 
 46 };
 47 
 48 complex operator+(double lhs, complex rhs);
 49 complex operator-(double lhs, complex rhs);
 50 complex operator*(double lhs, complex rhs);
 51 complex operator/(double lhs, complex rhs);
 52 
 53 complex exp(complex c);
 54 
 55 inline double complex::getRe() { return real; }
 56 inline double complex::getIm() { return imag; }
 57 inline void complex::setRe(double value) { real = value; }
 58 inline void complex::setIm(double value) { imag = value; }
 59 
 60 inline complex::complex(double re, double im) {
 61     this->real = re;
 62     this->imag = im;
 63 }
 64 inline complex::complex() {
 65     this->real = 0.0;
 66     this->imag = 0.0;
 67 }
 68 
 69 inline complex::complex(double re) {
 70     this->real = re;
 71     this->imag = 0.0;
 72 }
 73 inline complex complex::fromPolar(double radius, double angle) {
 74     return complex(radius*cos(angle), radius*sin(angle));
 75 }
 76 inline complex complex::operator+(complex rhs) {
 77     return complex(this->real + rhs.real, this->imag + rhs.imag);
 78 }
 79 inline complex complex::operator-(complex rhs) {
 80     return complex(this->real - rhs.real, this->imag - rhs.imag);
 81 }
 82 inline complex complex::operator*(complex rhs) {
 83     return complex(this->real*rhs.real - this->imag*rhs.imag,
 84                    this->real*rhs.imag + this->imag*rhs.real);
 85 }
 86 inline complex complex::operator+(double rhs) {
 87     return complex(this->real + rhs, this->imag);
 88 }
 89 inline complex complex::operator-(double rhs) {
 90     return complex(this->real - rhs, this->imag);
 91 }
 92 inline complex complex::operator*(double rhs) {
 93     return complex(this->real * rhs, this->imag * rhs);
 94 }
 95 inline complex complex::operator/(double rhs) {
 96     return complex(this->real/rhs, this->imag/rhs);
 97 }
 98 inline complex complex::operator/(complex rhs) {
 99     return (*this)*rhs.conjugate()/rhs.norm();
100 }
101 inline double complex::norm() {
102     return this->real*this->real + this->imag*this->imag;
103 }
104 inline complex complex::conjugate() {
105     return complex(this->real, -this->imag);
106 }
107 inline complex operator+(double lhs, complex rhs) {
108     return rhs + lhs;
109 }
110 inline complex operator-(double lhs, complex rhs) {
111     return complex(lhs - rhs.getRe(), rhs.getIm());
112 }
113 inline complex operator*(double lhs, complex rhs) {
114     return rhs * lhs;
115 }
116 inline complex operator/(double lhs, complex rhs) {
117     return rhs.conjugate()*lhs/rhs.norm();
118 }
119 
120 #endif // #ifndef _COMPLEX_H_
121 


hijacker
hijacker
hijacker
hijacker