001    package calhoun.analysis.crf.test;
002    
003    import org.apache.commons.logging.Log;
004    import org.apache.commons.logging.LogFactory;
005    
006    import calhoun.util.AbstractTestCase;
007    import calhoun.util.ColtUtil;
008    import cern.colt.matrix.DoubleMatrix2D;
009    import cern.colt.matrix.impl.DenseDoubleMatrix2D;
010    
011    public class MatrixOperationsTest extends AbstractTestCase {
012            private static final Log log = LogFactory.getLog(CRFIOTest.class);
013            boolean debug = log.isDebugEnabled();
014    
015            public void testMatrixExponential() throws Exception {
016                    //S = [-2.050871e-03, 5.984135e-04, 9.348390e-04, 5.176184e-04; 7.120564e-04, -2.164514e-03, 9.348390e-04, 5.176184e-04; 7.120564e-04, 5.984135e-04, -1.828088e-03, 5.176184e-04; 7.120564e-04, 5.984135e-04, 9.348390e-04, -2.245309e-03]
017    
018                            System.out.println("Hello world");
019    
020                    
021                    double[][] X = new double[4][4];
022                    X[0][0]=-2.050871e-03;   X[0][1]= 5.984135e-04;   X[0][2]= 9.348390e-04;   X[0][3]= 5.176184e-04;
023                    X[1][0]= 7.120564e-04;   X[1][1]=-2.164514e-03;   X[1][2]= 9.348390e-04;   X[1][3]= 5.176184e-04;
024                    X[2][0]= 7.120564e-04;   X[2][1]= 5.984135e-04;   X[2][2]=-1.828088e-03;   X[2][3]= 5.176184e-04;
025                    X[3][0]= 7.120564e-04;   X[3][1]= 5.984135e-04;   X[3][2]= 9.348390e-04;   X[3][3]=-2.245309e-03;
026    
027                    DoubleMatrix2D S = new DenseDoubleMatrix2D(4,4);
028                    S.assign(X);
029                    System.out.println("The matrix S is: ");
030                    System.out.println(ColtUtil.format(S));
031                    
032                    DoubleMatrix2D T = new DenseDoubleMatrix2D(4,4);
033                    ColtUtil.exponentiate_real_matrix(S,T,200);
034                    System.out.println("The matrix T (the computed exponential of S) is: ");
035                    System.out.println(ColtUtil.format(T));         
036                    
037                    
038                    // Below we paste in the correct answer as computed by Matlab U = expm(S)
039                    double[][] Y = new double[4][4];
040                    Y[0][0]=0.9980;  Y[0][1]=0.0006;  Y[0][2]=0.0009;  Y[0][3]=0.0005;
041                    Y[1][0]=0.0007;  Y[1][1]=0.9978;  Y[1][2]=0.0009;  Y[1][3]=0.0005;
042                    Y[2][0]=0.0007;  Y[2][1]=0.0006;  Y[2][2]=0.9982;  Y[2][3]=0.0005;
043                    Y[3][0]=0.0007;  Y[3][1]=0.0006;  Y[3][2]=0.0009;  Y[3][3]=0.9978;
044                    DoubleMatrix2D U = new DenseDoubleMatrix2D(4,4);                
045                    U.assign(Y);
046                    System.out.println("The matrix U (the computed exponential of S using Matlab's U = expm(S)) is: ");
047                    System.out.println(ColtUtil.format(U));                 
048                    
049                    for (int i=0; i<4; i++) {
050                            for (int j=0; j<4; j++) {
051                                    assertEquals(T.getQuick(i,j),U.getQuick(i,j),0.001);
052                            }
053                    }
054                
055            }
056            
057    
058    }