# java help with inversing matrices

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• February 6th, 2013, 23:00
gspease839
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:
Code java:

```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; }```
• February 7th, 2013, 05:56
Norm
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?
• February 7th, 2013, 09:35
gspease839
Re: java help with inversing matrices
Quote:

Originally Posted by Norm
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.
• February 7th, 2013, 09:37
Norm
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?
• February 7th, 2013, 17:20
gspease839
Re: java help with inversing matrices
Quote:

Originally Posted by Norm
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.
• February 7th, 2013, 17:46
Norm
Re: java help with inversing matrices
Without an algorithm for how to invert a matrix, you can not write a program to do it.
• February 7th, 2013, 17:50
gspease839
Re: java help with inversing matrices
Quote:

Originally Posted by Norm
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.
• February 7th, 2013, 18:03
Norm
Re: java help with inversing matrices
You need to find the algorithm to be able to write the code.
• February 7th, 2013, 18:17
helloworld922
Re: java help with inversing matrices

There are many different ways to do it, the simplest to implement is probably Gauss-Jordan Elimination.
• February 7th, 2013, 18:48
gspease839
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

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?
• February 7th, 2013, 23:00
helloworld922
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.
• February 7th, 2013, 23:23
gspease839
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);
}
• February 8th, 2013, 11:09
Norm
Re: java help with inversing matrices
Please copy the full text of the error messages and paste it here.

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 8th, 2013, 11:38
gspease839
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:
Code java:

```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); }```
• February 8th, 2013, 11:47
Norm
Re: java help with inversing matrices
Quote:

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.
• February 8th, 2013, 11:51
helloworld922
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.
• February 8th, 2013, 16:06
gspease839
Re: java help with inversing matrices
sorry guys heres the code again:

Code java:

```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); }```
• February 8th, 2013, 16:42
Norm
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?
• February 8th, 2013, 17:49
gspease839
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
• February 8th, 2013, 18:01
Norm
Re: java help with inversing matrices
Remove the isolated, separate definitions for i,j,k

Define them in the for loops:
Code :

` for(int i = 0; ....)`
same for j and k

Look at how row and col were defined at the top of the method.
• February 8th, 2013, 21:40
gspease839
Re: java help with inversing matrices
i took the variables out. im still getting the runtimeexception for b[i][j] = b[i][j] / pivot;
• February 8th, 2013, 23:07
helloworld922
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:

Code java:

```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)?
• February 9th, 2013, 07:21
gspease839
Re: java help with inversing matrices
Quote:

Originally Posted by helloworld922
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:

Code java:

```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;
• February 9th, 2013, 07:27
Norm
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?
• February 9th, 2013, 07:29
gspease839
Re: java help with inversing matrices
i had to keep j isolated but i was able to remove i and j
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last