Complex Number 0.1.2 |
00001 // Copyright (c) 2010 Matthew Krupcale 00002 00003 // Permission is hereby granted, free of charge, to any person 00004 // obtaining a copy of this software and associated documentation 00005 // files (the "Software"), to deal in the Software without 00006 // restriction, including without limitation the rights to use, 00007 // copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 // copies of the Software, and to permit persons to whom the 00009 // Software is furnished to do so, subject to the following 00010 // conditions: 00011 00012 // The above copyright notice and this permission notice shall be 00013 // included in all copies or substantial portions of the Software. 00014 00015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00016 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00017 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00018 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00019 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00020 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00021 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00022 // OTHER DEALINGS IN THE SOFTWARE. 00023 00024 #ifndef COMPLEX_NUMBER_H 00025 #define COMPLEX_NUMBER_H 00026 00027 //============================================================================ 00028 // Name : ComplexNumber.h 00029 // Author : Matthew Krupcale 00030 // Version : 0.1.2 00031 // Copyright : Copyright (C) 2010 Matthew Krupcale 00032 // Description : A class that deals with complex numbers 00033 //============================================================================ 00034 00047 #include "Namespace.h" 00048 #include <iosfwd> 00049 00057 BEGIN_NAMESPACE(math) 00058 00059 00066 BEGIN_NAMESPACE(complex) 00067 00075 template <class T> 00076 class ComplexNumber { 00077 00078 /********************************************************************** 00079 * Complex friend member overloaded operators 00080 **********************************************************************/ 00081 00082 /* 00083 * Stream insertion & extraction operators 00084 */ 00085 00099 template <class U> 00100 friend std::ostream& operator<<(std::ostream& out, 00101 const ComplexNumber<U>& z); 00102 00117 template <class U> 00118 friend std::istream& operator>>(std::istream& in, 00119 ComplexNumber<U>& z); 00120 00121 public: 00122 00123 /********************************************************************** 00124 * Constructors & Destructor 00125 **********************************************************************/ 00126 00134 ComplexNumber(); 00135 00148 ComplexNumber(const T& real, const T& imaginary); 00149 00158 ComplexNumber(const ComplexNumber<T>& z); 00159 00167 virtual ~ComplexNumber(); 00168 00169 /********************************************************************** 00170 * Complex member overloaded operators 00171 **********************************************************************/ 00172 00185 ComplexNumber<T> operator+() const; 00186 00199 ComplexNumber<T> operator-() const; 00200 00210 template <class U> 00211 operator ComplexNumber<U>() const; 00212 00213 /* 00214 * Assignment operators 00215 */ 00216 00233 template <class U> 00234 ComplexNumber<T>& operator=(const ComplexNumber<U>& z); 00235 00251 template <class U> 00252 ComplexNumber<T>& operator=(const U& real); 00253 00254 /* 00255 * Compound assignment operators 00256 */ 00257 00274 template <class U> 00275 ComplexNumber<T>& operator+=(const ComplexNumber<U>& z); 00276 00293 template <class U> 00294 ComplexNumber<T>& operator+=(const U& real); 00295 00312 template <class U> 00313 ComplexNumber<T>& operator-=(const ComplexNumber<U>& z); 00314 00331 template <class U> 00332 ComplexNumber<T>& operator-=(const U& real); 00333 00350 template <class U> 00351 ComplexNumber<T>& operator*=(const ComplexNumber<U>& z); 00352 00369 template <class U> 00370 ComplexNumber<T>& operator*=(const U& real); 00371 00392 template <class U> 00393 ComplexNumber<T>& operator/=(const ComplexNumber<U>& z); 00394 00415 template <class U> 00416 ComplexNumber<T>& operator/=(const U& real); 00417 00418 /* 00419 * Static member functions 00420 */ 00421 00439 static ComplexNumber<T> polar(const T& r, 00440 const T& theta); 00441 00442 /********************************************************************** 00443 * Accessors 00444 **********************************************************************/ 00445 00453 T getReal() const; 00454 00463 T getImaginary() const; 00464 00465 /********************************************************************** 00466 * Mutators 00467 **********************************************************************/ 00468 00477 void setReal(const T& real); 00478 00487 void setImaginary(const T& imaginary); 00488 00495 const static ComplexNumber<double> i; 00496 00497 private: 00498 00499 /********************************************************************** 00500 * Private data 00501 **********************************************************************/ 00502 00509 T real; 00510 00517 T imaginary; 00518 00519 }; 00520 00521 /********************************************************************** 00522 * Complex non-member overloaded operators 00523 **********************************************************************/ 00524 00525 /* 00526 * Arithmetic operators 00527 */ 00528 00553 template <class T> 00554 ComplexNumber<T> operator+(const ComplexNumber<T>& lhs, 00555 const ComplexNumber<T>& rhs); 00556 00580 template <class T> 00581 ComplexNumber<T> operator+(const ComplexNumber<T>& lhs, 00582 const T& rhs); 00583 00607 template <class T> 00608 ComplexNumber<T> operator+(const T& lhs, 00609 const ComplexNumber<T>& rhs); 00610 00636 template <class T> 00637 ComplexNumber<T> operator-(const ComplexNumber<T>& lhs, 00638 const ComplexNumber<T>& rhs); 00639 00663 template <class T> 00664 ComplexNumber<T> operator-(const ComplexNumber<T>& lhs, 00665 const T& rhs); 00666 00690 template <class T> 00691 ComplexNumber<T> operator-(const T& lhs, 00692 const ComplexNumber<T>& rhs); 00693 00719 template <class T> 00720 ComplexNumber<T> operator*(const ComplexNumber<T>& lhs, 00721 const ComplexNumber<T>& rhs); 00722 00747 template <class T> 00748 ComplexNumber<T> operator*(const ComplexNumber<T>& lhs, 00749 const T& rhs); 00750 00774 template <class T> 00775 ComplexNumber<T> operator*(const T& lhs, 00776 const ComplexNumber<T>& rhs); 00777 00807 template <class T> 00808 ComplexNumber<T> operator/(const ComplexNumber<T>& lhs, 00809 const ComplexNumber<T>& rhs); 00810 00838 template <class T> 00839 ComplexNumber<T> operator/(const ComplexNumber<T>& lhs, 00840 const T& rhs); 00841 00869 template <class T> 00870 ComplexNumber<T> operator/(const T& lhs, 00871 const ComplexNumber<T>& rhs); 00872 00873 /* 00874 * Relational operators 00875 */ 00876 00895 template <class T, class U> 00896 bool operator==(const ComplexNumber<T>& lhs, 00897 const ComplexNumber<U>& rhs); 00898 00917 template <class T, class U> 00918 bool operator==(const ComplexNumber<T>& lhs, 00919 const U& rhs); 00920 00939 template <class T, class U> 00940 bool operator==(const T& lhs, 00941 const ComplexNumber<U>& rhs); 00942 00961 template <class T, class U> 00962 bool operator!=(const ComplexNumber<T>& lhs, 00963 const ComplexNumber<U>& rhs); 00964 00983 template <class T, class U> 00984 bool operator!=(const ComplexNumber<T>& lhs, 00985 const U& rhs); 00986 01005 template <class T, class U> 01006 bool operator!=(const T& lhs, 01007 const ComplexNumber<U>& rhs); 01008 01009 END_NAMESPACE(complex) 01010 END_NAMESPACE(math) 01011 01012 #endif /* COMPLEX_NUMBER_H */