Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: java help with inversing matrices

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java help with inversing matrices

    hey, ive got to find the inverse of an 8x8 matrix. i have only coded a 2x2 matrix method. i have no clue or idea how to do anything larger than a 2x2. help is greatly needed.

    code:
    public static double[][] inverse(double[][] A) 
    { 
    double[][] result = new double[A.length][A.length]; 
     
    double ValueOfA = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
     
     
     
    result[0][0] = A[1][1];
    result[0][1] = -A[0][1];
    result[1][0] = -A[1][0]; 
    result[1][1] = A[0][0];
     
     
     
    result[0][0]/=ValueOfA;
    result[0][1]/=ValueOfA;
    result[1][0]/=ValueOfA;
    result[1][1]/=ValueOfA;
     
    for(int row= 0; row < result.length;row++)
    {
    for(int col = 0; col < result[row].length; col++)
    {
    System.out.print(result[row][col] + " ");
    }
    System.out.println();
    }
     
    return result; 
    }
    Last edited by helloworld922; February 7th, 2013 at 12:36 AM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    What is the algorithm for inverting a matrix? Can you post the details of what is done and give an example of a matrix before and after?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    Quote Originally Posted by Norm View Post
    What is the algorithm for inverting a matrix? Can you post the details of what is done and give an example of a matrix before and after?
    im sorry im a little confused on what your asking. the method i have above only works for a 2x2 matrix. the matrix im working on now is an 8x8. i need to find the inverse of that (8x8). ive done some research and ive looked thru my textbook but i dont understand the pseudo code and i cant find any examples throughout the internet coded for large matrices.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    How will you know if the matrix has been correctly inverted if you can not describe what an inverted matrix is?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    Quote Originally Posted by Norm View Post
    How will you know if the matrix has been correctly inverted if you can not describe what an inverted matrix is?
    i believe its finding the adjoint of the matrix divided by determinant. idk man. i know ive been checking with software on different websites. ive been working at this for days.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    Without an algorithm for how to invert a matrix, you can not write a program to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    Quote Originally Posted by Norm View Post
    Without an algorithm for how to invert a matrix, you can not write a program to do it.
    sorry dude your stumping me. basically this is where im at. i got to find an inverse of a pre determined 8 x 8 matrix. this is all lol. i aint got the slightest clue on how to do this.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    You need to find the algorithm to be able to write the code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java help with inversing matrices

    Wikipedia is your friend

    There are many different ways to do it, the simplest to implement is probably Gauss-Jordan Elimination.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    whats been troubling me the most is being able to derive a method for this. ill take a look at what you sent me

    --- Update ---

    Quote Originally Posted by helloworld922 View Post
    Wikipedia is your friend

    There are many different ways to do it, the simplest to implement is probably Gauss-Jordan Elimination.
    so the identity matrix used is a whole new matrix? so starting out ive got 2 matrices correct. 1 identity and my starter matrix?

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java help with inversing matrices

    Yes, that's one way to do it. At the end your original identity matrix will be the inverted matrix and the original matrix will be the identity matrix. All you need to do at the end is copy the inverted matrix back.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    ok having mad difficulty but heres some code: its not compiling can i get some help?

    public static double[][] GaussianEliminverse(double[][] A,int B)
    {
    double[][] b = new double[B][B];
    for (int row = 0; row < B; row++) {
    for (int col = 0; col < B; col++) {
    if (row == col) {
    b[row][col] = 1;
    } else {
    b[row][col] = 0;
    }
    }
    }

    double pivot, factor;
    int i, j, k;

    int n = b.length;

    for (i = 0; i < n; i++) {
    pivot = A[i][i];

    for (j = 0; j < n; j++)
    A[i][j] = A[i][j] / pivot;

    b[i][j] = b[i][j] / pivot;

    for (k = 0; k < n; k++) {
    if (k != i) {
    factor = A[k][i];

    for (j = 0; j < n; j++)
    A[k][j] = A[k][j] - factor * A[i][j];

    b[k][j] = b[k][j] - factor * b[i][j];
    }
    }
    }
    for (int row = 0; row < b.length; row++) {
    for (int col = 0; col < b[row].length; col++) {
    System.out.print(b[row][col] + " ");
    }
    System.out.println();
    }
    return (b);
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    Please copy the full text of the error messages and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at matrix.GaussianEliminverse(matrix.java:2387)
    at matrix.main(matrix.java:2048)

    on: b[i][j] = b[i][j] / pivot;


    this is my test:
    double [][] inversetest = { {1,2,3},{4,8,7},{3,2,4}};


    GaussianEliminverse(inversetest, 3);


    and code:
    public static double[][] GaussianEliminverse(double[][] A,int B) 
    {
    double[][] b = new double[B][B];
    for (int row = 0; row < B; row++) {
    for (int col = 0; col < B; col++) {
    if (row == col) {
    b[row][col] = 1;
    } else {
    b[row][col] = 0;
    }
    }
    }
     
    double pivot, factor;
    int i; 
    int j;
    int k;
     
    int n = b.length;
     
    for (i = 0; i < n; i++) {
    pivot = A[i][i];
     
    for (j = 0; j < n; j++)
    A[i][j] = A[i][j] / pivot;
     
    b[i][j] = b[i][j] / pivot;
     
    for (k = 0; k < n; k++) {
    if (k != i) {
    factor = A[k][i];
     
    for (j = 0; j < n; j++)
    A[k][j] = A[k][j] - factor * A[i][j];
     
    b[k][j] = b[k][j] - factor * b[i][j];
    }
    }
    }
    for (int row = 0; row < b.length; row++) {
    for (int col = 0; col < b[row].length; col++) {
    System.out.print(b[row][col] + " ");
    }
    System.out.println();
    }
    return (b);
    }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at matrix.GaussianEliminverse(matrix.java:2387)
    That's a runtime exception, not a compiler error.
    At line 2387 the index to an array went past the end of the array.
    Remember that the range of indexes for an array is from 0 to the array length-1.

    Some problems I see in the code:
    The loop control variables: i,j,k are defined outside of the for loops instead of inside the (). The value of those variables can be used outside the loop, but you must be careful.
    Another problem I see is that there are for() loops that are not using {} to surround the code that is looped through. Although allowed, using them will prevent errors and confusions.

    The posted code has lost its indentations which makes it hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java help with inversing matrices

    Please use tab-formatting on your code. Even with syntax highlighting it's extremely difficult to read code which is all left-aligned. Your problem should become apparent once you properly format your code.

  17. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    sorry guys heres the code again:

    public static double[][] GaussianEliminverse(double[][] A,int B)           
    {
        double[][] b = new double[B][B];
        for (int row = 0; row < B; row++) {
    	for (int col = 0; col < B; col++) {
    		if (row == col) {
    			b[row][col] = 1;
    		} else {
    			b[row][col] = 0;
    		}
    	}
        }
     
        double pivot, factor;
        int i; 
        int j;
        int k;
     
        int n = b.length;
     
        for (i = 0; i < n; i++) 
        {
    	pivot = A[i][i];
     
    	for (j = 0; j < n; j++)
    	{
            	A[i][j] = A[i][j] / pivot;
    	}
     
    	b[i][j] = b[i][j] / pivot;
     
    	for (k = 0; k < n; k++) 
    	{
    	    if (k != i) 
                {
     
    	        factor = A[k][i];
     
    	         for (j = 0; j < n; j++)
    		 {
    		    A[k][j] = A[k][j] - factor * A[i][j];
    		 }
     
    		b[k][j] = b[k][j] - factor * b[i][j];
    				}
    	    }
    	}
    		for (int row = 0; row < b.length; row++) {
    			for (int col = 0; col < b[row].length; col++) {
    				System.out.print(b[row][col] + " ");
    			}
    			System.out.println();
    		}
    		return (b);
    	}

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    What happens now?
    I see that i,j,k are still defined outside the for loops. Why is that?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    can you show me where i should put them. im trying man. im not sure what else to do in order to make this work

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    Remove the isolated, separate definitions for i,j,k

    Define them in the for loops:
      for(int i = 0; ....)
    same for j and k

    Look at how row and col were defined at the top of the method.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    i took the variables out. im still getting the runtimeexception for b[i][j] = b[i][j] / pivot;

  22. #22
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java help with inversing matrices

    You shouldn't get a runtime exception if you've followed norm's suggestion, it should be a compile-time exception.

    Take a look at this part of your code:

    for (j = 0; j < n; j++)
    {
        A[k][j] = A[k][j] - factor * A[i][j];
    }
    b[k][j] = b[k][j] - factor * b[i][j];

    Can you see why this code will result in a runtime exception (or better yet, a compile-time exception if you did follow Norm's suggestion)?

  23. #23
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    Quote Originally Posted by helloworld922 View Post
    You shouldn't get a runtime exception if you've followed norm's suggestion, it should be a compile-time exception.

    Take a look at this part of your code:

    for (j = 0; j < n; j++)
    {
        A[k][j] = A[k][j] - factor * A[i][j];
    }
    b[k][j] = b[k][j] - factor * b[i][j];

    Can you see why this code will result in a runtime exception (or better yet, a compile-time exception if you did follow Norm's suggestion)?



    i have to have this project turned in tonight. i got to get this part, im stuck with what to do. can anyone please tell me what im doing wrong and fix the problem. its a outofboundsException at b[k][j] = b[k][j] - factor * b[i][j]; and b[i][j] = b[i][j] / pivot;

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java help with inversing matrices

    Look at the algorithm and see if the code is following it.

    If you removed the isolated definitions for i,j,k how does the code compile without an error?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Feb 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java help with inversing matrices

    i had to keep j isolated but i was able to remove i and j

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: November 27th, 2012, 10:19 AM
  2. Solving linear equation systems including sparse matrices
    By GregXel in forum Java Theory & Questions
    Replies: 1
    Last Post: November 24th, 2012, 01:05 PM
  3. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM