-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.ino
196 lines (167 loc) · 4.12 KB
/
Matrix.ino
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
187
188
189
190
191
192
193
194
195
196
class Matrix{
int rows,columns;
float[][] matrix;
//Constructer
Matrix(int r,int c){
this.rows = r;
this.columns = c;
this.matrix = new float[r][c];
}
//Random numbers between 0 and 10
void randomize(){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = random(0,10);
}
}
}
//Random numbers between 0 and 10
void randomizeInt(){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = (int)random(0,10);
}
}
}
//Overloaded randomize
void randomize(int upper){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = random(0,upper);
}
}
}
//Overloaded randomize
void randomizeInt(int upper){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = (int)random(0,upper);
}
}
}
//overloaded Randomize
void randomize(int lower,int upper){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = random(lower,upper);
}
}
}
//overloaded Randomize
void randomizeInt(int lower,int upper){
for(int i = 0;i < rows;i++){
for(int j = 0;j < columns;j++){
this.matrix[i][j] = (int)random(lower,upper);
}
}
}
//Mutate
Matrix mutate(){
for(int i = 0;i < this.rows;i++){
for(int j = 0;j < this.columns;j++){
if(random(0,1) < 0.5){
this.matrix[i][j] += randomGaussian()*0.5;
}
}
}
return this;
}
//DISPLAY THE MATRIX
void display(){
if(this.rows == 0 && this.columns == 0){
println("Null Matrix");
return;
}
for(int j = 0;j < this.columns;j++){
print(" ",j," ");
}
println();
for(int i = 0;i < this.rows;i++){
print(i," |");
for(int j = 0;j < this.columns;j++){
print(this.matrix[i][j],"| ");
}
println();
for(int j = 0;j < this.columns;j++){
print("____________");
}
println();
}
}
//MULTIPLY A NUMBER
void multiply(float n){
for(int i=0; i < this.rows;i++){
for(int j = 0; j < this.columns;j++){
this.matrix[i][j] *= n;
}
}
}
//ADD A NUMBER
void add(float n){
for(int i=0; i < this.rows;i++){
for(int j = 0; j < this.columns;j++){
this.matrix[i][j] += n;
}
}
}
//SUBTRACT A NUMBER
void subtract(float n){
this.add(-n);
}
void subtract(Matrix inp){
float row = inp.rows;
float col = inp.columns;
if(this.rows != row || this.columns != col){
println("Invalid Subtraction of matrix !! .. ");
return;
}
for(int i=0; i < this.rows;i++){
for(int j = 0; j < this.columns;j++){
this.matrix[i][j] -= inp.matrix[i][j];
}
}
}
//ADD A MATRIX TO A MATRIX
Matrix add(Matrix arg){
if(this.rows != arg.rows || this.columns != arg.columns){
println("Invalid operation (add) as rows or columns did not match ! ");
return new Matrix(0,0);
}
Matrix newM = new Matrix(this.rows,this.columns);
for(int i = 0;i < this.rows;i++){
for(int j = 0;j < this.columns;j++){
newM.matrix[i][j] = this.matrix[i][j] + arg.matrix[i][j];
}
}
return newM;
}
//MULTIPLY A MATRIX WITH A MATRIX
Matrix multiply(Matrix arg){
if(this.columns != arg.rows){
println("Invalid Multiplication of matrices !");
return new Matrix(0,0);
}
Matrix newM = new Matrix(this.rows,arg.columns);
for(int i =0;i < this.rows;i++){
for(int j = 0;j < arg.columns;j++){
float sum = 0;
for(int k = 0;k < this.columns;k++){
sum += this.matrix[i][k]*arg.matrix[k][j];
}
newM.matrix[i][j] = sum;
}
}
return newM;
}
Matrix transpose(){
int newCol = this.rows;
int newRow = this.columns;
Matrix newM = new Matrix(newRow,newCol);
for(int i = 0; i < this.rows;i++){
for(int j = 0; j < this.columns;j++){
newM.matrix[j][i] = this.matrix[i][j];
}
}
return newM;
}
}