Matrix<_Type> invert() { int size = getRowCount(); if (size != getColCount()){ throw MatrixException("Invalid size"); return *this; } Matrix<_Type> result(size, size, 0); _Type det = determinant(); if (det == 0) { throw MatrixException("Determinant = 0"); return *this; } _Type temp; bool _invert = false; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp = _invert ? 0 : 1; temp = minor(i, j); temp /= det; result.put(i, j, temp); _invert = !_invert; } } result = result.transposition(); return result; }
std::vector<T> Matrix<T>::operator[](unsigned int index) const { std::vector<T> result; if (index >= m) { throw MatrixException(MATRIX_ERR_WRONG_ROW_INDEX); } else if ((index + 1) * n > size()) { throw MatrixException(MATRIX_ERR_TOO_FEW_DATA); } else { unsigned int begin = index * n; unsigned int end = begin + n; for (unsigned int i = begin; i < end; i++) { result.push_back(v[i]); } } return result; }
IMS_Matrix<T>::IMS_Matrix(unsigned int n, unsigned int m, std::initializer_list<T> init) : rows(n), cols(m) { if (init.size() != n*m) { throw MatrixException("Can't initalize a matrix unless given N*M elements"); } //insert values into list data.insert(data.end(), init.begin(), init.end()); }
Matrix<T> Matrix<T>::operator*(const Matrix<T>& other) { Matrix result(m, other.n); if (n != other.m) { throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_NOT_EQUAL); } else if (m <= 0 || n <= 0 || other.n <= 0) { throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_BE_GREATER_THAN_ZERO); } else if (m * n > size() || other.m * other.n > other.size()) { throw MatrixException(MATRIX_ERR_TOO_FEW_DATA); } else { for (unsigned int i = 0; i < m; i++) { for (unsigned int j = 0; j < other.n; j++) { T temp = v[i * n] * other.v[j]; for (unsigned int k = 1; k < n; k++) { temp += v[i * n + k] * other.v[k * other.n + j]; } result.v.push_back(temp); } } } return result; }
const Matrix Matrix::operator+(const Matrix& b) { if (this->x != b.x || this->y != b.y) { throw MatrixException("Bad sizes!"); } Matrix c(this->x, this->y); for (int i = 0; i < this->x; i++){ for (int j = 0; j < this->y; j++) { uint tmp = (*this)[i][j] + b.getElem(i, j); c[i][j] = tmp; } } return c; }
linearSystem::linearSystem(const Matrix<double> A, const Vector<double> d): _A(A), _d(d){ _dim = _A.getFiDimension(); if(_dim!=d.dimension()) throw MatrixException((char*)"El sistema no se puede resolver, las dimensiones no coinciden",Default); }