Matrix dan Implementasinya dalam Pemrograman JAVA ( Part III )

11. Determinan ( Matrix ) adalah suatu fungsi tertentu yang menghubungkan suatu bilangan real dengan suatu matriks bujur sangkar ( Wikipedia ). Berikut contoh determinan dari suatu matrix

import java.util.Random;

public class MatrixDeterminan2{

	public static void main( String [] args ){
		int ordo = Integer.parseInt( args[0] );
		int[][] matrix = createMatrix( ordo );
		showMatrix( matrix, 2);
		System.out.println( "Determinan Is " + getDeterminan( matrix ) );
	}
	
	static int[][] createMatrix( int ordo ){
		int [][] result = new int[ordo][ordo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++){
				result[a][b] = new Random().nextInt(9) + 1;
			}
		}
		return result;
	}
		
	static int getDeterminan( int[][] matrixOrdo2 ){
		int result = ( matrixOrdo2[0][0] * matrixOrdo2[1][1]) - ( matrixOrdo2[0][1] * matrixOrdo2[1][0] );
		return result;
	}
	
	static void showMatrix( int [][] matrix, int ordo){
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				System.out.print( matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
	
}


12. Matrix Invers ( Kebalikan ) adalah matrix yang jika A dan B matriks bujur sangkar sedemikian rupa sehingga A B = B A = I ( Matrix Identitas ), maka B disebut balikan atau invers dari A dan dapat dituliskan B = A(invers).

import java.util.Random;

public class MatrixInvers2{

	public static void main( String [] args ){
		int ordo = Integer.parseInt( args[0] );
		double [][] matrix = createMatrix( ordo );
		showMatrix( matrix, ordo );
		double determinan = getDeterminan( matrix );
		System.out.println( "Determinan Is " +  determinan );
		double [][] invers = createInvers ( ordo, determinan, matrix );
		System.out.println( "Invers is" );
		showMatrix( invers, ordo );
	}
	
	static double[][] createMatrix( int ordo ){
		double [][] result = new double[ordo][ordo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++){
				result[a][b] = new Random().nextInt(9) + 1;
			}
		}
		return result;
	}
		
	static double getDeterminan( double [][] matrixOrdo2 ){
		double result = ( matrixOrdo2[0][0] * matrixOrdo2[1][1]) - ( matrixOrdo2[0][1] * matrixOrdo2[1][0] );
		return result;
	}
	
	static void showMatrix( double [][] matrix, int ordo){
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				System.out.print( matrix[a][b] + " ");
			}
			System.out.println();
		}
	}
	
	static double[][] createInvers ( int ordo, double determinan, double[][] matrix ){
		double [][] result = new double[ordo][ordo];
		System.out.println( "Switching index position" );
		double temp = matrix[0][0];
		matrix[0][0] = matrix[1][1];
		matrix[1][1] = temp;				
		matrix[1][0] = -1 * matrix[1][0];
		matrix[0][1] = -1 * matrix[0][1];
		System.out.println( "After switching index position" );
		showMatrix( matrix, 2);
		double vector = 1/ determinan;
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				result[a][b] = vector * matrix[a][b];
			}
		}
		return result;
	}
	
	
}

13. Matrix invers untuk matrix 3 x 3 harus diproses menggunakan proses pencarian adjoin/ kofator.

import java.util.Random;

public class MatrixInvers3{

	public static void main ( String [] args ){
		int ordo = 3;
		double [][] matrix = createMatrix( ordo );
		showMatrix( ordo, ordo, matrix );
		System.out.println("=====================");
		double [][] matrixToAdjunctionCramer = createMatrixToAdjunctionCramer( ordo, matrix );		
		showMatrix( ordo, 5,  matrixToAdjunctionCramer );
		System.out.println("=====================");
		double determinan = getDeterminan( 3, 5, matrixToAdjunctionCramer );
		System.out.println( "Determinant is : "+ determinan );
		double[][] transposedMatrix = createTransposeMatrix( matrix, ordo );
		System.out.println("TRANSPOSE MATRIX=====================");
		showMatrix( ordo, ordo, transposedMatrix );
		
		double [][] adjMatrix = getAdjunction( transposedMatrix, ordo );
		System.out.println("ADJ MATRIX=====================");
		showMatrix( ordo, ordo, adjMatrix );
		double[][] inversMatrix = getInvers( adjMatrix, determinan, 	ordo );
		System.out.println("INVERS MATRIX=====================");
		showMatrix( ordo, ordo, inversMatrix );
	}
	
