123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- //#include "stdafx.h"
- #include <math.h>
- #include "cmplxvec.h"
- #include <stdlib.h>
- /******************************* constructors ******************************/
- CmplxVector::CmplxVector(int n) {
-
- Complex zero(0.,0.);
- if (n < 0) {
- cerr << "complex vector: negative dimension." << endl;
- exit(1);
- }
- d = n;
- if (d > 0) {
- v = new Complex[d];
- while (n--) v[n] = zero;
- }
- else v = NULL;
- }
- CmplxVector::CmplxVector(int n, double a) {
- if (n <= 0) {
- cerr << "complex vector: undefined dimension." << endl;
- exit(1);
- }
- d = n;
- v = new Complex[d];
- while (n--) v[n] = a;
- }
- CmplxVector::CmplxVector(int n, Complex a) {
- if (n < 0) {
- cerr << "complex vector: negative dimension." << endl;
- exit(1);
- }
- d = n;
- v = new Complex[d];
- while (n--) v[n] = a;
- }
- CmplxVector::CmplxVector(const CmplxVector& p) {
-
- d = p.d;
- if (d > 0) {
- v = new Complex [d];
- for(int i = 0; i < d; i++) v[i] = p.v[i];
- }
- else v = NULL;
- }
- CmplxVector::CmplxVector(const Vector& v1) {
-
- d = v1.dim();
- v = new Complex[d];
- for(int i = 0; i < d; i++) v[i] = Complex(v1[i]);
- }
- /****************************** members ******************************/
- void CmplxVector::check_dimensions(const CmplxVector& vec) const {
-
- if (d != vec.d) {
- cerr << "Complex vector arguments have different dimensions." << endl;
- exit(1);
- }
- }
- /***********************************************************
- Complex CmplxVector::operator[](int i) const {
-
- if (i < 0 || i >= d) {
- cerr << "Complex vector: index out of range" << endl;
- exit(1);
- }
- return (v[i]);
- }
- Complex& CmplxVector::operator[](int i) {
-
- if (i < 0 || i >= d) {
- cerr << "Complex vector: index out of range" << endl;
- exit(1);
- }
- return (v[i]);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- CmplxVector& CmplxVector::operator+=(const CmplxVector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] += vec.v[n];
- return (*this);
- }
- CmplxVector& CmplxVector::operator+=(const Vector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] += vec[n];
- return (*this);
- }
- CmplxVector& CmplxVector::operator-=(const CmplxVector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] -= vec.v[n];
- return (*this);
- }
- CmplxVector& CmplxVector::operator-=(const Vector& vec) {
-
- check_dimensions(vec);
- int n = d;
- while (n--) v[n] -= vec[n];
- return (*this);
- }
- CmplxVector& CmplxVector::operator*=(double a) {
-
- int n = d;
- while (n--) v[n] *= a;
- return (*this);
- }
- CmplxVector& CmplxVector::operator*=(Complex a) {
-
- int n = d;
- while (n--) v[n] *= a;
- return (*this);
- }
- CmplxVector CmplxVector::operator-() {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = -v[n];
- return (result);
- }
- CmplxVector CmplxVector::operator*(double x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] * x;
- return (result);
- }
- CmplxVector CmplxVector::operator/(double x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] / x;
- return (result);
- }
- CmplxVector CmplxVector::operator*(const Complex& x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] * x;
- return (result);
- }
- CmplxVector CmplxVector::operator/(const Complex& x) const {
-
- int n = d;
- CmplxVector result(n);
- while (n--) result.v[n] = v[n] / x;
- return (result);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- 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);
- }
- CmplxVector& CmplxVector::operator=(double a) {
- int n = d;
- while (n--) v[n] = a;
- return (*this);
- }
- CmplxVector& CmplxVector::operator=(Complex a) {
- int n = d;
- while (n--) v[n] = a;
- return (*this);
- }
- 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);
- }
- ***********************************/
- CmplxVector CmplxVector::conjg() const {
-
- int n = d;
- CmplxVector tmp(n);
- for(int i = 0; i < n; i++) tmp[i] = ::conjg(v[i]);
- return (tmp);
- }
- Vector CmplxVector::real() const {
-
- Vector tmp(d);
- int n = d;
- while(n--) tmp[n] = ::real(v[n]);
- return (tmp);
- }
- Vector CmplxVector::imag() const {
-
- Vector tmp(d);
- int n = d;
- while(n--) tmp[n] = ::imag(v[n]);
- return (tmp);
- }
- Vector CmplxVector::abs() const {
- Vector result(d);
- for(int i = 0; i < d; i++)
- result[i]=::cabs(v[i]);
- return (result);
- }
- CmplxVector& CmplxVector::resize(int new_dim) {
- if(d != new_dim) {
- delete v;
- v = new Complex [new_dim];
- d = new_dim;
- }
- while (new_dim--) v[new_dim] = 0.0;
- return (*this);
- }
- /************ friends ***********************/
- CmplxVector operator*(double f, const CmplxVector& v)
- {
- return v*f;
- }
- CmplxVector operator*(const Complex& f, const CmplxVector& v)
- {
- return v*f;
- }
- Complex inner(const CmplxVector& x, const CmplxVector& x0)
- {
- return(x.conjg()*x0);
- }
- ostream& operator<<(ostream& out, const CmplxVector& v)
- {
- out.precision(10);
- for (int i = 0; i < v.d; i++)
- //out << v.v[i] << endl;
- out << " [" << i << "]=" << v.v[i] << endl;
- return (out);
- }
- istream& operator>>(istream& in, CmplxVector& x)
- {
- int i = 0;
- while (i < x.d && in >> x.v[i++]);
- return (in);
- }
- double norm(const CmplxVector& x)
- {
- double sum = 0.;
- int n = x.dim();
- for(int i = 0; i < n; i++) sum += cabs(x[i]);
- return (sum);
- }
- double norm2(const CmplxVector& x)
- {
- double sum = 0;
- int n = x.dim();
- //while (n--) sum += real(x[n])*real(x[n])+imag(x[n])*imag(x[n]);
- for(int i = 0; i < n; i++) sum += cabs(x[i])*cabs(x[i]);
- return (sqrt(sum));
- }
- double normsa(const CmplxVector& x)
- {
- Complex sum = 0.;
- int n = x.dim();
- for(int i = 0; i < n; i++) sum += x[i];
- return (cabs(sum));
- }
- CmplxVector operator^(const CmplxVector& v,const CmplxVector& w) {
- if (v.dim() != w.dim()) {
- cerr << "CmplxVector=: vectors have different length" << endl;
- exit(1);
- }
- int n = v.dim();
- CmplxVector result(n, 0.0);
- while (n--) result[n] = v[n] * w[n];
- return (result);
- }
|