Results 1 to 6 of 6
Thread: LUDecomposition, random arrays.
 12232009, 02:03 PM #1Member
 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 Nequations 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.
Thanks for your time!
 12232009, 02:52 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Filling a matrix with random values in the interval [0, 1) is easy:
Java 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
kind regards,
Jos
 12232009, 03:01 PM #3Member
 Join Date
 Dec 2009
 Posts
 6
 Rep Power
 0
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); }
 12232009, 03:08 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 12232009, 03:56 PM #5Member
 Join Date
 Dec 2009
 Posts
 6
 Rep Power
 0
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();}
 12232009, 04:32 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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,
JosLast edited by JosAH; 12232009 at 07:48 PM.
Similar Threads

Java program problem.. Arrays.. Random Numbers
By Chewart in forum New To JavaReplies: 16Last Post: 11162009, 11:21 PM 
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10192009, 10:57 AM 
Random numbers and arrays
By caro in forum New To JavaReplies: 6Last Post: 06102009, 01:09 AM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 05:50 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM
Bookmarks