Added more random tests for SchurTransformer.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1362647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-07-17 21:10:34 +00:00
parent d8273182a6
commit 35c5d7143b
1 changed files with 70 additions and 18 deletions

View File

@ -17,6 +17,9 @@
package org.apache.commons.math3.linear;
import java.util.Random;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.junit.Test;
import org.junit.Assert;
@ -63,18 +66,6 @@ public class SchurTransformerTest {
checkAEqualPTPt(MatrixUtils.createRealMatrix(testRandom));
}
private void checkAEqualPTPt(RealMatrix matrix) {
SchurTransformer transformer = new SchurTransformer(matrix);
RealMatrix p = transformer.getP();
RealMatrix t = transformer.getT();
RealMatrix pT = transformer.getPT();
RealMatrix result = p.multiply(t).multiply(pT);
double norm = result.subtract(matrix).getNorm();
Assert.assertEquals(0, norm, 4.0e-14);
}
@Test
public void testPOrthogonal() {
checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getP());
@ -89,12 +80,6 @@ public class SchurTransformerTest {
checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getPT());
}
private void checkOrthogonal(RealMatrix m) {
RealMatrix mTm = m.transpose().multiply(m);
RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14);
}
@Test
public void testSchurForm() {
checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT());
@ -102,6 +87,73 @@ public class SchurTransformerTest {
checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getT());
}
@Test
public void testRandomData() {
for (int run = 0; run < 100; run++) {
Random r = new Random(System.currentTimeMillis());
// matrix size
int size = r.nextInt(20) + 4;
double[][] data = new double[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
data[i][j] = r.nextInt(100);
}
}
RealMatrix m = MatrixUtils.createRealMatrix(data);
RealMatrix s = checkAEqualPTPt(m);
checkSchurForm(s);
}
}
@Test
public void testRandomDataNormalDistribution() {
for (int run = 0; run < 100; run++) {
Random r = new Random(System.currentTimeMillis());
NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5);
// matrix size
int size = r.nextInt(20) + 4;
double[][] data = new double[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
data[i][j] = dist.sample();
}
}
RealMatrix m = MatrixUtils.createRealMatrix(data);
RealMatrix s = checkAEqualPTPt(m);
checkSchurForm(s);
}
}
///////////////////////////////////////////////////////////////////////////
// Test helpers
///////////////////////////////////////////////////////////////////////////
private RealMatrix checkAEqualPTPt(RealMatrix matrix) {
SchurTransformer transformer = new SchurTransformer(matrix);
RealMatrix p = transformer.getP();
RealMatrix t = transformer.getT();
RealMatrix pT = transformer.getPT();
RealMatrix result = p.multiply(t).multiply(pT);
double norm = result.subtract(matrix).getNorm();
Assert.assertEquals(0, norm, 1.0e-10);
return t;
}
private void checkOrthogonal(RealMatrix m) {
RealMatrix mTm = m.transpose().multiply(m);
RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension());
Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14);
}
private void checkSchurForm(final RealMatrix m) {
final int rows = m.getRowDimension();
final int cols = m.getColumnDimension();