netgen/libsrc/linalg/vector.hpp

162 lines
3.4 KiB
C++
Raw Permalink 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 */
/* *************************************************************************/
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);
}
friend double operator* (const FlatVector & v1, const FlatVector & v2);
};
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
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
}
}
};
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;
}
#endif