Added more random data tests for HessenbergTransformer.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1362645 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2012-07-17 21:07:08 +00:00
parent abd69cafa6
commit d8273182a6
1 changed files with 84 additions and 23 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;
@ -62,15 +65,6 @@ public class HessenbergTransformerTest {
checkAEqualPHPt(MatrixUtils.createRealMatrix(testRandom));
}
private void checkAEqualPHPt(RealMatrix matrix) {
HessenbergTransformer transformer = new HessenbergTransformer(matrix);
RealMatrix p = transformer.getP();
RealMatrix pT = transformer.getPT();
RealMatrix h = transformer.getH();
double norm = p.multiply(h).multiply(pT).subtract(matrix).getNorm();
Assert.assertEquals(0, norm, 4.0e-14);
}
@Test
public void testPOrthogonal() {
checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare5)).getP());
@ -83,27 +77,52 @@ public class HessenbergTransformerTest {
checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare3)).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 testHessenbergForm() {
checkHessenbergForm(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare5)).getH());
checkHessenbergForm(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare3)).getH());
}
private void checkHessenbergForm(RealMatrix m) {
final int rows = m.getRowDimension();
final int cols = m.getColumnDimension();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i > j + 1) {
Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
@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 h = checkAEqualPHPt(m);
checkHessenbergForm(h);
}
}
@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 h = checkAEqualPHPt(m);
checkHessenbergForm(h);
}
}
@ -141,8 +160,50 @@ public class HessenbergTransformerTest {
});
}
private void checkMatricesValues(double[][] matrix, double[][] pRef, double[][] hRef) {
///////////////////////////////////////////////////////////////////////////
// Test helpers
///////////////////////////////////////////////////////////////////////////
private RealMatrix checkAEqualPHPt(RealMatrix matrix) {
HessenbergTransformer transformer = new HessenbergTransformer(matrix);
RealMatrix p = transformer.getP();
RealMatrix pT = transformer.getPT();
RealMatrix h = transformer.getH();
RealMatrix result = p.multiply(h).multiply(pT);
double norm = result.subtract(matrix).getNorm();
Assert.assertEquals(0, norm, 1.0e-10);
for (int i = 0; i < matrix.getRowDimension(); ++i) {
for (int j = 0; j < matrix.getColumnDimension(); ++j) {
if (i > j + 1) {
Assert.assertEquals(matrix.getEntry(i, j), result.getEntry(i, j), 1.0e-12);
}
}
}
return transformer.getH();
}
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 checkHessenbergForm(RealMatrix m) {
final int rows = m.getRowDimension();
final int cols = m.getColumnDimension();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (i > j + 1) {
Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16);
}
}
}
}
private void checkMatricesValues(double[][] matrix, double[][] pRef, double[][] hRef) {
HessenbergTransformer transformer =
new HessenbergTransformer(MatrixUtils.createRealMatrix(matrix));