SMatrix add(const SMatrix& m1, const SMatrix& m2) { SMatrix newMat; newMat.setRows(m1.getRows()); newMat.setCols(m1.getCols()); int result; for( int i = 0; i < m1.getRows(); i++ ) { for( int j = 0; j < m1.getCols(); j++ ) { result = m1.valAt(i+1, j+1) + m2.valAt(i+1, j+1); newMat.setVal(i, j, result); } } return newMat; }
void output(std::ostream& outs, const SMatrix& src) { // NOTE: r and c are row # and coloum #, NOT indices for(int r = 1; r <= src.getRows(); ++r) { for(int c = 1; c <= src.getCols(); ++c) outs << setw(SMatrix::FIELD_WIDTH) << src.valAt(r, c); outs << endl; } }
SMatrix SMatrix::minus(const SMatrix& otherMat) const { SMatrix newMat; newMat.setRows(rows); newMat.setCols(cols); int result; for( int i = 0; i < rows; i++ ) { for( int j = 0; j < cols; j++ ) { result = data[i][j] - otherMat.valAt(i+1, j+1); newMat.setVal(i, j, result); } } return newMat; }
SMatrix SMatrix::times(const SMatrix& otherMat) const { SMatrix newMat; newMat.setRows(rows); newMat.setCols(otherMat.getCols()); int result; for( int i = 0; i < rows; i++ ) { for( int k = 0; k < otherMat.getCols(); k++ ) { result = 0; for(int j = 0; j < otherMat.getRows(); j++ ) { result+=(data[i][j] * otherMat.valAt(j+1, k+1)); } newMat.setVal(i, k, result); } } return newMat; }