Path: blob/devel/ElmerGUI/netgen/libsrc/linalg/densemat.hpp
3206 views
#ifndef FILE_DENSEMAT1#define FILE_DENSEMAT23/**************************************************************************/4/* File: densemat.hh */5/* Author: Joachim Schoeberl */6/* Date: 01. Oct. 94 */7/**************************************************************************/89/**10Data type dense matrix11*/121314#include <assert.h>151617class DenseMatrix18{19protected:20int height;21int width;22double * data;2324public:25///26DenseMatrix ();27///28DenseMatrix (int h, int w = 0);29///30DenseMatrix (const DenseMatrix & m2);31///32~DenseMatrix ();3334///35void SetSize (int h, int w = 0);3637int Height() const { return height; }38int Width() const {return width; }3940double & operator() (int i, int j) { return data[i*width+j]; }41double operator() (int i, int j) const { return data[i*width+j]; }42double & operator() (int i) { return data[i]; }43double operator() (int i) const { return data[i]; }4445///46DenseMatrix & operator= (const DenseMatrix & m2);47///48DenseMatrix & operator+= (const DenseMatrix & m2);49///50DenseMatrix & operator-= (const DenseMatrix & m2);5152///53DenseMatrix & operator= (double v);54///55DenseMatrix & operator*= (double v);5657///58void Mult (const FlatVector & v, FlatVector & prod) const59{60double sum;61const double * mp, * sp;62double * dp;6364#ifdef DEBUG65if (prod.Size() != height)66{67cerr << "Mult: wrong vector size " << endl;68assert (1);69// prod.SetSize (height);70}717273if (!height)74{75cout << "DenseMatrix::Mult height = 0" << endl;76}77if (!width)78{79cout << "DenseMatrix::Mult width = 0" << endl;80}8182if (width != v.Size())83{84(*myerr) << "\nMatrix and Vector don't fit" << endl;85}86else if (Height() != prod.Size())87{88(*myerr) << "Base_Matrix::operator*(Vector): prod vector not ok" << endl;89}90else91#endif92{93mp = data;94dp = &prod.Elem(1);95for (int i = 1; i <= height; i++)96{97sum = 0;98sp = &v.Get(1);99100for (int j = 1; j <= width; j++)101{102// sum += Get(i,j) * v.Get(j);103sum += *mp * *sp;104mp++;105sp++;106}107108*dp = sum;109dp++;110}111}112}113114///115void MultTrans (const Vector & v, Vector & prod) const;116///117void Residuum (const Vector & x, const Vector & b, Vector & res) const;118///119double Det () const;120121///122friend DenseMatrix operator* (const DenseMatrix & m1, const DenseMatrix & m2);123///124friend DenseMatrix operator+ (const DenseMatrix & m1, const DenseMatrix & m2);125126///127friend void Transpose (const DenseMatrix & m1, DenseMatrix & m2);128///129friend void Mult (const DenseMatrix & m1, const DenseMatrix & m2, DenseMatrix & m3);130///131friend void CalcInverse (const DenseMatrix & m1, DenseMatrix & m2);132///133friend void CalcAAt (const DenseMatrix & a, DenseMatrix & m2);134///135friend void CalcAtA (const DenseMatrix & a, DenseMatrix & m2);136///137friend void CalcABt (const DenseMatrix & a, const DenseMatrix & b, DenseMatrix & m2);138///139friend void CalcAtB (const DenseMatrix & a, const DenseMatrix & b, DenseMatrix & m2);140///141void Solve (const Vector & b, Vector & x) const;142///143void SolveDestroy (const Vector & b, Vector & x);144///145const double & Get(int i, int j) const { return data[(i-1)*width+j-1]; }146///147const double & Get(int i) const { return data[i-1]; }148///149void Set(int i, int j, double v) { data[(i-1)*width+j-1] = v; }150///151double & Elem(int i, int j) { return data[(i-1)*width+j-1]; }152///153const double & ConstElem(int i, int j) const { return data[(i-1)*width+j-1]; }154};155156157extern ostream & operator<< (ostream & ost, const DenseMatrix & m);158159160161template <int WIDTH>162class MatrixFixWidth163{164protected:165int height;166double * data;167168public:169///170MatrixFixWidth ()171{ height = 0; data = 0; }172///173MatrixFixWidth (int h)174{ height = h; data = new double[WIDTH*height]; }175///176~MatrixFixWidth ()177{ delete [] data; }178179void SetSize (int h)180{181if (h != height)182{183delete data;184height = h;185data = new double[WIDTH*height];186}187}188189///190int Height() const { return height; }191192///193int Width() const { return WIDTH; }194195///196MatrixFixWidth & operator= (double v)197{198for (int i = 0; i < height*WIDTH; i++)199data[i] = v;200return *this;201}202203///204void Mult (const FlatVector & v, FlatVector & prod) const205{206double sum;207const double * mp, * sp;208double * dp;209210/*211if (prod.Size() != height)212{213cerr << "MatrixFixWidth::Mult: wrong vector size " << endl;214assert (1);215}216*/217218mp = data;219dp = &prod[0];220for (int i = 0; i < height; i++)221{222sum = 0;223sp = &v[0];224225for (int j = 0; j < WIDTH; j++)226{227sum += *mp * *sp;228mp++;229sp++;230}231232*dp = sum;233dp++;234}235}236237double & operator() (int i, int j)238{ return data[i*WIDTH+j]; }239240const double & operator() (int i, int j) const241{ return data[i*WIDTH+j]; }242243244MatrixFixWidth & operator*= (double v)245{246if (data)247for (int i = 0; i < height*WIDTH; i++)248data[i] *= v;249return *this;250}251252253254const double & Get(int i, int j) const { return data[(i-1)*WIDTH+j-1]; }255///256const double & Get(int i) const { return data[i-1]; }257///258void Set(int i, int j, double v) { data[(i-1)*WIDTH+j-1] = v; }259///260double & Elem(int i, int j) { return data[(i-1)*WIDTH+j-1]; }261///262const double & ConstElem(int i, int j) const { return data[(i-1)*WIDTH+j-1]; }263};264265266template <int WIDTH>267extern ostream & operator<< (ostream & ost, const MatrixFixWidth<WIDTH> & m)268{269for (int i = 0; i < m.Height(); i++)270{271for (int j = 0; j < m.Width(); j++)272ost << m.Get(i+1,j+1) << " ";273ost << endl;274}275return ost;276};277278279280extern void CalcInverse (const DenseMatrix & m1, DenseMatrix & m2);281282283#endif284285286