Vector linalg::gaussElimination(Matrix m)
{
int n = m.Cols;
for (int i = 0; i < n; i++) {
double maxEl = abs(m.cell[i][i]);
int maxRow = i;
for (int k = i + 1; k < n; k++) {
if (abs(m.cell[k][i]) > maxEl) {
maxEl = abs(m.cell[k][i]);
maxRow = k;
}
}
for (int k = i; k < n + 1; k++) {
double tmp = m.cell[maxRow][k];
m.cell[maxRow][k] = m.cell[i][k];
m.cell[i][k] = tmp;
}
for (int k = i + 1; k < n; k++) {
double c = -m.cell[k][i] / m.cell[i][i];
for (int j = i; j < n + 1; j++) {
if (i == j) {
m.cell[k][j] = 0;
}
else {
m.cell[k][j] += c * m.cell[i][j];
}
}
}
}
Vector x(n);// this is my new operator to return value
for (int i = n - 1; i >= 0; i--) {
x[i] = m.cell[i][n] / m.cell[i][i];// i have problem with x[i]
for (int k = i - 1; k >= 0; k--) {
m.cell[k][n] -= m.cell[k][i] * x[i];
}
}
return x;
}
to make the code programmable i added Vector.cpp and Vector.h files to make the code work.
1 2 3 4 5 6
Vector::Vector(int n)
{
for (int i = 0; i < n; i++) {
element.push_back(0);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#pragma once
#include <vector>
#include <iostream>
#include "Matrix.h"
#include <initializer_list>
class Vector : public Matrix
{
private:
int Index;
std::vector<float> element;
public:
Vector(int n);