# LUDecomposition, random arrays.

• 12-23-2009, 02:03 PM
rootpi
LUDecomposition, random arrays.
Hello!

I'm Spanish and I'm studying 1st Year of Physics, I've started to use Java this year (we work with NeatBeans) and I'm having many problems with numerical techniques.

I have to do a program that solves systems of N-equations by using the LUDecompositionImpl.

What I have to do is to assign random values to each matrix. The problem is I don't know how to introduce it. I know I have to use Math.Random but not how to use it.

If somebody could help i'd be really grateful.

• 12-23-2009, 02:52 PM
JosAH
Quote:

Originally Posted by rootpi
What I have to do is to assign random values to each matrix. The problem is I don't know how to introduce it. I know I have to use Math.Random but not how to use it.

Filling a matrix with random values in the interval [0, 1) is easy:

Code:

```double[][] matrix= new double[3][3]; // the matrix ... for (int r= 0; r < matrix.length; r++) // for every row ...   for (int c= 0; c < matrix[r].length; c++) // for every column in row r       matrix[r][c]= Math.random(); // set it to a random value```
That's all there is to it; note that this has nothing to do with the LU decomposition of a matrix.

kind regards,

Jos
• 12-23-2009, 03:01 PM
rootpi
Quote:

Originally Posted by JosAH
Filling a matrix with random values in the interval [0, 1) is easy:

Code:

```double[][] matrix= new double[3][3]; // the matrix ... for (int r= 0; r < matrix.length; r++) // for every row ...   for (int c= 0; c < matrix[r].length; c++) // for every column in row r       matrix[r][c]= Math.random(); // set it to a random value```
That's all there is to it; note that this has nothing to do with the LU decomposition of a matrix.

kind regards,

Jos

Hey, thanks!
But I still have a problem, my matrix has to be of nxn dymension, and then I also have a vector which is the result of my system equation.

The code i'm using is:

Code:

```public static void main(String[] args) {         for (int n = 1; n < 10000; n++) {             double[][] A = new double[n][n];             double[] b = new double[n];             Array2DRowRealMatrix mA = new Array2DRowRealMatrix(A);             ArrayRealVector vb = new ArrayRealVector(b);         }```
Thanks!
• 12-23-2009, 03:08 PM
JosAH
Quote:

Originally Posted by rootpi
The code i'm using is:

Code:

```public static void main(String[] args) {         for (int n = 1; n < 10000; n++) {             double[][] A = new double[n][n];             double[] b = new double[n];             Array2DRowRealMatrix mA = new Array2DRowRealMatrix(A);             ArrayRealVector vb = new ArrayRealVector(b);         }```

We don't know what an Array2DRowRealMatrix or ArrayRealVector is; b.t.w. those two loops of mine don't care what the dimensions of that matrix are, i.e. they simply 'walk' over any two dimensional matrix and set each element to a (pseudo) random value.

kind regards,

Jos
• 12-23-2009, 03:56 PM
rootpi
Quote:

Originally Posted by JosAH
We don't know what an Array2DRowRealMatrix or ArrayRealVector is; b.t.w. those two loops of mine don't care what the dimensions of that matrix are, i.e. they simply 'walk' over any two dimensional matrix and set each element to a (pseudo) random value.

kind regards,

Jos

OK, thanks! I think I got it, the Array2DRowRealMatrix and the ArrayRealVector are the codes I have to write in order to do the LUDecomposition, it's the code of the library from Apache Common Mahts. Now I've got a problem, when I try to print an element of my matrix it gives an exception. What should I do??
And where is the mistake in using this code?:

Code:

```for (int n = 0; n < 10000; n++) {             double[][] A = new double[n][n];             A[n][n] = Math.random();             double[] b = new double[n];             b[n] = Math.random();}```
Thank you very much, jos!! :)
• 12-23-2009, 04:32 PM
JosAH
Quote:

Originally Posted by rootpi
OK, thanks! I think I got it, the Array2DRowRealMatrix and the ArrayRealVector are the codes I have to write in order to do the LUDecomposition, it's the code of the library from Apache Common Mahts. Now I've got a problem, when I try to print an element of my matrix it gives an exception. What should I do??
And where is the mistake in using this code?:

Code:

```for (int n = 0; n < 10000; n++) {             double[][] A = new double[n][n];             A[n][n] = Math.random();             double[] b = new double[n];             b[n] = Math.random();}```
Thank you very much, jos!! :)

Please mention the exact error message; my guess is that this was an ArrayIndexOutOfBoundsException. Look at your code: say n == 3, you allocate a 3x3 matrix and you want to set element [3][3] to a random value but a matrix with three rows has rows 0, 1 and 2; 3 is an out of bound value. Use my nested loops instead for the filling of a matrix; even better, turn it into a separate method, e.g. fillMatrix(double[][] matrix).

kind regards,

Jos