-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMath.cpp
More file actions
118 lines (105 loc) · 3.94 KB
/
Copy pathMatrixMath.cpp
File metadata and controls
118 lines (105 loc) · 3.94 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
#include"Matrix.h"
#include<iostream>
//overloading operator = for matrices
void Matrix::operator = (const Matrix &b){
this->rows = b.rows;
this->columns = b.columns;
for(int i = 0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
this->ele[i][j] = b.ele[i][j];
}
//overloading + operator for scalar and vector additions:
//vector
Matrix Matrix::operator + (const Matrix &b) const{
Matrix result(this->rows, this->columns);
if(this->rows != b.rows && this->columns != b.columns){
std::cerr<<"Matrices not fit for addition!";
return result;
}
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] + b.ele[i][j];
return result;
}
//scalar:
Matrix Matrix::operator + (const float a) const{
Matrix result(this->rows, this->columns);
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] + a;
return result;
}
//overloading operators - for scalar and vector substraction
//vector:
Matrix Matrix::operator - (const Matrix &b) const{
Matrix result(this->rows, this->columns);
if(this->rows != b.rows && this->columns != b.columns){
std::cerr<<"Matrices not fit for substraction!";
return result;
}
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] - b.ele[i][j];
return result;
}
//scalar:
Matrix Matrix::operator - (const float a) const{
Matrix result(this->rows, this->columns);
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] - a;
return result;
}
//overloading operator * for scalar and vector multiplication
//vector:
Matrix Matrix::operator * (const Matrix &b) const{
Matrix result(this->rows, b.columns);
float sum;
if(this->columns != b.rows){
std::cerr<<"Matrices not fit for multiplication!";
return result;
}
for(int i = 0; i < this-> rows; i++){
for(int j = 0; j < b.columns; j++){
sum = 0.0f;
for(int k = 0; k < this->columns; k++) //this needs
sum += this->ele[i][j] * b.ele[i][j]; //improvement
result.ele[i][j] = sum;
}
}
return result;
}
//scalar:
Matrix Matrix::operator * (const float a) const{
Matrix result(this->rows, this->columns);
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] * a;
return result;
}
//overloading operator / for scalar division
Matrix Matrix::operator / (const float a) const{
Matrix result(this->rows, this->columns);
for(int i =0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = this->ele[i][j] / a;
return result;
}
//tranpose
Matrix Matrix::transpose() const{
Matrix result(this->columns, this->rows);
//some error checking here...
for(int i = 0; i < result.rows; i++)
for(int j = 0; j < result.columns; j++)
result.ele[i][j] = this->ele[j][i];
result.print();
return result;
}
//function to map a function to all values
Matrix Matrix::map(float (*functionToMap)(float x)) const{
Matrix result(this->rows, this->columns);
for(int i = 0; i < this->rows; i++)
for(int j = 0; j < this->columns; j++)
result.ele[i][j] = (*functionToMap)(this->ele[i][j]);
return result;
}