• Main Page
  • Modules
  • Classes
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/include/Matrix.h

Go to the documentation of this file.
00001 /* $Revision: 1.6 $ $Date: 2006/09/08 18:56:52 $ $Author: dgl $ $Name:  $ $Id: Matrix.h,v 1.6 2006/09/08 18:56:52 dgl Exp $ */
00002 #ifndef MATRIX_H
00003 #define MATRIX_H
00004 
00005 // The Musimat Tutorial � 2006 Gareth Loy
00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 
00007 // and published exclusively by The MIT Press.
00008 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00014 // The Musimat website:                 http://www.musimat.com/
00015 // This program is released under the terms of the GNU General Public License
00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
00017 
00021 
00022 #include <iostream>
00023 #include "List.h"
00024 
00026 template <class Type>
00027 class Matrix {
00028 
00030         friend ostream& operator<<(ostream& os,Const Matrix& m) {
00031                 // os << m.m_lenRow << " " << m.m_lenCol << " " <<endl;
00032                 for (Integer r = 0; r < m.m_lenRow; r++)
00033                         cout << m.m_matrix[r];
00034                 Return os;
00035         }
00036         
00038         friend istream& operator>>(istream& is, Matrix& m) {
00039                 Integer rows,cols;
00040                 is >> rows >> cols;
00041                 m.SetSize(rows, cols);
00042                 for (Integer r = 0; r < rows; r++)
00043                         for (Integer c = 0; c < cols; c++)
00044                                 is >> m[r][c];
00045                 Return is;
00046                 }
00047 public:
00048 
00050     Matrix(Integer rows=0, Integer cols=0): m_matrix(NULL) {
00051         SetSize(rows,cols);
00052     }
00053 
00055     Matrix(Const Matrix& m): m_matrix(NULL) {
00056         SetSize(m.m_lenRow, m.m_lenCol);
00057         for (Integer r=0; r < m_lenRow; r++)
00058             m_matrix[r]=List<Type>(m.m_matrix[r]);
00059     }
00060 
00064     Void SetSize(Integer rows, Integer cols) {
00065                 If (m_matrix != 0) { 
00066                         delete[] m_matrix;
00067                         m_matrix = 0;
00068                 }
00069         If (cols > 0  And rows > 0) {
00070             m_matrix = new List<Type>[rows];
00071             for (Integer i = 0; i < rows; i++)
00072                 m_matrix[i].SetListSize(cols);
00073         }
00074         Else
00075             rows = 0;
00076         m_lenCol = cols;
00077                 m_lenRow = rows;
00078     }
00079 
00081         List<Type>& operator[](Integer index) {
00082         assert(index < m_lenRow);
00083         Return m_matrix[index];
00084     }
00085 
00087     Matrix& operator=(Const Matrix& m) {
00088                 If (this == &m)
00089                         Return *this;
00090         SetSize(m.m_lenRow, m.m_lenCol);
00091                 for (Integer r=0; r < m_lenRow; r++) {
00092             m_matrix[r] = List<Type>(m.m_matrix[r]);
00093                 }
00094         Return *this;
00095     }
00096 
00098         Bool operator==(Const Matrix& m) {
00099                 // Must have same dimensions
00100                 If (m_lenRow != m.m_lenRow || m_lenCol != m.m_lenCol)
00101                         Return False;
00102 
00103                 // All rows must match
00104                 For (Integer r=0; r < m_lenRow; r++) {
00105                         If (m_matrix[r] != m.m_matrix[r])
00106                                 Return False;
00107                 }
00108                 Return( True );
00109         }
00110 
00112         Bool operator!=(Const Matrix& m) {
00113                 Return( ! (*this == m) );
00114         }
00115 
00118         Const Matrix operator+(Const Matrix& m) {
00119         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
00120         Matrix newMatrix(m_lenRow, m_lenCol);
00121                 For (Integer r=0; r < m_lenRow; r++)
00122             for (Integer c=0; c < m_lenCol;c++)
00123                 newMatrix[r][c] = m_matrix[r][c] + m.m_matrix[r][c];
00124         Return newMatrix;
00125     }
00126 
00129         Const Matrix operator-(Const Matrix& m) {
00130         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
00131         Matrix newMatrix(m_lenRow, m_lenCol);
00132                 For (Integer r = 0; r < m_lenRow; r++)
00133             for (Integer c = 0; c < m_lenCol; c++)
00134                 newMatrix[r][c] = m_matrix[r][c] - m.m_matrix[r][c];
00135         Return newMatrix;
00136     }
00137 
00139         Const Matrix operator*(Const Integer R) {
00140         Matrix newMatrix(m_lenRow, m_lenCol);
00141                 For (Integer r = 0; r < m_lenRow; r++)
00142             for (Integer c = 0; c < m_lenCol; c++)
00143                 newMatrix[r][c] = m_matrix[r][c] * R;
00144         Return newMatrix;
00145     }
00146 
00148         Const Matrix operator*(Const Real R) {
00149         Matrix newMatrix(m_lenRow, m_lenCol);
00150                 For (Integer r = 0; r < m_lenRow; r++)
00151             for (Integer c = 0; c < m_lenCol; c++)
00152                 newMatrix[r][c] = m_matrix[r][c] * R;
00153         Return newMatrix;
00154     }
00155 
00157         Const Matrix operator+(Const Integer R) {
00158         Matrix newMatrix(m_lenRow, m_lenCol);
00159                 For (Integer r = 0; r < m_lenRow; r++)
00160             for (Integer c = 0; c < m_lenCol; c++)
00161                 newMatrix[r][c] = m_matrix[r][c] + R;
00162         Return newMatrix;
00163     }
00164 
00166         Const Matrix operator+(Const Real R) {
00167         Matrix newMatrix(m_lenRow, m_lenCol);
00168                 For (Integer r = 0; r < m_lenRow; r++)
00169             for (Integer c = 0; c < m_lenCol; c++)
00170                 newMatrix[r][c] = m_matrix[r][c] + R;
00171         Return newMatrix;
00172     }
00173 
00175         Const Matrix operator-(Const Integer R) {
00176         Matrix newMatrix(m_lenRow, m_lenCol);
00177                 For (Integer r = 0; r < m_lenRow; r++)
00178             for (Integer c = 0; c < m_lenCol; c++)
00179                 newMatrix[r][c] = m_matrix[r][c] - R;
00180         Return newMatrix;
00181     }
00182 
00184         Const Matrix operator-(Const Real R) {
00185         Matrix newMatrix(m_lenRow, m_lenCol);
00186                 For (Integer r = 0; r < m_lenRow; r++)
00187             for (Integer c = 0; c < m_lenCol; c++)
00188                 newMatrix[r][c] = m_matrix[r][c] - R;
00189         Return newMatrix;
00190     }
00191 
00193         Const Matrix operator*(Matrix& m) {
00194         assert(m_lenCol==m.m_lenRow);
00195         Matrix newMatrix(m_lenRow,m.m_lenCol);
00196         for (Integer r = 0; r < m_lenRow; r++) {
00197             for (Integer c = 0; c < m.m_lenCol; c++) {
00198                 for (Integer i = 0; i < m_lenCol; i++) {
00199                     newMatrix[r][c]+= m_matrix[r][i] * m[i][c];
00200                 }
00201             }
00202         }
00203         Return newMatrix;
00204     }
00205 
00208         Void print(String s = 0) {
00209                 If (s)
00210                         cout<<s;
00211                 cout << "{\n";
00212                 For (Integer i = 0; i < m_lenRow; i++)
00213                         cout << m_matrix[i];
00214                 cout << "}\n";
00215         }
00216 
00218         Type rows() { Return m_lenRow; }
00219 
00221         Type cols() { Return m_lenCol; }
00222 
00226         Void length(Integer& r, Integer& c) { r = rows(); c = cols(); }
00227 
00228 private:
00229     Integer m_lenCol, m_lenRow;
00230     List<Type>* m_matrix;
00231 
00232 };
00233 
00234 #endif // MATRIX_H

Generated on Fri Nov 26 2010 16:18:25 for MusimatLib by  doxygen 1.7.2