Repository navigation
Expand file tree
/
Copy pathMatrix.cpp
More file actions
181 lines (154 loc) · 5.28 KB
/
Copy pathMatrix.cpp
File metadata and controls
181 lines (154 loc) · 5.28 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "Matrix.h"
#include <cassert>
#include <iostream>
#include "LU.h"
using std::ostream;
Matrix::Matrix(const size_t rows, const size_t cols) : mRows(rows), mCols(cols)
{
assert(mRows > 0 && "Number of rows should be greater than zero.");
assert(mCols > 0 && "Number of columns should be greater than zero.");
// Create a vector of vectors and initialize all elements to zero
mData = std::vector(mRows, std::vector(mCols, 0.0));
}
Matrix::Matrix(const Matrix& other) : mData(other.mData), mRows(other.mRows), mCols(other.mCols)
{
std::cout << "Copy constructor \n";
}
Matrix::Matrix(const std::vector<std::vector<double>>& data) : mData(data), mRows(data.size()), mCols(data[0].size())
{
assert(mRows > 0 && "Number of rows should be greater than zero.");
mCols = data[0].size();
// Check if all rows have the same number of columns
for (const auto& row : data)
assert(row.size() == mCols && "All rows must have the same number of columns.");
}
Matrix Matrix::identity(const size_t dimension)
{
Matrix identity(dimension, dimension);
for (size_t i = 0; i < dimension; i++)
identity(i,i) = 1.0;
return identity;
}
double Matrix::determinant() const
{
if (mRows != mCols) {
std::cout << "Determinant can only be computed for square matrix.";
return std::numeric_limits<double>::max();
}
const LU lu(*this);
return lu.determinant();
if (mRows <= 3) {
// This is terrible and useless but was a nice exercise
// of course, writing the terms out explicitly would be a lot better.
double determinant = 0;
// walk +1 +1 through the matrix
int count = mRows == 2 ? 1 : 0;
int i = 0, j = 0;
while (count++ < mCols) {
double tmp = 1;
for (size_t countInner = 0; countInner < mCols; ++countInner)
tmp *= mData[i++][j++ % mCols];
i = 0;
j = count;
determinant += tmp;
}
j = mCols - 1;
count = mRows == 2 ? 1 : 0;
while (count++ < mCols) {
double tmp = 1;
size_t countInner = 0;
while (countInner++ < mCols) {
tmp *= mData [i++][j--];
if (j == -1)
j = mRows - 1;
}
i = 0;
j = mCols - count - 1;
determinant -= tmp;
}
return determinant;
}
}
Matrix Matrix::operator*(const Matrix& other) const
{
const auto thisDimensions = this->dimensions();
const auto otherDimensions = other.dimensions();
std::cout << "Multiply " << thisDimensions.first << "X" << thisDimensions.second << " by " << otherDimensions.first << "X" << otherDimensions.second << '\n';
assert(thisDimensions.second == otherDimensions.first && "The columns of first matrix have to be the same as rows of the right one.");
Matrix multiplied(thisDimensions.first, otherDimensions.second);
for (size_t i = 0; i < multiplied.mRows; ++i)
for (size_t j = 0; j < multiplied.mCols; ++j)
for (size_t k = 0; k < mCols; ++k)
multiplied(i,j) += this->mData[i][k] * other(k,j);
return multiplied;
}
Matrix& Matrix::operator*=(const Matrix& other)
{
return *this = this->operator*(other);
}
Matrix Matrix::operator+(const Matrix& other) const
{
const auto thisDimensions = this->dimensions();
const auto otherDimensions = other.dimensions();
std::cout << "Adding " << thisDimensions.first << "X" << thisDimensions.second << " to " << otherDimensions.first << "X" << otherDimensions.second << '\n';
Matrix added(*this);
for (size_t i = 0; i < added.mRows; ++i)
for (size_t j = 0; j < added.mCols; ++j)
added(i,j) += other(i,j);
return added;
}
Matrix& Matrix::operator+=(const Matrix& other)
{
return *this = this->operator+(other);
}
Matrix Matrix::operator-(const Matrix& other) const
{
const auto thisDimensions = this->dimensions();
const auto otherDimensions = other.dimensions();
std::cout << "Subbing " << thisDimensions.first << "X" << thisDimensions.second << " to " << otherDimensions.first << "X" << otherDimensions.second << '\n';
Matrix added(*this);
for (size_t i = 0; i < added.mRows; ++i)
for (size_t j = 0; j < added.mCols; ++j)
added(i,j) -= other(i,j);
return added;
}
Matrix& Matrix::operator-=(const Matrix& other)
{
return *this = this->operator-(other);
}
std::pair<size_t, size_t> Matrix::dimensions() const
{
return { mRows, mCols };
}
bool Matrix::operator==(const Matrix& other) const
{
return mData == other.mData && mRows == other.mRows && mCols == other.mCols;
}
double& Matrix::operator()(const size_t row, const size_t col)
{
return mData[row][col];
}
const double& Matrix::operator()(const size_t row, const size_t col) const
{
return mData[row][col];
}
ostream& operator<<(ostream& os, const Matrix& m)
{
for (size_t i = 0; i < m.mRows; ++i) {
os << m(i,0);
for (size_t j = 1; j < m.mCols; ++j)
os << " " << m(i,j);
os << '\n';
}
return os;
}
// Copy assignment operator
Matrix& Matrix::operator=(const Matrix& other)
{
if (this != &other) {
mData = other.mData;
mRows = other.mRows;
mCols = other.mCols;
}
return *this;
}