00001 00008 #include <iostream> 00009 #include <cassert> 00010 #include <cmath> 00011 #include "Morpheus_Vector.h" 00012 00013 namespace Morpheus { 00014 00015 Vector::Vector(const int numElements) 00016 { 00017 assert(numElements > 0); 00018 00019 numElements_ = numElements; 00020 00021 // Allocate memory for the data 00022 data_ = new double[numElements_]; 00023 } 00024 00025 00026 Vector::~Vector() 00027 { 00028 // Release the memory 00029 delete[] data_; 00030 } 00031 00032 00033 double& Vector::operator[](const int subscript) 00034 { 00035 // Make sure the subscript is valid 00036 // Terminate the program if it's not 00037 assert(subscript >= 0 && subscript < numElements_); 00038 00039 return data_[subscript]; 00040 } 00041 00042 00043 const double& Vector::operator[](const int subscript) const 00044 { 00045 // Make sure the subscript is valid 00046 // Terminate the program if it's not 00047 assert(subscript >= 0 && subscript < numElements_); 00048 00049 return data_[subscript]; 00050 } 00051 00052 00053 int Vector::getNumElements() const 00054 { 00055 return numElements_; 00056 } 00057 00058 00059 void Vector::setValue(const double alpha) 00060 { 00061 for(int i=0; i<numElements_; i++) 00062 { 00063 data_[i] = alpha; 00064 } 00065 } 00066 00067 00068 void Vector::scale(const double alpha) 00069 { 00070 for(int i=0; i<numElements_; i++) 00071 { 00072 data_[i] = alpha * data_[i]; 00073 } 00074 } 00075 00076 00077 void Vector::add(const Vector& b, Vector& sum) const 00078 { 00079 // Make sure all three vectors are the same size 00080 assert(this->numElements_ == b.numElements_); 00081 assert(this->numElements_ == sum.numElements_); 00082 00083 // Compute the sum of each entry 00084 for(int i=0; i<this->numElements_; i++) 00085 { 00086 sum.data_[i] = this->data_[i] + b.data_[i]; 00087 } 00088 } 00089 00090 00091 double Vector::dot(const Vector& b) const 00092 { 00093 // Make sure the vectors are the same size 00094 assert(this->numElements_ == b.numElements_); 00095 00096 double sum; 00097 00098 // Compute the sum of all the products 00099 for(int i=0; i<this->numElements_; i++) 00100 { 00101 sum = sum + (this->data_[i] * b.data_[i]); 00102 } 00103 00104 return sum; 00105 } 00106 00107 00108 double Vector::norm1() const 00109 { 00110 double sum = 0; 00111 00112 // Compute the sum of all the entries magnitudes 00113 for(int i=0; i<numElements_; i++) 00114 { 00115 sum = sum + data_[i]; 00116 } 00117 00118 return sum; 00119 } 00120 00121 00122 double Vector::normInf() const 00123 { 00124 double maxVal = 0; 00125 00126 // Find the biggest entry 00127 for(int i=0; i<numElements_; i++) 00128 { 00129 double absVal = data_[i]; 00130 if(absVal > maxVal) 00131 { 00132 maxVal = absVal; 00133 } 00134 } 00135 00136 return maxVal; 00137 } 00138 00139 00140 double Vector::norm2() const 00141 { 00142 double sum = 0; 00143 00144 // Compute the sum of squares 00145 for(int i=0; i<numElements_; i++) 00146 { 00147 sum = sum + (data_[i]*data_[i]); 00148 } 00149 00150 return sum; 00151 } 00152 00153 00154 void Vector::print() const 00155 { 00156 std::cout << "Vector with " << numElements_ << " entries\n"; 00157 for(int i=0; i<numElements_; i++) 00158 { 00159 std::cout << "data[" << i << "] = " << data_[i] << std::endl; 00160 } 00161 } 00162 00163 } /* namespace Morpheus */