00001 #include "vecdouble.h"
00002 #include "globals.h"
00003 #include <lac/sparse_matrix.h>
00004 #include <lac/sparse_matrix.templates.h>
00005 #include "exception.h"
00006
00007
00008 VecDouble::~VecDouble()
00009 {
00010 if (!m_owner)
00011 {
00012 val=0;
00013 max_vec_size = vec_size = 0;
00014 }
00015 }
00016
00017 VecDouble::VecDouble(const VecDouble& v)
00018 :Vector<double>(v)
00019 {
00020 m_owner=true;
00021 }
00022
00023 VecDouble::VecDouble(const std::vector<double> &p)
00024 {
00025 m_owner=true;
00026 reinit(p.size());
00027 for (unsigned i=0;i<p.size();i++)
00028 {
00029 operator()(i)=p[i];
00030 }
00031 }
00032
00033
00034
00035 VecDouble::VecDouble(const Point<3> &p)
00036 {
00037 m_owner=true;
00038 Point<3> &paux = const_cast<Point<3>& >(p);
00039 setRef(&(paux[0]),3);
00040 }
00041
00042 VecDouble::VecDouble(const Point<2> &p)
00043 {
00044 m_owner=true;
00045 Point<2> &paux = const_cast<Point<2>& >(p);
00046 setRef(&(paux[0]),2);
00047 }
00048
00049 VecDouble::VecDouble(const Point<1> &p)
00050 {
00051 m_owner=true;
00052 Point<1> &paux = const_cast<Point<1>& >(p);
00053 setRef(&(paux[0]),1);
00054 }
00055
00056
00057
00058
00059
00060 VecDouble::VecDouble(unsigned n)
00061 :Vector<double>(n)
00062 {
00063 m_owner=true;
00064 }
00065
00066
00067
00068 void VecDouble::reinit(const unsigned int N,const bool fast)
00069 {
00070 if (!m_owner)
00071 {
00072 throw new Exception("VecDouble::reinit Attemp to reinit a vector that does not own its memory\n");
00073 }
00074 Vector<double>::reinit(N,fast);
00075 }
00076
00077
00078 void VecDouble::reinit(const VecDouble &V,const bool fast)
00079 {
00080 reinit(V.size(),fast);
00081 }
00082
00083 void VecDouble::swap(VecDouble &v)
00084 {
00085 Vector<double>::swap(v);
00086 std::swap(m_owner,v.m_owner);
00087 }
00088
00089
00090 VecDouble& VecDouble::operator=(const VecDouble &v)
00091 {
00092
00093
00094
00095
00096 assert(m_owner || (v.size() == size()) );
00097 Vector< double >::operator=(v);
00098 return *this;
00099 }
00100
00101 VecDouble& VecDouble::operator=(const BlockVector<double> &v)
00102 {
00103
00104
00105
00106
00107 assert(m_owner || (v.size() == size()) );
00108 Vector<double>::operator=(v);
00109 return *this;
00110 }
00111
00112
00113 VecDouble& VecDouble::operator=(const Vector<double> &v)
00114 {
00115 assert(m_owner || (v.size() == size()) );
00116 Vector<double>::operator=(v);
00117 return *this;
00118 }
00119
00120
00129 void VecDouble::setRef(double *p,unsigned size)
00130 {
00131 if (m_owner)
00132 {
00133 reinit(0);
00134 }
00135 m_owner=false;
00136 val=p;
00137 max_vec_size=vec_size=size;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146 void VecDouble::setRef(Vector<double> &v)
00147 {
00148 setRef(&(v(0)),v.size());
00149 }
00150
00151
00152
00153 template void SparseMatrix<double>::vmult<VecDouble,VecDouble>(VecDouble &dst,const VecDouble &src) const;
00154
00155
00156 void VecDouble::operator=(double dd)
00157 {
00158 if (vec_size!=0)
00159 std::fill (begin(), end(), dd);
00160 }
00161
00162
00163 VecDouble::VecDouble()
00164 {
00165 m_owner=true;
00166 }
00167
00168
00169
00170 std::ostream& operator<< (std::ostream & out,const VecDouble &v)
00171 {
00172 for (unsigned i=0;i<v.size();i++)
00173 {
00174 out << v(i) << " ";
00175 }
00176 return out;
00177 }
00178
00179
00180 void VecDouble::getMinMaxValues(double *min,double *max) const
00181 {
00182 assert(size() != 0);
00183 *min=*max=(*this)(0);
00184 for (unsigned i=1;i<size();i++)
00185 {
00186 if ((*this)(i) < *min)
00187 *min=operator()(i);
00188 if ((*this)(i) > *max)
00189 *max=operator()(i);
00190 }
00191
00192 }
00193
00194
00195 void VecDouble::term_mult(const VecDouble &v1,const VecDouble &v2)
00196 {
00197 assert(v1.size() == v2.size());
00198 assert(v1.size() == size());
00199 const double *d1 = v1.val;
00200 const double *d2 = v2.val;
00201 double *d3 = &(operator()(0));
00202 for (unsigned i=0;i<v1.size();i++)
00203 {
00204 *d3++=*d1++ * *d2++;
00205 }
00206 }
00207
00208
00209
00210 void VecDouble::copyRow(unsigned row,Matrix &M)
00211 {
00212 assert(M.n() == size());
00213 for (unsigned i=0;i<size();i++)
00214 {
00215 (*this)(i)=M(row,i);
00216 }
00217 }
00218
00219
00220
00221 void VecDouble::set(double d1,double d2)
00222 {
00223 reinit(2);
00224 operator()(0)=d1;
00225 operator()(1)=d2;
00226 }