Advertisement
sve_vash

Untitled

Oct 24th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. double calcDet(vector<vector<double>> &Matrix) {
  2. double det = 0;
  3. if (Matrix.size() == 1) {
  4. return Matrix[0][0];
  5. }
  6. else if (Matrix.size() == 2) {
  7. det = Matrix[0][0] * Matrix[1][1] - Matrix[0][1] * Matrix[1][0];
  8. return det;
  9. }
  10. else {
  11. for (int p = 0; p < Matrix[0].size(); p++) {
  12. vector<vector<double>> TempMatrix;
  13. for (int i = 1; i < Matrix.size(); i++) {
  14. vector<double> TempRow;
  15. for (int j = 0; j < Matrix[i].size(); j++) {
  16. if (j != p) {
  17. TempRow.push_back(Matrix[i][j]);
  18. }
  19. }
  20. if (!TempRow.empty())
  21. TempMatrix.push_back(TempRow);
  22. }
  23. det = det + Matrix[0][p] * pow(-1, p) * calcDet(TempMatrix);
  24. }
  25. return det;
  26. }
  27. }
  28.  
  29. double Matrix::det() const {
  30. if (matSizeRow != matSizeCol) {
  31. throw("matrix is not square");
  32. }
  33. vector<vector<double>> matvec(matSizeRow, vector<double>(matSizeRow));
  34. for (int i = 0; i < matSizeRow; i++) {
  35. for (int j = 0; j < matSizeRow; ++j) {
  36. matvec[i][j] = get(i, j);
  37. }
  38. }
  39. return calcDet(matvec);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement