1  #ifndef MATRIX1_H
  2  #define MATRIX1_H
  3  
  4  #include <iostream>
  5  
  6  using namespace std;
  7  
  8  /**
  9     This class describes a row in a matrix.
 10  */
 11  class Matrix; // Forward definition
 12  class MatrixRow
 13  {
 14  public:
 15     /**
 16        Remember a row for a given matrix
 17        @param m a pointer to the matrix
 18        @param s the size of the row
 19     */
 20     MatrixRow(Matrix* m, int s);
 21  
 22     /**
 23        Accesses a row element.
 24        @param j the column index
 25        @return a reference to the element with the given index
 26     */
 27     double& operator[](int j);
 28  
 29  private:
 30     Matrix* mat;
 31     int i;
 32  };
 33  
 34  /**
 35     This class describes a row in a constant matrix.
 36  */
 37  class ConstMatrixRow
 38  {
 39  public:
 40     /**
 41        Constructs a row with a given start and size.
 42        @param m a pointer to the matrix
 43        @param s the size of the row
 44     */
 45     ConstMatrixRow(const Matrix* m, int s);
 46  
 47     /**
 48        Accesses a row element.
 49        @param j the column index
 50        @return a reference to the element with the given index
 51     */
 52     double operator[](int j) const;
 53  
 54  private:
 55     const Matrix* mat;
 56     int i;
 57  };
 58  
 59  /**
 60     This class describes a 3 x 3 matrix.
 61  */
 62  class Matrix
 63  {
 64  public:
 65     /**
 66        Constructs a matrix filled with zero elements.
 67     */
 68     Matrix();
 69  
 70     /**
 71        Accesses a matrix element.
 72        @param i the row index
 73        @param j the column index
 74        @return a reference to the element with the given indexes
 75     */
 76     double& operator()(int i, int j);
 77  
 78     /**
 79        Accesses a matrix element.
 80        @param i the row index
 81        @param j the column index
 82        @return the element with the given indexes
 83     */
 84     double operator()(int i, int j) const;
 85  
 86     /**
 87        Accesses a matrix row.
 88        @param i the row index
 89        @return the row with the given index
 90     */
 91     MatrixRow operator[](int i);   
 92  
 93     /**
 94        Accesses a matrix row.
 95        @param i the row index
 96        @return the row with the given index
 97     */
 98     ConstMatrixRow operator[](int i) const;
 99  
100     /**
101        Computes the matrix sum
102        @param right another matrix
103        @return the updated matrix
104     */
105     Matrix& operator+=(const Matrix& right);
106  
107     static const int ROWS = 3;
108     static const int COLUMNS = 3;
109  private:
110     double elements[ROWS * COLUMNS];
111  };
112  
113  /**
114     Computes the matrix sum.
115     @param right another matrix
116     @return the sum of two matricies
117  */
118  Matrix operator+(const Matrix& left, const Matrix& right);
119  
120  /**
121     Computes the matrix product.
122     @param right another matrix
123     @return the product of two matricies
124  */
125  Matrix operator*(const Matrix& left, const Matrix& right);
126  
127  /**
128     Computes the scalar product of a scalar value and a matrix.
129     @param left a scalar value
130     @param right a matrix
131     @return the product of the given value and the given matrix
132  */
133  Matrix operator*(double left, const Matrix& right);
134  
135  /**
136     Computes the scalar product of a matrix and a scalar value.
137     @param right a scalar value
138     @return the product of this matrix and the given value
139  */
140  Matrix operator*(const Matrix& left, double right);
141  
142  /**
143     Prints a matrix to an output stream.
144     @param left an output stream
145     @param right a matrix
146     @return the given output stream
147  */
148  ostream& operator<<(ostream& left, const Matrix& right);
149  
150  #endif