// // Programmer: Craig Stuart Sapp // Creation Date: Mon Apr 21 21:18:35 GMT-0800 1997 // Last Modified: Mon Apr 21 21:18:35 GMT-0800 1997 // Filename: ...sig/code/base/Vector/Vector.cpp // Syntax: C++ // $Smake: cc -Wall -g -c %b.cpp -I../../../include && rm -f %b.o // #ifndef _VECTOR_CPP_INCLUDED #define _VECTOR_CPP_INCLUDED #include "Vector.h" #include #include #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // Vector::Vector // template Vector::Vector(void) { dimension = 0; points = NULL; } template Vector::Vector(int aDimension) { dimension = aDimension; points = new type[dimension]; } template Vector::Vector(type xx, type yy) { dimension = 2; points = new type[dimension]; points[0] = xx; points[1] = yy; } template Vector::Vector(type xx, type yy, type zz) { dimension = 3; points = new type[dimension]; points[0] = xx; points[1] = yy; points[2] = zz; } template Vector::Vector(const Vector& aVector) { dimension = aVector.getDimension(); points = new type[dimension]; for (int i=0; i Vector::~Vector() { if (dimension != 0) delete [] points; } ////////////////////////////// // // Vector::getDimension // template int Vector::getDimension(void) const { return dimension; } ////////////////////////////// // // Vector::norm -- returns the length of a Vector // template double Vector::norm(void) { double output = 0; for (int i=0; i type& Vector::operator[](int index) { if (index < 0 || index >= getDimension()) { cerr << "Error: invalid array reference: " << index << " range is 0 to " << getDimension() << endl; exit(1); } return points[index]; } ////////////////////////////// // // Vector::operator[] const // template type Vector::operator[](int index) const { if (index < 0 || index >= getDimension()) { cerr << "Error: invalid array reference: " << index << " range is 0 to " << getDimension() << endl; exit(1); } return points[index]; } ////////////////////////////// // // Vector::operator= // template Vector& Vector::operator=(const Vector& aVector) { if (&aVector == this) return *this; // Assigning to self if (dimension == aVector.getDimension()) { // do nothing to storage size } else if (dimension != 0) { delete [] points; points = new type[aVector.getDimension()]; dimension = aVector.getDimension(); } for (int i=0; i Vector& Vector::operator+=(const Vector& aVector) { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be added." << endl; exit(1); } for (int i=0; i Vector Vector::operator+(const Vector& aVector) const { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be added." << endl; exit(1); } Vector output(*this); output += aVector; return output; } ////////////////////////////// // // Vector::operator-= // template Vector& Vector::operator-=(const Vector& aVector) { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be added." << endl; exit(1); } for (int i=0; i Vector Vector::operator-(const Vector& aVector) const { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be added." << endl; exit(1); } Vector output(*this); output -= aVector; return output; } // uniary minus sign template Vector Vector::operator-(void) const { Vector output(*this); for (int i=0; i Vector& Vector::operator*=(const Vector& aVector) { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be multiplied." << endl; exit(1); } for (int i=0; i Vector& Vector::operator*=(type aNumber) { for (int i=0; i Vector Vector::operator*(const Vector& aVector) const { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be multiplied." << endl; exit(1); } Vector output(*this); output *= aVector; return output; } // multiplication by a scalar: template Vector Vector::operator*(type aNumber) const { Vector output(*this); for (int i=0; i Vector& Vector::operator/=(const Vector& aVector) { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be multiplied." << endl; exit(1); } for (int i=0; i Vector& Vector::operator/=(type aNumber) { for (int i=0; i Vector Vector::operator/(const Vector& aVector) const { if (getDimension() != aVector.getDimension()) { cerr << "Error: different size vectors cannot be multiplied." << endl; exit(1); } Vector output(*this); output /= aVector; return output; } // division by a scalar: template Vector Vector::operator/(type aNumber) const { Vector output(*this); output /= aNumber; return output; } ////////////////////////////// // // Vector::operator== // template int Vector::operator==(Vector& aVector) { if (getDimension() != aVector.getDimension()) { return 0; } for (int i=0; i void Vector::print(void) { cout << *this << endl; } ////////////////////////////// // // Vector::setDimension // template void Vector::setDimension(int aDimension) { if (aDimension < 0) { cerr << "Invalid size for vector: " << aDimension << endl; exit(1); } int oldDimension = dimension; dimension = aDimension; type* temp; temp = new type[dimension]; for (int i=0; i type& Vector::x(void) { return points[0]; } ////////////////////////////// // // Vector::y // template type& Vector::y(void) { return points[1]; } ////////////////////////////// // // Vector::z // template type& Vector::z(void) { return points[2]; } ////////////////////////////// // // Vector::zero // default value: aValue = 0 // template void Vector::zero(type aValue) { for (int i=0; i Vector operator*(type aNumber, Vector& aVector) { Vector output(aVector); output *= aNumber; return output; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // operator<< // template ostream& operator<<(ostream& output, const Vector& aVector) { output << "("; for (int i=0; i double norm(const Vector& aVector) { double output = 0; for (int i=0; i type normSquare(const Vector& aVector) { type output = 0; for (int i=0; i& vector1, const Vector& vector2) { if (vector1.getDimension() != vector2.getDimension()) { cerr << "Error: vectors must have same dimension to calculate" << " inner product." << endl; exit(1); } ComplexD output = 0; for (int i=0; i type ip(const Vector& vector1, const Vector& vector2) { if (vector1.getDimension() != vector2.getDimension()) { cerr << "Error: vectors must have same dimension to calculate" << " inner product." << endl; exit(1); } type output = 0; for (int i=0; i Vector midpoint(const Vector& a, const Vector& b) { return (b+a)/2; } #endif // _VECTOR_CC_INCLUDED // md5sum: 2c75545994bbad7d4785c7e1ee5c3864 Vector.cpp [20050403]