0

How exactly do I format my code so that It would take

1x+2y+3z+4w=5e
6x+7y+8z+9w=10e
11x+12y+13z+14w=15e
16x+17y+18z+19w=20e

and return x,y,z,w?

I was trying to follow the format described in this answer, Currently what I have is the following code which which throws IllegalArgumentException: "java.lang.IllegalArgumentException: Can't solve for wide systems. More variables than equations. when it attempts to calculate the 4 results:

                    double[20] args = {1,2,3... ,20};
                    SimpleMatrix A = new SimpleMatrix(4,5);
                    SimpleMatrix b = new SimpleMatrix(4,1);
                    int val=0;
                    for(int i =0;i<4;i++){
                        for(int j=0;j<5;j++){
                            A.setRow(i, j, args[val]);
                            val++;
                        }
                        b.setRow(i,0, args[val-1]);
                    }
                    double[] result = new double[4];  //results for x y z w
                    try {
                        SimpleMatrix solution = A.solve(b);   //throws IllegalArgumentException!
                        for(int i=0;i<solution.getNumElements();i++) {
                            result[i] = solution.get(i, 0);
                        }

                        --print results--
                    }
                    catch ( SingularMatrixException e ) {
                       throw new IllegalArgumentException();
                    }

What am I doing wrong?

Community
  • 1
  • 1
daedsidog
  • 1,732
  • 2
  • 17
  • 36
  • Your matrix has a determinant of 0 so it can't be inverted... Change (for example) `16x` to `20x` and it will work better. – assylias Jan 19 '16 at 14:26
  • Looks like I was incorrect. It was actually throwing an `IllegalArgumentException` and not a `SingularMatrixException` `"java.lang.IllegalArgumentException: Can't solve for wide systems. More variables than equations.`. I edited my question. Regardless, changing to 16x to 20x did not solve this. – daedsidog Jan 19 '16 at 14:48
  • You shouldn't use setRow() to set an individual element in the matrix. use A.set(row,vol,value) for that. – lessthanoptimal Dec 21 '16 at 20:25

1 Answers1

1

I think it may have to do with your matrix dimensions.

new SimpleMatrix A(4,5) should be replaced with new SimpleMatrix A(4,4).

djebeeb
  • 191
  • 4
  • I've done that as well, but I get a matrix exception saying that the result contains uncountable numbers, – daedsidog Jan 19 '16 at 15:47
  • Even if you change 16x to 20x, you end up with one free variable. I'm not familiar with the SimpleMatrix class, but I don't think it would give a solution if it involved free variables. I suggest trying to change up the numbers in args because the 4th row is a linear combination of the first 3 rows, and the 3rd row is a linear combination of the first 2 rows if you use the numbers 1-20 consecutively. In other words, your matrix is probably still singular, so change it enough that it isn't singular. – djebeeb Jan 19 '16 at 16:12
  • Are you familiar with other methods other than SimpleMatrix used in EJML that I could use instead? – daedsidog Jan 19 '16 at 16:19
  • A quick search found JAMA, which has similar syntax, but its solve() function returns a least squared solution if the matrix is singular (if that's what you're looking for). [JAMA](http://math.nist.gov/javanumerics/jama/doc/) – djebeeb Jan 19 '16 at 16:30
  • If you're dealing with singular systems you want to use SVD that's true for any linear algebra library. You will need to read up on SVD to understand what it does and how to use it. SimpleMatrix does provide access to SVD. – lessthanoptimal Dec 21 '16 at 20:27