1   /*
2    * $Id: AlgorithmsTest.java,v 1.2 2010/01/11 21:22:46 pah Exp $
3    * 
4    * Created on 9 Dec 2008 by Paul Harrison (paul.harrison@manchester.ac.uk)
5    * Copyright 2008 Astrogrid. All rights reserved.
6    *
7    * This software is published under the terms of the Astrogrid 
8    * Software License, a copy of which has been included 
9    * with this distribution in the LICENSE.txt file.  
10   *
11   */ 
12  
13  package org.astrogrid.matrix;
14  
15  import static org.junit.Assert.*;
16  
17  import java.io.IOException;
18  import java.io.InputStreamReader;
19  
20  import no.uib.cipr.matrix.AGDenseMatrix;
21  import no.uib.cipr.matrix.DenseVector;
22  import no.uib.cipr.matrix.Vector;
23  import no.uib.cipr.matrix.io.MatrixVectorReader;
24  
25  import org.junit.Before;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  
29  public class AlgorithmsTest extends AbstractMatrixTest {
30  
31      @BeforeClass
32      public static void setUpBeforeClass() throws Exception {
33      }
34  
35      @Before
36      public void setUp() throws Exception {
37      }
38  
39      @Test
40      public void testKmeans() throws IOException {
41          
42          Matrix input = new AGDenseMatrix(new MatrixVectorReader(new InputStreamReader(MatrixUtilsTest.class.getResourceAsStream("inputmatrix.txt"))));
43          input=input.sliceCol(0, 5);
44          Matrix result = Algorithms.centre_kmeans(input, 2, 5);
45          Matrix expected = new AGDenseMatrix(new MatrixVectorReader(new InputStreamReader(MatrixUtilsTest.class.getResourceAsStream("matrixkmeans.txt"))));
46          assertResult(result, expected, "kmeans");
47      }
48  
49      @Test
50      public void testRandperm() {
51          int[] a = Algorithms.randperm(20);
52          assertEquals("array length ",20, a.length);
53      }
54      
55      @Test
56      public void testDist2() {
57          Matrix a = new AGDenseMatrix(new double[][]{{1, 1, 1},{1, 2, 3},{1, 3, 6}});
58          Matrix b = new AGDenseMatrix(new double[][]{{2, 1, 3},{4, 4, 6}});
59          
60          Matrix result = Algorithms.dist2(a,b);
61          Matrix expected =  new AGDenseMatrix(new double[][]{{5,43},{2,22},{14,10}});
62          assertResult(result, expected, "dist2");
63         
64      }
65      
66      @Test
67      public void testMultinorm() {
68          Matrix x = new AGDenseMatrix(new double[][]{{1, 1, 1, 4},{1, 2, 3, 4},{1, 3, 6, 4}});
69          Vector mu = new DenseVector(new double[]{1,2,3});
70          Matrix cov = new AGDenseMatrix(new double[][]{{1,.4,.2},{0,1,.3},{.1,0.2,1.9}});
71          
72          Vector result = Algorithms.multinorm(x, mu, cov);
73          Vector expected = new DenseVector(new double[]{121.1596e-4,  469.1022e-4,   35.0938e-4 ,   2.3030e-4});
74          expected.add(-1.0, result);  
75          double norm = expected.norm(Vector.Norm.Infinity);
76          if(Math.abs(norm)> 1e-8){
77              fail("multinorm function does not return correct value");
78          }
79       }
80      @Test
81      public void testtMultinorm() {
82          Matrix x = new AGDenseMatrix(new double[][]{{1, 1, 1, 4},{1, 2, 3, 4},{1, 3, 6, 4}});
83          Vector mu = new DenseVector(new double[]{1,2,3});
84          Matrix cov = new AGDenseMatrix(new double[][]{{1,.4,.2},{0,1,.3},{.1,0.2,1.9}});
85          
86          Vector result = Algorithms.t_multinorm(x, mu, cov, 1.0);
87          Vector expected = new DenseVector(new double[]{54.4619e-4,  748.5788e-4,   19.5648e-4,    5.5315e-4,});
88          expected.add(-1.0, result);  
89          double norm = expected.norm(Vector.Norm.Infinity);
90          if(Math.abs(norm)> 1e-8){
91              fail("t_multinorm function does not return correct value");
92          }
93      }
94      
95      @Test
96      public void testDist3(){
97          Matrix x = new AGDenseMatrix(new double[][]{{1,2,3},{4,5,6},{7,8,9},{10,11,12},{1,1,1}});//
98          Matrix centres = new AGDenseMatrix(new double[][]{{1,1,1},{2,2,2}});
99          Matrix covars[] = new AGDenseMatrix[2];
100         covars[0] = new AGDenseMatrix(new double[][]{  {  0.2000 ,   0.3000 ,  -0.200},
101             {0.3000,    1.5000,    0.6000},
102             {-0.2000,    0.6000,    1.0000}});
103         covars[1] = new AGDenseMatrix(new double[][]{ {1.2000 ,   0.3000,    0.2000},
104             {0.3000 ,   0.5000,   -0.1000},
105             {0.2000 ,  -0.1000,    0.4000}});
106         
107         Matrix expectedFull = new AGDenseMatrix(new double[][]{    {46.6666666667,     6},
108             {2531.666666667,       84},
109             {8826.666666667 ,      270},
110             {18931.66666667  ,    564},
111             {0    ,   6}
112 
113 });
114         
115         Matrix results = Algorithms.dist3_free(x, centres, covars);
116         assertResult(results, expectedFull, "dist3_full");
117        //TODO test diag and spherical;
118         
119     }
120 
121    
122     }
123 
124 
125 /*
126  * $Log: AlgorithmsTest.java,v $
127  * Revision 1.2  2010/01/11 21:22:46  pah
128  * reasonable numerical stability and fidelity to MATLAB results achieved
129  *
130  * Revision 1.1  2009/09/07 16:06:11  pah
131  * initial transcription of the core
132  *
133  */