/* Substraction. Throw exception if their dimensions don't match. */ Mat operator -(const Mat& rhs){ if (rhs.get_n()!=n || rhs.get_m()!=m){ throw invalid_argument("Matrix dimensions are not consistent when trying '+' two matrices"); return Mat(); } Mat res = Mat(*this); for (int i=0; i<res.get_n(); i++){ for (int j=0; j<res.get_m(); j++) res[i][j] -= rhs.get(i, j); } return res; }
/* Return a matrix which is the elementwise square. */ Mat Sqr(){ Mat res = Mat(*this); for (int i=0; i<res.get_n(); i++) for (int j=0; j<res.get_m(); j++) res[i][j] = res[i][j]*res[i][j]; return res; }
/* Multiply by a number. */ Mat operator *(const double& mult){ Mat res = Mat(*this); for (int i=0; i<res.get_n(); i++){ for (int j=0; j<res.get_m(); j++) res[i][j] *= mult; } return res; }
/* Copy constructor. Deep copy. */ Mat(const Mat &cp){ InitMat(cp.get_n(),cp.get_m()); for (int i=0; i<n; i++) for (int j=0; j<m; j++) arr[i][j] = cp.get(i,j); }