Skip to content

Commit 42e5af1

Browse files
committed
Added JUnit tests
1 parent c442e32 commit 42e5af1

16 files changed

+845
-303
lines changed

.classpath

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
<attributes>
1616
<attribute name="optional" value="true"/>
1717
<attribute name="maven.pomderived" value="true"/>
18+
<attribute name="test" value="true"/>
1819
</attributes>
1920
</classpathentry>
20-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
21+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
2122
<attributes>
22-
<attribute name="module" value="true"/>
23+
<attribute name="maven.pomderived" value="true"/>
2324
</attributes>
2425
</classpathentry>
2526
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">

.settings/org.eclipse.jdt.core.prefs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ org.eclipse.jdt.core.compiler.annotation.nonnullbydefault.secondary=
88
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
99
org.eclipse.jdt.core.compiler.annotation.nullable.secondary=
1010
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
11+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
12+
org.eclipse.jdt.core.compiler.compliance=11
1113
org.eclipse.jdt.core.compiler.problem.APILeak=warning
1214
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
1315
org.eclipse.jdt.core.compiler.problem.autoboxing=warning
@@ -98,3 +100,5 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
98100
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
99101
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
100102
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
103+
org.eclipse.jdt.core.compiler.release=disabled
104+
org.eclipse.jdt.core.compiler.source=11

JamaTestMatrix.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
2.0547E-01 9.5168E-01 2.4994E-01 7.8568E-01
3+
2.5971E-01 8.1245E-01 8.2570E-02 1.5420E-01
4+
8.8487E-01 1.2853E-01 5.0150E-01 3.6827E-02
5+

TMPMATRIX.serial

0 Bytes
Binary file not shown.

