Advertisement
nate23nate23

HW 6

Sep 29th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. /**
  2.  * Nate Wheeler
  3.  * compsci 220
  4.  * Hw 6
  5.  * Email: nrwheel@student.stcc.edu
  6.  *
  7.  */
  8.  
  9. import java.util.Arrays;
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class Hw6 {
  14.  
  15.     public static void main(String[] args) {
  16.         // TODO Auto-generated method stub
  17.         System.out.println("Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ");
  18.     Scanner input =new Scanner(System.in);
  19.      double[][] matrix = new double[3][3];
  20.     for(int i=0; i< matrix.length; i++){
  21.         for(int j=0; j< matrix[i].length; j++){
  22.             matrix[i][j]=input.nextDouble();
  23.         }
  24.     }
  25.     System.out.println("");
  26.     System.out.println(Arrays.deepToString(matrix));
  27.     if(inverse(matrix)==null)
  28.         System.out.println("No inverse matrix");
  29.     else
  30.     System.out.println(Arrays.deepToString(inverse(matrix)));
  31.    
  32.    
  33.     }
  34.     public static double[][] inverse(double[][] a){
  35.     double determinate;
  36.     determinate= a[0][0]*a[1][1]*a[2][2] + a[2][0]*a[0][1]*a[1][2] + a[0][2]*a[1][0]*a[2][0]
  37.             - a[0][2]*a[1][1]*a[2][0] - a[0][0]*a[1][2]*a[2][1] - a[2][2]*a[1][0]*a[0][1];
  38.     if(determinate==0){
  39.         return null;
  40.     }
  41.     //first row
  42.     a[0][0]= a[1][1]* a[2][2] - a[1][2]* a[2][1];
  43.     a[0][1]= a[0][2]* a[2][1] - a[0][1]* a[2][2];
  44.     a[0][2]= a[0][1]* a[1][2] - a[0][2]* a[1][1];
  45.     //second row
  46.     a[1][0]= a[1][2]* a[2][0] - a[1][0]* a[2][2];
  47.     a[1][1]= a[0][0]* a[2][2] - a[0][2]* a[2][0];
  48.     a[1][2]= a[0][2]* a[1][0] - a[0][0]* a[1][0];
  49.     //third row
  50.     a[2][0]= a[1][0]* a[2][1] - a[1][1]* a[2][0];
  51.     a[2][1]= a[0][1]* a[2][0] - a[0][0]* a[2][1];
  52.     a[2][1]= a[0][0]* a[1][1] - a[0][1]* a[1][0];
  53.    
  54.     for(int i=0; i< a.length; i++){
  55.         for(int j=0; j< a[i].length; j++){
  56.             a[i][j]= 1/determinate*a[i][j];
  57.         }
  58.     }
  59.     return a;
  60.     }
  61.  
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement