123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #ifndef _cmplxvec__h_
- #define _cmplxvec__h_
- #include <stdlib.h>
- #include <math.h>
- #include "complex.h"
- #include "vector.h"
- class CmplxVector
- {
- friend class Complex;
- friend class CmplxMatrix;
- friend ostream& operator<<(ostream&, const CmplxVector&);
- friend istream& operator>>(istream&, CmplxVector&);
- friend CmplxVector operator*(double, const CmplxVector&);
- friend CmplxVector operator*(const Complex&, const CmplxVector&);
- friend Complex inner(const CmplxVector&, const CmplxVector&);
- public:
- CmplxVector(int=0);
- CmplxVector(int, double);
- CmplxVector(int, Complex);
- CmplxVector(const CmplxVector&);
- CmplxVector(const Vector&);
- ~CmplxVector() { if (v) delete []v; }
- void free() { if (v) delete []v; }
- int dim() const { return d; }
- CmplxVector conjg() const;
- Vector real() const ;
- Vector imag() const ;
- Vector abs() const;
- CmplxVector& resize(int);
- CmplxVector& operator=(const CmplxVector&);
- CmplxVector& operator=(const Vector&);
- CmplxVector& operator=(double);
- CmplxVector& operator=(Complex);
- Complex& operator[](int);
- Complex operator[](int) const;
- CmplxVector operator+(const CmplxVector&) const;
- CmplxVector operator+(const Vector&) const;
- CmplxVector operator-(const CmplxVector&) const;
- CmplxVector operator-(const Vector&) const;
- CmplxVector& operator+=(const CmplxVector&);
- CmplxVector& operator+=(const Vector&);
- CmplxVector& operator-=(const CmplxVector&);
- CmplxVector& operator-=(const Vector&);
- CmplxVector& operator*=(double);
- CmplxVector& operator*=(Complex);
- Complex operator*(const CmplxVector&) const;
- Complex operator*(const Vector&) const;
- CmplxVector operator-();
- CmplxVector operator*(double) const;
- CmplxVector operator/(double) const;
- CmplxVector operator*(const Complex&) const;
- CmplxVector operator/(const Complex&) const;
- int operator==(const CmplxVector&) const;
- int operator!=(const CmplxVector& w) const { return !(*this == w); }
- private:
- Complex* v;
- int d;
- void check_dimensions(const CmplxVector&) const;
- };
- inline void Print(const CmplxVector& v, ostream& out=cout) { out << v; }
- inline void Read(CmplxVector& v, istream& in=cin) { in >> v; }
- inline Vector real(const CmplxVector& x) { return x.real(); }
- inline Vector imag(const CmplxVector& x) { return x.imag(); }
- inline CmplxVector conjg(const CmplxVector& x) { return x.conjg(); }
- inline Complex CmplxVector::operator[](int i) const {
-
- if (i < 0 || i >= d) {
- cerr << "Complex vector: index out of range" << endl;
- exit(1);
- }
- return (v[i]);
- }
- inline Complex& CmplxVector::operator[](int i) {
-
- if (i < 0 || i >= d) {
- cerr << "Complex vector: index out of range" << endl;
- exit(1);
- }
- return (v[i]);
- }
- inline CmplxVector CmplxVector::operator+(const CmplxVector& vec) const {
-
- check_dimensions(vec);
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] + vec.v[n];
- return (result);
- }
- inline CmplxVector CmplxVector::operator+(const Vector& vec) const {
-
- check_dimensions(vec);
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] + vec[n];
- return (result);
- }
- inline CmplxVector CmplxVector::operator-(const CmplxVector& vec) const {
-
- check_dimensions(vec);
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] - vec.v[n];
- return (result);
- }
- inline CmplxVector CmplxVector::operator-(const Vector& vec) const {
-
- check_dimensions(vec);
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] - vec[n];
- return (result);
- }
- inline CmplxVector& CmplxVector::operator+=(const CmplxVector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] += vec.v[n];
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator+=(const Vector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] += vec[n];
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator-=(const CmplxVector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] -= vec.v[n];
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator-=(const Vector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] -= vec[n];
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator*=(double a) {
-
- int n = d;
- while (n--) v[n] *= a;
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator*=(Complex a) {
-
- int n = d;
- while (n--) v[n] *= a;
- return (*this);
- }
- inline CmplxVector CmplxVector::operator-() {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = -v[n];
- return (result);
- }
- inline CmplxVector CmplxVector::operator*(double x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] * x;
- return (result);
- }
- inline CmplxVector CmplxVector::operator/(double x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] / x;
- return (result);
- }
- inline CmplxVector CmplxVector::operator*(const Complex& x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] * x;
- return (result);
- }
- inline CmplxVector CmplxVector::operator/(const Complex& x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] / x;
- return (result);
- }
- inline Complex CmplxVector::operator*(const CmplxVector& vec) const {
-
- check_dimensions(vec);
- Complex result(0.,0.);
- int n = d;
- while (n--) result += v[n] * vec.v[n];
- return (result);
- }
- inline Complex CmplxVector::operator*(const Vector& vec) const {
-
- check_dimensions(vec);
- Complex result(0.,0.);
- int n = d;
- while (n--) result += v[n] * vec[n];
- return (result);
- }
- inline CmplxVector& CmplxVector::operator=(const CmplxVector& vec) {
- if (this != &vec) {
- int n = vec.d;
- if (n != d) {
- delete v;
- v = new Complex [n];
- d = n;
- }
- while (n--) v[n] = vec.v[n];
- }
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator=(const Vector& vec) {
- int n = vec.dim();
- if (n != d) {
- delete v;
- v = new Complex [n];
- d = n;
- }
- while (n--) v[n] = vec[n];
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator=(double a) {
- int n = d;
- while (n--) v[n] = a;
- return (*this);
- }
- inline CmplxVector& CmplxVector::operator=(Complex a) {
- int n = d;
- while (n--) v[n] = a;
- return (*this);
- }
- inline int CmplxVector::operator==(const CmplxVector& vec) const {
-
- if (vec.d != d) return (0);
- int i = 0;
- while ((i < d) && (v[i]==vec.v[i])) i++;
- return ((i == d) ? 1 : 0);
- }
- double norm(const CmplxVector& x);
- double norm2(const CmplxVector& x);
- double normsa(const CmplxVector& x);
- CmplxVector operator^(const CmplxVector& v, const CmplxVector& w);
- #endif
|