00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Matrix.h"
00010
00011
00025 Matrix::Matrix(int nl,int nc, double x)
00026 : _nlines(nl), _ncols(nc), _lines(nl, MVector(nc, x))
00027 {
00028 }
00029
00039 Matrix::Matrix(MVector mv, bool transp)
00040 : _nlines(1),
00041 _ncols(mv.size()),
00042 _lines(_ncols)
00043 {
00044 _lines[0] = mv;
00045 if (transp)
00046 transpose();
00047 }
00048
00055 Matrix::operator MVector() const
00056 {
00057 if (_nlines == 1) return _lines[0];
00058 else if (_ncols == 1)
00059 return column(0);
00060 else throw Bad_Dimensions();
00061 }
00062
00068 MVector Matrix::column(int j) const
00069 {
00070 if (j < 0 || j >= _ncols) throw Out_Of_Bounds();
00071 MVector mv(_nlines);
00072 for (int i = 0; i < _nlines; ++i)
00073 mv[i] = _lines[i][j];
00074 return mv;
00075 }
00076
00082 MVector Matrix::line(int i) const
00083 {
00084 if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00085 return _lines[i];
00086 }
00087
00091 Matrix Matrix::transpose() const
00092 {
00093 Matrix mat(_ncols, _nlines);
00094 for (int i = 0; i < _ncols; ++i)
00095 mat._lines[i] = column(i);
00096 return mat;
00097 }
00098
00102 Matrix operator+(const Matrix& mat1, const Matrix& mat2)
00103 {
00104 if (mat1._nlines != mat2._nlines || mat1._ncols != mat2._ncols)
00105 throw Matrix::Bad_Dimensions();
00106
00107 int nl = mat1._nlines;
00108 int nc = mat1._ncols;
00109 Matrix mat(nl, nc);
00110 for (int i = 0; i < nl; ++i)
00111 mat._lines[i] = mat1._lines[i] + mat2._lines[i];
00112 return mat;
00113 }
00114
00118 Matrix operator-(const Matrix& mat1, const Matrix& mat2)
00119 {
00120 if (mat1._nlines != mat2._nlines || mat1._ncols != mat2._ncols)
00121 throw Matrix::Bad_Dimensions();
00122
00123 int nl = mat1._nlines;
00124 int nc = mat1._ncols;
00125 Matrix mat(nl, nc);
00126 for (int i = 0; i < nl; ++i)
00127 mat._lines[i] = mat1._lines[i] - mat2._lines[i];
00128 return mat;
00129 }
00130
00134 Matrix operator*(const Matrix& mat1, const Matrix& mat2)
00135 {
00136 if (mat1._ncols != mat2._nlines)
00137 throw Matrix::Bad_Dimensions();
00138
00139 int nl = mat1._nlines;
00140 int nc = mat2._ncols;
00141 Matrix mat(nl, nc);
00142 for (int i = 0; i < nl; ++i)
00143 for (int j = 0; j < nc; ++j)
00144 mat._lines[i][j] = mat1.line(i) * mat2.column(j);
00145 return mat;
00146 }
00147
00151 bool operator==(const Matrix& mat1, const Matrix& mat2)
00152 {
00153 if (mat1._nlines != mat2._nlines) return false;
00154 for (int i = 0; i < mat1._nlines; ++i)
00155 if (mat1._lines[i] != mat2._lines[i]) return false;
00156 return true;
00157 }
00158
00163 MVector& Matrix::operator[](int i)
00164 {
00165 if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00166 return _lines[i];
00167 }
00168
00173 const MVector& Matrix::operator[](int i) const
00174 {
00175 if (i < 0 || i >= _nlines) throw Out_Of_Bounds();
00176 return _lines[i];
00177 }
00178
00179
00180 double& Matrix::operator()(int i, int j)
00181 {
00182 if (i < 0 || i >= _nlines || j < 0 || j >= _ncols) throw Out_Of_Bounds();
00183 return _lines[i][j];
00184 }
00185
00186 double Matrix::operator()(int i, int j) const
00187 {
00188 if (i < 0 || i >= _nlines || j < 0 || j >= _ncols) throw Out_Of_Bounds();
00189 return _lines[i][j];
00190 }
00191
00196 ostream& operator<<(ostream& os, const Matrix& mat)
00197 {
00198 os << "[\n";
00199 for (int i = 0; i < mat._nlines; ++i)
00200 os << " " << mat._lines[i] << '\n';
00201 os << ']';
00202 return os;
00203 }