Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- class Matrix;
- ostream& operator << (ostream& cout, const Matrix& matrix);
- void output(int& step, Matrix base, bool permutation);
- class SquareMatrix {
- friend Matrix;
- private:
- int size;
- double** matrix;
- public:
- SquareMatrix(int size) {
- (*this).size = size;
- matrix = new double* [size];
- for (int i = 0; i < size; i++) {
- matrix[i] = new double[size];
- }
- }
- void permutation(int r1, int r2) {
- double* temp = *(matrix + r1);
- *(matrix + r1) = *(matrix + r2);
- *(matrix + r2) = temp;
- }
- friend istream& operator>>(istream& cin, SquareMatrix& base) {
- for (int i = 0; i < base.size; i++)
- for (int j = 0; j < base.size; j++)
- cin >> base.matrix[i][j];
- return cin;
- }
- friend ostream& operator<<(ostream& cout, SquareMatrix* base) {
- for (int i = 0; i < base->size; i++) {
- for (int j = 0; j < base->size; j++) {
- if (base->matrix[i][j] < 0.005 && base->matrix[i][j] > -0.005)
- cout << "0.00";
- else
- cout << fixed << setprecision(2) << base->matrix[i][j];
- if (j != base->size - 1)
- cout << " ";
- }
- cout << endl;
- }
- return cout;
- }
- };
- class ColumnVector {
- friend Matrix;
- private:
- int size;
- double* matrix;
- public:
- ColumnVector(int n) {
- size = n;
- matrix = new double[size];
- }
- void permutation(int r1, int r2) {
- double temp = matrix[r1];
- matrix[r1] = matrix[r2];
- matrix[r2] = temp;
- }
- friend istream& operator>>(istream& cin, ColumnVector& base) {
- int n;
- cin >> n;
- for (int i = 0; i < base.size; i++)
- cin >> base.matrix[i];
- return cin;
- }
- friend ostream& operator<<(ostream& cout, ColumnVector* base) {
- for (int i = 0; i < base->size; i++) {
- if (base->matrix[i] < 0.005 && base->matrix[i] > -0.005)
- cout << "0.00";
- else
- cout << fixed << setprecision(2) << base->matrix[i];
- cout << endl;
- }
- return cout;
- }
- };
- class Matrix {
- private:
- int size;
- SquareMatrix* square;
- ColumnVector* vector;
- public:
- Matrix(int size) {
- this->size = size;
- square = new SquareMatrix(size);
- vector = new ColumnVector(size);
- }
- void directOrder(int& step) {
- for (int line = 0; line < size; line++) {
- int maxLine = line;
- for (int i = line + 1; i < size; i++) {
- if (abs(square->matrix[i][line]) > abs(square->matrix[maxLine][line]))
- maxLine = i;
- }
- if (maxLine != line) {
- square->permutation(line, maxLine);
- vector->permutation(line, maxLine);
- output(step, *this, true);
- }
- for (int i = line + 1; i < size; i++) {
- double constant = square->matrix[i][line] / square->matrix[line][line];
- if (square->matrix[i][line] != 0) {
- for (int j = 0; j < size; j++)
- square->matrix[i][j] -= square->matrix[line][j] * constant;
- vector->matrix[i] -= vector->matrix[line] * constant;
- }
- output(step, *this, false);
- }
- }
- }
- void reversOrder(int& step) {
- double constant;
- for (int i = size - 1; i >= 0; i--) {
- for (int j = i - 1; j >= 0; j--) {
- constant = square->matrix[j][i] / square->matrix[i][i];
- square->matrix[j][i] -= square->matrix[i][i] * constant;
- vector->matrix[j] -= vector->matrix[i] * constant;
- output(step, *this, false);
- }
- }
- }
- void normalization() {
- for (int i = 0; i < size; i++) {
- double now = square->matrix[i][i];
- square->matrix[i][i] /= now;
- vector->matrix[i] /= now;
- }
- }
- void result() {
- cout << vector;
- }
- friend istream& operator >> (istream& cin, Matrix& matrix) {
- cin >> *(matrix.square);
- cin >> *(matrix.vector);
- return cin;
- }
- friend ostream& operator << (ostream& cout, const Matrix& matrix) {
- cout << matrix.square << matrix.vector;
- return cout;
- }
- };
- void output(int& step, Matrix base, bool permutation) {
- cout << "step #" << step << ": ";
- if (permutation)
- cout << "permutation";
- else {
- cout << "elimination";
- ++step;
- }
- cout << endl << base;
- }
- int main()
- {
- int n;
- cin >> n;
- Matrix matrix(n);
- cin >> matrix;
- int step = 1;
- cout << "step #0:\n" << matrix;
- matrix.directOrder(step);
- cout << "Way back:\n";
- matrix.reversOrder(step);
- matrix.normalization();
- cout << "Diagonal normalization:" << endl << matrix;
- cout << "result:" << endl;
- matrix.result();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement