indii/ml/aux/matrix.hpp

00001 #ifndef INDII_ML_AUX_MATRIX_HPP
00002 #define INDII_ML_AUX_MATRIX_HPP
00003 
00004 #include "vector.hpp"
00005 
00006 #include "boost/numeric/ublas/matrix.hpp"
00007 #include "boost/numeric/ublas/matrix_sparse.hpp"
00008 #include "boost/numeric/ublas/matrix_proxy.hpp"
00009 #include "boost/numeric/ublas/symmetric.hpp"
00010 #include "boost/numeric/ublas/triangular.hpp"
00011 #include "boost/numeric/ublas/banded.hpp"
00012 #include "boost/numeric/ublas/io.hpp"
00013 #include "boost/numeric/ublas/storage.hpp"
00014 
00015 namespace indii {
00016   namespace ml {
00017     namespace aux {
00018 
00019       /**
00020        * General matrix.
00021        */
00022       typedef boost::numeric::ublas::matrix<double,
00023           boost::numeric::ublas::column_major,
00024           boost::numeric::ublas::unbounded_array<double> > matrix;
00025 
00026       /**
00027        * Symmetric matrix.
00028        */
00029       typedef boost::numeric::ublas::symmetric_matrix<double,
00030           boost::numeric::ublas::lower, boost::numeric::ublas::column_major,
00031           boost::numeric::ublas::unbounded_array<double> > symmetric_matrix;
00032 
00033       /**
00034        * Lower triangular matrix.
00035        */
00036       typedef boost::numeric::ublas::triangular_matrix<double,
00037           boost::numeric::ublas::lower, boost::numeric::ublas::column_major,
00038           boost::numeric::ublas::unbounded_array<double> >
00039           lower_triangular_matrix;
00040 
00041       /**
00042        * Upper triangular matrix.
00043        */
00044       typedef boost::numeric::ublas::triangular_matrix<double,
00045           boost::numeric::ublas::upper, boost::numeric::ublas::column_major,
00046           boost::numeric::ublas::unbounded_array<double> >
00047           upper_triangular_matrix;
00048 
00049       /**
00050        * Identity matrix.
00051        */
00052       typedef boost::numeric::ublas::identity_matrix<double> identity_matrix;
00053       
00054       /**
00055        * Zero matrix.
00056        */
00057       typedef boost::numeric::ublas::zero_matrix<double> zero_matrix;
00058 
00059       /**
00060        * Scalar matrix.
00061        */
00062       typedef boost::numeric::ublas::scalar_matrix<double> scalar_matrix;
00063 
00064       /**
00065        * Banded matrix.
00066        */
00067       typedef boost::numeric::ublas::banded_matrix<double,
00068           boost::numeric::ublas::column_major,
00069           boost::numeric::ublas::unbounded_array<double> > banded_matrix;
00070 
00071       /**
00072        * Sparse matrix.
00073        */
00074       typedef boost::numeric::ublas::mapped_matrix<double,
00075           boost::numeric::ublas::column_major> sparse_matrix;
00076       //typedef boost::numeric::ublas::compressed_matrix<double,
00077       //    boost::numeric::ublas::column_major> sparse_matrix;
00078       // ^ seems to cause segfaults in some situations, perhaps when
00079       //   assertions are disabled?
00080 
00081       /**
00082        * Projection matrix.
00083        */
00084       typedef boost::numeric::ublas::mapped_matrix<short int,
00085           boost::numeric::ublas::column_major> projection_matrix;
00086 
00087       /**
00088        * Inverse of a square matrix.
00089        *
00090        * @param A matrix to invert.
00091        * @param AI matrix into which to write the inverse of A.
00092        *
00093        * @warning Will change the contents of @p A!
00094        */
00095       void inv(matrix& A, matrix& AI);
00096 
00097       /**
00098        * Diagonal of a square matrix.
00099        *
00100        * @param A square matrix.
00101        */
00102       template <class T>
00103       boost::numeric::ublas::matrix_vector_range<T> diag(T& A);
00104 
00105       /**
00106        * Convert vector to matrix.
00107        *
00108        * @param x Vector to convert.
00109        * @param A Matrix into which to write conversion.
00110        *
00111        * The vector is assumed to have column-wise dense storage and must
00112        * have the same number of elements as the matrix.
00113        */
00114       template <class VT, class MT>
00115       void vectorToMatrix(const VT& x, MT& A);
00116 
00117       /**
00118        * Convert matrix to vector.
00119        *
00120        * @param A Matrix to convert.
00121        * @param x Vector into which to write conversion.
00122        *
00123        * The matrix is written into the vector using column-wise dense
00124        * storage and must have the same number of elements as the vector.
00125        */
00126       template <class MT, class VT>
00127       void matrixToVector(const MT& A, VT& x);
00128 
00129     }
00130   }
00131 }
00132 
00133 template <class T>
00134 boost::numeric::ublas::matrix_vector_range<T> indii::ml::aux::diag(T& A) {
00135   /* pre-condition */
00136   assert (A.size1() == A.size2());
00137 
00138   const boost::numeric::ublas::range step(0, A.size1());
00139 
00140   return boost::numeric::ublas::matrix_vector_range<T>(A, step, step);
00141 }
00142 
00143 template <class VT, class MT>
00144 void indii::ml::aux::vectorToMatrix(const VT& x, MT& A) {
00145   /* pre-condition */
00146   assert (x.size() == A.size1() * A.size2());
00147   
00148   const unsigned int M = A.size1(), N = A.size2();
00149   unsigned int col;
00150   for (col = 0; col < N; col++) {
00151     column(A,col) = subrange(x, col*M, (col+1)*M);
00152   }
00153 }
00154 
00155 template <class MT, class VT>
00156 void indii::ml::aux::matrixToVector(const MT& A, VT& x) {
00157   /* pre-condition */
00158   assert (x.size() == A.size1() * A.size2());
00159 
00160   const unsigned int M = A.size1(), N = A.size2();
00161   unsigned int col;
00162   for (col = 0; col < N; col++) {
00163     subrange(x, col*M, (col+1)*M) = column(A,col);
00164   }
00165 }
00166 
00167 #endif
00168 

Generated on Wed Dec 17 15:11:57 2008 for dysii Dynamical Systems Library by  doxygen 1.5.3