Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <vector>
- using std::vector;
- using std::count;
- using std::cin;
- using std::endl;
- class System
- {
- private:
- short size = 0;
- vector<double> numbers;
- public:
- System(short size)
- {
- this->size = size;
- }
- bool lineAdd(vector<double> line)
- {
- if (this->size < 1) { /*std::cout << 'C';*/ return false; }
- if(line.size() != this->size) { /*std::cout << 'S';*/ return false; }
- if (this->numbers.size() % this->size != 0) { /*std::cout << 'V';*/ return false; }
- for (short i = 0; i < this->size; i++)
- {
- this->numbers.push_back(line[i]);
- }
- return true;
- }
- void output()
- {
- short size = this->size;
- short ss = this->numbers.size();
- std::cout << std::endl;
- for (short i = 0; i < ss; i++)
- {
- std::cout << "\t" << this->numbers[i];
- if ((i + 1) % size == 0) std::cout << std::endl;
- }
- }
- vector<double> getLine(short n)
- {
- vector<double> res_line;
- short start = (n - 1) * this->size;
- short finish = (n - 1) * this->size + (this->size - 1);
- if (start < finish && this->numbers.size() > finish)
- {
- for (short i = start; i <= finish; i++)
- {
- res_line.push_back(this->numbers[i]);
- }
- }
- return res_line;
- }
- // k1 * (n1) + k2 * (n2)
- vector<double> calculateLines(double k1, short n1, double k2, short n2)
- {
- vector<double> line1 = this->getLine(n1);
- vector<double> line2 = this->getLine(n2);
- vector<double> res_line;
- if (line1.size() == line2.size())
- {
- for (short i = 0; i < line1.size(); i++)
- {
- res_line.push_back( k1 * line1[i] + k2 * line2[i] );
- }
- }
- return res_line;
- }
- bool replaceLine(short n, vector<double> line)
- {
- double lsize = line.size();
- if (lsize != this->size) return false;
- short c_start = (n - 1) * lsize;
- short c_finish = n * lsize - 1;
- for (short i = 0; i < lsize; i++)
- {
- this->numbers[c_start + i] = line[i];
- }
- return true;
- }
- void swapLines(short n1, short n2)
- {
- if (n1 <= this->size && n2 <= this->size)
- {
- vector<double> line1 = this->getLine(n1);
- vector<double> line2 = this->getLine(n2);
- this->replaceLine(n1, line2);
- this->replaceLine(n2, line1);
- }
- }
- bool isNullLine(short n)
- {
- bool success = true;
- for (short i = 1; i <= size; i++)
- {
- success = success && (abs(this->getElement(n, i)) < 0.1);
- }
- return success;
- }
- double getElement(short line, short column)
- {
- if (line >= 1 && line <= this->size)
- {
- if (column >= 1 && column <= this->size)
- {
- return this->numbers[ (line - 1) * this->size + (column - 1) ];
- }
- }
- }
- bool isUpperTriangleForm()
- {
- bool result = true;
- for (short i = 1; i < this->size; i++) // Столбец
- {
- for(short j = i + 1; j <= this->size; j++) // Строка
- {
- result = result && ( this->isNullElement(j, i) );
- }
- }
- return result;
- }
- void setUpperTriangleForm() // (i, j) заменяем
- {
- if (this->isUpperTriangleForm()) return;
- for (short i = 1; i < this->size; i++) // Столбец
- {
- for (short j = i + 1; j <= this->size; j++) // Строка
- {
- if (this->isNullElement(j, i)) continue;
- if (this->isNullElement(i, i)) continue;
- else
- {
- double k = -(this->getElement(j, i) / this->getElement(i, i));
- this->replaceLine(j, this->calculateLines(k, i, 1, j));
- }
- }
- }
- for (short i = 1; i < this->size; i++)
- {
- short line = this->getMaxNullLine(this->size - (i - 1));
- if (line != 0)
- {
- this->swapLines(line, this->size - i);
- }
- }
- if (!(this->isUpperTriangleForm()))
- {
- this->setUpperTriangleForm();
- return;
- }
- }
- bool isNullElement(short line, short column)
- {
- return abs( this->getElement(line, column) ) < 0.1;
- }
- short getMaxNullLine(short inv)
- {
- short result = 0;
- short count = 0;
- for (short i = 1; i < inv; i++) // строка
- {
- short temp = 0;
- for (short j = 1; j <= this->size; j++) // столбец
- {
- if (this->isNullElement(i, j)) temp++;
- }
- if (temp > count) {
- result = i; count = temp;
- }
- }
- return result;
- }
- double determinant()
- {
- if (this->isUpperTriangleForm())
- {
- double result = 1;
- for (short i = 1; i <= this->size; i++)
- {
- result = result * this->getElement(i, i);
- }
- return result;
- }
- return -1;
- }
- vector<double> roots()
- {
- double d = this->determinant();
- vector<float> roots; // x4 x3 x2 x1
- if (d == 0)
- {
- }
- else
- {
- vector<double> result;
- for (short i = this->size - 1; i >= 1; i--)
- {
- double sum = 0.0;
- if (i != this->size - 1)
- {
- for (short k = i + 1; k <= (size - 1); k++)
- {
- sum += (result[ k - (i + 1) ] * this->getElement(i, k));
- }
- }
- result.push_back( (this->getElement(i, this->size) - sum) / this->getElement(i, i) );
- }
- return result;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement