1 package JSci.tests; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.Map ; 5 import java.util.HashMap ; 6 import junit.framework.*; 7 import JSci.GlobalSettings; 8 import JSci.maths.*; 9 import JSci.maths.matrices.*; 10 import JSci.maths.vectors.*; 11 import JSci.maths.polynomials.*; 12 13 17 public class MatrixTest extends junitx.extensions.EqualsHashCodeTestCase { 18 private final int N = 10; 19 private final int M = 10; 20 private Constructor constructor; 21 private double array[][]; 22 private double array2[][]; 23 24 public static void main(String arg[]) { 25 junit.textui.TestRunner.run(suite()); 26 } 27 public static Test suite() { 28 Class [] classes = new Class [] { 29 DoubleMatrix.class, DoubleTridiagonalMatrix.class, 30 DoubleDiagonalMatrix.class, DoubleSparseMatrix.class 31 }; 32 TestSuite suite = new TestSuite(MatrixTest.class.toString()); 33 for(int i=0; i<classes.length; i++) { 34 Map properties = new HashMap (); 35 properties.put("test.matrix.class", classes[i]); 36 suite.addTest(new junit.extensions.RepeatedTest(new TestProperties(new TestSuite(MatrixTest.class, classes[i].toString()), properties), 5)); 37 } 38 return suite; 39 } 40 public MatrixTest(String name) { 41 super(name); 42 } 43 protected void setUp() throws Exception { 44 GlobalSettings.ZERO_TOL=1.0e-6; 45 array=new double[N][M]; 46 array2=new double[N][M]; 47 Class matrixClass = (Class ) TestProperties.getProperties().get("test.matrix.class"); 48 constructor = matrixClass.getConstructor(new Class [] {double[][].class}); 49 if(DiagonalMatrix.class.isAssignableFrom(matrixClass)) 50 setUpDiagonal(); 51 else if(TridiagonalMatrix.class.isAssignableFrom(matrixClass)) 52 setUpTridiagonal(); 53 else 54 setUpRectangular(); 55 super.setUp(); 56 } 57 private void setUpRectangular() { 58 for(int i=0;i<N;i++) { 59 for(int j=0;j<M;j++) { 60 array[i][j]=ExtraMath.random(-1.0, 1.0); 61 array2[i][j]=ExtraMath.random(-1.0, 1.0); 62 } 63 } 64 } 65 private void setUpTridiagonal() { 66 for(int i=0;i<N;i++) { 67 for(int j=0;j<M;j++) { 68 if(j>=i-1 && j<=i+1) { 69 array[i][j]=ExtraMath.random(-1.0, 1.0); 70 array2[i][j]=ExtraMath.random(-1.0, 1.0); 71 } 72 } 73 } 74 } 75 private void setUpDiagonal() { 76 for(int i=0;i<N;i++) { 77 array[i][i]=ExtraMath.random(-1.0, 1.0); 78 array2[i][i]=ExtraMath.random(-1.0, 1.0); 79 } 80 } 81 protected Object createInstance() { 82 try { 83 return constructor.newInstance(new Object [] {array}); 84 } catch(Exception e) { 85 throw new RuntimeException (e); 86 } 87 } 88 protected Object createNotEqualInstance() { 89 try { 90 return constructor.newInstance(new Object [] {array2}); 91 } catch(Exception e) { 92 throw new RuntimeException (e); 93 } 94 } 95 public void testConstructor() { 96 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 97 assertEquals(N, mat.rows()); 98 assertEquals(M, mat.columns()); 99 for(int i=0;i<mat.rows();i++) { 100 for(int j=0;j<mat.columns();j++) 101 assertEquals(array[i][j], mat.getElement(i,j), GlobalSettings.ZERO_TOL); 102 } 103 } 104 public void testSetGet() { 105 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 106 for(int i=0;i<mat.rows();i++) { 107 for(int j=0;j<mat.columns();j++) { 108 try { 109 mat.setElement(i,j,-1.0); 110 assertEquals(-1.0, mat.getElement(i,j), GlobalSettings.ZERO_TOL); 111 mat.setElement(i,j,array[i][j]); 112 } catch(MatrixDimensionException e) { 113 } 115 } 116 } 117 } 118 public void testZeroTolerance() { 119 AbstractDoubleMatrix delta = (AbstractDoubleMatrix) createInstance(); 120 delta = delta.scalarMultiply(Math.random()*GlobalSettings.ZERO_TOL/delta.frobeniusNorm()); 121 AbstractDoubleMatrix m = (AbstractDoubleMatrix) createNotEqualInstance(); 122 assertTrue("m == m + delta", m.equals(m.add(delta))); 123 } 124 public void testAdd() { 125 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 126 AbstractDoubleMatrix ans=mat.add(mat); 127 for(int i=0;i<ans.rows();i++) { 128 for(int j=0;j<ans.columns();j++) { 129 String msg = mat.getClass().toString()+": ("+i+","+j+")"; 130 assertEquals(msg, array[i][j]+array[i][j], ans.getElement(i,j), GlobalSettings.ZERO_TOL); 131 } 132 } 133 } 134 public void testSubtract() { 135 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 136 AbstractDoubleMatrix ans=mat.subtract(mat); 137 for(int i=0;i<ans.rows();i++) { 138 for(int j=0;j<ans.columns();j++) { 139 String msg = mat.getClass().toString()+": ("+i+","+j+")"; 140 assertEquals(msg, array[i][j]-array[i][j], ans.getElement(i,j), GlobalSettings.ZERO_TOL); 141 } 142 } 143 } 144 public void testMultiply() { 145 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 146 AbstractDoubleMatrix ans=(AbstractDoubleMatrix)mat.multiply(mat.transpose()); 147 for(int i=0;i<ans.rows();i++) { 148 for(int j=0;j<ans.columns();j++) { 149 double sum=0.0; 150 for(int k=0;k<mat.columns();k++) 151 sum+=array[i][k]*array[j][k]; 152 String msg = mat.getClass().toString()+": ("+i+","+j+")"; 153 assertEquals(msg, sum, ans.getElement(i,j), GlobalSettings.ZERO_TOL); 154 } 155 } 156 } 157 public void testMapElements() { 158 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 159 RealPolynomial map = new RealPolynomial(new double[] {2.0, 1.0}); 160 AbstractDoubleMatrix ans = mat.mapElements(map); 161 for(int i=0;i<ans.rows();i++) { 162 for(int j=0;j<ans.columns();j++) { 163 String msg = mat.getClass().toString()+": ("+i+","+j+")"; 164 assertEquals(msg, map.map(array[i][j]), ans.getElement(i,j), GlobalSettings.ZERO_TOL); 165 } 166 } 167 } 168 public void testGMRes() { 169 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance(); 170 AbstractDoubleVector y = JSci.util.VectorToolkit.randomVector(mat.rows()); 171 try { 172 AbstractDoubleVector x = LinearMath.solveGMRes(mat, y, 100, GlobalSettings.ZERO_TOL); 173 assertEquals(y, mat.multiply(x)); 174 } catch(MaximumIterationsExceededException e) { 175 throw new RuntimeException (e); 176 } 177 } 178 } 179 180 | Popular Tags |