1. Member Join Date
Dec 2009
Posts
6
Rep Power
0

## 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.  Reply With Quote

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

Java Code:
```double[][] matrix= new double; // 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  Reply With Quote

3. Member Join Date
Dec 2009
Posts
6
Rep Power
0

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

Java Code:
```double[][] matrix= new double; // 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:

Java 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!  Reply With Quote

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

Java 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  Reply With Quote

5. Member Join Date
Dec 2009
Posts
6
Rep Power
0

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

Java 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!! :)  Reply With Quote

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

Java 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  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
Last edited by JosAH; 12-23-2009 at 06:48 PM.  Reply With Quote

array, java, numerical techiques, random 