-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmatrix.cpp
152 lines (127 loc) · 4.52 KB
/
vmatrix.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
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
#include "vmatrix.h"
#include <cstdlib>
#include <ctime>
#include <vector>
#include <iostream>
#include <iomanip>
VMatrix::Matrix::Matrix(VMatrix::MatrixVector& hopefulMatrixShape, int rows, int columns) {
if (rows != hopefulMatrixShape.size()) {
throw "Incorrect row size passed in.";
}
for (int r = 0; r < rows; r++) {
if (columns != hopefulMatrixShape[r].size()) {
throw "Incorrect column size passed in.";
}
}
this->colCount = columns;
this->rowCount = rows;
this->matrix = hopefulMatrixShape;
}
VMatrix::Matrix::Matrix(const unsigned int rows, const unsigned int columns) {
this->colCount = columns;
this->rowCount = rows;
for (int i = 0; i < rows; i++) {
this->matrix.push_back(std::vector<int>(columns));
}
}
void VMatrix::Matrix::printMatrix() {
if (this->rowCount <= 0) {
std::cout << "Nothing to print" << std::endl;
return;
}
for (int i = 0; i < this->rowCount; i++) {
for (int c = 0; c < this->colCount; c++) {
if (c == 0) {
std::cout << "R" << i + 1 << "|";
}
std::cout <<std::right << std::setw(6) << this->matrix[i][c];
}
std::cout << " |\n";
}
std::cout << "\n";
}
bool VMatrix::Matrix::fillMatrixWithRandNums(const unsigned int lowNum, const unsigned int highNum) {
if (this->rowCount <= 0) {
throw "Matrix is too small.";
}
if (lowNum > highNum) {
throw "lowNum must be lower than highNum when doing random fill.";
}
try {
for (int i = 0; i < this->rowCount; i++) {
for (int k = 0; k < this->colCount; k++) {
this->matrix[i][k] = rand() % (highNum - lowNum) + lowNum;
}
}
} catch (const std::exception _) {
return false;
}
return true;
}
namespace VMatrix {
Matrix operator + (VMatrix::Matrix const& m1, VMatrix::Matrix const& m2) {
if (
m1.rowCount != m2.rowCount
|| m1.colCount != m2.colCount
) {
throw "Row x Col needs to be the same for both Matrices to be added.";
}
VMatrix::MatrixVector newMatrix;
for (int r = 0; r < m1.rowCount; r++) {
newMatrix.push_back(std::vector<int>(m1.colCount));
for (int c = 0; c < m1.colCount; c++) {
newMatrix[r][c] = (m1.matrix[r][c] + m2.matrix[r][c]);
}
}
return VMatrix::Matrix(newMatrix, m1.rowCount, m1.colCount);
}
VMatrix::Matrix operator - (VMatrix::Matrix const& m1, VMatrix::Matrix const& m2) {
if (
m1.rowCount != m2.rowCount
|| m1.colCount != m2.colCount
) {
throw "Row x Col needs to be the same for both Matrices to be added.";
}
VMatrix::MatrixVector newMatrix;
for (int r = 0; r < m1.rowCount; r++) {
newMatrix.push_back(std::vector<int>(m1.colCount));
for (int c = 0; c < m1.colCount; c++) {
newMatrix[r][c] = (m1.matrix[r][c] - m2.matrix[r][c]);
}
}
return Matrix(newMatrix, m1.rowCount, m1.colCount);
}
Matrix operator * (VMatrix::Matrix const& ma1, VMatrix::Matrix const& ma2) {
// in case we want to change the order of the matrix.
auto m1 = ma1;
auto m2 = ma2;
if (m1.colCount != m2.rowCount) {
if (m2.colCount != m1.rowCount) {
throw "Cannot multiply these matrices.";
}
// we can multiply them, but we need to switch them around.
// Example: 3x2 * 2x3 is legal, but 2x3 * 2x3 is not.
// m1 col size needs to equal m2 row size.
auto tmp = m1;
m1 = m2;
m2 = tmp;
}
// Now we have validated our matrix sizes, we are ready to multiply.
VMatrix::MatrixVector mulMatrix;
for (int i = 0; i < m1.rowCount; i++) {
mulMatrix.push_back(std::vector<int>(m2.colCount));
}
int multiplier;
for (int col = 0; col < m2.colCount; col++) {
for (int r = 0; r < m1.rowCount; r++) {
//mulMatrix.push_back(std::vector<int>(m1.colCount));
multiplier = 0;
for (int c = 0; c < m1.colCount; c++) {
multiplier += (m1.matrix[r][c] * m2.matrix[c][col]);
}
mulMatrix[r][col] = multiplier;
}
}
return VMatrix::Matrix(mulMatrix, m1.rowCount, m2.colCount);
}
}