cmplxvec.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #ifndef _cmplxvec__h_
  2. #define _cmplxvec__h_
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "complex.h"
  6. #include "vector.h"
  7. class CmplxVector
  8. {
  9. friend class Complex;
  10. friend class CmplxMatrix;
  11. friend ostream& operator<<(ostream&, const CmplxVector&);
  12. friend istream& operator>>(istream&, CmplxVector&);
  13. friend CmplxVector operator*(double, const CmplxVector&);
  14. friend CmplxVector operator*(const Complex&, const CmplxVector&);
  15. friend Complex inner(const CmplxVector&, const CmplxVector&);
  16. public:
  17. CmplxVector(int=0);
  18. CmplxVector(int, double);
  19. CmplxVector(int, Complex);
  20. CmplxVector(const CmplxVector&);
  21. CmplxVector(const Vector&);
  22. ~CmplxVector() { if (v) delete []v; }
  23. void free() { if (v) delete []v; }
  24. int dim() const { return d; }
  25. CmplxVector conjg() const;
  26. Vector real() const ;
  27. Vector imag() const ;
  28. Vector abs() const;
  29. CmplxVector& resize(int);
  30. CmplxVector& operator=(const CmplxVector&);
  31. CmplxVector& operator=(const Vector&);
  32. CmplxVector& operator=(double);
  33. CmplxVector& operator=(Complex);
  34. Complex& operator[](int);
  35. Complex operator[](int) const;
  36. CmplxVector operator+(const CmplxVector&) const;
  37. CmplxVector operator+(const Vector&) const;
  38. CmplxVector operator-(const CmplxVector&) const;
  39. CmplxVector operator-(const Vector&) const;
  40. CmplxVector& operator+=(const CmplxVector&);
  41. CmplxVector& operator+=(const Vector&);
  42. CmplxVector& operator-=(const CmplxVector&);
  43. CmplxVector& operator-=(const Vector&);
  44. CmplxVector& operator*=(double);
  45. CmplxVector& operator*=(Complex);
  46. Complex operator*(const CmplxVector&) const;
  47. Complex operator*(const Vector&) const;
  48. CmplxVector operator-();
  49. CmplxVector operator*(double) const;
  50. CmplxVector operator/(double) const;
  51. CmplxVector operator*(const Complex&) const;
  52. CmplxVector operator/(const Complex&) const;
  53. int operator==(const CmplxVector&) const;
  54. int operator!=(const CmplxVector& w) const { return !(*this == w); }
  55. private:
  56. Complex* v;
  57. int d;
  58. void check_dimensions(const CmplxVector&) const;
  59. };
  60. inline void Print(const CmplxVector& v, ostream& out=cout) { out << v; }
  61. inline void Read(CmplxVector& v, istream& in=cin) { in >> v; }
  62. inline Vector real(const CmplxVector& x) { return x.real(); }
  63. inline Vector imag(const CmplxVector& x) { return x.imag(); }
  64. inline CmplxVector conjg(const CmplxVector& x) { return x.conjg(); }
  65. inline Complex CmplxVector::operator[](int i) const {
  66. if (i < 0 || i >= d) {
  67. cerr << "Complex vector: index out of range" << endl;
  68. exit(1);
  69. }
  70. return (v[i]);
  71. }
  72. inline Complex& CmplxVector::operator[](int i) {
  73. if (i < 0 || i >= d) {
  74. cerr << "Complex vector: index out of range" << endl;
  75. exit(1);
  76. }
  77. return (v[i]);
  78. }
  79. inline CmplxVector CmplxVector::operator+(const CmplxVector& vec) const {
  80. check_dimensions(vec);
  81. int n = d;
  82. CmplxVector result(n);
  83. while (n--) result.v[n] = v[n] + vec.v[n];
  84. return (result);
  85. }
  86. inline CmplxVector CmplxVector::operator+(const Vector& vec) const {
  87. check_dimensions(vec);
  88. int n = d;
  89. CmplxVector result(n);
  90. while (n--) result.v[n] = v[n] + vec[n];
  91. return (result);
  92. }
  93. inline CmplxVector CmplxVector::operator-(const CmplxVector& vec) const {
  94. check_dimensions(vec);
  95. int n = d;
  96. CmplxVector result(n);
  97. while (n--) result.v[n] = v[n] - vec.v[n];
  98. return (result);
  99. }
  100. inline CmplxVector CmplxVector::operator-(const Vector& vec) const {
  101. check_dimensions(vec);
  102. int n = d;
  103. CmplxVector result(n);
  104. while (n--) result.v[n] = v[n] - vec[n];
  105. return (result);
  106. }
  107. inline CmplxVector& CmplxVector::operator+=(const CmplxVector& vec) {
  108. check_dimensions(vec);
  109. int n = d;
  110. while (n--) v[n] += vec.v[n];
  111. return (*this);
  112. }
  113. inline CmplxVector& CmplxVector::operator+=(const Vector& vec) {
  114. check_dimensions(vec);
  115. int n = d;
  116. while (n--) v[n] += vec[n];
  117. return (*this);
  118. }
  119. inline CmplxVector& CmplxVector::operator-=(const CmplxVector& vec) {
  120. check_dimensions(vec);
  121. int n = d;
  122. while (n--) v[n] -= vec.v[n];
  123. return (*this);
  124. }
  125. inline CmplxVector& CmplxVector::operator-=(const Vector& vec) {
  126. check_dimensions(vec);
  127. int n = d;
  128. while (n--) v[n] -= vec[n];
  129. return (*this);
  130. }
  131. inline CmplxVector& CmplxVector::operator*=(double a) {
  132. int n = d;
  133. while (n--) v[n] *= a;
  134. return (*this);
  135. }
  136. inline CmplxVector& CmplxVector::operator*=(Complex a) {
  137. int n = d;
  138. while (n--) v[n] *= a;
  139. return (*this);
  140. }
  141. inline CmplxVector CmplxVector::operator-() {
  142. int n = d;
  143. CmplxVector result(n);
  144. while (n--) result.v[n] = -v[n];
  145. return (result);
  146. }
  147. inline CmplxVector CmplxVector::operator*(double x) const {
  148. int n = d;
  149. CmplxVector result(n);
  150. while (n--) result.v[n] = v[n] * x;
  151. return (result);
  152. }
  153. inline CmplxVector CmplxVector::operator/(double x) const {
  154. int n = d;
  155. CmplxVector result(n);
  156. while (n--) result.v[n] = v[n] / x;
  157. return (result);
  158. }
  159. inline CmplxVector CmplxVector::operator*(const Complex& x) const {
  160. int n = d;
  161. CmplxVector result(n);
  162. while (n--) result.v[n] = v[n] * x;
  163. return (result);
  164. }
  165. inline CmplxVector CmplxVector::operator/(const Complex& x) const {
  166. int n = d;
  167. CmplxVector result(n);
  168. while (n--) result.v[n] = v[n] / x;
  169. return (result);
  170. }
  171. inline Complex CmplxVector::operator*(const CmplxVector& vec) const {
  172. check_dimensions(vec);
  173. Complex result(0.,0.);
  174. int n = d;
  175. while (n--) result += v[n] * vec.v[n];
  176. return (result);
  177. }
  178. inline Complex CmplxVector::operator*(const Vector& vec) const {
  179. check_dimensions(vec);
  180. Complex result(0.,0.);
  181. int n = d;
  182. while (n--) result += v[n] * vec[n];
  183. return (result);
  184. }
  185. inline CmplxVector& CmplxVector::operator=(const CmplxVector& vec) {
  186. if (this != &vec) {
  187. int n = vec.d;
  188. if (n != d) {
  189. delete v;
  190. v = new Complex [n];
  191. d = n;
  192. }
  193. while (n--) v[n] = vec.v[n];
  194. }
  195. return (*this);
  196. }
  197. inline CmplxVector& CmplxVector::operator=(const Vector& vec) {
  198. int n = vec.dim();
  199. if (n != d) {
  200. delete v;
  201. v = new Complex [n];
  202. d = n;
  203. }
  204. while (n--) v[n] = vec[n];
  205. return (*this);
  206. }
  207. inline CmplxVector& CmplxVector::operator=(double a) {
  208. int n = d;
  209. while (n--) v[n] = a;
  210. return (*this);
  211. }
  212. inline CmplxVector& CmplxVector::operator=(Complex a) {
  213. int n = d;
  214. while (n--) v[n] = a;
  215. return (*this);
  216. }
  217. inline int CmplxVector::operator==(const CmplxVector& vec) const {
  218. if (vec.d != d) return (0);
  219. int i = 0;
  220. while ((i < d) && (v[i]==vec.v[i])) i++;
  221. return ((i == d) ? 1 : 0);
  222. }
  223. double norm(const CmplxVector& x);
  224. double norm2(const CmplxVector& x);
  225. double normsa(const CmplxVector& x);
  226. CmplxVector operator^(const CmplxVector& v, const CmplxVector& w);
  227. #endif