Path: blob/devel/ElmerGUI/netgen/libsrc/linalg/vector.cpp
3206 views
#ifdef abc1#include <mystdlib.h>2#include <linalg.hpp>3#include <algorithm>45namespace netgen6{78double BaseVector :: shit = 0;910// %FD Constructs a vector of length zero11BaseVector :: BaseVector ()12{13length = 0;14}1516// %FD Constructs a vector of given length17BaseVector :: BaseVector (18INDEX alength // length of the vector19)20{21length = alength;22}2324// %FD Sets length of the vector, old vector will be destroyed25void26BaseVector :: SetLength (27INDEX alength // new length of the vector28)29{30length = alength;31}3233// %FD Changes length of the vector, old values stay valid34void35BaseVector :: ChangeLength (36INDEX alength // new length of the vector37)38{39length = alength;40}41424344// %FD { Write a vector with the help of the '<<' operator onto a stream }45ostream & // stream for further use46operator<< (47ostream & s, // stream to write vector onto48const BaseVector & v // vector to write49)50{51return v.Print (s);52}535455// %FD{ Divides every component of the vector by the scalar c.56// The function checks for division by zero }57BaseVector & // result vector58BaseVector :: operator/= (59double c // scalar to divide by60)61{62if (c)63return (*this) *= (1/c);64else65{66(*myerr) << "operator/=: division by zero" << endl;67return *this;68}69}707172// %FD Creates a copy of the object73BaseVector * // pointer to the new vector74BaseVector :: Copy () const75{76cerr << "Base_vector::Copy called" << endl << flush;77return NULL;78}7980818283void BaseVector :: GetElementVector (const ARRAY<INDEX> & pnum,84BaseVector & elvec) const85{86int i;87for (i = 1; i <= pnum.Size(); i++)88elvec(i) = (*this)(pnum.Get(i));89}9091void BaseVector :: SetElementVector (const ARRAY<INDEX> & pnum,92const BaseVector & elvec)93{94int i;95for (i = 1; i <= pnum.Size(); i++)96(*this)(pnum.Get(i)) = elvec(i);97}9899100void BaseVector :: AddElementVector (const ARRAY<INDEX> & pnum,101const BaseVector & elvec)102{103int i;104for (i = 1; i <= pnum.Size(); i++)105(*this)(pnum.Get(i)) += elvec(i);106}107108109110111112113114115116117118TempVector :: ~TempVector ()119{120delete vec;121}122123TempVector BaseVector :: operator+ (const BaseVector & v2) const124{125return (*Copy()) += v2;126}127128TempVector BaseVector :: operator- (const BaseVector & v2) const129{130return (*Copy()) -= v2;131}132133TempVector BaseVector :: operator- () const134{135return (*Copy()) *= -1;136}137138139TempVector operator* (const BaseVector & v1, double scal)140{141return (*v1.Copy()) *= scal;142}143144TempVector operator/ (const BaseVector & v1, double scal)145{146return (*v1.Copy()) /= scal;147}148149150TempVector operator* (double scal, const BaseVector & v1)151{152return v1 * scal;153}154155156157158159BaseVector * TempVector :: Copy () const160{161return vec->Copy();162}163164165166167168169170171172173double Vector :: shit = 0;174175class clVecpool176{177public:178ARRAY<double *> vecs;179ARRAY<INDEX> veclens;180181~clVecpool();182};183184clVecpool :: ~clVecpool()185{186int i;187for (i = 1; i <= vecs.Size(); i++)188delete vecs.Elem(i);189}190191static clVecpool vecpool;192193194195static double * NewDouble (INDEX len)196{197if (len < 10)198return new double[len];199else200{201int i;202for (i = 1; i <= vecpool.veclens.Size(); i++)203if (vecpool.veclens.Get(i) == len)204{205double * hvec = vecpool.vecs.Get(i);206vecpool.vecs.DeleteElement(i);207vecpool.veclens.DeleteElement(i);208return hvec;209}210211return new double[len];212}213}214215static void DeleteDouble (INDEX len, double * dp)216{217if (len < 10)218delete [] dp;219else220{221vecpool.vecs.Append (dp);222vecpool.veclens.Append (len);223}224}225226227228Vector :: Vector () : BaseVector()229{230data = NULL;231}232233Vector :: Vector (INDEX alength) : BaseVector (alength)234{235if (length)236{237// data = new double[length];238data = NewDouble (length);239240if (!data)241{242length = 0;243(*myerr) << "Vector not allocated" << endl;244}245}246else247data = NULL;248}249250251Vector :: Vector (const Vector & v2)252{253length = v2.length;254255if (length)256{257// data = new double[length];258data = NewDouble (length);259260if (data)261{262memcpy (data, v2.data, length * sizeof (double));263}264else265{266length = 0;267(*myerr) << "Vector::Vector : Vector not allocated" << endl;268}269}270else271data = NULL;272}273274275Vector :: ~Vector ()276{277// veclenfile << "~Vector delete: " << length << endl;278if (data)279{280DeleteDouble (length, data);281// delete [] data;282}283284}285286void Vector :: SetLength (INDEX alength)287{288if (length == alength) return;289290if (data)291{292DeleteDouble (length, data);293// delete [] data;294}295data = NULL;296length = alength;297298if (length == 0) return;299// data = new double[length];300data = NewDouble (length);301302if (!data)303{304length = 0;305(*myerr) << "Vector::SetLength: Vector not allocated" << endl;306}307}308309void Vector :: ChangeLength (INDEX alength)310{311(*mycout) << "Vector::ChangeLength called" << endl;312if (length == alength) return;313314if (alength == 0)315{316// delete [] data;317DeleteDouble (length, data);318length = 0;319return;320}321322double * olddata = data;323324data = NewDouble (alength);325// data = new double[alength];326if (!data)327{328length = 0;329(*myerr) << "Vector::SetLength: Vector not allocated" << endl;330delete [] olddata;331}332333memcpy (data, olddata, min2(alength, length));334335delete [] olddata;336length = alength;337}338339/// NEW RM340void Vector::SetBlockLength (INDEX /* blength */)341{342MyError("BaseVector::SetBlockLength was called for a Vector");343}344345346double & Vector :: operator() (INDEX i)347{348if (i >= 1 && i <= length) return Elem(i);349else (*myerr) << "\nindex " << i << " out of range ("350<< 1 << "," << Length() << ")\n";351return shit;352}353354double Vector :: operator() (INDEX i) const355{356if (i >= 1 && i <= length) return Get(i);357else (*myerr) << "\nindex " << i << " out of range ("358<< 1 << "," << Length() << ")\n" << flush;359return shit;360}361362363364double Vector :: SupNorm () const365{366double sup = 0;367368for (INDEX i = 1; i <= Length(); i++)369if (fabs (Get(i)) > sup)370sup = fabs(Get(i));371372return sup;373}374375double Vector :: L2Norm () const376{377double sum = 0;378379for (INDEX i = 1; i <= Length(); i++)380sum += Get(i) * Get(i);381382return sqrt (sum);383}384385double Vector :: L1Norm () const386{387double sum = 0;388389for (INDEX i = 1; i <= Length(); i++)390sum += fabs (Get(i));391392return sum;393}394395double Vector :: Max () const396{397if (!Length()) return 0;398double m = Get(1);399for (INDEX i = 2; i <= Length(); i++)400if (Get(i) > m) m = Get(i);401return m;402}403404double Vector :: Min () const405{406if (!Length()) return 0;407double m = Get(1);408for (INDEX i = 2; i <= Length(); i++)409if (Get(i) < m) m = Get(i);410return m;411}412413414/*415ostream & operator<<(ostream & s, const Vector & v)416{417int w = s.width();418if (v.Length())419{420s.width(0);421s << '(';422for (INDEX i = 1; i < v.Length(); i++)423{424s.width(w);425s << v.Get(i) << ",";426if (i % 8 == 0) s << endl << ' ';427}428s.width(w);429s << v.Get(v.Length()) << ')';430}431else432s << "(Vector not allocated)";433434return s;435}436*/437438ostream & Vector :: Print (ostream & s) const439{440int w = s.width();441if (Length())442{443s.width(0);444s << '(';445for (INDEX i = 1; i < Length(); i++)446{447s.width(w);448s << Get(i) << ",";449if (i % 8 == 0) s << endl << ' ';450}451s.width(w);452s << Get(Length()) << ')';453}454else455s << "(Vector not allocated)";456457return s;458}459460461462BaseVector & Vector :: operator+= (const BaseVector & v2)463{464const Vector & hv2 = v2.CastToVector();465466if (Length() == hv2.Length())467for (INDEX i = 1; i <= Length(); i++)468Elem (i) += hv2.Get(i);469else470(*myerr) << "operator+= illegal dimension" << endl;471return *this;472}473474BaseVector & Vector :: operator-= (const BaseVector & v2)475{476const Vector & hv2 = v2.CastToVector();477478if (Length() == hv2.Length())479for (INDEX i = 1; i <= Length(); i++)480Elem (i) -= hv2.Get(i);481else482(*myerr) << "operator-= illegal dimension" << endl;483return *this;484}485486BaseVector & Vector :: operator*= (double c)487{488for (INDEX i = 1; i <= Length(); i++)489Elem(i) *= c;490return *this;491}492493494495BaseVector & Vector :: Add (double scal, const BaseVector & v2)496{497const Vector & hv2 = v2.CastToVector();498499if (Length() == hv2.Length())500{501double * p1 = data;502double * p2 = hv2.data;503504for (INDEX i = Length(); i > 0; i--)505{506(*p1) += scal * (*p2);507p1++; p2++;508}509}510else511(*myerr) << "Vector::Add: illegal dimension" << endl;512return *this;513}514515BaseVector & Vector :: Add2 (double scal, const BaseVector & v2,516double scal3, const BaseVector & v3)517{518const Vector & hv2 = v2.CastToVector();519const Vector & hv3 = v3.CastToVector();520521if (Length() == hv2.Length())522{523double * p1 = data;524double * p2 = hv2.data;525double * p3 = hv3.data;526527for (INDEX i = Length(); i > 0; i--)528{529(*p1) += (scal * (*p2) + scal3 * (*p3));530p1++; p2++; p3++;531}532}533else534(*myerr) << "Vector::Add: illegal dimension" << endl;535return *this;536}537538BaseVector & Vector :: Set (double scal, const BaseVector & v2)539{540const Vector & hv2 = v2.CastToVector();541542if (Length() == hv2.Length())543{544double * p1 = data;545double * p2 = hv2.data;546547for (INDEX i = Length(); i > 0; i--)548{549(*p1) = scal * (*p2);550p1++; p2++;551}552}553else554(*myerr) << "Vector::Set: illegal dimension" << endl;555return *this;556}557558559BaseVector & Vector :: Set2 (double scal , const BaseVector & v2,560double scal3, const BaseVector & v3)561{562const Vector & hv2 = v2.CastToVector();563const Vector & hv3 = v3.CastToVector();564565if (Length() == hv2.Length() && Length() == hv3.Length())566{567double * p1 = data;568double * p2 = hv2.data;569double * p3 = hv3.data;570571for (INDEX i = Length(); i > 0; i--)572{573(*p1) = scal * (*p2) + scal3 * (*p3);574p1++; p2++; p3++;575}576}577else578(*myerr) << "Vector::Set: illegal dimension" << endl;579return *this;580}581582583void Vector :: GetPart (int startpos, BaseVector & v2) const584{585Vector & hv2 = v2.CastToVector();586587if (Length() >= startpos + v2.Length() - 1)588{589const double * p1 = &Get(startpos);590double * p2 = &hv2.Elem(1);591592memcpy (p2, p1, hv2.Length() * sizeof(double));593}594else595MyError ("Vector::GetPart: Vector to short");596}597598599// NEW RM600void Vector :: SetPart (int startpos, const BaseVector & v2)601{602const Vector & hv2 = v2.CastToVector();603INDEX i;604INDEX n = v2.Length();605606if (Length() >= startpos + n - 1)607{608double * p1 = &Elem(startpos);609const double * p2 = &hv2.Get(1);610611for (i = 1; i <= n; i++)612{613(*p1) = (*p2);614p1++;615p2++;616}617}618else619MyError ("Vector::SetPart: Vector to short");620}621622void Vector :: AddPart (int startpos, double val, const BaseVector & v2)623{624const Vector & hv2 = v2.CastToVector();625INDEX i;626INDEX n = v2.Length();627628if (Length() >= startpos + n - 1)629{630double * p1 = &Elem(startpos);631const double * p2 = &hv2.Get(1);632633for (i = 1; i <= n; i++)634{635(*p1) += val * (*p2);636p1++;637p2++;638}639}640else641MyError ("Vector::AddPart: Vector to short");642}643644645646647double Vector :: operator* (const BaseVector & v2) const648{649const Vector & hv2 = v2.CastToVector();650651double sum = 0;652double * p1 = data;653double * p2 = hv2.data;654655if (Length() == hv2.Length())656{657for (INDEX i = Length(); i > 0; i--)658{659sum += (*p1) * (*p2);660p1++; p2++;661}662}663else664(*myerr) << "Scalarproduct illegal dimension" << endl;665return sum;666}667668void Vector :: SetRandom ()669{670INDEX i;671for (i = 1; i <= Length(); i++)672Elem(i) = rand ();673674double l2 = L2Norm();675if (l2 > 0)676(*this) /= l2;677// Elem(i) = 1.0 / double(i);678// Elem(i) = drand48();679}680681682/*683TempVector Vector :: operator- () const684{685Vector & sum = *(Vector*)Copy();686687if (sum.Length () == Length())688{689for (INDEX i = 1; i <= Length(); i++)690sum.Set (i, Get(i));691}692else693(*myerr) << "operator+ (Vector, Vector): sum.Length() not ok" << endl;694return sum;695}696*/697698BaseVector & Vector::operator= (const Vector & v2)699{700SetLength (v2.Length());701702if (data == v2.data) return *this;703704if (v2.Length() == Length())705memcpy (data, v2.data, sizeof (double) * Length());706else707(*myerr) << "Vector::operator=: not allocated" << endl;708709return *this;710}711712BaseVector & Vector::operator= (const BaseVector & v2)713{714const Vector & hv2 = v2.CastToVector();715716SetLength (hv2.Length());717718if (data == hv2.data) return *this;719720if (hv2.Length() == Length())721memcpy (data, hv2.data, sizeof (double) * Length());722else723(*myerr) << "Vector::operator=: not allocated" << endl;724725return *this;726}727728729BaseVector & Vector::operator= (double scal)730{731if (!Length()) (*myerr) << "Vector::operator= (double) : data not allocated"732<< endl;733734for (INDEX i = 1; i <= Length(); i++)735Set (i, scal);736737return *this;738}739740741BaseVector * Vector :: Copy () const742{743return new Vector (*this);744}745746747void Vector :: Swap (BaseVector & v2)748{749Vector & hv2 = v2.CastToVector();750swap (length, hv2.length);751swap (data, hv2.data);752}753754755756757void Vector :: GetElementVector (const ARRAY<INDEX> & pnum,758BaseVector & elvec) const759{760int i;761Vector & helvec = elvec.CastToVector();762for (i = 1; i <= pnum.Size(); i++)763helvec.Elem(i) = Get(pnum.Get(i));764}765766void Vector :: SetElementVector (const ARRAY<INDEX> & pnum,767const BaseVector & elvec)768{769int i;770const Vector & helvec = elvec.CastToVector();771for (i = 1; i <= pnum.Size(); i++)772Elem(pnum.Get(i)) = helvec.Get(i);773}774775776void Vector :: AddElementVector (const ARRAY<INDEX> & pnum,777const BaseVector & elvec)778{779int i;780const Vector & helvec = elvec.CastToVector();781for (i = 1; i <= pnum.Size(); i++)782Elem(pnum.Get(i)) += helvec.Get(i);783}784}785#endif786787788