Skip to content
Snippets Groups Projects
Commit 4344bf6d authored by Axel Modave's avatar Axel Modave
Browse files

pp

parent b01261ae
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,16 @@ ...@@ -17,6 +17,16 @@
#define F77NAME(x) (x##_) #define F77NAME(x) (x##_)
#endif #endif
/*==============================================================================
* This file improve the methods of fullVector and fullMatrix using
* - BLAS : Basic Linear Algebra Subprograms
* - LAPACK : Linear Algebra PACKage
*============================================================================*/
#if defined(HAVE_BLAS) #if defined(HAVE_BLAS)
extern "C" { extern "C" {
...@@ -130,6 +140,8 @@ void fullMatrix<std::complex<double> >::mult(const fullVector<std::complex<doubl ...@@ -130,6 +140,8 @@ void fullMatrix<std::complex<double> >::mult(const fullVector<std::complex<doubl
#endif #endif
#if defined(HAVE_LAPACK) #if defined(HAVE_LAPACK)
extern "C" { extern "C" {
...@@ -307,6 +319,12 @@ bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S) ...@@ -307,6 +319,12 @@ bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S)
#endif #endif
/*==============================================================================
* BINDINGS
*============================================================================*/
#include "Bindings.h" #include "Bindings.h"
template<> template<>
...@@ -317,6 +335,9 @@ void fullMatrix<double>::registerBindings(binding *b) ...@@ -317,6 +335,9 @@ void fullMatrix<double>::registerBindings(binding *b)
"The memory is allocated in one continuous block and stored " "The memory is allocated in one continuous block and stored "
"in column major order (like in fortran)."); "in column major order (like in fortran).");
methodBinding *cm; methodBinding *cm;
cm = cb->setConstructor<fullMatrix<double>,int,int>();
cm->setDescription ("A new matrix of size 'nRows' x 'nColumns'");
cm->setArgNames("nRows","nColumns",NULL);
cm = cb->addMethod("size1", &fullMatrix<double>::size1); cm = cb->addMethod("size1", &fullMatrix<double>::size1);
cm->setDescription("Returns the number of rows in the matrix"); cm->setDescription("Returns the number of rows in the matrix");
cm = cb->addMethod("size2", &fullMatrix<double>::size2); cm = cb->addMethod("size2", &fullMatrix<double>::size2);
...@@ -340,9 +361,6 @@ void fullMatrix<double>::registerBindings(binding *b) ...@@ -340,9 +361,6 @@ void fullMatrix<double>::registerBindings(binding *b)
cm = cb->addMethod("print", &fullMatrix<double>::print); cm = cb->addMethod("print", &fullMatrix<double>::print);
cm->setArgNames("name",NULL); cm->setArgNames("name",NULL);
cm->setDescription("print the matrix"); cm->setDescription("print the matrix");
cm = cb->setConstructor<fullMatrix<double>,int,int>();
cm->setDescription ("A new matrix of size 'nRows' x 'nColumns'");
cm->setArgNames("nRows","nColumns",NULL);
cm = cb->addMethod("invertInPlace", &fullMatrix<double>::invertInPlace); cm = cb->addMethod("invertInPlace", &fullMatrix<double>::invertInPlace);
cm->setDescription("invert the matrix and return the determinant"); cm->setDescription("invert the matrix and return the determinant");
} }
...@@ -14,29 +14,57 @@ ...@@ -14,29 +14,57 @@
class binding; class binding;
template <class scalar> class fullMatrix; template <class scalar> class fullMatrix;
/*==============================================================================
* class fullVector : An abstract interface for vectors of scalar
*============================================================================*/
template <class scalar> template <class scalar>
class fullVector class fullVector
{ {
private: private:
int _r;
scalar *_data; int _r; // Size of the vector
scalar *_data; // Pointer on the first element
friend class fullMatrix<scalar>; friend class fullMatrix<scalar>;
public: public:
inline const scalar* getDataPtr() const{
return _data; // Constructor and destructor
}
fullVector(void) : _r(0),_data(0) {}
fullVector(int r) : _r(r) fullVector(int r) : _r(r)
{ {
_data = new scalar[_r]; _data = new scalar[_r];
setAll(0.); setAll(0.);
} }
fullVector(void) : _r(0),_data(0) {}
fullVector(const fullVector<scalar> &other) : _r(other._r) fullVector(const fullVector<scalar> &other) : _r(other._r)
{ {
_data = new scalar[_r]; _data = new scalar[_r];
for(int i = 0; i < _r; ++i) _data[i] = other._data[i]; for(int i = 0; i < _r; ++i) _data[i] = other._data[i];
} }
~fullVector() { if(_data) delete [] _data; } ~fullVector()
{
if(_data) delete [] _data;
}
// Get information (size, value)
inline int size() const { return _r; }
inline const scalar * getDataPtr() const { return _data; }
inline scalar operator () (int i) const { return _data[i]; }
inline scalar & operator () (int i) { return _data[i]; }
// Operations
inline scalar norm() const
{
scalar n = 0.;
for(int i = 0; i < _r; ++i) n += _data[i] * _data[i];
return sqrt(n);
}
bool resize(int r) bool resize(int r)
{ {
if (_r < r) { if (_r < r) {
...@@ -49,26 +77,10 @@ class fullVector ...@@ -49,26 +77,10 @@ class fullVector
} }
void setAsProxy(const fullVector<scalar> &original, int r_start, int r) void setAsProxy(const fullVector<scalar> &original, int r_start, int r)
{ {
if(_data) if(_data) delete [] _data;
delete [] _data;
_r = r; _r = r;
_data = original._data + r_start; _data = original._data + r_start;
} }
inline scalar operator () (int i) const
{
return _data[i];
}
inline int size() const { return _r; }
inline scalar & operator () (int i)
{
return _data[i];
}
inline scalar norm() const
{
scalar n = 0.;
for(int i = 0; i < _r; ++i) n += _data[i] * _data[i];
return sqrt(n);
}
inline void scale(const scalar s) inline void scale(const scalar s)
{ {
if(s == 0.) if(s == 0.)
...@@ -103,6 +115,9 @@ class fullVector ...@@ -103,6 +115,9 @@ class fullVector
{ {
for (int i = 0; i < _r; i++) _data[i] *= x._data[i]; for (int i = 0; i < _r; i++) _data[i] *= x._data[i];
} }
// Printing and file treatment
void print(const char *name="") const void print(const char *name="") const
{ {
printf("Printing vector %s:\n", name); printf("Printing vector %s:\n", name);
...@@ -112,46 +127,36 @@ class fullVector ...@@ -112,46 +127,36 @@ class fullVector
} }
printf("\n"); printf("\n");
} }
void binarySave (FILE *f) const{ void binarySave (FILE *f) const
{
fwrite (_data, sizeof(scalar), _r, f); fwrite (_data, sizeof(scalar), _r, f);
} }
void binaryLoad (FILE *f){ void binaryLoad (FILE *f)
{
if(fread (_data, sizeof(scalar), _r, f) != _r) return; if(fread (_data, sizeof(scalar), _r, f) != _r) return;
} }
}; };
/*==============================================================================
* class fullMatrix : An abstract interface for matrix of scalar
*============================================================================*/
template <class scalar> template <class scalar>
class fullMatrix class fullMatrix
{ {
private: private:
bool _own_data; // should data be freed on delete ? bool _own_data; // should data be freed on delete ?
int _r, _c; int _r, _c; // Size of the matrix
scalar *_data; scalar *_data; // Pointer on the first element
public: public:
inline scalar get(int r, int c) const {
#ifdef _DEBUG // Constructor and destructor
if (r >= _r || r < 0 || c >= _c || c < 0)
Msg::Fatal("invalid index to access fullMatrix : %i %i (size = %i %i)",
r, c, _r, _c);
#endif
return (*this)(r, c);
}
inline void set(int r, int c, scalar v){
#ifdef _DEBUG
if (r >= _r || r < 0 || c >= _c || c < 0)
Msg::Fatal("invalid index to access fullMatrix : %i %i (size = %i %i)",
r, c, _r, _c);
#endif
(*this)(r, c) = v;
}
inline scalar norm() const
{
scalar n = 0.;
for(int i = 0; i < _r; ++i)
for(int j = 0; j < _c; ++j)
n += (*this)(i, j) * (*this)(i, j);
return sqrt(n);
}
fullMatrix(scalar *original, int r, int c) fullMatrix(scalar *original, int r, int c)
{ {
_r = r; _r = r;
...@@ -184,7 +189,42 @@ class fullMatrix ...@@ -184,7 +189,42 @@ class fullMatrix
for(int i = 0; i < _r * _c; ++i) _data[i] = other._data[i]; for(int i = 0; i < _r * _c; ++i) _data[i] = other._data[i];
} }
fullMatrix() : _own_data(false),_r(0), _c(0), _data(0) {} fullMatrix() : _own_data(false),_r(0), _c(0), _data(0) {}
~fullMatrix() { if(_data && _own_data) delete [] _data; } ~fullMatrix()
{
if(_data && _own_data) delete [] _data;
}
// Get information (size, value)
inline int size1() const { return _r; }
inline int size2() const { return _c; }
inline scalar get(int r, int c) const {
#ifdef _DEBUG
if (r >= _r || r < 0 || c >= _c || c < 0)
Msg::Fatal("invalid index to access fullMatrix : %i %i (size = %i %i)",
r, c, _r, _c);
#endif
return (*this)(r, c);
}
// Operations
inline void set(int r, int c, scalar v){
#ifdef _DEBUG
if (r >= _r || r < 0 || c >= _c || c < 0)
Msg::Fatal("invalid index to access fullMatrix : %i %i (size = %i %i)",
r, c, _r, _c);
#endif
(*this)(r, c) = v;
}
inline scalar norm() const
{
scalar n = 0.;
for(int i = 0; i < _r; ++i)
for(int j = 0; j < _c; ++j)
n += (*this)(i, j) * (*this)(i, j);
return sqrt(n);
}
bool resize(int r, int c, bool resetValue = true) // data will be owned (same as constructor) bool resize(int r, int c, bool resetValue = true) // data will be owned (same as constructor)
{ {
if ((r * c > _r * _c) || !_own_data) { if ((r * c > _r * _c) || !_own_data) {
...@@ -234,8 +274,6 @@ class fullMatrix ...@@ -234,8 +274,6 @@ class fullMatrix
_own_data = false; _own_data = false;
_data = original._data; _data = original._data;
} }
inline int size1() const { return _r; }
inline int size2() const { return _c; }
fullMatrix<scalar> & operator = (const fullMatrix<scalar> &other) fullMatrix<scalar> & operator = (const fullMatrix<scalar> &other)
{ {
if(this != &other){ if(this != &other){
...@@ -276,8 +314,7 @@ class fullMatrix ...@@ -276,8 +314,7 @@ class fullMatrix
#endif #endif
return _data[i + _r * j]; return _data[i + _r * j];
} }
void copy(const fullMatrix<scalar> &a, int i0, int ni, int j0, int nj, void copy(const fullMatrix<scalar> &a, int i0, int ni, int j0, int nj, int desti0, int destj0)
int desti0, int destj0)
{ {
for(int i = i0, desti = desti0; i < i0 + ni; i++, desti++) for(int i = i0, desti = desti0; i < i0 + ni; i++, desti++)
for(int j = j0, destj = destj0; j < j0 + nj; j++, destj++) for(int j = j0, destj = destj0; j < j0 + nj; j++, destj++)
...@@ -301,7 +338,6 @@ class fullMatrix ...@@ -301,7 +338,6 @@ class fullMatrix
for(int k = 0; k < _c; k++) for(int k = 0; k < _c; k++)
c._data[i + _r * j] += (*this)(i, k) * b(k, j); c._data[i + _r * j] += (*this)(i, k) * b(k, j);
} }
;
void mult(const fullMatrix<scalar> &b, fullMatrix<scalar> &c) const void mult(const fullMatrix<scalar> &b, fullMatrix<scalar> &c) const
#if !defined(HAVE_BLAS) #if !defined(HAVE_BLAS)
{ {
...@@ -313,8 +349,7 @@ class fullMatrix ...@@ -313,8 +349,7 @@ class fullMatrix
{ {
for (int i = 0; i < _r * _c; i++) _data[i] *= a._data[i]; for (int i = 0; i < _r * _c; i++) _data[i] *= a._data[i];
} }
void gemm_naive(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b, void gemm_naive(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b, scalar alpha=1., scalar beta=1.)
scalar alpha=1., scalar beta=1.)
{ {
fullMatrix<scalar> temp(a.size1(), b.size2()); fullMatrix<scalar> temp(a.size1(), b.size2());
a.mult_naive(b, temp); a.mult_naive(b, temp);
...@@ -322,9 +357,7 @@ class fullMatrix ...@@ -322,9 +357,7 @@ class fullMatrix
scale(beta); scale(beta);
add(temp); add(temp);
} }
; void gemm(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b, scalar alpha=1., scalar beta=1.)
void gemm(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b,
scalar alpha=1., scalar beta=1.)
#if !defined(HAVE_BLAS) #if !defined(HAVE_BLAS)
{ {
gemm_naive(a,b,alpha,beta); gemm_naive(a,b,alpha,beta);
...@@ -435,12 +468,10 @@ class fullMatrix ...@@ -435,12 +468,10 @@ class fullMatrix
int ni = size1(); int ni = size1();
int nj = size2(); int nj = size2();
fullMatrix<scalar> cof(ni - 1, nj - 1); fullMatrix<scalar> cof(ni - 1, nj - 1);
for(int I = 0; I < ni; I++){ for(int I = 0; I < ni; I++)
for(int J = 0; J < nj; J++){ for(int J = 0; J < nj; J++)
if(J != j && I != i) if(J != j && I != i)
cof(I < i ? I : I - 1, J < j ? J : J - 1) = (*this)(I, J); cof(I < i ? I : I - 1, J < j ? J : J - 1) = (*this)(I, J);
}
}
return cof; return cof;
} }
scalar determinant() const scalar determinant() const
...@@ -459,6 +490,9 @@ class fullMatrix ...@@ -459,6 +490,9 @@ class fullMatrix
} }
#endif #endif
; ;
// Printing
void print(const std::string name = "") const void print(const std::string name = "") const
{ {
printf("Printing matrix %s:\n", name.c_str()); printf("Printing matrix %s:\n", name.c_str());
...@@ -472,6 +506,9 @@ class fullMatrix ...@@ -472,6 +506,9 @@ class fullMatrix
printf("\n"); printf("\n");
} }
} }
// Bindings
static void registerBindings(binding *b); static void registerBindings(binding *b);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment