TinyMatrix< N, N, T > Class Template Reference

#include <TinyMatrix.hpp>

Collaboration diagram for TinyMatrix< N, N, T >:

Collaboration graph
[legend]

List of all members.

Public Types

enum  { NumberOfLines = N, NumberOfRow = N }
typedef TinyVector< N > AssociatedVector
typedef TinyVector< N > AssociatedTransposedVector
typedef T ValueType

Public Member Functions

size_t size () const
T & operator() (const size_t &i, const size_t &j)
const T & operator() (const size_t &i, const size_t &j) const
TinyMatrix< N, N, T > invert () const
TinyMatrix< N, N, T > & operator= (const TinyMatrix< N, N, T > &B)
const TinyMatrix< N, N, T > & operator+= (const TinyMatrix< N, N, T > &B)
const TinyMatrix< N, N, T > & operator-= (const TinyMatrix< N, N, T > &B)
const TinyMatrix< N, N, T > & operator= (const T &t)
template<size_t P>
TinyMatrix< N, P > operator* (const TinyMatrix< N, P > &B) const
TinyVector< N, T > operator* (const TinyVector< N, T > &v) const
TinyMatrix< N, N, T > operator* (const T &t) const
TinyMatrix< N, N, T > operator/ (const T &t) const
const TinyMatrix< N, N, T > & operator*= (const T &t)
TinyMatrix< N, N, T > & operator/= (const T &t)
TinyMatrix< N, N, T > operator+ (const TinyMatrix< N, N, T > &B) const
TinyMatrix< N, N, T > operator- (const TinyMatrix< N, N, T > &B) const
TinyMatrix< N, N, T > operator- () const
void swapLines (const size_t &n, const size_t &m)
TinyVector< N, T > applyLU (const TinyVector< N, T > &b) const
 TinyMatrix ()
 TinyMatrix (const TinyMatrix< N, N, T > &B)
 TinyMatrix (const T &t)
 ~TinyMatrix ()

Private Attributes

__x [N][N]

Friends

TinyMatrix< N, N, T > operator* (const T &t, const TinyMatrix< N, N, T > &A)
std::ostream & operator<< (std::ostream &os, const TinyMatrix< N, N, T > &A)
det (const TinyMatrix< N, N, T > &A)
template<typename T2>
gaussPivot (const TinyMatrix< N, N, T > &A, const TinyVector< N, T2 > &rightHandSide, TinyVector< N, T2 > &X)
void LU (const TinyMatrix< N, N, T > &A, TinyMatrix< N, N, T > &lu, TinyMatrix< N, N, T > &P)
TinyVector< N, T > operator/ (const TinyVector< N, T > &b, const TinyMatrix< N, N, T > &A)


Detailed Description

template<size_t N, typename T>
class TinyMatrix< N, N, T >

Definition at line 364 of file TinyMatrix.hpp.


Member Typedef Documentation

template<size_t N, typename T>
typedef TinyVector<N> TinyMatrix< N, N, T >::AssociatedVector

Type of compatible vector

Definition at line 368 of file TinyMatrix.hpp.

template<size_t N, typename T>
typedef TinyVector<N> TinyMatrix< N, N, T >::AssociatedTransposedVector

Type of compatible transposed vector

Definition at line 371 of file TinyMatrix.hpp.

template<size_t N, typename T>
typedef T TinyMatrix< N, N, T >::ValueType

The type of the elements of the TinyMatrix

Definition at line 374 of file TinyMatrix.hpp.


Member Enumeration Documentation

template<size_t N, typename T>
anonymous enum

Enumerator:
NumberOfLines  number of lines
NumberOfRow  number of rows

Definition at line 377 of file TinyMatrix.hpp.

00377        {
00378     NumberOfLines = N,          
00379     NumberOfRow   = N           
00380   };


Constructor & Destructor Documentation

template<size_t N, typename T>
TinyMatrix< N, N, T >::TinyMatrix (  )  [inline]

Default constructor

Note:
sets matrix to 0

Definition at line 884 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00885   {
00886     for (size_t i=0; i<N; i++)
00887       for (size_t j=0; j<N; j++)
00888         __x[i][j] = 0;
00889   }

template<size_t N, typename T>
TinyMatrix< N, N, T >::TinyMatrix ( const TinyMatrix< N, N, T > &  B  )  [inline]

Copy constructor

Parameters:
B the matrix to copy

Definition at line 896 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00897   {
00898     for (size_t i=0; i<N; i++)
00899       for (size_t j=0; j<N; j++)
00900         __x[i][j] = B.__x[i][j];
00901   }

template<size_t N, typename T>
TinyMatrix< N, N, T >::TinyMatrix ( const T &  t  )  [inline]

Constructs a matrix using a given T

Parameters:
t the given T
Note:
the built matrix is diagonal. Its coefficients are t for diagonal terms 0 for others

Definition at line 911 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00912   {
00913     for (size_t i=0; i<N; i++)
00914       for (size_t j=0; j<N; j++)
00915         __x[i][j] = (i==j)?t:0;
00916   }

template<size_t N, typename T>
TinyMatrix< N, N, T >::~TinyMatrix (  )  [inline]

Destructor

Definition at line 922 of file TinyMatrix.hpp.

00923   {
00924     ;
00925   }


Member Function Documentation

template<size_t N, typename T>
size_t TinyMatrix< N, N, T >::size (  )  const [inline]

Returns the sizes of the matrix

Returns:
N

Definition at line 392 of file TinyMatrix.hpp.

00393   {
00394     return N;
00395   }

template<size_t N, typename T>
T& TinyMatrix< N, N, T >::operator() ( const size_t &  i,
const size_t &  j 
) [inline]

Access to the (i,j) coefficient of the matrix

Parameters:
i the line number
j the row number
Returns:
$ A_{ij} $

Definition at line 405 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x, and ASSERT.

00406   {
00407     ASSERT ((i<N) && (j<N));
00408     return __x[i][j];
00409   }

template<size_t N, typename T>
const T& TinyMatrix< N, N, T >::operator() ( const size_t &  i,
const size_t &  j 
) const [inline]

Read-only access to the (i,j) coefficient of the matrix

Parameters:
i the line number
j the row number
Returns:
$ A_{ij} $

Definition at line 419 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x, and ASSERT.

00420   {
00421     ASSERT ((i<N) && (j<N));
00422     return __x[i][j];
00423   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::invert (  )  const [inline]

Computes the invert of the matrix

Returns:
$ A^{-1} $
Note:
uses a PA=LU decomposition

Definition at line 432 of file TinyMatrix.hpp.

00433   {
00434     TinyMatrix<N,N,T> M;
00435     TinyMatrix<N,N,T> lu;
00436     TinyMatrix<N,N,T> P;
00437     LU(*this, lu, P);
00438 
00439     TinyVector<N,T> v;
00440     TinyVector<N,T> u;
00441 
00442     for (size_t i=0; i<N; ++i) {
00443       for (size_t j=0; j<N; ++j)
00444         v[j] = (i==j)?1:0;
00445 
00446       u = lu.applyLU(P*v);
00447       for (size_t j=0; j<N; ++j)
00448         M(j,i) = u[j];
00449     }
00450 
00451     return M;
00452   }

template<size_t N, typename T>
TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator= ( const TinyMatrix< N, N, T > &  B  )  [inline]

Makes the current matrix be equal to a given one

Parameters:
B the given matrix
Returns:
the matrix $ A := B $

Definition at line 461 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00462   {
00463     for (size_t i=0; i<N; i++)
00464       for (size_t j=0; j<N; j++)
00465         __x[i][j] = B.__x[i][j];
00466     return (*this);
00467   }

template<size_t N, typename T>
const TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator+= ( const TinyMatrix< N, N, T > &  B  )  [inline]

Adds a matrix to a given one

Parameters:
B the matrix to add
Returns:
$ A := A +B $

Definition at line 476 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00477   {
00478     for (size_t i=0; i<N; i++)
00479       for (size_t j=0; j<N; j++)
00480         __x[i][j] += B.__x[i][j];
00481     return (*this);
00482   }

template<size_t N, typename T>
const TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator-= ( const TinyMatrix< N, N, T > &  B  )  [inline]

Substracts a given matrix

Parameters:
B the matrix to substract
Returns:
$ A := A-B $

Definition at line 491 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00492   {
00493     for (size_t i=0; i<N; i++)
00494       for (size_t j=0; j<N; j++)
00495         __x[i][j] -= B.__x[i][j];
00496     return (*this);
00497   }

template<size_t N, typename T>
const TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator= ( const T &  t  )  [inline]

Makes matrix diagonal of value of a given T

Parameters:
t the given value
Returns:
$ A := \textrm{diag}(t) $

Definition at line 506 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00507   {
00508     for (size_t i=0; i<N; i++)
00509       for (size_t j=0; j<N; j++)
00510         __x[i][j] = (i==j)?t:0;
00511     return (*this);
00512   }

template<size_t N, typename T>
template<size_t P>
TinyMatrix<N,P> TinyMatrix< N, N, T >::operator* ( const TinyMatrix< N, P > &  B  )  const [inline]

Computes the product on the right to a given compatible matrix

Parameters:
B the given matrix
Returns:
$ AB $

Definition at line 522 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00523   {
00524     TinyMatrix<N,P> C(0);
00525     for (size_t i=0; i<N; i++)
00526       for (size_t j=0; j<P; j++)
00527         for (size_t k=0; k<N; k++)
00528           C(i,j) += __x[i][k] * B(k,j);
00529     return C;
00530   }

template<size_t N, typename T>
TinyVector<N,T> TinyMatrix< N, N, T >::operator* ( const TinyVector< N, T > &  v  )  const [inline]

Computes the product with a given compatible vector

Parameters:
v the given vector
Returns:
$ Av $

Definition at line 539 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00540   {
00541     TinyVector<N,T> u = 0;
00542     for (size_t i=0; i<N; i++)
00543       for (size_t j=0; j<N; j++)
00544           u[i] += __x[i][j] * v[j];
00545     return u;
00546   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::operator* ( const T &  t  )  const [inline]

Computes the product with a given T on the right

Parameters:
t the given T
Returns:
$ t A $

Definition at line 555 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00556   {
00557     TinyMatrix<N,N,T> B;
00558     for (size_t i=0; i<N; i++)
00559       for (size_t j=0; j<N; j++)
00560           B.__x[i][j] = __x[i][j] * t;
00561     return B;
00562   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::operator/ ( const T &  t  )  const [inline]

Computes the product with the invert of a given vector on the right

Parameters:
t the given vector
Returns:
$ t^{-1} A $

Definition at line 571 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x, and ASSERT.

00572   {
00573     ASSERT (t!=0);
00574     const T t_1 = 1./t;
00575     TinyMatrix<N,N,T> B;
00576     for (size_t i=0; i<N; i++)
00577       for (size_t j=0; j<N; j++)
00578           B.__x[i][j] = __x[i][j] * t_1;
00579     return B;
00580   }

template<size_t N, typename T>
const TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator*= ( const T &  t  )  [inline]

Multiplies a matrix by a given T on the right

Parameters:
t the given T
Returns:
$ A := t A $

Definition at line 589 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00590   {
00591     for (size_t i=0; i<N; i++)
00592       for (size_t j=0; j<N; j++)
00593         __x[i][j] *= t;
00594     return (*this);
00595   }

template<size_t N, typename T>
TinyMatrix<N,N,T>& TinyMatrix< N, N, T >::operator/= ( const T &  t  )  [inline]

Multiplies a matrix by the invert of a given T on the right

Parameters:
t the given T
Returns:
$ A := t^{-1} A $

Definition at line 604 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00604                                                      {
00605     const T t_1 = 1./t;
00606     for (size_t i=0; i<N; i++)
00607       for (size_t j=0; j<N; j++)
00608         __x[i][j] *= t_1;
00609     return (*this);
00610   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::operator+ ( const TinyMatrix< N, N, T > &  B  )  const [inline]

Computes the sum of two matrices

Parameters:
B the second matrix
Returns:
the matrix $ C=A+B$

Definition at line 619 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00620   {
00621     TinyMatrix<N,N,T> C;
00622     for (size_t i=0; i<N; i++)
00623       for (size_t j=0; j<N; j++)
00624           C.__x[i][j] = __x[i][j] + B.__x[i][j];
00625     return C;
00626   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::operator- ( const TinyMatrix< N, N, T > &  B  )  const [inline]

Computes the difference of two matrices

Parameters:
B the second matrix
Returns:
$ A - B $

Definition at line 635 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00636   {
00637     TinyMatrix<N,N,T> C;
00638     for (size_t i=0; i<N; i++)
00639       for (size_t j=0; j<N; j++)
00640           C.__x[i][j] = __x[i][j] - B.__x[i][j];
00641     return C;
00642   }

template<size_t N, typename T>
TinyMatrix<N,N,T> TinyMatrix< N, N, T >::operator- (  )  const [inline]

Returns the opposed matrix

Returns:
$ -A $

Definition at line 664 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00665   {
00666     TinyMatrix<N,N,T> B;
00667     for (size_t i=0; i<N; i++)
00668       for (size_t j=0; j<N; j++)
00669         B.__x[i][j] = - __x[i][j];
00670     return B;
00671   }

template<size_t N, typename T>
void TinyMatrix< N, N, T >::swapLines ( const size_t &  n,
const size_t &  m 
) [inline]

Swaps two lines of the current matrix

Parameters:
n first line
m second line

Definition at line 783 of file TinyMatrix.hpp.

References TinyMatrix< M, N, T >::__x.

00784   {
00785     if (n==m)
00786       return;
00787 
00788     T temp;
00789     for (size_t i=0; i<N; ++i) {
00790       temp = __x[n][i];
00791       __x[n][i] = __x[m][i];
00792       __x[m][i] = temp;
00793     }
00794   }

template<size_t N, typename T>
TinyVector<N,T> TinyMatrix< N, N, T >::applyLU ( const TinyVector< N, T > &  b  )  const [inline]

Applies LU matrices to a given vector

Parameters:
b the given vector
Returns:
$ (LU)^{-1}b $

Definition at line 839 of file TinyMatrix.hpp.

00840   {
00841     const TinyMatrix<N,N,T>& lu = (*this);
00842 
00843     TinyVector<N,T> y = b;
00844     // Go down
00845     for (size_t i=0; i<N; i++) {
00846       for (size_t j=0; j<i; j++)
00847         y[i] -= y[j]*lu(i,j);
00848     }
00849 
00850     // go up
00851     for (size_t i=N-1; i<N; i--) {
00852       for (size_t j=i+1; j<N; j++)
00853         y[i] -= y[j]*lu(i,j);
00854 
00855       y[i] /= lu(i,i);
00856     }
00857         
00858     return y;
00859   }


Friends And Related Function Documentation

template<size_t N, typename T>
TinyMatrix<N,N,T> operator* ( const T &  t,
const TinyMatrix< N, N, T > &  A 
) [friend]

Computes the product one the left of a T and a matrix

Parameters:
t a given T
A a given matrix
Returns:
$ t A $

Definition at line 652 of file TinyMatrix.hpp.

00654   {
00655     return (A*t);
00656   }

template<size_t N, typename T>
std::ostream& operator<< ( std::ostream &  os,
const TinyMatrix< N, N, T > &  A 
) [friend]

Prints a matrix to a stream

Parameters:
os the given stream
A the given matrix
Returns:
the modified stream

Definition at line 681 of file TinyMatrix.hpp.

00683   {
00684     for (size_t i=0; i<N; i++) {
00685       for (size_t j=0; j<N; j++)
00686         os << A(i,j) << " ";
00687       os << '\n';
00688     }
00689     return os;
00690   }

template<size_t N, typename T>
T det ( const TinyMatrix< N, N, T > &  A  )  [friend]

Computes the determinent of a square matrix

Parameters:
A the matrix
Returns:
$ |A| $

Definition at line 699 of file TinyMatrix.hpp.

00700   {
00701     T d = 1.;
00702     TinyMatrix<N,N,T> B = A;
00703 
00704     for (size_t k=0; k<N-1; k++) {
00705       size_t maxLine = k;
00706       for (size_t i=k+1; i<N; i++) {
00707         maxLine = (std::abs(B(i,k))>std::abs(B(maxLine,k)))?i:maxLine;
00708       }
00709       if (k != maxLine) {
00710         B.swapLines(k,maxLine);
00711         d *= -1;
00712       }
00713 
00714       for(size_t i=k+1; i<N; ++i) {
00715         for (size_t j=k+1; j<N; j++) {
00716           B(i,j) -= B(i,k)*B(k,j)/B(k,k);
00717         }
00718         B(i,k) = 0;
00719       }
00720     }
00721     for (size_t i=0; i<N; ++i)
00722       d *= B(i,i);
00723     return d;
00724   }

template<size_t N, typename T>
template<typename T2>
T gaussPivot ( const TinyMatrix< N, N, T > &  A,
const TinyVector< N, T2 > &  rightHandSide,
TinyVector< N, T2 > &  X 
) [friend]

Solves a linear system using the Gauss method

Parameters:
A the given matrix
rightHandSide the right hand and side
X the solution vector
Returns:
the determinent of A (none 0 if invertible)

Definition at line 736 of file TinyMatrix.hpp.

00739   {
00740     T t = 1.;
00741     TinyMatrix<N,N,T> M = A;
00742     TinyVector<N,T2> b = rightHandSide;
00743 
00744     for (size_t k=0; k<N-1; k++) {
00745       size_t maxLine = k;
00746       for (size_t i=k+1; i<N; i++) {
00747         maxLine = (std::abs(M(i,k))>std::abs(M(maxLine,k)))?i:maxLine;
00748       }
00749       if (k != maxLine) {
00750         M.swapLines(k,maxLine);
00751         std::swap(b[k], b[maxLine]);
00752         t *= -1;
00753       }
00754 
00755       for(size_t i=k+1; i<N; ++i) {
00756         const real_t coef = M(i,k)/M(k,k);
00757         for (size_t j=k; j<N; j++) {
00758           M(i,j) -= coef * M(k,j);
00759         }
00760         b[i] -= coef*b[k];
00761       }
00762     }
00763     for (size_t i=0; i<N; ++i)
00764       t *= M(i,i);
00765 
00766     for (size_t i = N-1; i < N; --i) {
00767       X[i] = 0;
00768       for (size_t j = i+1; j<N; j++) {
00769         X[i] -= M(i,j)*X[j];
00770       }
00771       X[i] += b[i];
00772       X[i] /= M(i,i);
00773     }
00774     return t;
00775   }

template<size_t N, typename T>
void LU ( const TinyMatrix< N, N, T > &  A,
TinyMatrix< N, N, T > &  lu,
TinyMatrix< N, N, T > &  P 
) [friend]

Computes the PA=LU factorization

Parameters:
A the given $ A $ matrix
lu the computed $ LU $
P the computation $ P $ (permutation) matrix

Definition at line 803 of file TinyMatrix.hpp.

00806   {
00807     // Reinitialize P to Identity
00808     for (size_t i=0; i<N; i++)
00809       for (size_t j=0; j<N; j++) {
00810         P(i,j) = (i==j);
00811       }
00812 
00813     lu = A;
00814     
00815     for (size_t k=0; k<N-1; k++) {
00816       // Checkes for Pivot.
00817       size_t maxLine = k;
00818       for (size_t i=k+1; i<N; i++) {
00819         maxLine = (std::abs(lu(i,k))<std::abs(lu(maxLine,k)))?maxLine:i;
00820       }
00821       lu.swapLines(k,maxLine);
00822       P.swapLines(k,maxLine);
00823 
00824       for (size_t i=k+1; i<N; i++) {
00825         lu(i,k) /= lu(k,k);
00826         for (size_t j=k+1; j<N; j++)
00827           lu(i,j) -=lu(i,k)*lu(k,j);
00828       }
00829     }
00830   }

template<size_t N, typename T>
TinyVector<N,T> operator/ ( const TinyVector< N, T > &  b,
const TinyMatrix< N, N, T > &  A 
) [friend]

Solves a linear system

Parameters:
b the given second member
A the given matrix
Returns:
$ A^{-1} b $

Definition at line 869 of file TinyMatrix.hpp.

00871   {
00872     TinyMatrix<N,N,T> lu;
00873     TinyMatrix<N,N,T> P;
00874     LU(A,lu,P);
00875 
00876     return lu.applyLU(P*b);
00877   }


Member Data Documentation

template<size_t N, typename T>
T TinyMatrix< N, N, T >::__x[N][N] [private]

values of the matrix

Definition at line 383 of file TinyMatrix.hpp.


The documentation for this class was generated from the following file:

Generated on Wed Nov 19 00:16:27 2008 for FreeFEM3D (aka ff3d) by  doxygen 1.5.6