11 template <
typename T>
class Matrix;
22 class SymMatrix :
public Array<T> {
23 typedef Array<T> Base;
25 size_t sz(
int n) {
return size_t(n)*(n+1)/2; }
62 SymMatrix(T* t,
int N,
bool handleDelete=
false) : Base(t,sz(N),handleDelete), _n(N) { }
71 for (
int j=0; j<A.
nrow(); j++)
72 equalize(j+1,A.
data()+size_t(j)*A.
nrow(),1,this->
data()+sz(j),1);
94 equalize((
int)this->
size(),this->
data(),1,A.data(),1);
117 int nrow()
const {
return _n; }
124 int ncol()
const {
return _n; }
143 memset(Z.data(),0,Z.size()*
sizeof(T));
155 for (
int i=0;i<n;i++)
168 assert(i>=0 && i<_n && j>=0 && j<_n);
170 return (*
this)[i+size_t(j)*(j+1)/2];
172 return (*
this)[j+size_t(i)*(i+1)/2];
183 assert(i>=0 && i<_n && j>=0 && j<_n);
185 return (*
this)[i+size_t(j)*(j+1)/2];
187 return (*
this)[j+size_t(i)*(i+1)/2];
199 assert (i0>=0 && i0+m<=
nrow() && j0>=0 && j0+n<=
ncol());
203 retMat(i,j)=(*this)(i0+i,j0+j);
214 for (
int i=0;i<_n;i++)
226 assert((
size_t)_n==v.
size());
230 vectorProduct(_nn,T(1),this->
data(),v.
data(),T(0),y.data());
244 T *pivots=
new T[A.nrow()*A.nrow()];
248 symInverse(&n,invA.data(),pivots,&info);
253 std::cerr <<
"Cannot invert symmetric matrix" << std::endl;
270 T *pivots=
new T[A.nrow()*A.nrow()];
274 symDPInverse(&n,invA.data(),pivots,&info);
279 std::cerr <<
"Cannot invert symmetric matrix" << std::endl;
298 d = symDeterminant(&n,symtemp.data(),&info);
300 std::cerr <<
"Cannot compute determinant" << std::endl;
321 symEigenValues(&n,symtemp.data(),Lambda.
data(),Q.
data(),&info);
323 std::cerr <<
"Cannot compute EVD decomposition" << std::endl;
336 assert(
ncol()==B.nrow());
338 for (
int j=0;j<B.ncol();j++)
339 for (
int i=0;i<
nrow();i++) {
341 for (
int k=0;k<
ncol();k++)
342 C(i,j)+=(*this)(i,k)*B(k,j);
357 for (
int j=0;j<B.
ncol();j++)
358 for (
int i=0;i<
nrow();i++) {
360 for (
int k=0;k<
ncol();k++)
361 C(i,j)+=(*this)(i,k)*B(k,j);
384 assert(
nrow()==B.nrow());
385 combine((
int)this->
size(), T(1), B.data(), this->
data());
407 assert(
nrow()==B.nrow());
408 combine((
int)this->
size(), T(-1), B.data(), this->
data());
418 return (*
this)*T(-1);
459 multiply((
int)this->
size(), x, this->
data());
470 multiply((
int)this->
size(), T(1)/x, this->
data());
484 assert(b.
size()==(size_t)A.nrow());
488 int *pivots=
new int[A.nrow()];
491 symSolve(&n,invA.data(),pivots,x.
data(),&info);
494 std::cerr<<
"Cannot solve linear system"<<std::endl;
506 void write(std::ostream& out)
const {
508 out.write((
const char*)&n,
sizeof(
int));
509 out.write((
const char*)this->
data(),(std::streamsize)this->
size()*
sizeof(T));
520 in.read((
char*)&n,
sizeof(
int));
522 in.read((
char*)this->
data(),(std::streamsize)this->
size()*
sizeof(T));
534 out<<A.nrow()<<
" "<<A.ncol()<< std::endl;
535 for (
int i=0;i<A.nrow();i++) {
536 for (
int j=0;j<A.ncol();j++) {
538 if (j<A.ncol()-1) out<<
" ";
558 for (
int i=0;i<A.nrow();i++)
559 for (
int j=0;j<A.ncol();j++)
565 #ifndef DOXYGEN_SHOULD_SKIP_THIS 567 template <
typename T>
569 for (
int j=0; j<
ncol(); j++)
570 for (
int i=0; i<
nrow(); i++)
574 template <
typename T>
577 assert(
ncol()==B.nrow());
579 for (
int j=0;j<B.ncol();j++)
580 for (
int i=0;i<
nrow();i++) {
582 for (
int k=0;k<
ncol();k++)
583 C(i,j)+=(*this)(i,k)*B(k,j);
friend std::ostream & operator<<(std::ostream &out, const SymMatrix &A)
ASCII write.
Definition: SymMatrix.h:533
Vector & fill(T x)
Filling.
Definition: Vector.h:93
Vector clone() const
Cloning.
Definition: Vector.h:80
Vector of variable size.
Definition: Vector.h:24
SymMatrix(const SymMatrix &A)
Copy constructor.
Definition: SymMatrix.h:50
Vector< T > getDiagonal() const
Get diagonal.
Definition: SymMatrix.h:212
SymMatrix & fill(T x)
Filling.
Definition: SymMatrix.h:133
Matrix< T > getSubMat(int i0, int m, int j0, int n) const
Get sub matrix.
Definition: SymMatrix.h:198
Vector< T > operator*(const Vector< T > &v) const
Product with vector.
Definition: Matrix.h:283
SymMatrix & operator=(const SymMatrix &A)
Assignment.
Definition: SymMatrix.h:84
SymMatrix()
Empty constructor.
Definition: SymMatrix.h:36
static SymMatrix Identity(int n)
Identity.
Definition: SymMatrix.h:153
static SymMatrix Zero(int n)
Zero matrix.
Definition: SymMatrix.h:141
void setSize(int n)
Change sizes.
Definition: SymMatrix.h:107
SymMatrix & operator+=(const SymMatrix &B)
In place Addition.
Definition: SymMatrix.h:383
Array & operator=(const Array &A)
Assignment.
Definition: Array.h:154
~SymMatrix()
Destructor.
Definition: SymMatrix.h:76
size_t size() const
Size.
Definition: Array.h:194
T & operator()(int i, int j)
Write access.
Definition: SymMatrix.h:182
friend SymMatrix posDefInverse(const SymMatrix &A)
Inverse (variant).
Definition: SymMatrix.h:267
int ncol() const
Number of columns.
Definition: Matrix.h:108
friend void EVD(const SymMatrix &A, Matrix< T > &Q, Vector< T > &Lambda)
Eigen values.
Definition: SymMatrix.h:316
friend std::istream & operator>>(std::istream &in, SymMatrix &A)
ASCII read.
Definition: SymMatrix.h:553
int nrow() const
Number of rows.
Definition: SymMatrix.h:117
SymMatrix & operator*=(T x)
Scalar in place multiplication.
Definition: SymMatrix.h:458
int nrow() const
Number of rows.
Definition: Matrix.h:101
int ncol() const
Number of columns.
Definition: SymMatrix.h:124
Base::const_iterator const_iterator
Const iterator type.
Definition: SymMatrix.h:30
void setSize(size_t size)
Change size.
Definition: Array.h:143
SymMatrix clone() const
Cloning.
Definition: SymMatrix.h:91
Matrix()
Empty constructor.
Definition: Matrix.h:36
T * data()
Data pointer (read/write).
Definition: Array.h:217
Base::iterator iterator
Iterator type.
Definition: SymMatrix.h:28
void read(std::istream &in)
Binary read.
Definition: SymMatrix.h:518
const T & operator()(int i, int j) const
Read access.
Definition: SymMatrix.h:167
SymMatrix(int N)
Constructor (known size).
Definition: SymMatrix.h:43
SymMatrix(const Matrix< T > &A)
Conversion from Matrix.
Definition: SymMatrix.h:69
SymMatrix operator/(T x) const
Scalar division.
Definition: SymMatrix.h:447
SymMatrix & operator-=(const SymMatrix &B)
In place Substraction.
Definition: SymMatrix.h:406
void write(std::ostream &out) const
Binary write.
Definition: SymMatrix.h:506
SymMatrix operator+(const SymMatrix &B) const
Addition.
Definition: SymMatrix.h:372
SymMatrix & operator/=(T x)
Scalar in place division.
Definition: SymMatrix.h:469
SymMatrix(T *t, int N, bool handleDelete=false)
Constructor (pre-allocated).
Definition: SymMatrix.h:62
friend Vector< T > linSolve(const SymMatrix &A, const Vector< T > &b)
Linear system.
Definition: SymMatrix.h:482
Array & fill(const T &x)
Filling.
Definition: Array.h:276
Imagine++ namespace.
Definition: Array.h:7
Matrix of variable size.
Definition: Matrix.h:23
RED GREEN BLUE color.
Definition: Color.h:26
const T * const_iterator
Const iterator type.
Definition: Array.h:73
Vector< T > operator*(const Vector< T > &v) const
Product with vector.
Definition: SymMatrix.h:225
friend T det(const SymMatrix &A)
Determinant.
Definition: SymMatrix.h:293
friend SymMatrix inverse(const SymMatrix &A)
Inverse.
Definition: SymMatrix.h:241
SymMatrix operator-() const
Opposite.
Definition: SymMatrix.h:417
Symmetric Matrix of variable size.
Definition: Matrix.h:12