fix solver
Some checks failed
Linux arm64 / Build (push) Failing after 2m0s

This commit is contained in:
2024-05-12 09:23:37 +02:00
parent 2af915057a
commit 7f1ef38286
3 changed files with 58 additions and 15 deletions

View File

@@ -18,7 +18,7 @@
*/ */
class Matrix { class Matrix {
public: public:
typedef BigInt Element; typedef long double Element;
typedef std::vector<Element>::iterator iterator; typedef std::vector<Element>::iterator iterator;
private: private:

View File

@@ -2,6 +2,15 @@
#include "Gauss.h" #include "Gauss.h"
static int FirstNotNullElementIndexOnLine(const Matrix& mat, std::size_t line) {
for (std::size_t i = 0; i < mat.GetColumnCount(); i++) {
if (!IsEqualZero(mat.at(line, i))) {
return i;
}
}
return -1;
}
Vect Solver::Image(Matrix&& a_Matrix) const { Vect Solver::Image(Matrix&& a_Matrix) const {
a_Matrix.Transpose(); a_Matrix.Transpose();
Gauss::GaussJordan(a_Matrix, false, false); Gauss::GaussJordan(a_Matrix, false, false);
@@ -40,11 +49,10 @@ VectAffine Solver::RectangularSystem(Matrix&& a_MatrixA, const Matrix& a_VectorB
Matrix fullOrigin {mat.GetColumnCount() - 1, 1}; Matrix fullOrigin {mat.GetColumnCount() - 1, 1};
for (std::size_t i = 0; i < mat.GetRawCount(); i++) { for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
fullOrigin.at(i, 0) = origin.at(i, 0); int pivot_index = FirstNotNullElementIndexOnLine(mat, i);
} if(pivot_index >= 0){
fullOrigin.at(pivot_index, 0) = origin.at(i, 0);
for (std::size_t i = mat.GetRawCount(); i < mat.GetColumnCount() - 1; i++) { }
fullOrigin.at(i, 0) = 0;
} }
return {noyau, fullOrigin}; return {noyau, fullOrigin};

View File

@@ -8,18 +8,53 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
void TestRectangular() { const static int EXECUTION_COUNT = 100;
Matrix mat2 = {2, 4, { static constexpr int MATRIX_MAX_SIZE = 5;
1, 1, 1, 1,
1, -1, -1, 2
}};
VectAffine aff {Matrix::ColumnVector({0, -1, 1}), Matrix::ColumnVector({3, 0, -1})}; static unsigned int GetRandomInt() {
return rand() % MATRIX_MAX_SIZE + 1;
}
static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) {
Matrix matrix {a_Raw, a_Column};
for (std::size_t i = 0; i < matrix.GetRawCount(); i++) {
for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) {
matrix.at(i, j) = GetRandomInt();
}
}
return matrix;
}
void TestRectangular(const Matrix& system, const Matrix& origin) {
Solver solver; Solver solver;
std::cout << solver.RectangularSystem(std::move(mat2), Matrix::ColumnVector({1, 2})).GetLinearSystem() << std::endl; VectAffine solution = solver.RectangularSystem(std::move(Matrix(system)), origin);
std::cout << aff.GetLinearSystem() << std::endl;
for (std::size_t i = 0; i < solution.GetBase().GetCardinal(); i++) {
Matrix vector = solution.GetBase().GetVector(i) + solution.GetOrigin();
for (std::size_t j = 0; j < system.GetRawCount(); j++) {
Matrix line = system.SubMatrix(j, 0, 1, system.GetColumnCount());
test_assert(line * vector == origin.SubMatrix(j, 0, 1, 1));
}
}
}
void RandomRectangular() {
for (int i = 0; i < EXECUTION_COUNT; i++) {
Matrix system = GetRandomMatrix(GetRandomInt(), GetRandomInt());
if (system.GetColumnCount() == system.GetRawCount())
continue;
Matrix origin = GetRandomMatrix(system.GetRawCount(), 1);
std::cout << "PIPI\n";
TestRectangular(system, origin);
}
} }
void TestKernelImage() { void TestKernelImage() {
@@ -51,6 +86,6 @@ void TestKernelImage() {
int main() { int main() {
TestKernelImage(); TestKernelImage();
TestRectangular(); RandomRectangular();
return 0; return 0;
} }