-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixFunctions.pde
264 lines (218 loc) · 7.42 KB
/
MatrixFunctions.pde
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* ===========================================================================
This function returns the sum of each row of a matrix
Arguments:
matrix: a 2d int array
Returns
array: an int array containing the sum of each row of the matrix
======================================================================= */
int[] rowSum (int[][] matrix) {
int [] array = new int [matrix.length];
for (int i = 0; i < matrix.length; i++) {
int sum = 0;
for (int j = 0; j < matrix.length; j++) {
sum = sum + matrix[i][j];
}
array[i] = sum;
}
return array;
}
/* ======================================================================
This function returns a matrix that is the product of two other matrices.
The rows of the first matrix must == the colums of the second or the
math will not work.
Argumetns:
matrixA: a 2d int array
matrixB: a 2d int array
Returns
matrixOut: a 2d int array with the same number of rows as the first
matrix and the same number of colums as the second matrix.
=================================================================== */
int[][] matrixProduct (int[][] matrixA, int[][] matrixB) {
int aRows = matrixA.length;
int aColumns = matrixA[0].length;
int bRows = matrixB.length;
int bColumns = matrixB[0].length;
int[][] matrixOut;
// Check to make sure the matrixies can be multiplied, otherwise return an
// array of -1
if (aColumns != bRows || aRows != bColumns) {
matrixOut = new int[1][1];
matrixOut[1][1] = -1;
} else {
matrixOut = new int[aRows][bColumns];
for (int i = 0; i < aRows; i++) {
for (int j = 0; j < bColumns; j++) {
// initalise the matrix to 0 so we can add to it latter
matrixOut[i][j] = 0;
for (int k = 0; k < aColumns; k++) {
matrixOut[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
}
return matrixOut;
}
/* ============================================================================
This function sets all of the values of a matrix to 0's
Arguments:
matrix: a 2d array who's internal arrays are all the same length
Returns:
matrix: a 2d array with all values = 0;
========================================================================= */
int [][] initMatrix (int [][] matrix ) {
for (int i = 0; i < matrix.length; i ++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
return matrix;
}
/* ============================================================================
This function sets the \ diagonal of a matrix all to 1;
Arguments:
matrix: a 2d array who's internal arrays are all the same length
Returns:
matrix: a 2d array with who's \ diagonals are all 1;
========================================================================= */
int [][] initNeighbor (int [][] matrix ) {
for (int i = 0; i < matrix.length; i ++) {
for (int j = 0; j < matrix[0].length; j++) {
if (i == j) {
matrix[i][j] = 1;
}
}
}
return matrix;
}
/* ============================================================================
This function takes two matrix and returs a single matrix containing the
lower positive number in each coordinate of both matrixes.
So if matrixA had a 3 in coordinate [3][4] and matrixB had a 2 in [3][4] the
output will have 2 in [3][4].
The two matrices must be of the same dementions
Arguments:
matrixA
matrixB
Returns:
matrixOut
========================================================================= */
int [][] matrixOverlay ( int [][] matrixA, int [][] matrixB ) {
int [][] matrixOut = new int[matrixA.length][matrixA[0].length];
String alp = "abcdefghijklmnopqrstuvwxyz";
for ( int i = 0; i < matrixA.length; i++ ) {
for (int j = 0; j < matrixA[0].length; j++) {
//print("matrixA["+alp.charAt(i)+"]["+alp.charAt(j)+"] = "+matrixA[i][j]);
//println(" matrixB["+alp.charAt(i)+"]["+alp.charAt(j)+"] = "+matrixB[i][j]);
if (matrixB[i][j] != 0 && matrixA[i][j] == 0) {
matrixOut[i][j] = matrixB[i][j];
//println("Gets B: "+matrixB[i][j]);
} else if (matrixA[i][j] != 0 && matrixB[i][j] == 0){
matrixOut[i][j] = matrixA[i][j];
//println("Gets A: "+matrixA[i][j]);
} else {
if (matrixA[i][j] <= matrixB[i][j]) {
matrixOut[i][j] = matrixA[i][j];
//println("Gets A: "+matrixA[i][j]);
} else {
if ( matrixA[i][j] > matrixB[i][j]) {
matrixOut[i][j] = matrixB[i][j];
//println("Gets B: "+matrixB[i][j]);
}
}
}
}
}
return matrixOut;
}
/* ===========================================================================
This is used to see if the provided array is filled with non zero numbers.
If it has no zero's then it returns a positie int, otherwise it returns 0.
Arguments:
array: an int array
Returns:
domNum: a intiger indicating if the array has any zero's
======================================================================== */
int checkDom ( int[] array ) {
int domNum = 1;
for (int i = 0; i < array.length; i++) {
domNum *= array[i];
}
return domNum;
}
/* ==========================================================================
This function adds all the elements at the same vertical coordinates into an
array.
Arguments:
iter: an array list of int indicating the rows of a matrix
matrix: a 2d square array
Returns:
arrayOut: an int array
======================================================================= */
int [] colAdd (IntList iter, int [][] matrix) {
int[] arrayOut = new int [matrix.length];
String alp = "ABCDEGFGHIJKLMNOPQRSTUV";
//println("ArrayOut starts as: ");
for (int i = 0; i < arrayOut.length; i++){
arrayOut[i] = 0;
//print(arrayOut[i]+" ");
}
for (int i = 0; i < iter.size(); i++){
//println("The iterator is: "+alp.charAt(iter.get(i)));
//println();
//println("Add to it: ");
for(int j = 0; j < matrix[i].length; j++) {
//print(matrix[iter.get(i)][j]+" ");
}
//println();
for (int j = 0; j < matrix[i].length; j++) {
arrayOut[j] += matrix[iter.get(i)][j];
}
}
//println("Giving ");
for (int j = 0; j < matrix.length; j++) {
//print(arrayOut[j]+" ");
}
//println();
//println();
return arrayOut;
}
/* ==============================================================================
This Function prints a 2d array to the consol. It can handel arrays of up to
size 26.
Argument:
matrix: a 2d array to be printed.
========================================================================== */
void printMatrix (int[][] matrix) {
String alp = "abcdefghijklmnopqrstuvwxyz";
println("================================================================");
print(" ");
for (int i = 0; i < matrix.length; i++) {
print("\t"+alp.charAt(i));
}
println();
for (int i = 0; i < matrix.length; i++) {
print(alp.charAt(i)+"| ");
for (int j = 0; j < matrix.length; j++) {
//print(alp.charAt(j));
print("\t"+matrix[i][j]);
}
print("\n");
}
}
/* ============================================================================
This function returns the contents of a 2d array. This is so you can create
copies of a matrix and not have them all refer back to the dame date.
Arguments:
matrixA: The array to be copied
Returns:
matrixOut
========================================================================= */
int[][] copyMatrix (int [][]matrixA) {
int [][] matrixOut = new int [matrixA.length][matrixA[0].length];
for (int i = 0; i < matrixA.length; i++){
for(int j =0; j < matrixA[i].length; j++) {
matrixOut[i][j] = matrixA[i][j];
}
}
return matrixOut;
}