Results 1 to 6 of 6
  1. #1
    rootpi is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default 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.

    Thanks for your time!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rootpi View Post
    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[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

  3. #3
    rootpi is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    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!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rootpi View Post
    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

  5. #5
    rootpi is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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!! :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,305
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by rootpi View Post
    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 [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
    Last edited by JosAH; 12-23-2009 at 06:48 PM.

Similar Threads

  1. Java program problem.. Arrays.. Random Numbers
    By Chewart in forum New To Java
    Replies: 16
    Last Post: 11-16-2009, 10:21 PM
  2. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  3. Random numbers and arrays
    By caro in forum New To Java
    Replies: 6
    Last Post: 06-10-2009, 01:09 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •