-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
102 lines (88 loc) · 2.57 KB
/
matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// Created by Imran on 05-Sep-18.
//
#ifndef MATRIX_CPP
#define MATRIX_CPP
#include "matrix.hpp"
#include <iostream>
#include <iomanip>
template<typename E>
void Matrix<E>::operator += (const Matrix<E>& m){
if(rows != m.rows || cols != m.cols){
std::cerr<<"addition: bad dimensions ["<<this->getRows()<<", "<<this->getCols()<<"] and ["<<m.getRows()<<", "<<m.getCols()<<"]\n";
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
mat[i*cols+j] += m.mat[i*cols+j];
}
}
template<typename E>
void Matrix<E>::operator -= (const Matrix<E>& m){
if(rows != m.rows || cols != m.cols){
std::cerr<<"subtraction: bad dimensions ["<<this->getRows()<<", "<<this->getCols()<<"] and ["<<m.getRows()<<", "<<m.getCols()<<"]\n";
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
mat[i*cols+j] -= m.mat[i*cols+j];
}
}
template<typename E>
void Matrix<E>::operator *= (E n){
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
mat[i*cols+j] *= n;
}
}
template<typename E>
Matrix<E>& Matrix<E>::hadamard(const Matrix<E>& m){
if(rows != m.rows || cols != m.cols){
std::cerr<<"hadamard: bad dimensions ["<<this->getRows()<<", "<<this->getCols()<<"] and ["<<m.getRows()<<", "<<m.getCols()<<"]\n";
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
mat[i*cols+j] *= m.mat[i*cols+j];
}
return *this;
}
template<typename E>
Matrix<E> Matrix<E>::T() const{
Matrix t(cols, rows);
t.cols = rows;
t.rows = cols;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
t.mat[i*cols+j] = mat[j*rows+i];
}
return t;
}
template<typename E>
E& Matrix<E>::operator ()(int i, int j){
return mat[i*cols+j];
}
template<typename E>
void Matrix<E>::print() const {
for(int i=0; i<rows; i++){
std::cout << "|";
for(int j=0; j<cols; j++)
std::cout << std::right << std::setw(10) << mat[i*cols+j] << " ";
std::cout << "|" << std::endl;
}
}
template<typename E>
std::string Matrix<E>::shape() const {
std::string shape = "(" + std::to_string(rows) + ", " + std::to_string(cols) + ")";
return shape;
}
template<typename E>
Matrix<E> operator + (const Matrix<E>& a, const Matrix<E>& b){
Matrix<E> res(a);
res += b;
return res;
}
template<typename E>
Matrix<E> operator - (const Matrix<E>& a, const Matrix<E>& b){
Matrix<E> res(a);
res -= b;
return res;
}
#endif