netgen/libsrc/linalg/vector.hpp

218 lines
4.6 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
#ifndef FILE_VECTOR
#define FILE_VECTOR
/* *************************************************************************/
/* File: vector.hpp */
/* Author: Joachim Schoeberl */
/* Date: 01. Oct. 94 */
/* *************************************************************************/
namespace netgen
{
2016-07-07 21:31:09 +05:00
template <typename T>
class TFlatVector
{
protected:
int s;
T * data;
public:
TFlatVector () { ; }
TFlatVector (int as, T * adata)
{ s = as; data = adata; }
int Size () const
{ return s; }
TFlatVector & operator= (const TFlatVector & v)
{ memcpy (data, v.data, s*sizeof(T)); return *this; }
TFlatVector & operator= (T scal)
{
for (int i = 0; i < s; i++) data[i] = scal;
return *this;
}
T & operator[] (int i) { return data[i]; }
const T & operator[] (int i) const { return data[i]; }
T & operator() (int i) { return data[i]; }
const T & operator() (int i) const { return data[i]; }
// double & Elem (int i) { return data[i-1]; }
// const double & Get (int i) const { return data[i-1]; }
// void Set (int i, double val) { data[i-1] = val; }
TFlatVector & operator*= (T scal)
{
for (int i = 0; i < s; i++) data[i] *= scal;
return *this;
}
};
2009-01-13 04:40:13 +05:00
class FlatVector
{
protected:
int s;
double *data;
public:
FlatVector () { ; }
FlatVector (int as, double * adata)
{ s = as; data = adata; }
int Size () const
{ return s; }
FlatVector & operator= (const FlatVector & v)
{ memcpy (data, v.data, s*sizeof(double)); return *this; }
FlatVector & operator= (double scal)
{
for (int i = 0; i < s; i++) data[i] = scal;
return *this;
}
double & operator[] (int i) { return data[i]; }
const double & operator[] (int i) const { return data[i]; }
double & operator() (int i) { return data[i]; }
const double & operator() (int i) const { return data[i]; }
// double & Elem (int i) { return data[i-1]; }
// const double & Get (int i) const { return data[i-1]; }
// void Set (int i, double val) { data[i-1] = val; }
2009-01-13 04:40:13 +05:00
FlatVector & operator*= (double scal)
{
for (int i = 0; i < s; i++) data[i] *= scal;
return *this;
}
FlatVector & Add (double scal, const FlatVector & v2)
{
for (int i = 0; i < s; i++)
data[i] += scal * v2[i];
return *this;
}
FlatVector & Set (double scal, const FlatVector & v2)
{
for (int i = 0; i < s; i++)
data[i] = scal * v2[i];
return *this;
}
FlatVector & Set2 (double scal1, const FlatVector & v1,
double scal2, const FlatVector & v2)
{
for (int i = 0; i < s; i++)
data[i] = scal1 * v1[i] + scal2 * v2[i];
return *this;
}
double L2Norm() const
{
double sum = 0;
for (int i = 0; i < s; i++)
sum += data[i] * data[i];
return sqrt (sum);
}
2016-07-07 21:31:09 +05:00
operator TFlatVector<double> () const { return TFlatVector<double> (s, data); }
2009-01-13 04:40:13 +05:00
friend double operator* (const FlatVector & v1, const FlatVector & v2);
};
2009-01-13 04:40:13 +05:00
class Vector : public FlatVector
{
2009-07-22 22:05:58 +06:00
bool ownmem;
2009-01-13 04:40:13 +05:00
public:
Vector ()
2009-07-22 22:05:58 +06:00
{ s = 0; data = 0; ownmem = false; }
2009-01-13 04:40:13 +05:00
Vector (int as)
2009-07-22 22:05:58 +06:00
{ s = as; data = new double[s]; ownmem = true; }
Vector (int as, double * mem)
{ s = as; data = mem; ownmem = false; }
2009-01-13 04:40:13 +05:00
~Vector ()
2009-07-22 22:05:58 +06:00
{ if (ownmem) delete [] data; }
2009-01-13 04:40:13 +05:00
template<typename ARCHIVE>
void DoArchive(ARCHIVE& ar)
2018-12-06 21:53:44 +05:00
{
auto size = s;
ar & ownmem & size;
if(!ar.Output())
SetSize(size);
ar.Do(data, size);
}
2009-01-13 04:40:13 +05:00
Vector & operator= (const FlatVector & v)
{ memcpy (data, &v(0), s*sizeof(double)); return *this; }
2009-01-13 04:40:13 +05:00
Vector & operator= (double scal)
{
for (int i = 0; i < s; i++) data[i] = scal;
return *this;
}
void SetSize (int as)
{
if (s != as)
{
s = as;
2009-07-22 22:05:58 +06:00
if (ownmem) delete [] data;
2009-01-13 04:40:13 +05:00
data = new double [s];
2009-07-22 22:05:58 +06:00
ownmem = true;
2009-01-13 04:40:13 +05:00
}
}
2016-07-07 21:31:09 +05:00
operator TFlatVector<double> () const { return TFlatVector<double> (s, data); }
2009-01-13 04:40:13 +05:00
};
2009-07-22 22:05:58 +06:00
template <int S>
class VectorMem : public Vector
{
double mem[S];
public:
VectorMem () : Vector(S, &mem[0]) { ; }
VectorMem & operator= (const FlatVector & v)
{ memcpy (data, &v(0), S*sizeof(double)); return *this; }
VectorMem & operator= (double scal)
{
for (int i = 0; i < S; i++) data[i] = scal;
return *this;
}
};
2009-01-13 04:40:13 +05:00
inline double operator* (const FlatVector & v1, const FlatVector & v2)
{
double sum = 0;
for (int i = 0; i < v1.s; i++)
sum += v1.data[i] * v2.data[i];
return sum;
}
inline ostream & operator<< (ostream & ost, const FlatVector & v)
{
for (int i = 0; i < v.Size(); i++)
ost << " " << setw(7) << v[i];
return ost;
}
} //namespace netgen
2009-01-13 04:40:13 +05:00
#endif