17 template <
typename T,
int M,
int N>
class FMatrix {
45 for (
int k=0;k<M*N;k++)
56 for (
int j=0;j<N;j++)
for (
int i=0;i<M;i++)
57 (*
this)(i,j) = t[i][j];
68 for (
int k=0;k<M*N;k++)
79 for (
int k=0;k<M*N;k++)
91 std::memset(Z.
_data,0,M*N*
sizeof(T));
103 template <
typename T2>
105 for (
int k=0;k<M*N;k++)
115 int size()
const {
return M*N; }
139 assert(i>=0 && i<M && j>=0 && j<N);
140 return (*
this)[i+M*j];
151 assert(i>=0 && i<M && j>=0 && j<N);
152 return (*
this)[i+M*j];
162 assert(k>=0 && k<M*N);
173 assert(k>=0 && k<M*N);
200 for (
int i=0;i<M;i++)
213 assert(M==N && M==3);
231 for (
int k=0;k<M*N;k++)
232 if ((*
this)[k] != B[k])
return false;
243 return !(*
this == B);
254 for (
int k=0;k<M*N;k++) C[k] = _data[k] + B[k];
266 for (
int k=0;k<M*N;k++) C[k] = _data[k] - B[k];
277 for (
int k=0;k<M*N;k++) _data[k] += B[k];
288 for (
int k=0;k<M*N;k++) _data[k] -= B[k];
300 for (
int k=0;k<M*N;k++) C[k] = _data[k] + s;
312 for (
int k=0;k<M*N;k++) C[k] = _data[k] - s;
323 for (
int k=0;k<M*N;k++) _data[k] += s;
334 for (
int k=0;k<M*N;k++) _data[k] -= s;
345 for (
int k=0;k<M*N;k++) C[k] = -_data[k];
357 for (
int k=0;k<M*N;k++) C[k] = _data[k] * s;
369 for (
int k=0;k<M*N;k++) C[k] = _data[k] / s;
380 for (
int k=0;k<M*N;k++) _data[k] *= s;
391 for (
int k=0;k<M*N;k++) _data[k] /= s;
404 for (
int j=0;j<O;j++)
for (
int i=0;i<M;i++) {
405 for (
int k=0;k<N;k++) C(i,j) += (*this)(i,k) * B(k,j);
418 for (
int j=0;j<N;j++)
419 for (
int i=0;i<M;i++) w[i] += (*
this)(i,j) * v[j];
432 for (
int i=0;i<M;i++) v[i] = (*
this)(i,j);
445 for (
int i=0;i<M;i++) (*
this)(i,j) = v[i];
458 for (
int j=0;j<N;j++) v[j] = (*
this)(i,j);
471 for (
int j=0;j<N;j++) (*
this)(i,j) = v[j];
483 for (
int k=0;k<M*N;k++) n += A.
_data[k] * A.
_data[k];
494 assert( !std::numeric_limits<T>::is_integer );
495 return ::sqrt(
norm2(A));
506 out.write((
const char*)A.
data(),std::streamsize(M*N*
sizeof(T)));
517 in.read((
char*)A.
data(),std::streamsize(M*N*
sizeof(T)));
529 for (
int i=0;i<A.
nrow();i++) {
530 for (
int j=0;j<A.
ncol();j++) {
532 if (j<A.ncol()-1) out<<
" ";
548 for (
int i=0;i<M;i++)
549 for (
int j=0;j<N;j++)
563 template <
typename T,
int M,
int N>
566 for (
int j=0;j<N;j++)
for (
int i=0;i<M;i++) C(j,i) = A(i,j);
578 template <
typename T,
int M,
int N>
590 template <
typename T,
int M,
int N>
602 template <
typename T,
int M,
int N>
614 template <
typename T,
int M,
int N,
int O>
617 for (
int j=0;j<O;j++)
for (
int i=0;i<N;i++)
618 for (
int k=0;k<M;k++) C(i,j) += A(k,i) * B(k,j);
629 template <
typename T,
int M,
int N,
int O>
632 for (
int j=0;j<O;j++)
for (
int i=0;i<M;i++)
633 for (
int k=0;k<N;k++) C(i,j) += A(i,k) * B(j,k);
644 template <
typename T,
int M,
int N,
int O>
647 for (
int j=0;j<O;j++)
for (
int i=0;i<N;i++)
648 for (
int k=0;k<M;k++) C(i,j) += A(k,i) * B(j,k);
659 template <
typename T,
int M,
int N>
662 for (
int j=0;j<N;j++)
663 for (
int i=0;i<M;i++) w[j] += A(i,j) * v[i];
677 template <
typename T,
int M>
683 std::cerr <<
"Non invertible matrix" << std::endl;
694 for (
int i=0;i<3;i++)
for (
int j=0;j<3;j++) {
695 const int ia = (i+1)%3;
696 const int ib = (i+2)%3;
697 const int ja = (j+1)%3;
698 const int jb = (j+2)%3;
699 I(j,i) = ( A(ia,ja) * A(ib,jb) - A(ib,ja) * A(ia,jb) ) / _det;
704 std::cerr <<
"Code for inverting matrices of size " << M <<
"*" << M <<
" is not available" << std::endl;
705 std::cerr <<
"Consider using inverseFMatrix" << std::endl;
716 template <
typename T,
int M>
721 return A(0,0)*A(1,1)-A(0,1)*A(1,0);
725 A(0,0) * (A(1,1) * A(2,2) - A(1,2) * A(2,1))
726 - A(1,0) * (A(0,1) * A(2,2) - A(2,1) * A(0,2))
727 + A(2,0) * (A(0,1) * A(1,2) - A(1,1) * A(0,2));
729 std::cerr <<
"Code for determinant of matrices of size " << M <<
"*" << M <<
" is not available" << std::endl;
730 std::cerr <<
"Consider using Matrix class instead of FMatrix" << std::endl;
742 template <
typename T,
int M>
745 for (
int i=0;i<M;i++)
friend void write(std::ostream &out, const FMatrix &A)
Binary write.
Definition: FMatrix.h:505
FVector< T, N > getRow(int i) const
Get row.
Definition: FMatrix.h:455
const T & operator[](int k) const
1D read access.
Definition: FMatrix.h:161
friend void read(std::istream &in, FMatrix &A)
Binary read.
Definition: FMatrix.h:516
FMatrix & operator+=(const FMatrix &B)
In place Addition.
Definition: FMatrix.h:276
FMatrix()
Empty constructor.
Definition: FMatrix.h:27
FMatrix(const FMatrix< T2, M, N > &A)
Copy constructor Constructs from another FMatrix (with a possibly different type) ...
Definition: FMatrix.h:67
static FMatrix Zero()
Zero matrix Matrix with constant 0 value.
Definition: FMatrix.h:89
bool operator!=(const FMatrix &B) const
Inequality test.
Definition: FMatrix.h:242
FMatrix & setCol(int j, const FVector< T, M > &v)
Set column.
Definition: FMatrix.h:443
const T & z() const
Read alias.
Definition: FVector.h:132
friend std::istream & operator>>(std::istream &in, FMatrix &A)
ASCII read.
Definition: FMatrix.h:547
static FMatrix Identity()
Identity.
Definition: FMatrix.h:197
FMatrix & operator/=(T s)
Scalar in place division.
Definition: FMatrix.h:390
friend std::ostream & operator<<(std::ostream &out, const FMatrix &A)
ASCII write.
Definition: FMatrix.h:528
FMatrix< T, M, M > Diagonal(const FVector< T, M > &d)
Diagonal.
Definition: FMatrix.h:743
FMatrix & setRow(int i, const FVector< T, N > &v)
Set row.
Definition: FMatrix.h:469
const T & x() const
Read alias.
Definition: FVector.h:104
T * data()
Data pointer (write).
Definition: FMatrix.h:189
bool operator==(const FMatrix &B) const
Equality test.
Definition: FMatrix.h:230
FMatrix & fill(const T &v)
Filling.
Definition: FMatrix.h:78
FMatrix operator-() const
Opposite.
Definition: FMatrix.h:343
static FMatrix CrossProd(const FVector< T, 3 > &v)
Cross product matrix.
Definition: FMatrix.h:212
FMatrix & operator*=(T s)
Scalar in place multiplication.
Definition: FMatrix.h:379
FMatrix< T, N, O > tmult(const FMatrix< T, M, N > &A, const FMatrix< T, M, O > &B)
Product.
Definition: FMatrix.h:615
FMatrix< T, M, O > multt(const FMatrix< T, M, N > &A, const FMatrix< T, O, N > &B)
Product.
Definition: FMatrix.h:630
FMatrix operator+(const FMatrix &B) const
Addition.
Definition: FMatrix.h:252
friend T norm2(const FMatrix &A)
Squared Frobenius norm.
Definition: FMatrix.h:481
friend T norm(const FMatrix &A)
Frobenius norm.
Definition: FMatrix.h:493
FMatrix< T, M, M > inverse(const FMatrix< T, M, M > &A)
Inverse.
Definition: FMatrix.h:678
const T * data() const
Data pointer (read).
Definition: FMatrix.h:182
int size() const
Size.
Definition: FMatrix.h:115
FMatrix< T, N, M > transpose(const FMatrix< T, M, N > &A)
Transpose.
Definition: FMatrix.h:564
FMatrix(const T &v)
Constructor with constant value.
Definition: FMatrix.h:34
FMatrix operator*(T s) const
Scalar multiplication.
Definition: FMatrix.h:355
FVector< T, M > getCol(int j) const
Get column.
Definition: FMatrix.h:429
FMatrix(const T t[M][N])
Constructor from bidim C array.
Definition: FMatrix.h:55
T _data[M *N]
internal storage.
Definition: FMatrix.h:20
FMatrix< T, N, O > tmultt(const FMatrix< T, M, N > &A, const FMatrix< T, O, M > &B)
Product.
Definition: FMatrix.h:645
FMatrix(const T t[M *N])
Constructor from C array.
Definition: FMatrix.h:44
int nrow() const
Number of rows.
Definition: FMatrix.h:122
FMatrix & operator-=(const FMatrix &B)
In place Substraction.
Definition: FMatrix.h:287
int ncol() const
Number of columns.
Definition: FMatrix.h:129
const T & y() const
Read alias.
Definition: FVector.h:118
Imagine++ namespace.
Definition: Array.h:7
FMatrix & operator=(const FMatrix< T2, M, N > &B)
Assignment.
Definition: FMatrix.h:104
FMatrix operator/(T s) const
Scalar division.
Definition: FMatrix.h:367
const T & operator()(int i, int j) const
Read access.
Definition: FMatrix.h:138
T det(const FMatrix< T, M, M > &A)
Determinant.
Definition: FMatrix.h:717
Matrix of fixed dimension.
Definition: FMatrix.h:17