C++ Code für LGS?

Ich möchte mit einen C++-Programm alle LGS lösen können (endlich, unendlich und keine Lösungen). Ich habe auch einen Code. Endlich viele und keine Lösungen kann er recht gut (bisher), aber unendlich viele Lösungen kann er nicht anzeigen lassen (soll dann auch t einführen, nicht einfach einen Wert einsetzen).

Wo liegt der Fehler?

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Funktion zur Berechnung des Determinanten einer Matrix
double determinant(vector<vector<double>>& matrix, int n) {
  double det = 0;

  if (n == 1) {
    return matrix[0][0];
  }

  if (n == 2) {
    return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
  }

  for (int i = 0; i < n; i++) {
    vector<vector<double>> subMatrix(n - 1, vector<double>(n - 1));

    for (int j = 1; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (k < i) {
          subMatrix[j - 1][k] = matrix[j][k];
        }
        else if (k > i) {
          subMatrix[j - 1][k - 1] = matrix[j][k];
        }
      }
    }

    det += pow(-1, i) * matrix[0][i] * determinant(subMatrix, n - 1);
  }

  return det;
}
 
// Funktion zur Durchführung der Gauss-Jordan-Elimination
void gaussJordan(vector<vector<double>>& matrix, vector<double>& constants) {
  int n = matrix.size();

  for (int i = 0; i < n; i++) {
    int maxIndex = i;

    for (int j = i + 1; j < n; j++) {
      if (abs(matrix[j][i]) > abs(matrix[maxIndex][i])) {
        maxIndex = j;
      }
    }

    if (maxIndex != i) {
      swap(matrix[maxIndex], matrix[i]);
      swap(constants[i], constants[maxIndex]); // Korrektur hier
    }

    double factor = matrix[i][i];

    for (int j = i; j < n; j++) {
      matrix[i][j] /= factor;
    }

    constants[i] /= factor;

    for (int j = 0; j < n; j++) {
      if (j != i) {
        double factor = matrix[j][i];

        for (int k = i; k < n; k++) {
          matrix[j][k] -= factor * matrix[i][k];
        }

        constants[j] -= factor * constants[i];
      }
    }
  }
}

// Funktion zur Überprüfung der Invertierbarkeit und zur Lösung des LGS
vector<double> solveLinearSystem(vector<vector<double>>& matrix, vector<double>& constants) {
  int n = matrix.size();
  double det = determinant(matrix, n);

  // Überprüfung auf unendlich viele Lösungen
  bool allZero = true;

  for (double constant : constants) {
    if (constant != 0) {
      allZero = false;
      break;
    }
  }

  if (det == 0) {
    if (allZero) {
      cout << "Das LGS hat unendlich viele Lösungen." << endl;
      return {}; // Leerer Vektor, da unendlich viele Lösungen existieren
    }
    else {
      cout << "Das LGS hat keine Lösung." << endl;
      return {}; // Leerer Vektor, da keine Lösung existiert
    }
  }

  // Anwendung der Gauss-Jordan-Elimination
  gaussJordan(matrix, constants);

  vector<double> solution(n);

  for (int i = 0; i < n; i++) {
    solution[i] = constants[i];
  }

  return solution;
}

int main() {
  vector<vector<double>> matrix = {
    { 1, -3,  5 },
    { 2, -5,  12 },
    { 3, -11,  11 }
  };
  vector<double> constants = { 2, 1, 12 };

  vector<double> solution = solveLinearSystem(matrix, constants);

  if (!solution.empty()) {
    cout << "Die Lösung des LGS ist: ";

    for (int i = 0; i < solution.size(); i++) {
      cout << "x" << i + 1 << " = " << solution[i] << ", ";
    }

    cout << endl;
  }
  else {
    cout << "Das LGS hat keine Lösung." << endl;
  }

  return 0;
}
Schule, Mathematik, programmieren, Cplusplus, Mathematiker, Programmiersprache, Algorithmus, lineare Gleichungssysteme

Meistgelesene Fragen zum Thema Lineare Gleichungssysteme