
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;
}

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?

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.

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?

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.

Re: java help with inversing matrices
Without an algorithm for how to invert a matrix, you can not write a program to do it.

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.

Re: java help with inversing matrices
You need to find the algorithm to be able to write the code.

Re: java help with inversing matrices
Wikipedia is your friend
There are many different ways to do it, the simplest to implement is probably GaussJordan Elimination.

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
so the identity matrix used is a whole new matrix? so starting out ive got 2 matrices correct. 1 identity and my starter matrix?

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.

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);
}

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.

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);
}

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 length1.
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.

Re: java help with inversing matrices
Please use tabformatting on your code. Even with syntax highlighting it's extremely difficult to read code which is all leftaligned. Your problem should become apparent once you properly format your code.

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);
}

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?

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

Re: java help with inversing matrices
Remove the isolated, separate definitions for i,j,k
Define them in the for loops: same for j and k
Look at how row and col were defined at the top of the method.

Re: java help with inversing matrices
i took the variables out. im still getting the runtimeexception for b[i][j] = b[i][j] / pivot;

Re: java help with inversing matrices
You shouldn't get a runtime exception if you've followed norm's suggestion, it should be a compiletime 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 compiletime exception if you did follow Norm's suggestion)?

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 compiletime 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 compiletime 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;

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?

Re: java help with inversing matrices
i had to keep j isolated but i was able to remove i and j