netgen/libsrc/linalg/vector.cpp

787 lines
16 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
#ifdef abc
#include <mystdlib.h>
#include <linalg.hpp>
#include <algorithm>
namespace netgen
{
double BaseVector :: shit = 0;
2009-01-13 04:40:13 +05:00
// %FD Constructs a vector of length zero
BaseVector :: BaseVector ()
2009-01-13 04:40:13 +05:00
{
length = 0;
2009-01-13 04:40:13 +05:00
}
// %FD Constructs a vector of given length
BaseVector :: BaseVector (
INDEX alength // length of the vector
)
2009-01-13 04:40:13 +05:00
{
length = alength;
2009-01-13 04:40:13 +05:00
}
// %FD Sets length of the vector, old vector will be destroyed
void
BaseVector :: SetLength (
INDEX alength // new length of the vector
)
2009-01-13 04:40:13 +05:00
{
length = alength;
2009-01-13 04:40:13 +05:00
}
// %FD Changes length of the vector, old values stay valid
void
BaseVector :: ChangeLength (
INDEX alength // new length of the vector
)
2009-01-13 04:40:13 +05:00
{
length = alength;
2009-01-13 04:40:13 +05:00
}
// %FD { Write a vector with the help of the '<<' operator onto a stream }
ostream & // stream for further use
operator<< (
ostream & s, // stream to write vector onto
const BaseVector & v // vector to write
)
2009-01-13 04:40:13 +05:00
{
return v.Print (s);
2009-01-13 04:40:13 +05:00
}
// %FD{ Divides every component of the vector by the scalar c.
// The function checks for division by zero }
BaseVector & // result vector
BaseVector :: operator/= (
double c // scalar to divide by
)
2009-01-13 04:40:13 +05:00
{
if (c)
return (*this) *= (1/c);
else
{
(*myerr) << "operator/=: division by zero" << endl;
return *this;
}
2009-01-13 04:40:13 +05:00
}
// %FD Creates a copy of the object
BaseVector * // pointer to the new vector
BaseVector :: Copy () const
2009-01-13 04:40:13 +05:00
{
cerr << "Base_vector::Copy called" << endl << flush;
return NULL;
2009-01-13 04:40:13 +05:00
}
void BaseVector :: GetElementVector (const Array<INDEX> & pnum,
BaseVector & elvec) const
{
int i;
for (i = 1; i <= pnum.Size(); i++)
elvec(i) = (*this)(pnum.Get(i));
}
2009-01-13 04:40:13 +05:00
void BaseVector :: SetElementVector (const Array<INDEX> & pnum,
const BaseVector & elvec)
{
int i;
for (i = 1; i <= pnum.Size(); i++)
(*this)(pnum.Get(i)) = elvec(i);
}
2009-01-13 04:40:13 +05:00
void BaseVector :: AddElementVector (const Array<INDEX> & pnum,
const BaseVector & elvec)
{
int i;
for (i = 1; i <= pnum.Size(); i++)
(*this)(pnum.Get(i)) += elvec(i);
}
2009-01-13 04:40:13 +05:00
TempVector :: ~TempVector ()
2009-01-13 04:40:13 +05:00
{
delete vec;
2009-01-13 04:40:13 +05:00
}
TempVector BaseVector :: operator+ (const BaseVector & v2) const
2009-01-13 04:40:13 +05:00
{
return (*Copy()) += v2;
2009-01-13 04:40:13 +05:00
}
TempVector BaseVector :: operator- (const BaseVector & v2) const
2009-01-13 04:40:13 +05:00
{
return (*Copy()) -= v2;
2009-01-13 04:40:13 +05:00
}
TempVector BaseVector :: operator- () const
2009-01-13 04:40:13 +05:00
{
return (*Copy()) *= -1;
2009-01-13 04:40:13 +05:00
}
TempVector operator* (const BaseVector & v1, double scal)
2009-01-13 04:40:13 +05:00
{
return (*v1.Copy()) *= scal;
2009-01-13 04:40:13 +05:00
}
TempVector operator/ (const BaseVector & v1, double scal)
2009-01-13 04:40:13 +05:00
{
return (*v1.Copy()) /= scal;
2009-01-13 04:40:13 +05:00
}
TempVector operator* (double scal, const BaseVector & v1)
2009-01-13 04:40:13 +05:00
{
return v1 * scal;
2009-01-13 04:40:13 +05:00
}
BaseVector * TempVector :: Copy () const
2009-01-13 04:40:13 +05:00
{
return vec->Copy();
2009-01-13 04:40:13 +05:00
}
double Vector :: shit = 0;
2009-01-13 04:40:13 +05:00
class clVecpool
{
public:
Array<double *> vecs;
Array<INDEX> veclens;
2009-01-13 04:40:13 +05:00
~clVecpool();
};
2009-01-13 04:40:13 +05:00
clVecpool :: ~clVecpool()
{
int i;
for (i = 1; i <= vecs.Size(); i++)
delete vecs.Elem(i);
}
2009-01-13 04:40:13 +05:00
static clVecpool vecpool;
2009-01-13 04:40:13 +05:00
static double * NewDouble (INDEX len)
{
if (len < 10)
2009-01-13 04:40:13 +05:00
return new double[len];
else
{
int i;
for (i = 1; i <= vecpool.veclens.Size(); i++)
if (vecpool.veclens.Get(i) == len)
{
double * hvec = vecpool.vecs.Get(i);
vecpool.vecs.DeleteElement(i);
vecpool.veclens.DeleteElement(i);
return hvec;
}
return new double[len];
}
}
2009-01-13 04:40:13 +05:00
static void DeleteDouble (INDEX len, double * dp)
{
if (len < 10)
delete [] dp;
else
{
vecpool.vecs.Append (dp);
vecpool.veclens.Append (len);
}
}
2009-01-13 04:40:13 +05:00
Vector :: Vector () : BaseVector()
2009-01-13 04:40:13 +05:00
{
data = NULL;
2009-01-13 04:40:13 +05:00
}
Vector :: Vector (INDEX alength) : BaseVector (alength)
2009-01-13 04:40:13 +05:00
{
if (length)
2009-01-13 04:40:13 +05:00
{
// data = new double[length];
data = NewDouble (length);
if (!data)
{
length = 0;
(*myerr) << "Vector not allocated" << endl;
}
2009-01-13 04:40:13 +05:00
}
else
data = NULL;
2009-01-13 04:40:13 +05:00
}
Vector :: Vector (const Vector & v2)
2009-01-13 04:40:13 +05:00
{
length = v2.length;
2009-01-13 04:40:13 +05:00
if (length)
2009-01-13 04:40:13 +05:00
{
// data = new double[length];
data = NewDouble (length);
if (data)
{
memcpy (data, v2.data, length * sizeof (double));
}
else
{
length = 0;
(*myerr) << "Vector::Vector : Vector not allocated" << endl;
}
2009-01-13 04:40:13 +05:00
}
else
data = NULL;
2009-01-13 04:40:13 +05:00
}
Vector :: ~Vector ()
{
// veclenfile << "~Vector delete: " << length << endl;
if (data)
{
DeleteDouble (length, data);
// delete [] data;
}
2009-01-13 04:40:13 +05:00
}
2009-01-13 04:40:13 +05:00
void Vector :: SetLength (INDEX alength)
2009-01-13 04:40:13 +05:00
{
if (length == alength) return;
2009-01-13 04:40:13 +05:00
if (data)
{
DeleteDouble (length, data);
// delete [] data;
}
data = NULL;
length = alength;
2009-01-13 04:40:13 +05:00
if (length == 0) return;
// data = new double[length];
data = NewDouble (length);
2009-01-13 04:40:13 +05:00
if (!data)
{
length = 0;
(*myerr) << "Vector::SetLength: Vector not allocated" << endl;
}
2009-01-13 04:40:13 +05:00
}
void Vector :: ChangeLength (INDEX alength)
{
(*mycout) << "Vector::ChangeLength called" << endl;
if (length == alength) return;
2009-01-13 04:40:13 +05:00
if (alength == 0)
{
// delete [] data;
DeleteDouble (length, data);
length = 0;
return;
}
2009-01-13 04:40:13 +05:00
double * olddata = data;
2009-01-13 04:40:13 +05:00
data = NewDouble (alength);
// data = new double[alength];
if (!data)
{
length = 0;
(*myerr) << "Vector::SetLength: Vector not allocated" << endl;
delete [] olddata;
}
2009-01-13 04:40:13 +05:00
memcpy (data, olddata, min2(alength, length));
2009-01-13 04:40:13 +05:00
delete [] olddata;
length = alength;
2009-01-13 04:40:13 +05:00
}
/// NEW RM
void Vector::SetBlockLength (INDEX /* blength */)
{
MyError("BaseVector::SetBlockLength was called for a Vector");
}
2009-01-13 04:40:13 +05:00
double & Vector :: operator() (INDEX i)
2009-01-13 04:40:13 +05:00
{
if (i >= 1 && i <= length) return Elem(i);
else (*myerr) << "\nindex " << i << " out of range ("
<< 1 << "," << Length() << ")\n";
return shit;
2009-01-13 04:40:13 +05:00
}
double Vector :: operator() (INDEX i) const
2009-01-13 04:40:13 +05:00
{
if (i >= 1 && i <= length) return Get(i);
else (*myerr) << "\nindex " << i << " out of range ("
<< 1 << "," << Length() << ")\n" << flush;
return shit;
2009-01-13 04:40:13 +05:00
}
double Vector :: SupNorm () const
2009-01-13 04:40:13 +05:00
{
double sup = 0;
2009-01-13 04:40:13 +05:00
for (INDEX i = 1; i <= Length(); i++)
if (fabs (Get(i)) > sup)
sup = fabs(Get(i));
2009-01-13 04:40:13 +05:00
return sup;
2009-01-13 04:40:13 +05:00
}
double Vector :: L2Norm () const
2009-01-13 04:40:13 +05:00
{
double sum = 0;
2009-01-13 04:40:13 +05:00
for (INDEX i = 1; i <= Length(); i++)
sum += Get(i) * Get(i);
2009-01-13 04:40:13 +05:00
return sqrt (sum);
2009-01-13 04:40:13 +05:00
}
double Vector :: L1Norm () const
2009-01-13 04:40:13 +05:00
{
double sum = 0;
2009-01-13 04:40:13 +05:00
for (INDEX i = 1; i <= Length(); i++)
sum += fabs (Get(i));
2009-01-13 04:40:13 +05:00
return sum;
2009-01-13 04:40:13 +05:00
}
double Vector :: Max () const
2009-01-13 04:40:13 +05:00
{
if (!Length()) return 0;
double m = Get(1);
for (INDEX i = 2; i <= Length(); i++)
if (Get(i) > m) m = Get(i);
return m;
2009-01-13 04:40:13 +05:00
}
double Vector :: Min () const
2009-01-13 04:40:13 +05:00
{
if (!Length()) return 0;
double m = Get(1);
for (INDEX i = 2; i <= Length(); i++)
if (Get(i) < m) m = Get(i);
return m;
2009-01-13 04:40:13 +05:00
}
/*
ostream & operator<<(ostream & s, const Vector & v)
{
int w = s.width();
if (v.Length())
2009-01-13 04:40:13 +05:00
{
s.width(0);
s << '(';
for (INDEX i = 1; i < v.Length(); i++)
{
s.width(w);
s << v.Get(i) << ",";
if (i % 8 == 0) s << endl << ' ';
}
2009-01-13 04:40:13 +05:00
s.width(w);
s << v.Get(v.Length()) << ')';
}
else
2009-01-13 04:40:13 +05:00
s << "(Vector not allocated)";
return s;
}
*/
2009-01-13 04:40:13 +05:00
ostream & Vector :: Print (ostream & s) const
2009-01-13 04:40:13 +05:00
{
int w = s.width();
if (Length())
2009-01-13 04:40:13 +05:00
{
s.width(0);
s << '(';
for (INDEX i = 1; i < Length(); i++)
{
s.width(w);
s << Get(i) << ",";
if (i % 8 == 0) s << endl << ' ';
}
s.width(w);
s << Get(Length()) << ')';
2009-01-13 04:40:13 +05:00
}
else
s << "(Vector not allocated)";
2009-01-13 04:40:13 +05:00
return s;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: operator+= (const BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
for (INDEX i = 1; i <= Length(); i++)
Elem (i) += hv2.Get(i);
else
(*myerr) << "operator+= illegal dimension" << endl;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: operator-= (const BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
for (INDEX i = 1; i <= Length(); i++)
Elem (i) -= hv2.Get(i);
else
(*myerr) << "operator-= illegal dimension" << endl;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: operator*= (double c)
2009-01-13 04:40:13 +05:00
{
for (INDEX i = 1; i <= Length(); i++)
Elem(i) *= c;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: Add (double scal, const BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
2009-01-13 04:40:13 +05:00
{
double * p1 = data;
double * p2 = hv2.data;
for (INDEX i = Length(); i > 0; i--)
{
(*p1) += scal * (*p2);
p1++; p2++;
}
2009-01-13 04:40:13 +05:00
}
else
(*myerr) << "Vector::Add: illegal dimension" << endl;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: Add2 (double scal, const BaseVector & v2,
double scal3, const BaseVector & v3)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
const Vector & hv3 = v3.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
2009-01-13 04:40:13 +05:00
{
double * p1 = data;
double * p2 = hv2.data;
double * p3 = hv3.data;
for (INDEX i = Length(); i > 0; i--)
{
(*p1) += (scal * (*p2) + scal3 * (*p3));
p1++; p2++; p3++;
}
2009-01-13 04:40:13 +05:00
}
else
(*myerr) << "Vector::Add: illegal dimension" << endl;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: Set (double scal, const BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
2009-01-13 04:40:13 +05:00
{
double * p1 = data;
double * p2 = hv2.data;
for (INDEX i = Length(); i > 0; i--)
{
(*p1) = scal * (*p2);
p1++; p2++;
}
2009-01-13 04:40:13 +05:00
}
else
(*myerr) << "Vector::Set: illegal dimension" << endl;
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector :: Set2 (double scal , const BaseVector & v2,
double scal3, const BaseVector & v3)
{
const Vector & hv2 = v2.CastToVector();
const Vector & hv3 = v3.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length() && Length() == hv3.Length())
{
double * p1 = data;
double * p2 = hv2.data;
double * p3 = hv3.data;
2009-01-13 04:40:13 +05:00
for (INDEX i = Length(); i > 0; i--)
{
(*p1) = scal * (*p2) + scal3 * (*p3);
p1++; p2++; p3++;
}
}
else
(*myerr) << "Vector::Set: illegal dimension" << endl;
return *this;
}
2009-01-13 04:40:13 +05:00
void Vector :: GetPart (int startpos, BaseVector & v2) const
{
Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
if (Length() >= startpos + v2.Length() - 1)
{
const double * p1 = &Get(startpos);
double * p2 = &hv2.Elem(1);
2009-01-13 04:40:13 +05:00
memcpy (p2, p1, hv2.Length() * sizeof(double));
}
else
MyError ("Vector::GetPart: Vector to short");
}
2009-01-13 04:40:13 +05:00
// NEW RM
void Vector :: SetPart (int startpos, const BaseVector & v2)
{
const Vector & hv2 = v2.CastToVector();
INDEX i;
INDEX n = v2.Length();
2009-01-13 04:40:13 +05:00
if (Length() >= startpos + n - 1)
{
double * p1 = &Elem(startpos);
const double * p2 = &hv2.Get(1);
for (i = 1; i <= n; i++)
{
(*p1) = (*p2);
p1++;
p2++;
}
}
else
MyError ("Vector::SetPart: Vector to short");
}
2009-01-13 04:40:13 +05:00
void Vector :: AddPart (int startpos, double val, const BaseVector & v2)
{
const Vector & hv2 = v2.CastToVector();
INDEX i;
INDEX n = v2.Length();
2009-01-13 04:40:13 +05:00
if (Length() >= startpos + n - 1)
{
double * p1 = &Elem(startpos);
const double * p2 = &hv2.Get(1);
for (i = 1; i <= n; i++)
{
(*p1) += val * (*p2);
p1++;
p2++;
}
}
else
MyError ("Vector::AddPart: Vector to short");
}
2009-01-13 04:40:13 +05:00
double Vector :: operator* (const BaseVector & v2) const
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
double sum = 0;
double * p1 = data;
double * p2 = hv2.data;
2009-01-13 04:40:13 +05:00
if (Length() == hv2.Length())
2009-01-13 04:40:13 +05:00
{
for (INDEX i = Length(); i > 0; i--)
{
sum += (*p1) * (*p2);
p1++; p2++;
}
2009-01-13 04:40:13 +05:00
}
else
(*myerr) << "Scalarproduct illegal dimension" << endl;
return sum;
2009-01-13 04:40:13 +05:00
}
void Vector :: SetRandom ()
2009-01-13 04:40:13 +05:00
{
INDEX i;
for (i = 1; i <= Length(); i++)
Elem(i) = rand ();
2009-01-13 04:40:13 +05:00
double l2 = L2Norm();
if (l2 > 0)
(*this) /= l2;
2009-01-13 04:40:13 +05:00
// Elem(i) = 1.0 / double(i);
// Elem(i) = drand48();
}
/*
TempVector Vector :: operator- () const
{
Vector & sum = *(Vector*)Copy();
2009-01-13 04:40:13 +05:00
if (sum.Length () == Length())
2009-01-13 04:40:13 +05:00
{
for (INDEX i = 1; i <= Length(); i++)
sum.Set (i, Get(i));
2009-01-13 04:40:13 +05:00
}
else
2009-01-13 04:40:13 +05:00
(*myerr) << "operator+ (Vector, Vector): sum.Length() not ok" << endl;
return sum;
}
*/
2009-01-13 04:40:13 +05:00
BaseVector & Vector::operator= (const Vector & v2)
2009-01-13 04:40:13 +05:00
{
SetLength (v2.Length());
2009-01-13 04:40:13 +05:00
if (data == v2.data) return *this;
2009-01-13 04:40:13 +05:00
if (v2.Length() == Length())
memcpy (data, v2.data, sizeof (double) * Length());
else
(*myerr) << "Vector::operator=: not allocated" << endl;
2009-01-13 04:40:13 +05:00
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector::operator= (const BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
const Vector & hv2 = v2.CastToVector();
2009-01-13 04:40:13 +05:00
SetLength (hv2.Length());
2009-01-13 04:40:13 +05:00
if (data == hv2.data) return *this;
2009-01-13 04:40:13 +05:00
if (hv2.Length() == Length())
memcpy (data, hv2.data, sizeof (double) * Length());
else
(*myerr) << "Vector::operator=: not allocated" << endl;
2009-01-13 04:40:13 +05:00
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector & Vector::operator= (double scal)
2009-01-13 04:40:13 +05:00
{
if (!Length()) (*myerr) << "Vector::operator= (double) : data not allocated"
<< endl;
2009-01-13 04:40:13 +05:00
for (INDEX i = 1; i <= Length(); i++)
Set (i, scal);
2009-01-13 04:40:13 +05:00
return *this;
2009-01-13 04:40:13 +05:00
}
BaseVector * Vector :: Copy () const
2009-01-13 04:40:13 +05:00
{
return new Vector (*this);
2009-01-13 04:40:13 +05:00
}
void Vector :: Swap (BaseVector & v2)
2009-01-13 04:40:13 +05:00
{
Vector & hv2 = v2.CastToVector();
swap (length, hv2.length);
swap (data, hv2.data);
2009-01-13 04:40:13 +05:00
}
void Vector :: GetElementVector (const Array<INDEX> & pnum,
BaseVector & elvec) const
{
int i;
Vector & helvec = elvec.CastToVector();
for (i = 1; i <= pnum.Size(); i++)
helvec.Elem(i) = Get(pnum.Get(i));
}
2009-01-13 04:40:13 +05:00
void Vector :: SetElementVector (const Array<INDEX> & pnum,
const BaseVector & elvec)
{
int i;
const Vector & helvec = elvec.CastToVector();
for (i = 1; i <= pnum.Size(); i++)
Elem(pnum.Get(i)) = helvec.Get(i);
}
2009-01-13 04:40:13 +05:00
void Vector :: AddElementVector (const Array<INDEX> & pnum,
const BaseVector & elvec)
{
int i;
const Vector & helvec = elvec.CastToVector();
for (i = 1; i <= pnum.Size(); i++)
Elem(pnum.Get(i)) += helvec.Get(i);
}
2009-01-13 04:40:13 +05:00
}
#endif