-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
executable file
·186 lines (166 loc) · 5.16 KB
/
matrix.cpp
File metadata and controls
executable file
·186 lines (166 loc) · 5.16 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
182
183
184
185
186
/*
Adapted from my Matrix assignment code.
Changes:
- Added throwing of exceptions.
- Added transformation matrices.
- Removed determinant calculation to save on lines of code.
*/
#include "matrix.h"
using namespace std;
// CONSTRUCTORS
// Parameterised constructor
Matrix::Matrix(int t_rows, int t_columns){
rows = t_rows;
columns = t_columns;
mdata = new double[rows*columns];
}
// Deep-copy constructor
Matrix::Matrix(const Matrix &mat){
rows = mat.getRows();
columns = mat.getColumns();
mdata = new double[rows*columns];
if (rows > 0 && columns > 0){
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
(*this)(row, column) = mat(row, column);
}
}
}
}
const double & Matrix::index(int row, int column) const {
if (row > 0 && column > 0 && row <= rows && column <= columns){
return mdata[(column - 1) + (row - 1)*columns];
}
else {
throw out_of_range("Matrix accessed out of range");
}
}
const double & Matrix::operator()(int row, int column) const { return this->index(row, column); }
double & Matrix::index(int row, int column){
if (row > 0 && column > 0 && row <= rows && column <= columns){
return mdata[(column - 1) + (row - 1)*columns];
}
else {
throw out_of_range("Matrix accessed out of range");
}
}
double & Matrix::operator()(int row, int column){ return this->index(row, column); }
// Arithmetic
Matrix Matrix::operator+ (const Matrix &mat) const {
if (mat.getColumns() == columns && mat.getRows() == rows){ // if matrices are of same dimension
Matrix result(rows, columns);
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
result(row, column) = (*this)(row, column) + mat(row, column); // add each element in turn
}
}
return result; // return the resulting matrix
}
else { // else, terminate the program
throw invalid_argument("Matrices must have same dimension for addition");
}
}
Matrix Matrix::operator-(const Matrix &mat) const {
if (mat.getColumns() == columns && mat.getRows() == rows){
Matrix result(rows, columns);
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
result(row, column) = (*this)(row, column) - mat(row, column);
}
}
return result;
}
else {
throw invalid_argument("Matrices must have same dimension for subtraction");
}
}
Matrix Matrix::operator*(const Matrix &mat) const {
if (mat.getRows() == columns){ // if dimensions satisfy m*n,n*p
int m_columns = mat.getColumns();
Matrix result(rows, m_columns);
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= m_columns; column++){
double temp = 0;
for (int k = 1; k <= columns; k++){
temp += (*this)(row, k)*mat(k, column); // do matrix multiplication
}
result(row, column) = temp;
}
}
return result;
}
else {
throw invalid_argument("Matrices must have dimension mxn,nxp for multiplication");
}
}
Matrix Matrix::operator*(double i) const { // Allows us to multiply matrix by a constant integer
Matrix result(rows, columns);
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
result(row, column) = i*(*this)(row, column);
}
}
return result;
}
// INPUT, OUTPUT & ASSIGNMENT
// Assignment operator
Matrix & Matrix::operator=(const Matrix &mat){
if (&mat == this) return *this; // handles self-assignment
delete mdata;
rows = mat.getRows();
columns = mat.getColumns();
mdata = new double[rows*columns];
if (rows > 0 && columns > 0){
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
(*this)(row, column) = mat(row, column); // performs deep copy cell by cell
}
}
}
return *this;
}
// BOOLEAN OPERATOR
bool Matrix::operator==(const Matrix &mat) const {
if (&mat == this) return true; // 'Self-consistency' executed cheaply.
if (rows != mat.getRows()) return false;
if (columns != mat.getColumns()) return false;
for (int row = 1; row <= rows; row++){
for (int column = 1; column <= columns; column++){
if ((*this)(row, column) != mat(row, column)) return false; // If a single cell doesn't match, we can escape.
}
}
return true; // "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth."
}
Matrix RotMatrix(double theta){
Matrix result(2,2);
result(1, 1) = result(2,2) = cos(theta);
result(1, 2) = sin(theta);
result(2, 1) = -result(1, 2);
return result;
}
Matrix ShearMatrix(double x, double y){
Matrix result(2,2);
result(1, 1) = result(2, 2) = 1;
result(1, 2) = x;
result(2, 1) = y;
return result;
}
Matrix ReflectMatrix(double x, double y){
Matrix result(2,2);
double l = x*x + y*y;
result(1, 1) = (x*x - y*y) / l;
if (x != 0 && y != 0) {
result(1, 2) = result(2, 1) = (2 * x*y) / l;
} else {
result(1, 2) = result(2, 1) = 0;
}
result(2, 2) = -result(1, 1);
return result;
}
Matrix ScaleMatrix(double x, double y){
Matrix result(2,2);
result(1,2) = result(2,1) = 0;
result(1,1) = x;
result(2,2) = y;
return result;
}