Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <math.h>
- using namespace std;
- class Matrix {
- public:
- int size;
- vector<vector<double>> m;
- Matrix() {
- this->size = 0;
- }
- Matrix(vector<vector<double>> m) {
- int n = m.size();
- for (int i = 0; i < n; ++ i) {
- if (m[i].size() != n) {
- n = -1;
- break;
- }
- }
- if (n == -1) {
- cout << "Init Error\n";
- this->size = 0;
- }
- else {
- this->size = m.size();
- this->m = m;
- }
- }
- void print() {
- for (int i = 0; i < this->size; ++ i) {
- for (int j = 0; j < this->size; ++ j)
- cout << this->m[i][j] << " ";
- cout << endl;
- }
- }
- Matrix minor(int a, int b) {
- if (this->size <= 1 || a >= this->size || b >= this->size) {
- cout << "Minor Error\n";
- return Matrix();
- }
- vector<vector<double>> m;
- for (int i = 0; i < this->size; ++ i) {
- if (i == a)
- continue;
- m.push_back(vector<double>());
- for (int j = 0; j < this->size; ++ j) {
- if (j == b)
- continue;
- m[m.size() - 1].push_back(this->m[i][j]);
- }
- }
- return Matrix(m);
- }
- double det() {
- if (this->size < 2) {
- cout << "Det Error\n";
- return 0;
- }
- else if (this->size == 2) {
- return this->m[0][0] * this->m[1][1] - this->m[0][1] * this->m[1][0];
- }
- else {
- double s = 0;
- for (int i = 0; i < this->size; ++ i)
- s += pow(-1, 1.0 * i) * this->m[0][i] * this->minor(0, i).det();
- return s;
- }
- }
- ~Matrix() {}
- Matrix operator+(Matrix M) {
- if (this->size != M.size) {
- cout << "op+ Error\n";
- return Matrix();
- }
- for (int i = 0; i < this->size; ++ i)
- for (int j = 0; j < this->size; ++ j)
- this->m[i][j] += M.m[i][j];
- return Matrix(this->m);
- }
- Matrix operator-(Matrix M) {
- if (this->size != M.size) {
- cout << "op- Error\n";
- return Matrix();
- }
- for (int i = 0; i < this->size; ++ i)
- for (int j = 0; j < this->size; ++ j)
- this->m[i][j] -= M.m[i][j];
- return Matrix(this->m);
- }
- Matrix operator*(Matrix M) {
- if (this->size != M.size) {
- cout << "op* Error\n";
- return Matrix();
- }
- vector<vector<double>> m;
- for (int i = 0; i < this->size; ++ i) {
- m.push_back(vector<double>());
- for (int j = 0; j < this->size; ++ j) {
- double s = 0;
- for (int k = 0; k < this->size; ++ k)
- s += this->m[i][k] * M.m[k][j];
- m[i].push_back(s);
- }
- }
- return Matrix(m);
- }
- Matrix operator*=(double n) {
- for (int i = 0; i < this->size; ++ i)
- for (int j = 0; j < this->size; ++ j)
- this->m[i][j] *= n;
- return Matrix(this->m);
- }
- };
- int main(void) {
- int s = 3;
- double x[3][3] = {
- {5, 1, 7},
- {-10, -2, 1},
- {0, 1, 2}
- }, y[3][3] = {
- {2, 4, 1},
- {3, 1, 0},
- {7, 2, 1}
- };
- vector<vector<double>> v1, v2;
- for (int i = 0; i < s; ++ i) {
- v1.push_back(vector<double>(s));
- v2.push_back(vector<double>(s));
- for (int j = 0; j < s; ++ j) {
- v1[i][j] = x[i][j];
- v2[i][j] = y[i][j];
- }
- }
- Matrix a(v1), b(v2), res;
- res = ((a - b) *= 2) * ((a * a) + b);
- res.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement