//*********************************************************************************
// class tig.maths.Matrix3
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************

package tig.maths;
import java.lang.Math;

import tig.GeneralConstants;
/**********************************************************************************
Simple representation of a 3 x 3 matrix based on doubles.
@author Thierry Graff
@history jan 11 2002 : creation
**********************************************************************************/
public class Matrix3 implements GeneralConstants{

  //=================================================================================
  //                            CONSTANTS
  //=================================================================================
  /** Term of the matrix */
  public double m00, m01, m02, m10, m11, m12, m20, m21, m22;
					 
  //=================================================================================
  //                            CONSRUCTORS
  //=================================================================================
  /** Unique constructor.
  
Fills the instance variables mij from parameters xij. */ public Matrix3(double x00, double x01, double x02, double x10, double x11, double x12, double x20, double x21, double x22){ m00 = x00; m01 = x01; m02 = x02; m10 = x10; m11 = x11; m12 = x12; m20 = x20; m21 = x21; m22 = x22; }// end Matrix3 //================================================================================= // PUBLIC METHODS //================================================================================= /** Returns a String representation of this Vector. The form is : (x0, x1, x2). */ public String toString(){ return this.m00 + "\t" + this.m01 + "\t" + this.m02 + LS + this.m10 + "\t" + this.m11 + "\t" + this.m12 + LS + this.m20 + "\t" + this.m21 + "\t" + this.m22 + LS; }// end toString() //================================================================================= // STATIC METHODS //================================================================================= //****************** getIdentityMatrix ************************* /** Returns the identity matrix. */ public static Matrix3 getIdentityMatrix(){ return new Matrix3(1, 0, 0, 0, 1, 0, 0, 0, 1); }// end getIdentityMatrix //****************** getRotX ************************* /** Returns a matrix that can be used as a rotation matrix around the X axis. @param alpha The rotation angle, in radians. */ public static Matrix3 getRotX(double alpha){ double c = Math.cos(alpha); double s = Math.sin(alpha); return new Matrix3(1, 0, 0, 0, c, s, 0, -s, c); }// end getRotX //****************** getRotY ************************* /** Returns a matrix that can be used as a rotation matrix around the Y axis. @param alpha The rotation angle, in radians. */ public static Matrix3 getRotY(double alpha){ double c = Math.cos(alpha); double s = Math.sin(alpha); return new Matrix3(c, 0, -s, 0, 1, 0, s, 0, c); }// end getRotY //****************** getRotZ ************************* /** Returns a matrix that can be used as a rotation matrix around the Z axis. @param alpha The rotation angle, in radians. */ public static Matrix3 getRotZ(double alpha){ double c = Math.cos(alpha); double s = Math.sin(alpha); return new Matrix3( c, s, 0, -s, c, 0, 0, 0, 1); }// end getRotZ //****************** mul ************************* /** Returns a matrix M such as M = M1 * M2. */ public static Matrix3 mul(Matrix3 M1, Matrix3 M2){ return new Matrix3( // first line M1.m00*M2.m00 + M1.m01*M2.m10 + M1.m02*M2.m20 , M1.m00*M2.m01 + M1.m01*M2.m11 + M1.m02*M2.m21 , M1.m00*M2.m02 + M1.m01*M2.m12 + M1.m02*M2.m22 , // second line M1.m10*M2.m00 + M1.m11*M2.m10 + M1.m12*M2.m20 , M1.m10*M2.m01 + M1.m11*M2.m11 + M1.m12*M2.m21 , M1.m10*M2.m02 + M1.m11*M2.m12 + M1.m12*M2.m22 , // third line M1.m20*M2.m00 + M1.m21*M2.m10 + M1.m22*M2.m20 , M1.m20*M2.m01 + M1.m21*M2.m11 + M1.m22*M2.m21 , M1.m20*M2.m02 + M1.m21*M2.m12 + M1.m22*M2.m22 ); }// end Matrix3 //****************** det ************************* /** Returns the determinant of matrix M. @param M The matrix whose determinant must be found. */ public static double det(Matrix3 M){ return M.m00*(M.m11*M.m22 - M.m21*M.m12) - M.m10*(M.m01*M.m22 - M.m21*M.m02) + M.m20*(M.m01*M.m12 - M.m11*M.m02); }// end det //****************** invert ************************* /** Returns the inverse matrix of M.
If the resulting matrix is called N, we have : M x N = I, the identity matrix. @param M The matrix to invert. @throws RuntimeException if the determinant of M is = 0. */ public static Matrix3 invert(Matrix3 M){ double del = Matrix3.det(M); return new Matrix3( M.minor(0,0)/del, -M.minor(1,0)/del, M.minor(2,0)/del, -M.minor(0,1)/del, M.minor(1,1)/del, -M.minor(2,1)/del, M.minor(0,2)/del, -M.minor(1,2)/del, M.minor(2,2)/del ); }// end invert //================================================================================= // PRIVATE METHODS //================================================================================= //****************** minor ************************* /** Returns the minor of row, col for the matrix. @param M The matrix whose determinant must be found. */ private double minor(int row, int col){ switch(row){ case 0 : { switch(col){ case 0 : return this.m11*this.m22 - this.m21*this.m12; case 1 : return this.m10*this.m22 - this.m20*this.m12; case 2 : return this.m10*this.m21 - this.m20*this.m11; default : throw new IllegalArgumentException("'col' must be between 0 and 2"); } } case 1 : { switch(col){ case 0 : return this.m01*this.m22 - this.m21*this.m02; case 1 : return this.m00*this.m22 - this.m20*this.m02; case 2 : return this.m00*this.m21 - this.m20*this.m01; default : throw new IllegalArgumentException("'col' must be between 0 and 2"); } } case 2 : { switch(col){ case 0 : return this.m01*this.m12 - this.m11*this.m02; case 1 : return this.m00*this.m12 - this.m10*this.m02; case 2 : return this.m00*this.m11 - this.m10*this.m01; default : throw new IllegalArgumentException("'col' must be between 0 and 2"); } } default : throw new IllegalArgumentException("'row' must be between 0 and 2"); } }// end minor //================================================================================= //================================================================================= // TESTS //================================================================================= //================================================================================= // **************** For tests only **************** public static void main(String[] args){ // no complete argument checking if(args[0].equalsIgnoreCase("testInvert")) testInvert(); else{ String possibleArgs = "'testInvert'"; System.out.println("first argument must be " + possibleArgs); } }// end main // **************** For tests only **************** private static void testInvert(){ Matrix3 M = new Matrix3(2, -1, 22, -1, 3, -34, 4, 3, 1); System.out.println("Matrix : " + LS + M.toString()); Matrix3 tmp = new Matrix3( M.minor(0,0), M.minor(0,1), M.minor(0,2), M.minor(1,0), M.minor(1,1), M.minor(1,2), M.minor(2,0), M.minor(2,1), M.minor(2,2) ); System.out.println("minors : " + LS + tmp.toString()); double det = Matrix3.det(M); System.out.println("det : " + det); if (det == 0) return; Matrix3 inv = Matrix3.invert(M); System.out.println("invert : " + LS + inv.toString()); Matrix3 id = Matrix3.mul(inv, M); System.out.println("identity M-1 x M: " + LS + id.toString()); id = Matrix3.mul(M, inv); System.out.println("identity : M x M-1" + LS + id.toString()); }//end testDoubleToDMS }//end class Matrix3

Ads help maintain this website.