Results 1 to 5 of 5
Thread: Formula Logic Help
 03112011, 02:35 AM #1
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; }
Last edited by sehudson; 03112011 at 02:38 AM.
 03112011, 03:02 AM #2
Think about what your loop is doing.
Java Code:int val; for(int i = 1; i <= 10; i++) { val = i; } System.out.println(val);
Java Code:int val = 10; System.out.println(val);
 03112011, 03:24 AM #3
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.
 03112011, 03:25 AM #4
not exactly sure.
 03112011, 04:17 AM #5
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

I need to execute a formula taken from a db at runtime HowTo
By fortwnty420 in forum Advanced JavaReplies: 9Last Post: 03102011, 05:30 PM 
Formula class
By imorio in forum New To JavaReplies: 3Last Post: 02232011, 09:38 PM 
changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09162010, 01:20 PM 
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07032009, 06:57 PM 
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04302007, 10:00 PM
Bookmarks