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

# Thread: java help with inversing matrices

1. ## 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;
}

2. ## 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?

3. ## Re: java help with inversing matrices

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.

4. ## 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?

5. ## Re: java help with inversing matrices

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.

6. ## Re: java help with inversing matrices

Without an algorithm for how to invert a matrix, you can not write a program to do it.

7. ## Re: java help with inversing matrices

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.

8. ## Re: java help with inversing matrices

You need to find the algorithm to be able to write the code.

9. ## Re: java help with inversing matrices

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

10. ## 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 ---

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?

11. ## 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. ## 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. ## 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.

14. ## 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. ## 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.

16. ## 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. ## 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. ## 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?

19. ## 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. ## 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.

21. ## 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. ## 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. ## Re: java help with inversing matrices

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:

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. ## 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?

25. ## 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 Last