- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/include/Matrix.h

Go to the documentation of this file.

    00001 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:53 $ $Author: dgl $ $Name:  $ $Id: _matrix_8h-source.html,v 1.2 2006/09/09 06:22:53 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 using namespace std;
    00024 #include "List.h"
    00025
    00027 template <class Type>
    00028 class Matrix {
    00029
    00031         friend ostream& operator<<(ostream& os,Const Matrix& m) {
    00032                 // os << m.m_lenRow << " " << m.m_lenCol << " " <<endl;
    00033                 for (Integer r = 0; r < m.m_lenRow; r++)
    00034                         cout << m.m_matrix[r];
    00035                 Return os;
    00036         }
    00037
    00039         friend istream& operator>>(istream& is, Matrix& m) {
    00040                 Integer rows,cols;
    00041                 is >> rows >> cols;
    00042                 m.SetSize(rows, cols);
    00043                 for (Integer r = 0; r < rows; r++)
    00044                         for (Integer c = 0; c < cols; c++)
    00045                                 is >> m[r][c];
    00046                 Return is;
    00047                 }
    00048 public:
    00049
    00051     Matrix(Integer rows=0, Integer cols=0): m_matrix(NULL) {
    00052         SetSize(rows,cols);
    00053     }
    00054
    00056     Matrix(Const Matrix& m): m_matrix(NULL) {
    00057         SetSize(m.m_lenRow, m.m_lenCol);
    00058         for (Integer r=0; r < m_lenRow; r++)
    00059             m_matrix[r]=List<Type>(m.m_matrix[r]);
    00060     }
    00061
    00065     Void SetSize(Integer rows, Integer cols) {
    00066                 If (m_matrix != 0) {
    00067                         delete[] m_matrix;
    00068                         m_matrix = 0;
    00069                 }
    00070         If (cols > 0  And rows > 0) {
    00071             m_matrix = new List<Type>[rows];
    00072             for (Integer i = 0; i < rows; i++)
    00073                 m_matrix[i].SetListSize(cols);
    00074         }
    00075         Else
    00076             rows = 0;
    00077         m_lenCol = cols;
    00078                 m_lenRow = rows;
    00079     }
    00080
    00082         List<Type>& operator[](Integer index) {
    00083         assert(index < m_lenRow);
    00084         Return m_matrix[index];
    00085     }
    00086
    00088     Matrix& operator=(Const Matrix& m) {
    00089                 If (this == &m)
    00090                         Return *this;
    00091         SetSize(m.m_lenRow, m.m_lenCol);
    00092                 for (Integer r=0; r < m_lenRow; r++) {
    00093             m_matrix[r] = List<Type>(m.m_matrix[r]);
    00094                 }
    00095         Return *this;
    00096     }
    00097
    00099         Bool operator==(Const Matrix& m) {
    00100                 // Must have same dimensions
    00101                 If (m_lenRow != m.m_lenRow || m_lenCol != m.m_lenCol)
    00102                         Return False;
    00103
    00104                 // All rows must match
    00105                 For (Integer r=0; r < m_lenRow; r++) {
    00106                         If (m_matrix[r] != m.m_matrix[r])
    00107                                 Return False;
    00108                 }
    00109                 Return( True );
    00110         }
    00111
    00113         Bool operator!=(Const Matrix& m) {
    00114                 Return( ! (*this == m) );
    00115         }
    00116
    00119         Const Matrix operator+(Const Matrix& m) {
    00120         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
    00121         Matrix newMatrix(m_lenRow, m_lenCol);
    00122                 For (Integer r=0; r < m_lenRow; r++)
    00123             for (Integer c=0; c < m_lenCol;c++)
    00124                 newMatrix[r][c] = m_matrix[r][c] + m.m_matrix[r][c];
    00125         Return newMatrix;
    00126     }
    00127
    00130         Const Matrix operator-(Const Matrix& m) {
    00131         assert(m_lenCol == m.m_lenCol  And m_lenRow == m.m_lenRow);
    00132         Matrix newMatrix(m_lenRow, m_lenCol);
    00133                 For (Integer r = 0; r < m_lenRow; r++)
    00134             for (Integer c = 0; c < m_lenCol; c++)
    00135                 newMatrix[r][c] = m_matrix[r][c] - m.m_matrix[r][c];
    00136         Return newMatrix;
    00137     }
    00138
    00140         Const Matrix operator*(Const Integer R) {
    00141         Matrix newMatrix(m_lenRow, m_lenCol);
    00142                 For (Integer r = 0; r < m_lenRow; r++)
    00143             for (Integer c = 0; c < m_lenCol; c++)
    00144                 newMatrix[r][c] = m_matrix[r][c] * R;
    00145         Return newMatrix;
    00146     }
    00147
    00149         Const Matrix operator*(Const Real R) {
    00150         Matrix newMatrix(m_lenRow, m_lenCol);
    00151                 For (Integer r = 0; r < m_lenRow; r++)
    00152             for (Integer c = 0; c < m_lenCol; c++)
    00153                 newMatrix[r][c] = m_matrix[r][c] * R;
    00154         Return newMatrix;
    00155     }
    00156
    00158         Const Matrix operator+(Const Integer R) {
    00159         Matrix newMatrix(m_lenRow, m_lenCol);
    00160                 For (Integer r = 0; r < m_lenRow; r++)
    00161             for (Integer c = 0; c < m_lenCol; c++)
    00162                 newMatrix[r][c] = m_matrix[r][c] + R;
    00163         Return newMatrix;
    00164     }
    00165
    00167         Const Matrix operator+(Const Real R) {
    00168         Matrix newMatrix(m_lenRow, m_lenCol);
    00169                 For (Integer r = 0; r < m_lenRow; r++)
    00170             for (Integer c = 0; c < m_lenCol; c++)
    00171                 newMatrix[r][c] = m_matrix[r][c] + R;
    00172         Return newMatrix;
    00173     }
    00174
    00176         Const Matrix operator-(Const Integer R) {
    00177         Matrix newMatrix(m_lenRow, m_lenCol);
    00178                 For (Integer r = 0; r < m_lenRow; r++)
    00179             for (Integer c = 0; c < m_lenCol; c++)
    00180                 newMatrix[r][c] = m_matrix[r][c] - R;
    00181         Return newMatrix;
    00182     }
    00183
    00185         Const Matrix operator-(Const Real R) {
    00186         Matrix newMatrix(m_lenRow, m_lenCol);
    00187                 For (Integer r = 0; r < m_lenRow; r++)
    00188             for (Integer c = 0; c < m_lenCol; c++)
    00189                 newMatrix[r][c] = m_matrix[r][c] - R;
    00190         Return newMatrix;
    00191     }
    00192
    00194         Const Matrix operator*(Matrix& m) {
    00195         assert(m_lenCol==m.m_lenRow);
    00196         Matrix newMatrix(m_lenRow,m.m_lenCol);
    00197         for (Integer r = 0; r < m_lenRow; r++) {
    00198             for (Integer c = 0; c < m.m_lenCol; c++) {
    00199                 for (Integer i = 0; i < m_lenCol; i++) {
    00200                     newMatrix[r][c]+= m_matrix[r][i] * m[i][c];
    00201                 }
    00202             }
    00203         }
    00204         Return newMatrix;
    00205     }
    00206
    00209         Void print(String s = 0) {
    00210                 If (s)
    00211                         printf(s);
    00212                 cout << "{\n";
    00213                 For (Integer i = 0; i < m_lenRow; i++)
    00214                         cout << m_matrix[i];
    00215                 cout << "}\n";
    00216         }
    00217
    00219         Type rows() { Return m_lenRow; }
    00220
    00222         Type cols() { Return m_lenCol; }
    00223
    00227         Void length(Integer& r, Integer& c) { r = rows(); c = cols(); }
    00228
    00229 private:
    00230     Integer m_lenCol, m_lenRow;
    00231     List<Type>* m_matrix;
    00232
    00233 };
    00234
    00235 #endif // MATRIX_H