Results 1 to 5 of 5

Thread: Matrix Inverse

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Matrix Inverse

    Hey everybody I am new to Java and to this forum. I really need help on a problem in a Java code. Any help would be really appreciated. The problem is to :
    Write a test program that prompts the user to enter a11, a12, a13, a21, a22, a23, a31, a32, a33 for a matrix and displays its inverse matrix. Here is the sample runs:

    Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: 1 2 1 2 3 1 4 5 3

    -2 0.5 0.5

    1 0.5 -0.5

    1 -1.5 0.5

    I never even heard of a inverse matrix but i have been doing some simple Java problem but this one is killing me please help me!

    This is what i got so far:

    import java.util.Scanner;


    public class MatrixProblem {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a11, a12, a13, a21, a22, a23, a31, a32, a33: ");

    double[][] A = new double[3][3];

    for (int i = 0; i < 3; i++)

    for (int j = 0; j < 3; j++)

    A[i][j] = input.nextDouble();



    double[][] inverseOfA = inverse(A);



    printMatrix(inverseOfA);

    }



    public static void printMatrix(double[][] m) {

    // ????

    }



    public static double[][] inverse(double[][] A) {

    double[][] result = new double[A.length][A.length];



    // Compute |A|

    double determinantOfA = 1; // A[0][0] * A[1][1] * A[2][2] * ...;



    result[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) / determinantOfA;

    result[0][1] = 1; // ??

    result[0][2] = 1; // ??

    result[1][0] = 1; // ??



    return result;

    }

    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I wouldn't recommend this for a beginner, in all honesty.

    In order to find the determinant, you will need a loop. Your code currently only checks for a 2x2 matrix. Have you studied for loops at all?

  3. #3
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Hey thanks for the advice but yes I have studied loops and I really do need help on this problem still. Thanks

  4. #4
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Can anybody just fill in the blanks and explain what you did please. Also i did some research and I found a Java code that might help to find the inverse/determinant. So this code should help and I believe needs to be in my original code to :

    public double determinant(double[][] mat) {
    double result = 0;

    if(mat.length == 1) {
    result = mat[0][0];
    return result;
    }

    if(mat.length == 2) {
    result = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
    return result;
    }

    for(int i = 0; i < mat[0].length; i++) {
    double temp[][] = new double[mat.length - 1][mat[0].length - 1];

    for(int j = 1; j < mat.length; j++) {
    for(int k = 0; k < mat[0].length; k++) {

    if(k < i) {
    temp[j - 1][k] = mat[j][k];
    } else if(k > i) {
    temp[j - 1][k - 1] = mat[j][k];
    }

    }
    }

    result += mat[0][i] * Math.pow(-1, (double)i) * determinant(temp);
    }

    return result;
    }

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    ...I really don't want to sound like this, but I can't find a better way to say it.

    Are you just copying code from other places and trying to Frankenstein a program? (Or rather, trying to get us to Frankenstein it?)

Similar Threads

  1. Matrix class
    By maz22 in forum New To Java
    Replies: 5
    Last Post: 10-15-2010, 06:53 AM
  2. help in matrix
    By Engineer in forum New To Java
    Replies: 7
    Last Post: 10-06-2010, 01:26 PM
  3. Inverse Cos
    By dilpreet28 in forum Java Applets
    Replies: 2
    Last Post: 06-11-2010, 12:23 AM
  4. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:37 AM
  5. inverse engineering
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •