Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Math/LA_Vector.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 #include <iomanip>
00003 #include <assert.h>
00004 #include <sstream>
00005 #include <math.h>
00006 #include "LA_Vector.h"
00007 //------------------------------------------------------------------------------
00008 namespace LA
00009 {
00010 //------------------------------------------------------------------------------
00011 Vector::Vector() : n(0), content(NULL)
00012 {
00013 }
00014 //------------------------------------------------------------------------------
00015 Vector::Vector(int n) : n(0), content(NULL)
00016 {
00017   create(n);
00018 }
00019 //------------------------------------------------------------------------------
00020 Vector::Vector(int n, const double* content) : n(0), content(NULL)
00021 {
00022   create(n, content);
00023 }
00024 //------------------------------------------------------------------------------
00025 Vector::Vector(const Vector& v) : n(0), content(NULL)
00026 {
00027   *this = v;
00028 }
00029 //------------------------------------------------------------------------------
00030 Vector::Vector(const Vector3<double>& v) : n(0), content(NULL)
00031 {
00032   *this = v;
00033 }
00034 //------------------------------------------------------------------------------
00035 Vector::~Vector()
00036 {
00037   destroy();
00038 }
00039 //------------------------------------------------------------------------------
00040 void Vector::create(int n)
00041 {
00042   destroy();
00043   this->n = n;
00044   if (n != 0)
00045   {
00046     content = new double[n];
00047     for (int i = 0; i < n; ++i)
00048       content[i] = 0.0;
00049   }
00050 }
00051 //------------------------------------------------------------------------------
00052 void Vector::create(int n, const double* v)
00053 {
00054   destroy();
00055   this->n = n;
00056   if (n != 0)
00057   {
00058     content = new double[n];
00059     for (int i = 0; i < n; ++i)
00060       content[i] = v[i];
00061   }
00062 }
00063 //------------------------------------------------------------------------------
00064 Vector& Vector::operator=(const Vector& v)
00065 {
00066   create(v.n, v.content);
00067   return *this;
00068 }
00069 //------------------------------------------------------------------------------
00070 Vector& Vector::operator=(const Vector3<double>& v)
00071 {
00072   create(3);
00073   content[0] = v.x;
00074   content[1] = v.y;
00075   content[2] = v.z;
00076   return *this;
00077 }
00078 //------------------------------------------------------------------------------
00079 const double& Vector::operator[](int i) const
00080 {
00081   assert((n > i) && (i >= 0));
00082   return content[i];
00083 }
00084 //------------------------------------------------------------------------------
00085 double& Vector::operator[](int i)
00086 {
00087   assert((n > i) && (i >= 0));
00088   return content[i];
00089 }
00090 //------------------------------------------------------------------------------
00091 const double& Vector::operator()(int i) const
00092 {
00093   assert((i >= 1) && (i <= n));
00094   return content[i-1];
00095 }
00096 //------------------------------------------------------------------------------
00097 double& Vector::operator()(int i)
00098 {
00099   assert((i >= 1) && (i <= n));
00100   return content[i-1];
00101 }
00102 //------------------------------------------------------------------------------
00103 int Vector::dim() const
00104 {
00105   return n;
00106 }
00107 //------------------------------------------------------------------------------
00108 Vector Vector::sub(int index, int length) const
00109 {
00110   assert((index >= 1) && ((index + length - 1) <= n));
00111   return Vector(length, &content[index-1]);
00112 }
00113 //------------------------------------------------------------------------------
00114 void Vector::set_sub(int index, const Vector& subv)
00115 {
00116   assert((index >= 1) && ((index + subv.n - 1) <= n));
00117   int i, j;
00118   for (i = 0, j = i + index - 1; i < subv.n; ++i, ++j)
00119     content[j] = subv.content[i];
00120 }
00121 //------------------------------------------------------------------------------
00122 Vector& Vector::operator+=(const Vector& v)
00123 {
00124   assert(n == v.n);
00125   for (int i = 0; i < n; ++i)
00126     content[i] += v.content[i];
00127   return *this;
00128 }
00129 //------------------------------------------------------------------------------
00130 Vector& Vector::operator-=(const Vector& v)
00131 {
00132   assert(n == v.n);
00133   for (int i = 0; i < n; ++i)
00134     content[i] -= v.content[i];
00135   return *this;
00136 }
00137 //------------------------------------------------------------------------------
00138 Vector& Vector::operator*=(const double& s)
00139 {
00140   for (int i = 0; i < n; ++i)
00141     content[i] *= s;
00142   return *this;
00143 }
00144 //------------------------------------------------------------------------------
00145 Vector& Vector::operator/=(const double& s)
00146 {
00147   for (int i = 0; i < n; ++i)
00148     content[i] /= s;
00149   return *this;
00150 }
00151 //------------------------------------------------------------------------------
00152 std::string Vector::asString() const
00153 {
00154   std::ostringstream os;
00155   os << *this;
00156   return os.str();
00157 }
00158 //------------------------------------------------------------------------------
00159 double Vector::length() const
00160 {
00161   double squaresum = 0.0;
00162   for(int i = 0; i < n; ++i)
00163     squaresum += content[i]*content[i];
00164   return sqrt(squaresum);
00165 }
00166 //------------------------------------------------------------------------------
00167 void Vector::destroy()
00168 {
00169   if (content != NULL)
00170   {
00171     delete [] content;
00172     content = NULL;
00173   }
00174 }
00175 //------------------------------------------------------------------------------
00176 Vector operator+(const Vector& v1, const Vector& v2)
00177 {
00178   Vector v(v1);
00179   v += v2;
00180   return v;
00181 }
00182 //------------------------------------------------------------------------------
00183 Vector operator-(const Vector& v1, const Vector& v2)
00184 {
00185   Vector v(v1);
00186   v -= v2;
00187   return v;
00188 }
00189 //------------------------------------------------------------------------------
00190 Vector operator*(const double& s, const Vector& v)
00191 {
00192   Vector res(v);
00193   res *= s;
00194   return res;
00195 }
00196 //------------------------------------------------------------------------------
00197 Vector operator*(const Vector& v, const double& s)
00198 {
00199   Vector res(v);
00200   res *= s;
00201   return res;
00202 }
00203 //------------------------------------------------------------------------------
00204 Vector operator/(const Vector& v, const double& s)
00205 {
00206   Vector res(v);
00207   res /= s;
00208   return res;
00209 }
00210 //------------------------------------------------------------------------------
00211 double operator*(const Vector& v1, const Vector& v2)
00212 {
00213   assert(v1.n == v2.n);
00214   double s = 0.0;
00215   for (int i = 0; i < v1.n; ++i)
00216     s += v1.content[i]*v2.content[i];
00217   return s;
00218 }
00219 //------------------------------------------------------------------------------
00220 std::ostream& operator<<(std::ostream& out, const Vector& v)
00221 {
00222   for (int i = 0; i < v.dim(); ++i)
00223   {
00224     out << "| "
00225         << std::setw(14) << std::setfill(' ') << v[i]
00226         << " |" << std::endl;
00227   }
00228   return out;
00229 }
00230 //------------------------------------------------------------------------------
00231 Out& operator<<(Out& stream, const Vector& v)
00232 {
00233   stream << v.n;
00234   for (int i = 0; i < v.n; ++i)
00235     stream << v.content[i];
00236 
00237   return stream;
00238 }
00239 //------------------------------------------------------------------------------
00240 In& operator>>(In& stream, Vector& v)
00241 {
00242   int n;
00243   stream >> n;
00244   v.create(n);
00245   for (int i = 0; i < n; ++i)
00246     stream >> v.content[i];
00247 
00248   return stream;
00249 }
00250 //------------------------------------------------------------------------------
00251 }
00252 //------------------------------------------------------------------------------

Generated on Mon Mar 20 22:00:06 2006 for GT2005 by doxygen 1.3.6