First develop commit. Adding all existing files.
This commit is contained in:
40
test/math/Eigenvalues.java
Normal file
40
test/math/Eigenvalues.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package math;
|
||||
|
||||
import math.matrix.EigenvalueDecomposition;
|
||||
import math.matrix.Matrix;
|
||||
import exception.IllegalDimensionException;
|
||||
|
||||
/**
|
||||
* Test client for computing eigenvalues and eigenvectors of a real
|
||||
* symmetric matrix A = V D V^T.
|
||||
*/
|
||||
public class Eigenvalues {
|
||||
public static void main(String[] args) throws IllegalDimensionException {
|
||||
int N = 5;
|
||||
|
||||
// create a symmetric positive definite matrix
|
||||
Matrix A = Matrix.random(N, N);
|
||||
A = A.transpose().times(A);
|
||||
|
||||
// compute the spectral decomposition
|
||||
EigenvalueDecomposition e = A.eig();
|
||||
Matrix V = e.getV();
|
||||
Matrix D = e.getD();
|
||||
|
||||
System.out.print("A =");
|
||||
A.print(9, 6);
|
||||
System.out.print("D =");
|
||||
D.print(9, 6);
|
||||
System.out.print("V =");
|
||||
V.print(9, 6);
|
||||
|
||||
// check that V is orthogonal
|
||||
System.out.print("||V * V^T - I|| = ");
|
||||
System.out.println(V.times(V.transpose()).minus(Matrix.identity(N, N)).normInf());
|
||||
|
||||
// check that A V = D V
|
||||
System.out.print("||AV - DV|| = ");
|
||||
System.out.println(A.times(V).minus(V.times(D)).normInf());
|
||||
}
|
||||
|
||||
}
|
||||
37
test/math/MatrixTest.java
Normal file
37
test/math/MatrixTest.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package math;
|
||||
|
||||
import math.matrix.Matrix;
|
||||
import exception.IllegalDimensionException;
|
||||
|
||||
public class MatrixTest {
|
||||
public static void main(String[] args) throws IllegalDimensionException {
|
||||
Matrix A = new Matrix(new double[][] { { 0, 1, 1 },
|
||||
{ 2, 4, -2 },
|
||||
{ 0, 3, 15 } });
|
||||
|
||||
Matrix b = new Matrix(new double[][] { { 4 },
|
||||
{ 2 },
|
||||
{ 36 } });
|
||||
Matrix x = A.solve(b);
|
||||
Matrix residual = A.times(x).minus(b);
|
||||
double rnorm = residual.normInf();
|
||||
|
||||
System.out.println("A");
|
||||
A.print(9, 6); // printf("%9.6f");
|
||||
|
||||
System.out.println("b");
|
||||
b.print(9, 6);
|
||||
|
||||
System.out.println("x");
|
||||
x.print(9, 6);
|
||||
System.out.println(x);
|
||||
|
||||
System.out.println("residual");
|
||||
residual.print(9, 6);
|
||||
|
||||
System.out.println("rnorm");
|
||||
System.out.println(rnorm);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
43
test/math/SVD.java
Normal file
43
test/math/SVD.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package math;
|
||||
|
||||
import math.matrix.Matrix;
|
||||
import math.matrix.SingularValueDecomposition;
|
||||
import exception.IllegalDimensionException;
|
||||
|
||||
/**
|
||||
* Test client for computing singular values of a matrix.
|
||||
*/
|
||||
public class SVD {
|
||||
public static void main(String[] args) throws IllegalDimensionException {
|
||||
|
||||
// create M-by-N matrix that doesn't have full rank
|
||||
int M = 8, N = 5;
|
||||
Matrix B = Matrix.random(5, 3);
|
||||
Matrix A = Matrix.random(M, N).times(B).times(B.transpose());
|
||||
System.out.print("A = ");
|
||||
A.print(9, 6);
|
||||
|
||||
// compute the singular vallue decomposition
|
||||
System.out.println("A = U S V^T");
|
||||
System.out.println();
|
||||
SingularValueDecomposition s = A.svd();
|
||||
System.out.print("U = ");
|
||||
Matrix U = s.getU();
|
||||
U.print(9, 6);
|
||||
System.out.print("Sigma = ");
|
||||
Matrix S = s.getS();
|
||||
S.print(9, 6);
|
||||
System.out.print("V = ");
|
||||
Matrix V = s.getV();
|
||||
V.print(9, 6);
|
||||
System.out.println("rank = " + s.rank());
|
||||
System.out.println("condition number = " + s.cond());
|
||||
System.out.println("2-norm = " + s.norm2());
|
||||
|
||||
// print out singular values
|
||||
System.out.print("singular values = ");
|
||||
Matrix svalues = new Matrix(s.getSingularValues(), 1);
|
||||
svalues.print(9, 6);
|
||||
}
|
||||
|
||||
}
|
||||
1099
test/math/TestMatrix.java
Normal file
1099
test/math/TestMatrix.java
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user