Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Nate Wheeler
- * compsci 220
- * Hw 6
- * Email: nrwheel@student.stcc.edu
- *
- */
- import java.util.Arrays;
- import java.util.Scanner;
- public class Hw6 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ");
- Scanner input = new Scanner(System.in);
- double[][] matrix = new double[3][3];
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- matrix[i][j] = input.nextDouble();
- }
- }
- System.out.println("");
- System.out.println(Arrays.deepToString(matrix));
- if (inverse(matrix) == null)
- System.out.println("No inverse matrix");
- else
- System.out.println(Arrays.deepToString(inverse(matrix)));
- }
- public static double[][] inverse(double[][] a) {
- double determinate;
- determinate = a[0][0] * a[1][1] * a[2][2] + a[2][1] * a[0][1] * a[1][2] + a[0][2] * a[1][0] * a[2][0]
- - 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];
- if (determinate == 0) {
- return null;
- }
- // first row
- a[0][0] = a[1][1] * a[2][2] - a[1][2] * a[2][1];
- a[0][1] = a[0][2] * a[2][1] - a[0][1] * a[2][2];
- a[0][2] = a[0][1] * a[1][2] - a[0][2] * a[1][1];
- // second row
- a[1][0] = a[1][2] * a[2][0] - a[1][0] * a[2][2];
- a[1][1] = a[0][0] * a[2][2] - a[0][2] * a[2][0];
- a[1][2] = a[0][2] * a[1][0] - a[0][0] * a[1][0];
- // third row
- a[2][0] = a[1][0] * a[2][1] - a[1][1] * a[2][0];
- a[2][1] = a[0][1] * a[2][0] - a[0][0] * a[2][1];
- a[2][1] = a[0][0] * a[1][1] - a[0][1] * a[1][0];
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- a[i][j] = 1 / determinate * a[i][j];
- }
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement