57 Matrix(T* t,
int M,
int N,
bool handleDelete=
false) : Base(t,M,N,handleDelete) {}
138 for (
int i=0;i<N;i++)
150 assert(j>=0 && j<
ncol());
163 assert(i>=0 && i<
nrow());
177 for (
int i=0;i<n;i++)
217 for (
int i=0;i<n;i++)
230 assert (j>=0 && j<
ncol());
244 assert (j>=0 && j<
ncol() && i0>=0 && m>=0 && i0+m<=
nrow());
257 assert (i0>=0 && i0+m<=
nrow() && j0>=0 && j0+n<=
ncol());
260 equalize(m,this->
data()+(j+j0)*
nrow()+i0, 1, retMat.
data()+j*m, 1);
273 assert (j0>=0 && n>=0 && j0+n<=
ncol());
287 affinity(
nrow(),
ncol(), T(1), this->
data(), v.
data(), T(0), y.data(),
'0');
302 affinity(A.
nrow(), A.
ncol(), T(1), A.
data(), v.
data(), T(0), y.data(),
'T');
314 for(
int i=0;i<A.
nrow();i++)
for(
int j=0;j<A.
ncol();j++) r(j,i)=A(i,j);
327 int *pivots=
new int[A.
nrow()];
330 matrixInverse(&n,invA.data(),&n,pivots,&info);
332 std::cerr<<
"Cannot invert matrix"<<std::endl;
352 matrixProduct(C.nrow(),C.ncol(),p,T(1),this->
data(),
nrow(),B.
data(),B.
nrow(),T(0),C.data(),
'N',
'N');
368 matrixProduct(C.nrow(),C.ncol(),p,T(1),A.
data(),A.
nrow(),B.
data(),B.
nrow(),T(0),C.data(),
'T',
'N');
384 matrixProduct(C.nrow(),C.ncol(),p,T(1),A.
data(),A.
nrow(),B.
data(),B.
nrow(),T(0),C.data(),
'N',
'T');
400 matrixProduct(C.nrow(),C.ncol(),p,T(1),A.
data(),A.
nrow(),B.
data(),B.
nrow(),T(0),C.data(),
'T',
'T');
466 return (*
this)*T(-1);
548 T n = matrixNorm(&type,&nr,&nc,(T*)A.
data(),&nr);
567 conditionNumber(&n,tmpA.
data(),&m,&
rcond,&info);
569 std::cerr <<
"Bad cond number " << std::endl;
596 singularValuesDecomposition(all?
"A":
"S",&nr,&nc,cpy.
data(),S.
data(),U.
data(),Vt.
data(),&info);
598 std::cerr<<
"Cannot compute SVD"<<std::endl;
617 assert(b.
size() == (size_t)A.
nrow());
624 int *pivots=
new int[A.
nrow()];
626 LUSystemSolve(&n,tmpA.
data(),pivots,tmpb.
data(),&info);
628 std::cerr<<
"Cannot solve linear system"<<std::endl;
633 std::cerr<<
"Cannot solve linear system"<<std::endl;
639 int info,m=A.
nrow(),n=A.
ncol(),o=std::max(m,n);
642 for (
int i=0;i<m;i++) tmpb[i] = b[i];
643 QRSystemSolve(&m,&n,tmpA.
data(),tmpb.
data(),&info);
646 std::cerr<<
"Cannot solve linear system"<<std::endl;
668 tolrel=std::numeric_limits<T>::epsilon();
669 T tol = S[0] * tolrel;
670 for (
int i=0;i<n;i++) {
671 T s=(S[i]>tol)?T(1)/S[i]:T(0);
672 for (
int j=0;j<n;j++)
690 int info,m=A.
nrow(),n=A.
ncol(),qn=(all?m:n);
696 QRFactorization(&m,&qn,&n,Q.
data(),R.data(),&info);
701 QRFactorization(&m,&qn,&n,Q.
data(),R.
data(),&info);
704 std::cerr <<
"Failed QR" << std::endl;
725 choleskyDecomposition(low?
"L":
"U",&n,b.data(),&info);
727 std::cerr <<
"Bad cholesky" << std::endl;
732 for (
int j=1; j<n; j++)
733 for (
int i=0; i<j; i++)
736 for (
int j=1; j<n; j++)
737 for (
int i=0; i<j; i++)
757 T d = determinant(&m,&n,b.data(),&info);
759 std::cerr<<
"Cannot compute LU decomposition"<<std::endl;
774 out<<A.
nrow()<<
" "<<A.
ncol()<< std::endl;
775 for (
int i=0;i<A.
nrow();i++) {
776 for (
int j=0;j<A.
ncol();j++) {
778 if (j<A.ncol()-1) out<<
" ";
797 for (
int i=0;i<A.
nrow();i++)
798 for (
int j=0;j<A.
ncol();j++)
813 template <
typename T>
817 for (
int i=0;i<n;i++)
829 template <
typename T>
832 int n=(int)v1.
size();
835 matrixProduct(n,n,1,T(1),v1.
data(),n,v2.
data(),1,T(0),A.
data(),
'N',
'N');
852 template <
typename T,
int N>
858 int *pivots=
new int[N];
861 matrixInverse(&n,invA.
data(),&n,pivots,&info);
863 std::cerr<<
"Cannot invert matrix"<<std::endl;
885 template <
typename T,
int M,
int N>
886 void svd(
const FMatrix<T,M,N>& A,
FMatrix<T,M,M> &U,
FVector<T,M> &S,
FMatrix<T,N,N> &Vt,
bool all=
false) {
890 singularValuesDecomposition(all?
"A":
"S",&nr,&nc,cpy.
data(),S.
data(),U.
data(),Vt.
data(),&info);
892 std::cerr<<
"Cannot compute SVD"<<std::endl;
906 template <
typename T>
916 eigenValues(&n, cpy.
data(), wr.
data(), wi.
data(), 0, &info);
918 std::cerr <<
"Cannot compute eigenvalues" << std::endl;
934 template <
typename T,
int M,
int N>
943 int *pivots=
new int[M];
946 LUSystemSolve(&n,tmpA.
data(),pivots,tmpb.
data(),&info);
948 std::cerr<<
"Cannot solve linear system"<<std::endl;
952 std::cerr<<
"Cannot solve linear system"<<std::endl;
962 for (
int i=0;i<m;i++) tmpb[i] = b[i];
963 QRSystemSolve(&m,&n,tmpA.
data(),tmpb.
data(),&info);
966 std::cerr<<
"Cannot solve linear system"<<std::endl;
972 for (
int i=0;i<m;i++) tmpb[i] = b[i];
973 QRSystemSolve(&m,&n,tmpA.
data(),tmpb.
data(),&info);
976 std::cerr<<
"Cannot solve linear system"<<std::endl;
994 template <
typename T,
int M,
int N>
1002 tolrel=std::numeric_limits<T>::epsilon();
1003 T tol = S[0] * tolrel;
1004 for (
int i=0;i<N;i++) {
1005 T s=(S[i]>tol)?T(1)/S[i]:T(0);
1006 for (
int j=0;j<N;j++)
1021 template <
typename T,
int M,
int N>
1026 QRFactorization(&m,&n,&n,Q.
data(),R.
data(),&info);
1028 std::cerr <<
"Failed QR" << std::endl;
1043 template <
typename T,
int M,
int N>
1047 equalize(M*N,A.
data(),1,Q.
data(),1);
1048 QRFactorization(&m,&m,&n,Q.
data(),R.
data(),&info);
1050 std::cerr <<
"Failed QR" << std::endl;
1066 template <
typename T,
int N>
1071 choleskyDecomposition(low?
"L":
"U",&n,b.
data(),&info);
1073 std::cerr <<
"Bad cholesky" << std::endl;
1078 for (
int j=1; j<n; j++)
1079 for (
int i=0; i<j; i++)
1082 for (
int j=1; j<n; j++)
1083 for (
int i=0; i<j; i++)
1098 template <
typename T,
int N>
1104 choleskyDecomposition((
char*)
"L",&n,b.
data(),&info);
1107 for (
int i=0; i<n; i++) d*=b(i,i);
1115 for (
int i=0; i<N; i++) d*=s[i];
FMatrix< T, N, N > inverseFMatrix(const FMatrix< T, N, N > &A)
Inverse.
Definition: Matrix.h:853
Vector & fill(T x)
Filling.
Definition: Vector.h:93
friend Matrix multt(const Matrix &A, const Matrix &B)
Product.
Definition: Matrix.h:379
Vector of fixed size.
Definition: FVector.h:17
Vector clone() const
Cloning.
Definition: Vector.h:80
Vector of variable size.
Definition: Vector.h:24
friend Matrix tmult(const Matrix &A, const Matrix &B)
Product.
Definition: Matrix.h:363
Matrix & setCol(int j, const Vector< T > &v)
Set column.
Definition: Matrix.h:189
friend void svd(const Matrix &A, Matrix &U, Vector< T > &S, Matrix &Vt, bool all=false)
SVD.
Definition: Matrix.h:589
FVector & fill(const T &v)
Filling.
Definition: FVector.h:75
friend T rcond(const Matrix &A)
Reciprocal condition number.
Definition: Matrix.h:558
friend Matrix pseudoInverse(const Matrix &A, T tolrel=0)
Pseudo inverse.
Definition: Matrix.h:660
void setSize(const Coords< dim > &sz)
Change sizes.
Definition: MultiArray.h:146
friend Matrix tmultt(const Matrix &A, const Matrix &B)
Product.
Definition: Matrix.h:395
Base::iterator iterator
Iterator type.
Definition: Matrix.h:27
Matrix< T > outerProduct(const Vector< T > &v1, const Vector< T > &v2)
Outer product matrix.
Definition: Matrix.h:830
Matrix & setDiagonal(const Vector< T > &v)
Set diagonal.
Definition: Matrix.h:214
Vector< T > operator*(const Vector< T > &v) const
Product with vector.
Definition: Matrix.h:283
Matrix operator+(const Matrix &B) const
Addition.
Definition: Matrix.h:418
~Matrix()
Destructor.
Definition: Matrix.h:74
Matrix & operator/=(T x)
Scalar in place division.
Definition: Matrix.h:508
FMatrix< T, M, M > Diagonal(const FVector< T, M > &d)
Diagonal.
Definition: FMatrix.h:743
size_t size() const
Size.
Definition: Array.h:194
MultiArray & operator=(const MultiArray &A)
Assignment.
Definition: MultiArray.h:175
friend Matrix inverse(const Matrix &A)
Inverse.
Definition: Matrix.h:324
friend Matrix cholesky(const Matrix &A, bool low=true)
Cholesky decomposition.
Definition: Matrix.h:720
int ncol() const
Number of columns.
Definition: Matrix.h:108
Vector< T > getSubColRef(int j, int i0, int m)
Get sub column by reference.
Definition: Matrix.h:243
Matrix & operator*=(T x)
Scalar in place multiplication.
Definition: Matrix.h:497
friend T det(const Matrix &A)
Determinant.
Definition: Matrix.h:752
T detFMatrix(const FMatrix< T, N, N > &A)
Determinant.
Definition: Matrix.h:1099
FMatrix & fill(const T &v)
Filling.
Definition: FMatrix.h:78
bool QRAll(const FMatrix< T, M, N > &A, FMatrix< T, M, M > &Q, FMatrix< T, M, N > &R)
QR decomposition.
Definition: Matrix.h:1044
int nrow() const
Number of rows.
Definition: Matrix.h:101
Matrix(const Base &A)
Copy constructor.
Definition: Matrix.h:64
Matrix getColsRef(int j0, int n)
Get part of columns by reference.
Definition: Matrix.h:272
size_t totalSize() const
Total Size.
Definition: MultiArray.h:218
Vector< T > getCol(int j) const
Get column.
Definition: Matrix.h:149
Vector< T > getRow(int i) const
Get row.
Definition: Matrix.h:162
Matrix()
Empty constructor.
Definition: Matrix.h:36
T * data()
Data pointer (read/write).
Definition: Array.h:217
Matrix operator-() const
Opposite.
Definition: Matrix.h:465
Matrix & operator=(const Matrix &A)
Assignment.
Definition: Matrix.h:82
Vector getSubVect(size_t offset, size_t size) const
Sub vector.
Definition: Vector.h:114
nD array of variable size.
Definition: MultiArray.h:21
friend std::ostream & operator<<(std::ostream &out, const Matrix &A)
ASCII write.
Definition: Matrix.h:773
friend std::istream & operator>>(std::istream &in, Matrix &A)
ASCII read.
Definition: Matrix.h:793
const T * data() const
Data pointer (read).
Definition: FMatrix.h:182
Matrix & setRow(int i, const Vector< T > &v)
Set row.
Definition: Matrix.h:202
Matrix getSubMat(int i0, int m, int j0, int n) const
Get sub matrix.
Definition: Matrix.h:256
MultiArray & fill(T x)
Filling.
Definition: MultiArray.h:204
friend bool QR(const Matrix &A, Matrix &Q, Matrix &R, bool all=false)
QR decomposition.
Definition: Matrix.h:688
Base::const_iterator const_iterator
Const iterator type.
Definition: Matrix.h:29
int height() const
Size alias 1.
Definition: MultiArray.h:239
friend Vector< T > tmult(const Matrix &A, const Vector< T > &v)
Product with vector.
Definition: Matrix.h:298
T * iterator
Iterator type.
Definition: MultiArray.h:45
friend T dot(const Matrix &a, const Matrix &b)
Scalar product.
Definition: Matrix.h:529
Vector< T > getDiagonal() const
Get diagonal.
Definition: Matrix.h:174
Matrix(T *t, int M, int N, bool handleDelete=false)
Constructor (pre-allocated).
Definition: Matrix.h:57
static Matrix Zero(int M, int N)
Zero matrix.
Definition: Matrix.h:124
void eigenvalues(const Matrix< T > &A, Vector< T > &wr, Vector< T > &wi)
Eigenvalues calculation Given square matrix, only eigenvalues are obtained.
Definition: Matrix.h:907
Matrix & operator+=(const Matrix &B)
In place Addition.
Definition: Matrix.h:440
Matrix(int M, int N)
Constructor (known size).
Definition: Matrix.h:44
friend Matrix transpose(const Matrix &A)
Transpose.
Definition: Matrix.h:312
static Matrix Identity(int N)
Identity.
Definition: Matrix.h:136
friend Vector< T > linSolve(const Matrix &A, const Vector< T > &b)
Linear system.
Definition: Matrix.h:615
Imagine++ namespace.
Definition: Array.h:7
Matrix of variable size.
Definition: Matrix.h:23
Matrix & operator-=(const Matrix &B)
In place Substraction.
Definition: Matrix.h:453
Matrix clone() const
Cloning.
Definition: Matrix.h:89
Matrix & fill(T x)
Filling.
Definition: Matrix.h:116
const T * data() const
Data pointer (read).
Definition: FArray.h:188
Vector< T > getColRef(int j)
Get column by reference.
Definition: Matrix.h:229
int width() const
Size alias 0.
Definition: MultiArray.h:232
Matrix operator/(T x) const
Scalar division.
Definition: Matrix.h:486
const T * const_iterator
Const iterator type.
Definition: MultiArray.h:47
Symmetric Matrix of variable size.
Definition: Matrix.h:12
friend T norm(const Matrix &A, char type='F')
Norm.
Definition: Matrix.h:546
Matrix of fixed dimension.
Definition: FMatrix.h:17