00001 //------------------------------------------------------------------------------ 00002 /** 00003 * @file LA_Vector.h 00004 * Contains class LinAlg::Vector 00005 * 00006 * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a> 00007 */ 00008 //------------------------------------------------------------------------------ 00009 #ifndef LINALG_VECTOR_H_INCLUDED 00010 #define LINALG_VECTOR_H_INCLUDED 00011 //------------------------------------------------------------------------------ 00012 #include <cstdlib> 00013 #include <iosfwd> 00014 #include <string> 00015 #include "LA_Exception.h" 00016 #include "Vector3.h" 00017 #include "Tools/Streams/InOut.h" 00018 //------------------------------------------------------------------------------ 00019 namespace LA 00020 { 00021 //------------------------------------------------------------------------------ 00022 class Matrix; 00023 //------------------------------------------------------------------------------ 00024 /** 00025 * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a> 00026 * 00027 * Represents a vector with arbitrary entries of type double. 00028 */ 00029 class Vector 00030 { 00031 public: 00032 /** 00033 * Standard constructor. Constructed vector has no entries. 00034 */ 00035 Vector(); 00036 00037 /** 00038 * Constructs a vector with the specified dimension. Entries are initialized 00039 * with 0.0. 00040 * @param n Dimension of vector. 00041 */ 00042 Vector(int n); 00043 00044 /** 00045 * Constructs a vector with the specified dimension. Entries are intialized 00046 * with the values in the passed double array. 00047 * @param n Dimension of vector. 00048 * @param v Array with double values that are used to initialize the vector. 00049 */ 00050 Vector(int n, const double* v); 00051 00052 /** 00053 * Copy constructor. 00054 * @param v Vector to copy. 00055 */ 00056 Vector(const Vector& v); 00057 00058 /** 00059 * Copy constructor. 00060 * @param v Vector3 to copy. 00061 */ 00062 Vector(const Vector3<double>& v); 00063 00064 /** 00065 * Destructor. 00066 */ 00067 virtual ~Vector(); 00068 00069 public: 00070 00071 /** 00072 * Recreates the vector with specified dimension and initalizes all entries 00073 * to 0.0. 00074 * @param n Dimension of recreated vector. 00075 */ 00076 void create(int n); 00077 00078 /** 00079 * Recreates the vector with specified dimension and initalizes all entries 00080 * with the values in the passed array. 00081 * @param n Dimension of recreated vector. 00082 * @param v Array with double values that are used to initialize the vector. 00083 */ 00084 void create(int n, const double* v); 00085 00086 /** 00087 * Copy operator. 00088 * @param v Vector to copy. 00089 * @return Reference to this vector. 00090 */ 00091 Vector& operator=(const Vector& v); 00092 00093 /** 00094 * Copy operator. 00095 * @param v Vector3 to copy. 00096 * @return Reference to this vector. 00097 */ 00098 Vector& operator=(const Vector3<double>& v); 00099 00100 /** 00101 * Constant element access operator (zero-based). 00102 * @param i Index of element to access (zero-based). 00103 * @return Constant reference to entry. 00104 */ 00105 const double& operator[](int i) const; 00106 00107 /** 00108 * Element access operator (zero-based). 00109 * @param i Index of element to access (zero-based). 00110 * @return Reference to entry. 00111 */ 00112 double& operator[](int i); 00113 00114 /** 00115 * Constant element access operator (one-based). 00116 * @param i Index of element to access (one-based). 00117 * @return Constant reference to entry. 00118 */ 00119 const double& operator()(int i) const; 00120 00121 /** 00122 * Element access operator (one-based). 00123 * @param i Index of element to access (one-based). 00124 * @return Reference to entry. 00125 */ 00126 double& operator()(int i); 00127 00128 /** 00129 * Dimension of vector. 00130 * @return Dimension of vector. 00131 */ 00132 int dim() const; 00133 00134 /** 00135 * Extracts a subvector. 00136 * @param index Index of first entry to extract (one-based). 00137 * @param length Number of entries to extract. 00138 * @return The subvector. 00139 */ 00140 Vector sub(int index, int length) const; 00141 00142 /** 00143 * Sets a subvector inside this vector. 00144 * @param index Index where to map the first entry of subv 00145 * @param subv The subvector to set. 00146 */ 00147 void set_sub(int index, const Vector& subv); 00148 00149 /** 00150 * Adds the passed vector to this vector. 00151 * @param v Vector to add. 00152 * @return Reference to this vector. 00153 */ 00154 Vector& operator+=(const Vector& v); 00155 00156 /** 00157 * Substracts the passed vector from this vector. 00158 * @param v Vector to subtract. 00159 * @return Reference to this vector. 00160 */ 00161 Vector& operator-=(const Vector& v); 00162 00163 /** 00164 * Multiplies this vector with the passed scalar. 00165 * @param s Scalar to multiply this vector with. 00166 * @return Reference to this vector. 00167 */ 00168 Vector& operator*=(const double& s); 00169 00170 /** 00171 * Divides this vector by the passed scalar. 00172 * @param s Scalar to divide this vector by. 00173 * @return Reference to this vector. 00174 */ 00175 Vector& operator/=(const double& s); 00176 00177 /** 00178 * Returns a human-readable representation of the vector. 00179 * @return Human-readable representation of the vector. 00180 */ 00181 std::string asString() const; 00182 00183 /** 00184 * Returns the euclidian length of the vector. 00185 * @return The euclidian length of the vector. 00186 */ 00187 double length() const; 00188 00189 private: 00190 void destroy(); 00191 00192 private: 00193 int n; 00194 double* content; 00195 00196 friend Vector operator+(const Vector& v1, const Vector& v2); 00197 friend Vector operator-(const Vector& v1, const Vector& v2); 00198 friend Vector operator*(const double& s, const Vector& v); 00199 friend Vector operator*(const Vector& v, const double& s); 00200 friend Vector operator*(const Matrix& M, const Vector& v); 00201 friend Vector operator/(const Vector& v, const double& s); 00202 friend double operator*(const Vector& v1, const Vector& v2); 00203 friend Matrix operator*(const Matrix& M1, const Matrix& M2); 00204 friend Out& operator<<(Out& stream, const Vector& v); 00205 friend In& operator>>(In& stream, Vector& v); 00206 friend Out& operator<<(Out& stream, const Matrix& M); 00207 friend In& operator>>(In& stream, Matrix& M); 00208 00209 friend class Matrix; 00210 }; 00211 //------------------------------------------------------------------------------ 00212 00213 /** 00214 * Adds to vectors. 00215 * @param v1 First vector. 00216 * @param v2 Second vector. 00217 * @return v1 + v2. 00218 */ 00219 Vector operator+(const Vector& v1, const Vector& v2); 00220 00221 /** 00222 * Subtracts to vectors. 00223 * @param v1 First vector. 00224 * @param v2 Second vector. 00225 * @return v1 - v2. 00226 */ 00227 Vector operator-(const Vector& v1, const Vector& v2); 00228 00229 /** 00230 * Multiplies a vector with a scalar. 00231 * @param s Scalar. 00232 * @param v Vector. 00233 * @return s*v. 00234 */ 00235 Vector operator*(const double& s, const Vector& v); 00236 00237 /** 00238 * Multiplies a vector with a scalar. 00239 * @param v Vector. 00240 * @param s Scalar. 00241 * @return v*s. 00242 */ 00243 Vector operator*(const Vector& v, const double& s); 00244 00245 /** 00246 * Divides a vector by a scalar. 00247 * @param v Vector. 00248 * @param s Scalar. 00249 * @return v/s. 00250 */ 00251 Vector operator/(const Vector& v, const double& s); 00252 00253 /** 00254 * Calculates the inner product of two vector. 00255 * @param v1 First vector. 00256 * @param v2 Second vector. 00257 * @return <v1, v2>. 00258 */ 00259 double operator*(const Vector& v1, const Vector& v2); 00260 //------------------------------------------------------------------------------ 00261 00262 /** 00263 * Prints a user-readable representation of the vector. 00264 * @param out An ostream. 00265 * @param v Vector to output. 00266 * @return Reference to passed ostream. 00267 */ 00268 std::ostream& operator<<(std::ostream& out, const Vector& v); 00269 00270 /** 00271 * Out operator for GT-streams. 00272 * @param stream Out-stream. 00273 * @param v Vector to output. 00274 * @return Reference to passed Out-stream. 00275 */ 00276 Out& operator<<(Out& stream, const Vector& v); 00277 00278 /** 00279 * In operator for GT-streams. 00280 * @param stream In-stream. 00281 * @param v Vector to read to. 00282 * @return Reference to passed In-stream. 00283 */ 00284 In& operator>>(In& stream, Vector& v); 00285 //------------------------------------------------------------------------------ 00286 } 00287 //------------------------------------------------------------------------------ 00288 #endif 00289 //------------------------------------------------------------------------------
1.3.6