Advertisement
Giftednarwhals

CSCI 401 ~ Homework # 5

Oct 1st, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. // Name         :   Kyle Blanchard
  2. // Due          :   10/1/2014
  3. // Class        :   CSCI-401
  4. // Assignment       :   Matrix Inverse ~ Homework # 5
  5. // Contact      :   Kwblanchard@student.stcc.edu
  6.  
  7. // package csci401;
  8.  
  9. import java.util.Scanner;
  10.  
  11. public class Homework5 {
  12.  
  13.     public static void main(String[] args) {
  14.        
  15.         @SuppressWarnings("resource")
  16.         Scanner input = new Scanner(System.in);
  17.         System.out.print("Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ");
  18.        
  19.         // Assigns the user entered values to a 3x3 Matrix called A
  20.         double[][] A = new double[3][3];
  21.         for (int i = 0; i < 3; i++) {
  22.             for (int j = 0; j < 3; j++) {
  23.                 A[i][j] = input.nextDouble();
  24.                 }
  25.         }
  26.        
  27.         // Gets the inverse of Matrix A
  28.         double[][] Ainverse = inverse(A);
  29.         // If there is no inverse matrix display the message saying so. If there is a matrix, print it.
  30.         if (Ainverse == null)
  31.             System.out.println("Unable to create an inverse matrix!");
  32.         else
  33.             displayInverse(Ainverse);
  34.     }
  35.  
  36.     public static double[][] inverse(double[][] A) {
  37.    
  38.         // Gets the value of the Determinate using the formula  aei+_bfg-cdh-ceg-bdi-afh
  39.         double determinant =  (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][1]) - (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]);
  40.         if (determinant == 0)
  41.             return null;
  42.         else {
  43.         // Gets the Inverse matrix of Matrix A using the given formula 
  44.             double[][] inverse = {
  45.     { A[1][1] * A[2][2] - A[1][2] * A[2][1], A[0][2] * A[2][1] - A[0][1] * A[2][2], A[0][1] * A[1][2] - A[0][2] * A[1][1] },
  46.     { A[1][2] * A[2][0] - A[1][0] * A[2][2], A[0][0] * A[2][2] - A[0][2] * A[2][0], A[0][2] * A[1][0] - A[0][0] * A[1][2] },
  47.     { A[1][0] * A[2][1] - A[1][1] * A[2][0], A[0][1] * A[2][0] - A[0][0] * A[2][1], A[0][0] * A[1][1] - A[0][1] * A[1][0] } };
  48.             for (int i = 0; i < 3; i++) {
  49.                 for (int j = 0; j < 3; j++) {
  50.                     inverse[i][j] = ( inverse[i][j] / determinant ) ;
  51.                 }
  52.             }
  53.             return inverse;
  54.         }
  55.     }
  56.  
  57.     public static void displayInverse(double[][] A) {
  58.  
  59.         // Prints each character of the inverse matrix followed by a space and a line break every 3 characters
  60.         for (int i = 0; i < 3; i++) {
  61.             for (int j = 0; j < 3; j++)
  62.                 System.out.print(A[i][j] + " ");
  63.             System.out.println("");
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement