-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
126 lines (101 loc) · 3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Matrix.hpp"
#include <stdexcept>
Matrix::Matrix() {
}
Matrix::Matrix(size_t rows, size_t cols, double val) {
this->rows = rows;
this->cols = cols;
matrix = std::vector<std::vector<double>>(rows, std::vector<double>(cols, val));
}
Matrix::Matrix(size_t rows, size_t cols) : Matrix(rows, cols, 0.0) {}
std::vector<double>& Matrix::operator[](int i) {
return matrix[i];
}
Matrix Matrix::multiply(Matrix& m) {
if (cols != m.rows) {
throw std::invalid_argument("Matrix dimensions do not match for multiplication.");
}
Matrix result(rows, m.cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < m.cols; ++j) {
result[i][j] = 0;
for (size_t k = 0; k < cols; ++k) {
result[i][j] += matrix[i][k] * m[k][j];
}
}
}
return result;
}
Matrix Matrix::transpose() {
Matrix result(cols, rows);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result[j][i] = matrix[i][j];
}
}
return result;
}
Matrix Matrix::add(Matrix& m) {
if (rows != m.rows || cols != m.cols) {
throw std::invalid_argument("Matrix dimensions do not match for addition.");
}
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result[i][j] = matrix[i][j] + m[i][j];
}
}
return result;
}
Matrix Matrix::subtract(Matrix& m) {
if (rows != m.rows || cols != m.cols) {
throw std::invalid_argument("Matrix dimensions do not match for subtraction.");
}
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result[i][j] = matrix[i][j] - m[i][j];
}
}
return result;
}
Matrix Matrix::hadamard(Matrix& m) {
if (rows != m.rows || cols != m.cols) {
throw std::invalid_argument("Matrix dimensions do not match for element-wise multiplication.");
}
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result[i][j] = matrix[i][j] * m[i][j];
}
}
return result;
}
Matrix Matrix::scale(double scalar) {
return this->apply_function([scalar](double val) { return val * scalar; });
}
Matrix Matrix::add_scalar(double scalar) {
return this->apply_function([scalar](double val) { return val + scalar; });
}
Matrix Matrix::apply_function(std::function<double(double)> f) {
Matrix result(rows, cols);
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
result[i][j] = f(matrix[i][j]);
}
}
return result;
}
double Matrix::max_index() {
double max = matrix[0][0];
double index = 0;
for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) {
if (matrix[i][j] > max) {
max = matrix[i][j];
index = i;
}
}
}
return index;
}