Matrix.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Jacques Gasselin                                *
00003  *   jacquesgasselin@hotmail.com                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef MATRIX_H
00021 #define MATRIX_H
00022 
00023 #ifdef MATRIX_IOSTREAM
00024     #include <iostream>
00025 #endif
00026 
00027 #include <cassert>
00028 #include <valarray>
00029 
00030 namespace mathglpp{
00031 
00032 
00034 template <typename T>
00035 class Matrix2
00036 {
00037 
00038 public:
00040     typedef size_t dim_type;
00041     typedef T value_type;
00042     typedef std::valarray<value_type> vector_type;
00043     typedef std::slice_array<value_type> sliced_vector_type;
00044 
00046     Matrix2(const dim_type& m, const dim_type& n)
00047     :rows(m),cols(n),vec(m*n)
00048     { }
00049 
00051     Matrix2(const Matrix2& mat)
00052     :rows(mat.rows),cols(mat.cols),vec(mat.vec)
00053     { }
00054 
00056     ~Matrix2()
00057     { }
00058 
00060     Matrix2& operator =(const Matrix2& rhs)
00061     {
00062         if(&rhs != this)
00063         { rows = rhs.rows; cols = rhs.cols; vec = rhs.vec; }
00064         return *this;
00065     }
00066 
00067     inline Matrix2& identity() 
00068     {   vec = 0; vec[std::slice(0,std::min(cols,rows),cols+1)] = 1; return *this; }
00069     
00070     
00071     inline Matrix2 filter(T (*filter_func)(const Matrix2&, const dim_type, const dim_type))
00072     {
00073         Matrix2 temp(*this);
00074         
00075         for(dim_type i = 0; i < rows; ++i)
00076             for(dim_type j = 0; j < cols; ++j)
00077                 temp(i,j) = filter_func(*this,i,j);
00078         
00079         return temp;
00080     }
00081     
00083     inline value_type& operator()(const dim_type i, const dim_type j) { return vec[i*cols+j]; }
00085     inline const value_type& operator()(const dim_type i, const dim_type j) const { return vec[i*cols+j]; }
00087     inline value_type& operator[](const dim_type ind) { return vec[ind]; }
00089     inline const value_type& operator[](const dim_type ind) const { return vec[ind]; }
00091     inline const sliced_vector_type& operator[](const std::slice& sl) { return vec[sl]; }
00092 
00093     inline const sliced_vector_type& row(dim_type rn, dim_type cstart = 0)
00094     { return vec[row_slice(rn,cstart)]; }
00095     inline const sliced_vector_type& row(dim_type rn, dim_type cstart, dim_type cend) 
00096     { return vec[row_slice(rn,cstart,cend)]; }
00097     inline const sliced_vector_type& col(dim_type cn, dim_type rstart = 0)
00098     { return vec[col_slice(cn,rstart)]; }
00099     inline const sliced_vector_type& col(dim_type cn, dim_type rstart, dim_type rend)
00100     { return vec[col_slice(cn,rstart,rend)]; }
00101 
00103     inline const std::slice row_slice(dim_type rn, dim_type cstart = 0) const 
00104     { return std::slice(cstart+rn*cols,cols-cstart,1); }
00106     inline const std::slice row_slice(dim_type rn, dim_type cstart, dim_type cend) const 
00107     { return std::slice(cstart+rn*cols,cend-cstart,1); }
00108 
00110     inline const std::slice col_slice(dim_type cn, dim_type rstart = 0) const 
00111     { return std::slice(cn+rstart*cols,rows-rstart,cols); }
00113     inline const std::slice col_slice(dim_type cn, dim_type rstart, dim_type rend) const 
00114     { return std::slice(cn+rstart*cols,rend-rstart,cols); }
00115 
00117     inline const dim_type& getM() const { return rows; }
00118     inline const dim_type& getN() const { return cols; }
00119     inline const dim_type& size() const { return vec.size(); }
00120 
00122     inline operator vector_type& () { return vec; }
00123     inline operator const vector_type& () const { return vec; }
00124     inline operator const value_type*() const  { return &vec[0]; }
00125  
00126     #ifdef MATRIX_IOSTREAM
00128     friend std::basic_istream<char, std::char_traits<char> >& operator >>
00129       ( std::basic_istream<char, std::char_traits<char> >& in, Matrix2& mat)
00130     {
00131         for(int i = 0; i < mat.rows; ++i)
00132             for(int j = 0; j < mat.cols; ++j)
00133                 in>>mat.vec[i*mat.cols+j];
00134         return in;
00135     }
00136 
00138     friend std::basic_ostream<char, std::char_traits<char> >& operator <<
00139       ( std::basic_ostream<char, std::char_traits<char> >& out, Matrix2& mat)
00140     {
00141         out<<"[ ";
00142         for(int i = 0; i < mat.rows; ++i)
00143         {
00144             for(int j = 0; j < mat.cols; ++j)
00145                 out<<mat.vec[i*mat.cols+j]<<" ";
00146             if(i < mat.rows-1)
00147                 out<<std::endl<<"  ";
00148         }
00149         out<<"]"<<std::endl;
00150         return out;
00151     }
00152     #endif
00153 
00154 protected:
00156     dim_type rows,cols;
00158     vector_type vec;
00159 };
00160 
00161 
00162 }
00163 #endif

Generated on Wed Oct 3 12:50:49 2007 for MathGL++ by  doxygen 1.5.2