blob: f9395ea6618142a1bf22a238a3b4dbe01c1efab0 (
plain)
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
|
package bezier.geom;
/**
* Matrix class.
*
* @author bjculkin
*
*/
public class Matrix {
/**
* Matrix values.
*/
public final double[][] mat;
/**
* Create a new matrix, with the specified values.
*
* @param mat
* The matrix to use.
*/
public Matrix(double[][] mat) {
this.mat = mat;
}
/**
* Add two matrices together.
*
* @param m
* The matrix to add.
* @return An added matrix.
*/
public Matrix add(Matrix m) {
return add(m.mat);
}
/**
* Multiply this matrix by a scalar vector.
*
* @param vec
* The vector to multiply by.
* @return The resulting vector.
*/
public double[] scalarMultiply(double[] vec) {
double[] ret = new double[vec.length];
for (int i = 0; i < mat[0].length; i++) {
for (int j = 0; j < mat.length; j++) {
ret[i] += mat[i][j] * vec[j];
}
}
return ret;
}
/**
* Add a matrix to this one.
*
* @param matr
* The matrix to add.
* @return The resulting matrix.
*/
public Matrix add(double[][] matr) {
double[][] ret = new double[mat.length][mat[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
ret[i][j] = mat[i][j] + matr[i][j];
}
}
return new Matrix(ret);
}
/**
* Multiply this matrix by another.
*
* @param m
* The matrix to multiply by
* @return The multiplied matrix.
*/
public Matrix multiply(Matrix m) {
return multiply(m.mat);
}
/**
* Multiply this matrix by another.
*
* @param matr
* The matrix to multiply by
* @return The multiplied matrix.
*/
public Matrix multiply(double[][] matr) {
double[][] ret = new double[mat.length][matr[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < matr[0].length; j++) {
for (int k = 0; k < matr.length; k++) {
ret[i][j] += mat[i][k] * matr[k][j];
}
}
}
return new Matrix(ret);
}
}
|