src/main/java/jama2/CholeskyDecomposition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @version 2.0
2020
* @see <a href="http://tweimer.github.io/java-matrix/">java-matrix</a>
2121
*/
22-
public class CholeskyDecomposition implements IMatrix, Serializable {
22+
public class CholeskyDecomposition implements FunctionalMatrix, Serializable {
2323
/**
2424
* For the Serializable interface.
2525
*/

src/main/java/jama2/DiagonalMatrix.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import java.util.Arrays;
55
import java.util.function.DoubleBinaryOperator;
66
import java.util.function.DoubleUnaryOperator;
7+
import java.util.function.IntToDoubleFunction;
78

89
/**
910
* This class represents a diagonal matrix of a fixed square size.
1011
*
1112
* @author Tobias Weimer
1213
*
1314
*/
14-
public class DiagonalMatrix implements Serializable, Cloneable, IMatrix {
15+
public class DiagonalMatrix implements Serializable, Cloneable, FunctionalMatrix {
1516
private static final long serialVersionUID = 1L;
1617

1718
/** array of the diagonal elements */
@@ -31,8 +32,21 @@ public DiagonalMatrix(final int size) {
3132
*
3233
* @param d Another diagonal Matrix
3334
*/
34-
public DiagonalMatrix(final DiagonalMatrix D) {
35-
diag = Arrays.copyOf(D.diag, D.diag.length);
35+
public DiagonalMatrix(final DiagonalMatrix d) {
36+
diag = Arrays.copyOf(d.diag, d.diag.length);
37+
}
38+
39+
/**
40+
* Constructs a diagonal matrix and initializes it with the given function
41+
*
42+
* @param size Size of the matrix
43+
* @param func Function to initialize the diagonal matrix, starting at 0.
44+
*/
45+
public DiagonalMatrix(final int size, final IntToDoubleFunction func) {
46+
this(size);
47+
for (var i = 0; i < size; i++) {
48+
set(i, func.applyAsDouble(i));
49+
}
3650
}
3751

3852
/**
@@ -42,6 +56,19 @@ public DiagonalMatrix(final DiagonalMatrix D) {
4256
DiagonalMatrix(final double[] diag) {
4357
this.diag = diag;
4458
}
59+
60+
/**
61+
* Constructs the identity Matrix
62+
*
63+
* @param size Size of the identity matrix
64+
*/
65+
public static DiagonalMatrix identity(final int size) {
66+
final var d = new DiagonalMatrix(size);
67+
for (var i = 0; i < size; i++) {
68+
d.set(i, 1D);
69+
}
70+
return d;
71+
}
4572

4673
/**
4774
* Creates a diagonal Matrix.
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package jama2;
22

33
/**
4-
* This functional interface represent a {@link Matrix} and can be used to build a Matrix.
4+
* This functional interface represent a {@link Matrix}.
5+
* This is the base interface for all implementations.
6+
*
7+
*
58
* @author The MathWorks, Inc. and the National Institute of Standards and
69
* Technology.
710
* @version 2.0
811
* @see jama2.Matrix#Matrix(int, int, IMatrix)
912
* @see <a href="http://tweimer.github.io/java-matrix/">java-matrix</a>
1013
*/
1114
@FunctionalInterface
12-
public interface IMatrix {
15+
public interface FunctionalMatrix {
1316
/**
1417
* Get a single element.
1518
* @param i row index
1619
* @param j column index
1720
* @return value
18-
* @see Matrix#Matrix(int, int, IMatrix)
21+
* @see Matrix#Matrix(int, int, FunctionalMatrix)
1922
*/
2023
double get(int i, int j);
2124
}

src/main/java/jama2/LUDecomposition.java

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* @version 2.0
2424
* @see <a href="http://tweimer.github.io/java-matrix/">java-matrix</a>
2525
*/
26-
public class LUDecomposition implements ISolver, IMatrix, Serializable {
26+
public class LUDecomposition implements ISolver, FunctionalMatrix, Serializable {
2727
/**
2828
* For the Serializeable interface
2929
*/
@@ -72,57 +72,57 @@ public class LUDecomposition implements ISolver, IMatrix, Serializable {
7272
*/
7373
LUDecomposition(final Matrix A, final boolean linpackflag) {
7474
// Initialize.
75-
this.LU = A.getArrayCopy();
76-
this.m = A.getRowDimension();
77-
this.n = A.getColumnDimension();
75+
LU = A.getArrayCopy();
76+
m = A.getRowDimension();
77+
n = A.getColumnDimension();
78+
piv = new int[this.m];
7879

79-
this.piv = new int[this.m];
80-
for (var i = 1; i < this.m; i++) {
81-
this.piv[i] = i;
80+
for (var i = 1; i < m; i++) {
81+
piv[i] = i;
8282
}
8383

8484
var pivsign = 1;
8585
if (linpackflag) {
8686
// Main loop.
87-
for (var k = 0; k < this.n; k++) {
87+
for (var k = 0; k < n; k++) {
8888
// Find pivot.
8989
var p = k;
90-
for (var i = k + 1; i < this.m; i++) {
91-
if (Math.abs(this.LU[i][k]) > Math.abs(this.LU[p][k])) {
90+
for (var i = k + 1; i < m; i++) {
91+
if (Math.abs(LU[i][k]) > Math.abs(LU[p][k])) {
9292
p = i;
9393
}
9494
}
9595

9696
// Exchange if necessary.
9797
if (p > k) {
98-
for (var j = 0; j < this.n; j++) {
99-
final var t = this.LU[p][j];
100-
this.LU[p][j] = this.LU[k][j];
101-
this.LU[k][j] = t;
98+
for (var j = 0; j < n; j++) {
99+
final var t = LU[p][j];
100+
LU[p][j] = LU[k][j];
101+
LU[k][j] = t;
102102
}
103103

104104
// Swap piv[p] and piv[k]
105-
final var t = this.piv[p];
106-
this.piv[p] = this.piv[k];
107-
this.piv[k] = t;
105+
final var t = piv[p];
106+
piv[p] = piv[k];
107+
piv[k] = t;
108108

109109
// alternate pivsign
110110
pivsign = -pivsign;
111111
}
112112

113113
// Compute multipliers and eliminate k-th column.
114-
if (this.LU[k][k] != 0D) {
115-
for (var i = k + 1; i < this.m; i++) {
116-
this.LU[i][k] /= this.LU[k][k];
117-
for (var j = k + 1; j < this.n; j++) {
118-
this.LU[i][j] -= this.LU[i][k] * this.LU[k][j];
114+
if (LU[k][k] != 0D) {
115+
for (var i = k + 1; i < m; i++) {
116+
LU[i][k] /= LU[k][k];
117+
for (var j = k + 1; j < n; j++) {
118+
LU[i][j] -= LU[i][k] * LU[k][j];
119119
}
120120
}
121121
}
122122
}
123123
} else {
124124
// Outer loop.
125-
for (var j = 0; j < this.n; j++) {
125+
for (var j = 0; j < n; j++) {
126126
final var LUcolj = new double[this.m];
127127

128128
// Make a copy of the j-th column to localize references.
@@ -131,8 +131,8 @@ public class LUDecomposition implements ISolver, IMatrix, Serializable {
131131
}
132132

133133
// Apply previous transformations.
134-
for (var i = 0; i < this.m; i++) {
135-
final var LUrowi = this.LU[i];
134+
for (var i = 0; i < m; i++) {
135+
final var LUrowi = LU[i];
136136

137137
// Most of the time is spent in the following dot product.
138138
final var kmax = Math.min(i, j);
@@ -145,31 +145,31 @@ public class LUDecomposition implements ISolver, IMatrix, Serializable {
145145

146146
// Find pivot and exchange if necessary.
147147
var p = j;
148-
for (var i = j + 1; i < this.m; i++) {
148+
for (var i = j + 1; i < m; i++) {
149149
if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) {
150150
p = i;
151151
}
152152
}
153153

154154
if (p > j) {
155155
// Swap this.LU[p][k] and this.LU[j][k]
156-
final var row_p = this.LU[p];
157-
this.LU[p] = this.LU[j];
158-
this.LU[j] = row_p;
156+
final var row_p = LU[p];
157+
LU[p] = LU[j];
158+
LU[j] = row_p;
159159

160160
// swap piv[p] and piv[j]
161-
final var k = this.piv[p];
162-
this.piv[p] = this.piv[j];
163-
this.piv[j] = k;
161+
final var k = piv[p];
162+
piv[p] = piv[j];
163+
piv[j] = k;
164164

165165
// alternate pivsign
166166
pivsign = -pivsign;
167167
}
168168

169169
// Compute multipliers.
170-
if ((j < this.m) && (this.LU[j][j] != 0D)) {
171-
for (var i = j + 1; i < this.m; i++) {
172-
this.LU[i][j] /= this.LU[j][j];
170+
if ((j < m) && (LU[j][j] != 0D)) {
171+
for (var i = j + 1; i < m; i++) {
172+
LU[i][j] /= LU[j][j];
173173
}
174174
}
175175
}
@@ -199,15 +199,15 @@ public class LUDecomposition implements ISolver, IMatrix, Serializable {
199199
* matrix is singular.
200200
*/
201201
public double det() {
202-
if (this.m == this.n) {
202+
if (m == n) {
203203
// go through the LU decomposition
204204
// For A = P*L*U, det(A)=det(P)*det(L)*det(U), where
205205
// det(P) is pivsign
206206
// det(L) is equal to 1
207207
// det(U) is the product of the diagonal entries
208208
// So lets start with pivsign and multiply with the diagonal entries.
209-
double det = this.pivsign;
210-
for (int j = 0; j < this.n; j++) {
209+
double det = pivsign;
210+
for (var j = 0; j < this.n; j++) {
211211
det *= this.LU[j][j];
212212
}
213213
return det;

0 commit comments

Comments
 (0)