Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Matrix
- //
- // Created by Anton Wetret on 02/03/15.
- // Copyright (c) 2015 mipt. All rights reserved.
- //
- #include <iostream>
- #include <vector>
- using namespace std;
- template <typename T>
- class Matrix {
- public:
- Matrix() {
- }
- Matrix(int n, int m): _n(n), _m(m) {
- }
- const int GetRows() {
- return _n;
- }
- const int GetCol() {
- return _m;
- }
- const T Trace() {
- T tr;
- for (int i = 0; i < _n; ++i) {
- tr += _data[i][i];
- }
- return tr;
- }
- const T Det() {
- vector<vector<double> > data = _data;
- int s;
- bool f;
- for (int i = 0; i < _m; ++i) {
- f = true;
- for (int j = i; j < _n-1; ++j) {
- if (data[i][i] == 0) {
- f = false;
- int k = i + 1;
- while (k < _n && data[k][i] == 0) { ++k; }
- if (k < _n) {
- s++;
- swap(data[i], data[k]);
- f = true;
- }
- }
- if (f) {
- T p = data[j+1][i]/data[i][i];
- for (int k = i; k < _m; ++k) {
- data[j+1][k] -= p * data[i][k];
- }
- }
- }
- }
- T det = 1;
- for (int i = 0; i < _m; ++i) {
- det *= data[i][i];
- }
- return det;
- }
- void Push(const vector<T> &temp) {
- _data.push_back(temp);
- }
- ///////////-----
- vector<T>* operator[](int i) {
- return &(_data[i]);
- }
- ///////-----
- Matrix& operator=(const Matrix& other) {
- if (this == &other) return *this;
- else {
- for (int i = 0; i < _n; ++i) {
- for (int j = 0; j < _m; ++j) {
- _data[i][j] = (*other[i])[j];
- }
- }
- return *this;
- }
- }
- Matrix& operator+=(const Matrix& other) {
- for (int i = 0; i < _n; ++i) {
- for (int j = 0; j < _m; ++j) {
- _data[i][j] += (*other[i])[j];
- }
- }
- return *this;
- }
- private:
- int _n; // Число строк
- int _m; // Число столбцов
- vector<vector<T> > _data; // Содержание матрицы
- };
- template <typename T>
- istream& operator>>(istream& stream, Matrix<T>& mat) {
- T x;
- for (int i = 0; i < mat.GetRows(); ++i) {
- vector<T> temp;
- for (int j = 0; j < mat.GetCol(); ++j) {
- cin >> x;
- temp.push_back(x);
- }
- mat.Push(temp);
- }
- return stream;
- }
- template <typename T>
- ostream& operator<<(ostream& stream, const Matrix<T>& mat) {
- for (int i = 0; i < mat.GetRows(); ++i) {
- for (int j = 0; j < mat.GetCol(); ++j) {
- stream << (*mat[i])[j] << ' ';
- }
- stream << endl;
- }
- stream << endl;
- return stream;
- }
- template <typename T>
- Matrix<T> operator+(const Matrix<T>& first, const Matrix<T>& second) {
- Matrix<T> result(first);
- result += second;
- return result;
- }
- int main() {
- Matrix<double> m(2,2);
- Matrix<double> n(2,2);
- cin >> m;
- cin >> n;
- Matrix<double> c(2,2);
- c = m + n;
- // cout << m+n;
- // cin >> n;
- // cout << (m + n);
- // c = m + n;
- // cout << m.Trace();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement