Results 1 to 5 of 5
Thread: Formula Logic Help
- 03-11-2011, 02:35 AM #1
Formula Logic Help
Hello,
I was given the following formula to calculate a determinant:
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?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; 03-11-2011 at 02:38 AM.
- 03-11-2011, 03:02 AM #2
Think about what your loop is doing.
The output is 10. The above code can be simplified toJava Code:int val; for(int i = 1; i <= 10; i++) { val = i; } System.out.println(val);
Do you understand what I'm getting at?Java Code:int val = 10; System.out.println(val);
- 03-11-2011, 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.
- 03-11-2011, 03:25 AM #4
not exactly sure.
- 03-11-2011, 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 How-To
By fortwnty420 in forum Advanced JavaReplies: 9Last Post: 03-10-2011, 05:30 PM -
Formula class
By imorio in forum New To JavaReplies: 3Last Post: 02-23-2011, 09:38 PM -
changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09-16-2010, 01:20 PM -
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07-03-2009, 06:57 PM -
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04-30-2007, 10:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks