Results 1 to 5 of 5
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Formula Logic Help

    Hello,

    I was given the following formula to calculate a determinant:

    Java Code:
      public double calculateDeterminant() {
    
    
            double det = 0;
            System.out.println("RowCount: " + RowCount);
            if (RowCount == 1) {
                det = CalculateBaseCase("1");
                return det;
            }
            //base case
            if (RowCount == 2) {
                det = CalculateBaseCase("2");
                return det;
            }
    
            for (int i = 0; i < RowCount; i++) {
                det = Math.pow(-1, i) * getValAt(i, 0) * minor(i,0).calculateDeterminant();
            }
            return det;
    
        }
    I have some println statements in place, and it seems like my i for the 'minor(i,0)' method stays at 0. Anybody able to tell me where the formula is wrong?
    Last edited by sehudson; 03-11-2011 at 02:38 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Think about what your loop is doing.
    Java Code:
    int val;
    for(int i = 1; i <= 10; i++) {
        val = i;
    }
    System.out.println(val);
    The output is 10. The above code can be simplified to
    Java Code:
    int val = 10;
    System.out.println(val);
    Do you understand what I'm getting at?

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    partially..since in your example the println is outside the loop, the only thing that gets printed is the last loop value, which is 10.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    not exactly sure.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    The print statement is irrelevant. It was included in case the code was compiled and run to see the output.

    The point I was trying to make is that your loop is pointless only the last value is retained. When i is 1, perform calculation, assign to det. Then i is 2, perform calculation, assign to det, throw away first result. Then i is 3, perform calculation, assign to det, throw away second result, etc. Your loop can be replaced with:
    Java Code:
    det = Math.pow(-1, RowCount - 1) * getValAt(RowCount - 1, 0) * minor(RowCount - 1,0).calculateDeterminant();

Similar Threads

  1. Replies: 9
    Last Post: 03-10-2011, 05:30 PM
  2. Formula class
    By imorio in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 09:38 PM
  3. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 01:20 PM
  4. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 06:57 PM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 PM

Posting Permissions

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