-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.java
194 lines (175 loc) · 6.39 KB
/
Matrix.java
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
import java.lang.Math;
class Matrix implements java.io.Serializable{
int rows;
int columns;
double[][] matrix;
public Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.matrix = new double[rows][columns];
}
public void fill(double n){
// fill the matrix with value n
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
this.matrix[i][j] = n;
}
}
}
public void randomize(){
// place random doubles (0.0 to 0.99) in each matrix element
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
this.matrix[i][j] = Math.random();
}
}
}
public void add(Matrix m) throws Exception{
// add a const to all elements of a matrix
if (this.rows == m.rows && this.columns == m.columns){
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
this.matrix[i][j] += m.matrix[i][j];
}
}
}
else{
throw new Exception("m.matrix has to be the same size as this.matrix");
}
}
public void add(double n){
// add a const to all elements of a matrix
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
this.matrix[i][j] += n;
}
}
}
public double sigmoid(double x){
// sigmoid function...
return (1/( 1 + Math.pow(Math.E,(-1*x))));
}
public void mapSigmoid(){
// map the sigmoid function to all elements of the matrix
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
this.matrix[i][j] = sigmoid(this.matrix[i][j]);
}
}
}
public void mutate(double chance) throws Exception{ // chance between 0 and 1
// x% chance of mutating an element in the matrix
double n;
if (chance >= 0 && chance <=1){
for (int i=0; i<this.rows; i++){
for(int j=0; j<this.columns; j++){
n = Math.random();
if (n < chance){ //todo check if prbability is correct
this.matrix[i][j] = Math.random();
}
}
}
}
else{
throw new Exception("ERROR: chance has to be between 0.0 and 1.0");
}
}
public void mutate(double chance, double rangeOfChange) throws Exception{ // chance between 0 and 1
// x% chance of mutating an element in the matrix by y values
double n;
double add;
if (chance >= 0 && chance <=1){
for (int i=0; i<this.rows; i++){// todo work on thisss
for(int j=0; j<this.columns; j++){
n = Math.random();
add = Math.random();
if (n < chance){ //todo check if prbability is correct
if (add >=0.5){
this.matrix[i][j] += Math.random();
}
else{
this.matrix[i][j] -= Math.random();
}
}
}
}
}
else{
throw new Exception("ERROR: chance has to be between 0.0 and 1.0");
}
}
public static Matrix matrixMultiplication(Matrix m1, Matrix m2) throws Exception{
// perform matrix multiplication
if (m1.columns == m2.rows){
Matrix matrixProduct = new Matrix(m1.rows, m2.columns);
for (int i=0; i<m1.rows; i++){
for(int j=0; j<m2.columns; j++){
for (int k=0; k<m1.columns; k++){
matrixProduct.matrix[i][j] += m1.matrix[i][k] * m2.matrix[k][j];
}
}
}
return matrixProduct;
}
else{
throw new Exception("m1.rows must be equal to m2.columns");
}
}
public static Matrix combineColumnsMatrices(Matrix m1, Matrix m2) throws Exception{
// combine 2 matrices that have the same amount of columns
if (m1.columns == m2.columns){
Matrix combinedMatrix = new Matrix(m1.rows+m2.rows, m1.columns);
for (int i=0; i<m1.rows; i++){
for(int j=0; j<m1.columns; j++){
combinedMatrix.matrix[i][j] = m1.matrix[i][j];
}
}
for (int i=0; i<m2.rows; i++){
for(int j=0; j<m2.columns; j++){
combinedMatrix.matrix[i+m1.rows][j] = m2.matrix[i][j];
}
}
return combinedMatrix;
}
else{
throw new Exception("m1.columns must be equal to m2.columns");
}
}
public static Matrix[] transverseCut(Matrix m, int incision) throws Exception{
// cut a matrix by separating rows; m --> [m1, m2]
// m.matrix[incision] is gonna be in the upper part of the cut
if (incision <= m.rows){
Matrix m1 = new Matrix(incision, m.columns);
Matrix m2 = new Matrix(m.rows-incision, m.columns);
for (int i=0; i<m1.rows; i++){
for(int j=0; j<m1.columns; j++){
m1.matrix[i][j] = m.matrix[i][j];
}
}
for (int i=0; i<m2.rows; i++){
for(int j=0; j<m2.columns; j++){
m2.matrix[i][j] = m.matrix[i+incision][j];
}
}
return new Matrix[] {m1, m2};
}
else{
throw new Exception("incision must be less than or equal to m.rows");
}
}
public static void print(Matrix m){
//print a matrix to terminal
boolean isEmpty = true;
for (int i=0; i<m.rows; i++){
isEmpty = false;
System.out.printf("row %d: ", i);
for(int j=0; j<m.columns; j++){
System.out.print(m.matrix[i][j]+" ");
}
System.out.println("");
}
if (isEmpty){
System.out.println("Matrix is empty");
}
}
}