commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1362647 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java
Date Tue, 17 Jul 2012 21:10:34 GMT
Author: tn
Date: Tue Jul 17 21:10:34 2012
New Revision: 1362647

URL: http://svn.apache.org/viewvc?rev=1362647&view=rev
Log:
Added more random tests for SchurTransformer.

Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java?rev=1362647&r1=1362646&r2=1362647&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java
Tue Jul 17 21:10:34 2012
@@ -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();



Mime
View raw message