-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Matrix.java
370 lines (326 loc) · 9 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package org.myrobotlab.kinematics;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import org.myrobotlab.logging.LoggerFactory;
import org.slf4j.Logger;
/**
* Encapsulates a 4x4 matrix
*
*
*/
public class Matrix implements Serializable {
public final static Logger log = LoggerFactory.getLogger(Matrix.class);
private static final long serialVersionUID = 1L;
protected int numRows;
protected int numCols;
public double[][] elements;
/**
* @param sx
* scaling in the x direction
* @param sy
* scaling in the y direction
* @param sz
* scaling in the z direction
* @return the associated scaling transformation matrix
*/
public static Matrix scaling(double sx, double sy, double sz) {
Matrix S = new Matrix();
S.elements[0][0] = sx;
S.elements[1][1] = sy;
S.elements[2][2] = sz;
S.elements[3][3] = 1;
return S;
}
/**
* @param tx
* translations in the x direction
* @param ty
* translations in the y direction
* @param tz
* translations in the z direction
* @return the associated translation transformation matrix
*/
public static Matrix translation(double tx, double ty, double tz) {
Matrix T = new Matrix();
T.elements[0][0] = 1;
T.elements[1][1] = 1;
T.elements[2][2] = 1;
T.elements[3][3] = 1;
T.elements[0][3] = tx;
T.elements[1][3] = ty;
T.elements[2][3] = tz;
return T;
}
/**
* @param theta
* an angle in radians
* @return the associated x-axis rotation transformation matrix
*/
public static Matrix xRotation(double theta) {
Matrix R = new Matrix();
double c = Math.cos(theta);
double s = Math.sin(theta);
R.elements[0][0] = 1;
R.elements[1][1] = c;
R.elements[2][2] = c;
R.elements[3][3] = 1;
R.elements[1][2] = s;
R.elements[2][1] = -s;
return R;
}
/**
* @param theta
* an angle in radians
* @return the associated y-axis rotation transformation matrix
*/
public static Matrix yRotation(double theta) {
Matrix R = new Matrix();
double c = Math.cos(theta);
double s = Math.sin(theta);
R.elements[0][0] = c;
R.elements[1][1] = 1;
R.elements[2][2] = c;
R.elements[3][3] = 1;
R.elements[2][0] = s;
R.elements[0][2] = -s;
return R;
}
/**
* @param theta
* an angle in radians
* @return the associated z-axis rotation transformation matrix
*/
public static Matrix zRotation(double theta) {
Matrix R = new Matrix();
double c = Math.cos(theta);
double s = Math.sin(theta);
R.elements[0][0] = c;
R.elements[1][1] = c;
R.elements[2][2] = 1;
R.elements[3][3] = 1;
R.elements[0][1] = s;
R.elements[1][0] = -s;
return R;
}
/**
* Constructs new 4x4 matrix, initializes to it to zeros
*/
Matrix() {
numRows = 4;
numCols = 4;
elements = new double[numRows][numCols];
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++) {
elements[r][c] = 0.0;
}
}
public Matrix(int rows, int cols) {
numRows = rows;
numCols = cols;
elements = new double[numRows][numCols];
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++) {
elements[r][c] = 0.0;
}
}
/**
* Copy constructor
*/
Matrix(Matrix m) {
numRows = m.numRows;
numCols = m.numCols;
elements = new double[numRows][numCols];
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++)
this.elements[r][c] = m.elements[r][c];
}
/**
* @param m
* a Matrix
* @return a new matrix which is equal to the sum of this + m
*/
public Matrix addTo(Matrix m) {
if (numRows != m.numRows || numCols != m.numCols) {
log.info("dimensions bad in addTo()");
return null;
}
Matrix ret = new Matrix(numRows, numCols);
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++)
ret.elements[r][c] = this.elements[r][c] + m.elements[r][c];
return ret;
}
/**
* Computes the dot product (or scalar product) of two matrices by multiplying
* corresponding elements and summing all the products.
*
* @param m
* A Matrix with the same dimensions
* @return the dot product (scalar product)
*/
public Double dot(Matrix m) {
if (numRows != m.numRows || numCols != m.numCols) {
log.info("dimensions bad in dot()");
return 0.0;
}
double sum = 0;
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++)
sum += this.elements[r][c] * m.elements[r][c];
return sum;
}
/**
* @param val
* a scalar
* @return true if and only if all elements of the matrix equal val
*/
public boolean equals(double val) {
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++)
if (Math.abs(elements[r][c] - val) > .0001)
return false;
return true;
}
public int getNumCols() {
return numCols;
}
public int getNumRows() {
return numRows;
}
/**
* Scalar multiplication- multiplies each element by a scalar
*
* @param s
* a scalar
* @return a new matrix which is equal to the product of s*this
*/
public Matrix multiply(double s) {
Matrix ret = new Matrix(numRows, numCols);
for (int i = 0; i < numRows; i++)
for (int j = 0; j < numCols; j++)
ret.elements[i][j] = elements[i][j] * s;
return ret;
}
/**
* @param m
* a Matrix
* @return a new matrix which is equal to the product of this*m
*/
public Matrix multiply(Matrix m) {
Matrix ret = new Matrix(numRows, m.numCols);
if (numCols != m.numRows) {
log.info("dimensions bad in multiply()");
return ret;
}
for (int r = 0; r < numRows; r++)
for (int c = 0; c < m.numCols; c++) {
for (int k = 0; k < numCols; k++) {
ret.elements[r][c] += this.elements[r][k] * m.elements[k][c];
}
}
return ret;
}
/**
* Calculates the matrix's Moore-Penrose pseudoinverse
*
* @return an MxN matrix which is the matrix's pseudoinverse.
*/
public Matrix pseudoInverse() {
int r, c;
int k = 1;
Matrix ak = new Matrix(numRows, 1);
Matrix dk, ck, bk;
Matrix R_plus;
try {
for (r = 0; r < numRows; r++) {
ak.elements[r][0] = this.elements[r][0];
}
if (!ak.equals(0.0)) {
R_plus = ak.transpose().multiply(1.0 / (ak.dot(ak)));
} else {
R_plus = new Matrix(1, numCols);
}
while (k < this.numCols) {
for (r = 0; r < numRows; r++) {
ak.elements[r][0] = this.elements[r][k];
}
dk = R_plus.multiply(ak);
Matrix T = new Matrix(numRows, k);
for (r = 0; r < numRows; r++) {
for (c = 0; c < k; c++) {
T.elements[r][c] = this.elements[r][c];
}
}
ck = ak.subtractFrom(T.multiply(dk));
if (!ck.equals(0.0)) {
bk = ck.transpose().multiply(1.0 / (ck.dot(ck)));
} else {
bk = dk.transpose().multiply(1.0 / (1.0 + dk.dot(dk))).multiply(R_plus);
}
Matrix N = R_plus.subtractFrom(dk.multiply(bk));
R_plus = new Matrix(N.numRows + 1, N.numCols);
for (r = 0; r < N.numRows; r++) {
for (c = 0; c < N.numCols; c++) {
R_plus.elements[r][c] = N.elements[r][c];
}
}
for (c = 0; c < N.numCols; c++) {
R_plus.elements[R_plus.numRows - 1][c] = bk.elements[0][c];
}
k++;
}
return R_plus;
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* @param m
* a Matrix
* @return a new matrix which is equal to the difference of this - m
*/
public Matrix subtractFrom(Matrix m) {
Matrix ret = new Matrix(numRows, numCols);
if (numRows != m.numRows || numCols != m.numCols) {
log.info("dimensions bad in substractFrom()");
return ret;
}
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numCols; c++) {
ret.elements[r][c] = this.elements[r][c] - m.elements[r][c];
}
}
return ret;
}
/**
* @return a String representation of the matrix
*/
@Override
public String toString() {
// TODO: do this better.
NumberFormat formatter = new DecimalFormat("#0.00000");
StringBuffer buf = new StringBuffer();
buf.append("[\n");
for (int r = 0; r < numRows; r++) {
buf.append(" [ ");
for (int c = 0; c < numCols; c++) {
buf.append(formatter.format(elements[r][c]));
buf.append(" ");
}
buf.append("]\n");
}
buf.append("]");
return buf.toString();
}
/**
* @return the transposed matrix with dimensions numCols x numRows
*/
public Matrix transpose() {
Matrix ret = new Matrix(numCols, numRows);
for (int r = 0; r < numRows; r++)
for (int c = 0; c < numCols; c++)
ret.elements[c][r] = elements[r][c];
return ret;
}
}