001        package com.croftsoft.core.math.matrix;
002    
003        /***********************************************************************
004        * Matrix test methods.
005        * 
006        * @version
007        *   $Id: MatrixTest.java,v 1.9 2008/05/03 02:48:38 croft Exp $
008        * @since
009        *   2008-04-19
010        * @author
011        *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
012        ***********************************************************************/
013    
014        public final class  MatrixTest
015        ////////////////////////////////////////////////////////////////////////
016        ////////////////////////////////////////////////////////////////////////
017        {
018          
019        public static void  main ( final String [ ]  args )
020        ////////////////////////////////////////////////////////////////////////
021        {
022          System.out.println ( test ( ) );
023        }
024        
025        public static boolean  test ( )
026        ////////////////////////////////////////////////////////////////////////
027        {
028          System.out.println (
029            Matrix3x3Lib.createRotationMatrixX (   0 ).toString ( ) );
030          
031          System.out.println ( 
032            Matrix3x3Lib.createRotationMatrixY ( 180 ).toString ( ) );
033          
034          System.out.println ( 
035            Matrix3x3Lib.createRotationMatrixZ ( 270 ).toString ( ) );
036          
037          System.out.println (
038            Matrix3x3Lib.createRotationMatrix ( 0, 180, 270 ).toString ( ) );
039          
040          final MatrixMut  matrixB = new MatrixImp ( 2, 2 );
041          
042          matrixB.set ( 0, 0, 0 );
043          
044          matrixB.set ( 0, 1, 1 );
045          
046          matrixB.set ( 1, 0, -2 );
047          
048          matrixB.set ( 1, 1, 5 );
049          
050          final MatrixMut  matrixD = new MatrixImp (
051            new double [ ] [ ] {
052              { -1, 0, 3 },
053              {  5, 7, 2 } } );
054          
055          System.out.println ( matrixB );
056          
057          System.out.println ( matrixD );
058          
059          System.out.println ( matrixB.multiply ( matrixD ) );
060          
061          final Matrix3x3  rotationMatrix
062            = Matrix3x3Lib.createRotationMatrix ( 90, 90, 90 );
063          
064          final double [ ]  eulerAngles
065            = Matrix3x3Lib.toEulerAngles ( rotationMatrix );
066          
067          for ( int  i = 0; i < 3; i++ )
068          {
069            System.out.print ( eulerAngles [ i ] + ", " );
070          }
071          
072          System.out.println ( "" );
073          
074          return testTranspose ( )
075            && testRotation (  90,   90,  90 )
076            && testRotation (   0,    0,   0 )
077            && testRotation ( 180,    0,   0 )
078            && testRotation ( 360,  180, 270 )
079            && testRotation ( -90, -180,   0 );
080        }
081        
082        public static boolean  testRotation (
083          final double  degreesX,
084          final double  degreesY,
085          final double  degreesZ )
086        ////////////////////////////////////////////////////////////////////////
087        {
088          final double  cy = Math.cos ( Math.toRadians ( degreesY ) );
089          
090          final double  sy = Math.sin ( Math.toRadians ( degreesY ) );
091          
092          final double  cz = Math.cos ( Math.toRadians ( degreesZ ) );
093          
094          final double  sz = Math.sin ( Math.toRadians ( degreesZ ) );
095          
096          final Matrix3x3Mut  rotationZY0 = new Matrix3x3Imp (
097            new double [ ] [ ] {
098              {  cy * cz, -sz, sy * cz },
099              {  cy * sz,  cz, sy * sz },
100              {      -sy,   0,      cy } } );
101          
102          final Matrix  rotationZY1
103            = Matrix3x3Lib.createRotationMatrixZ ( degreesZ ).multiply (
104              Matrix3x3Lib.createRotationMatrixY ( degreesY ) );
105          
106          if ( !rotationZY0.matches ( rotationZY1 ) )
107          {
108            System.out.println ( "rotation test 1 failed" );
109            
110            return false;
111          }
112          
113          final Matrix3x3Mut  rotationMatrix
114            = Matrix3x3Lib.createRotationMatrixZ ( degreesZ );
115    
116          rotationMatrix.multiplyToSelf (
117            Matrix3x3Lib.createRotationMatrixY ( degreesY ) );
118    
119          rotationMatrix.multiplyToSelf (
120            Matrix3x3Lib.createRotationMatrixX ( degreesX ) );
121          
122          System.out.print ( "expected ...:  " );
123          
124          System.out.println ( rotationMatrix );
125          
126          System.out.print ( "actual .....:  " );
127          
128          final Matrix3x3  actualRotationMatrix
129            = Matrix3x3Lib.createRotationMatrix (
130              degreesX,
131              degreesY,
132              degreesZ );
133          
134          System.out.println ( actualRotationMatrix );
135          
136          if ( !rotationMatrix.matches ( actualRotationMatrix ) )
137          {
138            System.out.println ( "rotation test 2 failed" );
139            
140            return false;
141          }
142          
143          return true;
144        }
145        
146        public static boolean  testTranspose ( )
147        ////////////////////////////////////////////////////////////////////////
148        {
149          final Matrix  matrix3x4 = new MatrixImp (
150            new double [ ] [ ] {
151              { 1,  2,  3,  4 },
152              { 5,  6,  7,  8 },
153              { 9, 10, 11, 12 } } );
154          
155          final Matrix  matrix4x3 = new MatrixImp (
156            new double [ ] [ ] {
157              { 1, 5,  9 },
158              { 2, 6, 10 },
159              { 3, 7, 11 },
160              { 4, 8, 12 } } );
161          
162          if ( !matrix3x4.matches ( matrix4x3.transpose ( ) ) )
163          {
164            System.out.println ( "transpose test 1 failed" );
165            
166            return false;
167          }
168          
169          final MatrixMut  matrix4x4Mut = new MatrixImp (
170            new double [ ] [ ] {
171              {  1,  2,  3,  4 },
172              {  5,  6,  7,  8 },
173              {  9, 10, 11, 12 },
174              { 13, 14, 15, 16 } } );
175          
176          final Matrix  matrix4x4 = new MatrixImp (
177            new double [ ] [ ] {
178              { 1, 5,  9, 13 },
179              { 2, 6, 10, 14 },
180              { 3, 7, 11, 15 },
181              { 4, 8, 12, 16 } } );
182          
183          if ( !matrix4x4Mut.transpose ( ).matches ( matrix4x4 ) )
184          {
185            System.out.println ( "transpose test 2 failed" );
186            
187            return false;
188          }
189          
190          matrix4x4Mut.transposeSelf ( );
191          
192          if ( !matrix4x4Mut.matches ( matrix4x4 ) )
193          {
194            System.out.println ( "transpose test 3 failed" );
195            
196            return false;
197          }
198          
199          return true;
200        }
201        
202        ////////////////////////////////////////////////////////////////////////
203        // private methods
204        ////////////////////////////////////////////////////////////////////////
205        
206        private  MatrixTest ( )
207        ////////////////////////////////////////////////////////////////////////
208        {
209          // empty
210        }
211        
212        ////////////////////////////////////////////////////////////////////////
213        ////////////////////////////////////////////////////////////////////////
214        }