Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- class Matrix: public std::vector<std::vector<double>> {
- public:
- // конструкторы
- Matrix () = default;
- Matrix (int n, int m);
- // полуконструкторы
- static Matrix eye(int n, double value = 1.0) {
- Matrix t(n, n);
- for(int i = 0; i < n; ++i)
- t[i][i] = value;
- return t;
- }
- // операторы
- Matrix operator* (const Matrix& other) const;
- // операции
- void add_row_to_row(int n, int m, double factor = 1.0);
- void swap_rows(int n, int m);
- void multiply_row(int n, double factor);
- int find_pivot(int n, double EPS = 1e-07);
- void set_zeros(double EPS = 1e-07);
- // алгебра
- Matrix inverse() const;
- double square_norm() const;
- // input and output
- friend std::ostream& operator<< (std::ostream& os, const Matrix& m);
- friend std::istream& operator>> (std::istream& is, Matrix& m);
- };
- // конструкторы
- Matrix::Matrix(int n, int m):
- std::vector<std::vector<double>> (n, std::vector<double> (m, 0)) {}
- // операторы
- Matrix Matrix::operator* (const Matrix& other) const {
- // по столбцам a_1_1 ... a_1_n
- // по строкам a_1_1 .. a_m_1
- int m = this->size();
- int n = (*this)[0].size();
- int other_m = other.size();
- int other_n = other[0].size();
- Matrix t (m, other_n);
- for(int i = 0; i < m; ++i){
- for(int j = 0; j < other_n; ++j){
- for(int k = 0; k < n; ++k)
- t[i][j] += (*this)[i][k] * other[k][j];
- }
- }
- return t;
- }
- // операции
- void Matrix::add_row_to_row(int n, int m, double factor){
- for(int j = 0; j < (*this)[0].size(); ++j)
- (*this)[n][j] += factor * (*this)[m][j];
- }
- void Matrix::swap_rows(int n, int m){
- std::swap((*this)[n], (*this)[m]);
- }
- void Matrix::multiply_row(int n, double factor){
- for(int j = 0; j < (*this)[0].size(); ++j)
- (*this)[n][j] *= factor;
- }
- int Matrix::find_pivot(int n, double EPS){
- for(int i = n; i < this->size(); ++i)
- if(std::abs((*this)[i][n]) > EPS)
- return i;
- return -1;
- }
- void Matrix::set_zeros(double EPS){
- for(auto& i: *this)
- for(auto& j: i)
- if(std::abs(j) < EPS)
- j = 0;
- }
- // алгебра
- Matrix Matrix::inverse() const {
- Matrix t = (*this);
- Matrix inversed = Matrix::eye(t.size());
- for(int i = 0; i < t.size(); ++i){
- int pivot = t.find_pivot(i);
- t.swap_rows(i, pivot);
- inversed.swap_rows(i, pivot);
- double factor = t[i][i];
- t.multiply_row(i, 1.0 / factor);
- inversed.multiply_row(i, 1.0 / factor);
- for(int j = 0; j < t.size(); ++j){
- if(j != i){
- factor = t[j][i];
- t.add_row_to_row(j, i, -factor);
- inversed.add_row_to_row(j, i, -factor);
- }
- }
- }
- return inversed;
- }
- double Matrix::square_norm() const {
- double norm = 0;
- for(auto&& i: *this){
- for(auto&& j: i){
- norm += j * j;
- }
- }
- return norm;
- };
- // input and output
- std::ostream& operator<< (std::ostream& os, const Matrix& m) {
- for(auto&& i: m){
- for(auto&& j: i){
- os << j << ' ';
- }
- os << '\n';
- }
- return os;
- }
- std::istream& operator>> (std::istream& is, Matrix& m) {
- for(auto& i: m)
- for(auto& j: i)
- is >> j;
- return is;
- }
- Matrix generate_matrix(int n){
- if(n < 3)
- return Matrix::eye(n);
- Matrix t(n, n);
- t[0][0] = -1.0;
- t[0][1] = 1.0;
- for(int i = 1; i < n - 1; ++i){
- t[i][i - 1] = 1.0;
- t[i][i] = -2.0;
- t[i][i + 1] = 1.0;
- }
- t[n - 1][n - 2] = 1.0;
- t[n - 1][n - 1] = -((n - 1) * 1.0 / n);
- return t;
- }
- int main(){
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement