From 0172b178de08565d3d6c22ebe021675cf8e99e82 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Mon, 31 Jul 2017 12:39:56 -0700 Subject: [PATCH] Optimize log. --- src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index c343c78..370b5d9 100755 --- a/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -203,11 +203,12 @@ public class StandardPlugin extends Plugin { private NumberInterface getLogPartialSum(NumberInterface x) { NumberInterface maxError = getMaxError(x); x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1). - NumberInterface currentTerm = x, sum = x; + NumberInterface currentNumerator = x, currentTerm = x, sum = x; int n = 1; while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) { n++; - currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n - 1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate(); + currentNumerator = currentNumerator.multiply(x).negate(); + currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass())); sum = sum.add(currentTerm); } return sum; @@ -273,7 +274,7 @@ public class StandardPlugin extends Plugin { * * @param maxError Maximum error permissible (This should probably be positive.) * @param x where the function is evaluated. - * @return the number of terms needed to evaluated the exponential function. + * @return the number of terms needed to evaluate the exponential function. */ private static int getNTermsExp(NumberInterface maxError, NumberInterface x) { //We need n such that |x^(n+1)| <= (n+1)! * maxError