-
Code double Matrix::GetValueOfDeterminant(){ if((2 == MaxRow) && (2 == MaxCol)){ return GetElement(1,1) * GetElement(2,2) - GetElement(1,2) * GetElement(2,1);} else{double ResultValue = 0;for(int c = 1; c <= MaxCol; c++){ int PowOfNegativeOne = std::pow(-1, c); ResultValue += GetCofactorMatrix(1,c).GetValueOfDeterminant() * GetElement(1,c) * PowOfNegativeOne;} return ResultValue;}}
Preloaded Code class Matrix { private: double * ptrElements; int MaxRow; int MaxCol; int ElementAmount; public: double & GetElement(int _Row, int _Col);//get element reference value of this matrix by give row and col public: Matrix(int _Row, int _Col); //default constructor for generate random element for matrix Matrix(const Matrix & _Matrix); ~Matrix(); public: Matrix GetCofactorMatrix(int _Row, int _Col); double GetValueOfDeterminant(); double Test(); }; double & Matrix::GetElement(int _Row, int _Col) { return ptrElements[((_Row - 1) * MaxCol) + _Col - 1]; } Matrix Matrix::GetCofactorMatrix(int _Row, int _Col) { Matrix TempMatrix(MaxRow - 1, MaxCol - 1); for(int i1 = 1; i1 <= _Row - 1; i1++) { for(int j1 = 1; j1 <= _Col - 1; j1++) { TempMatrix.GetElement(i1, j1) = GetElement(i1, j1); } for(int j2 = _Col + 1; j2 <= MaxCol; j2++) { TempMatrix.GetElement(i1, j2 - 1) = GetElement(i1, j2); } } for(int i2 = _Row + 1; i2 <= MaxRow; i2++) { for(int j1 = 1; j1 <= _Col - 1; j1++) { TempMatrix.GetElement(i2 - 1, j1) = GetElement(i2, j1); } for(int j2 = _Col + 1; j2 <= MaxCol; j2++) { TempMatrix.GetElement(i2 - 1, j2 - 1) = GetElement(i2, j2); } } return TempMatrix; } double Matrix::Test() { if((MaxRow == 2) && (MaxCol == 2)) return GetElement(1,1) * GetElement(2,2) - GetElement(1,2) * GetElement(2,1); double ResultValue = 0; for(int c = 1; c <= MaxCol; c++) { int PowsOfNegativeOne = std::pow(-1, c); ResultValue += GetCofactorMatrix(1,c).Test() * GetElement(1,c) * PowsOfNegativeOne; } return ResultValue; } Matrix::Matrix(int _Row, int _Col) { MaxRow = _Row; MaxCol = _Col; ElementAmount = _Row * _Col; ptrElements = new double[ElementAmount]; std::srand(time(NULL)); for(int i = 0; i < ElementAmount; i++) { ptrElements[i] = std::rand() % 100; } } Matrix::Matrix(const Matrix & _Matrix) { if(this != &_Matrix) { ptrElements = new double[_Matrix.ElementAmount]; for(int i = 0; i < ElementAmount; i++) { ptrElements[i] = _Matrix.ptrElements[i]; } MaxRow = _Matrix.MaxRow; MaxCol = _Matrix.MaxCol; ElementAmount = _Matrix.ElementAmount; } } Matrix::~Matrix() { delete [] ptrElements; } Matrix Test3_3Matrix(3,3); Matrix Test4_4Matrix(4,4); Matrix Test5_5Matrix(5,5);
Test Cases // TODO: Replace examples and use TDD development by writing your own tests Describe(calculate_determinant) { It(calculating) { Assert::That(Test3_3Matrix.GetValueOfDeterminant(), Equals(Test3_3Matrix.Test())); Assert::That(Test4_4Matrix.GetValueOfDeterminant(), Equals(Test4_4Matrix.Test())); Assert::That(Test5_5Matrix.GetValueOfDeterminant(), Equals(Test5_5Matrix.Test())); } };
Output:
-
Code - double Matrix::GetValueOfDeterminant(){
- if((2 == MaxRow) && (2 == MaxCol)){
- return GetElement(1,1) * GetElement(2,2) - GetElement(1,2) * GetElement(2,1);}
else{double ResultValue = 0;for(int c = 1; c <= MaxCol; c++){- else{double ResultValue = 0;for(int c = 1; c <= MaxCol; c++){
- int PowOfNegativeOne = std::pow(-1, c);
- ResultValue += GetCofactorMatrix(1,c).GetValueOfDeterminant() * GetElement(1,c) * PowOfNegativeOne;}
- return ResultValue;}}
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
Please sign in or sign up to leave a comment.