//Обратная матрица Matrix Matrix::invert() const throw (int) { if (!isSquare()) throw Matrix::NOT_SQUARE; Matrix Mu = *this; Matrix Ml(height, width, eps); Ml.makeE(); //Прямой ход unsigned int c, i, j, max; double x, y; for (c = 0; c < height; ++c) { max = Mu.findFabsMaxInCol(c, c); if (isNull(Mu.M[max][c])) throw Matrix::NO_DET; Ml.moveRow(max, c); Mu.moveRow(max, c); for (i = c + 1; i < height; ++i) { y = Mu.M[i][c] / Mu.M[c][c]; for (j = c; j < width; ++j) Mu.M[i][j] -= y * Mu.M[c][j]; for (j = 0; j < width; ++j) Ml.M[i][j] -= y * Ml.M[c][j]; } } //Проверка определителя double d = 1; for (i = 0; i < height; ++i) d *= Mu.M[i][i]; if (isNull(d)) throw Matrix::NO_DET; //Обратный ход for (i = 0; i < height; ++i) { y = 1 / Mu.M[i][i]; Mu.multRow(y, i); Ml.multRow(y, i); } for (c = height - 1; c > 0; --c) { for (i = c; i > 0; --i) { x = Mu.M[i - 1][c]; for (j = i; j < width; ++j) Mu.M[i - 1][j] -= x * Mu.M[c][j]; for (j = 0; j < width; ++j) Ml.M[i - 1][j] -= x * Ml.M[c][j]; } } return Ml; }
unsigned int Calculator::E (const std::string & stringViewOne, const std::string & stringViewTwo) { unsigned int e {0u}; if (stringViewOne.length () && stringViewTwo.length () ) { const auto ccf = Ccf (stringViewOne, stringViewTwo); e = std::accumulate (ccf.begin (), ccf.end (), e, [](const int a, const int x){return a + std::pow (x, 2);}); e -= std::pow (Ml (stringViewOne, stringViewTwo), 2); } return e; }
int main(int argc, char** argv) { int nrows = 6; if (argc >= 2) sscanf(argv[1], "%d", &nrows); int ncols = 6; if (argc >= 3) sscanf(argv[2], "%d", &ncols); Cmatrix Md = Cmatrix::Random(nrows, ncols); MatrixXv Mv(Md); MatrixXl Ml(Md); MatrixXh Mh(Md); cout << Md.str(Dense()) << endl; cout << Mv.str(Dense()) << endl; cout << Ml.str(Dense()) << endl; cout << Mh.str(Dense()) << endl; cout << Mh.nnz() << endl; }