	public static double [][] createMatrix( int ordo ){
		double [][] result = new double[ordo][ordo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				result[a][b] = new Random().nextInt(9)+1;
			}
		}
		return result;
	}
	
	public static void showMatrix( int bar, int kol, double [][] matrix ){
		for( int a=0; a<bar; a++ ){
			for( int b=0; b<kol; b++ ){
				System.out.print( matrix[a][b] + " ");
			}
			System.out.println("");
		}
	}
	
	public static double[][] createMatrixToAdjunctionCramer( int ordo, double [][] matrix ){
		int newOrdo = ordo+2;
		double[][] result = new double[ordo][newOrdo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<newOrdo; b++ ){
				if( b < 3 ){
					result[a][b] = matrix[a][b];
				}else{
					result[a][b] = matrix[a][b-3];					
				}
			}
		}
		return result;
	}
	
	public static double getDeterminan( int bar, int kol, double[][] matrix ){
		double result = 0.0;		
		result = ( ( matrix[0][0] * matrix[1][1] * matrix[2][2] ) + 
					( matrix[0][1] * matrix[1][2] * matrix[2][3] ) + 
					( matrix[0][2] * matrix[1][3] * matrix[2][4] ) ) - 
				 ( ( matrix[2][0] * matrix[1][1] * matrix[0][2] ) +
					( matrix[2][1] * matrix[1][2] * matrix[0][3] ) +
					( matrix[2][2] * matrix[1][3] * matrix[0][4] ) );				
		return result;
	}

	public static double [][] createTransposeMatrix( double[][] matrix, int ordo ){
		double [][] result = new double[ordo][ordo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				result[a][b] = matrix[b][a];
			}
		}
		return result;
	}
	public static double [][] getInvers( double[][] adjMatrix, double determinan, int ordo ){
		double multiplier = 1/determinan;
		double[][] result = new double[ordo][ordo];
		for ( int a=0; a<ordo; a++ ){
			for ( int b=0; b<ordo; b++ ){
				result[a][b] = multiplier * adjMatrix[a][b];
			}
		}
		return result;
	}
	
	public static double [][] getAdjunction( double [][] transposedMatrix, int ordo ){
		double[][] adjunction = new double[ordo][ordo];
		adjunction[0][0]=transposedMatrix[1][1]*transposedMatrix[2][2]-(transposedMatrix[2][1]*transposedMatrix[1][2]);
		adjunction[0][1]=(-1)*(transposedMatrix[1][0]*transposedMatrix[2][2]-(transposedMatrix[2][0]*transposedMatrix[1][2]));
		adjunction[0][2]=transposedMatrix[1][0]*transposedMatrix[2][1]-(transposedMatrix[2][0]*transposedMatrix[1][1]);
		
		adjunction[1][0]=(-1)*(transposedMatrix[0][1]*transposedMatrix[2][2]-transposedMatrix[2][1]*transposedMatrix[0][2]);
		adjunction[1][1]=transposedMatrix[0][0]*transposedMatrix[2][2]-transposedMatrix[2][0]*transposedMatrix[0][2];
		adjunction[1][2]=(-1)*(transposedMatrix[0][0]*transposedMatrix[2][1]-transposedMatrix[2][0]*transposedMatrix[0][1]);

		adjunction[2][0]=transposedMatrix[0][1]*transposedMatrix[1][2]-transposedMatrix[1][1]*transposedMatrix[0][2];
		adjunction[2][1]=(-1)*(transposedMatrix[0][0]*transposedMatrix[1][2]-transposedMatrix[1][0]*transposedMatrix[0][2]);
		adjunction[2][2]=transposedMatrix[0][0]*transposedMatrix[1][1]-transposedMatrix[1][0]*transposedMatrix[0][1];
		
		return adjunction;
	}	
}

Tagged: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: