You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/01/25 23:05:43 UTC

svn commit: r1897477 - in /poi/trunk/poi/src: main/java/org/apache/poi/ss/formula/atp/ main/java/org/apache/poi/ss/formula/eval/ main/java/org/apache/poi/ss/formula/functions/ test/java/org/apache/poi/ss/formula/functions/

Author: fanningpj
Date: Tue Jan 25 23:05:43 2022
New Revision: 1897477

URL: http://svn.apache.org/viewvc?rev=1897477&view=rev
Log:
normsdist function

Added:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java
      - copied, changed from r1897476, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
    poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java
      - copied, changed from r1897476, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
Modified:
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
    poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
    poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java?rev=1897477&r1=1897476&r2=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java Tue Jan 25 23:05:43 2022
@@ -149,6 +149,8 @@ public final class AnalysisToolPak imple
         r(m, "MULTINOMIAL", null);
         r(m, "NETWORKDAYS", NetworkdaysFunction.instance);
         r(m, "NOMINAL", null);
+        r(m, "NORM.DIST", NormDist.instance);
+        r(m, "NORM.S.DIST", NormSDist.instance);
         r(m, "NUMBERVALUE", NumberValueFunction.instance);
         r(m, "OCT2BIN", null);
         r(m, "OCT2DEC", Oct2Dec.instance);

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java?rev=1897477&r1=1897476&r2=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java Tue Jan 25 23:05:43 2022
@@ -273,7 +273,7 @@ public final class FunctionEval {
         // 291: LOGINV
         // 292: NEGBINOMDIST
         retval[293] = NormDist.instance;
-        // 294: NORMSDIST
+        retval[294] = NormSDist.instance;
         // 295: NORMINV
         // 296: NORMSINV
         // 297: STANDARDIZE

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java?rev=1897477&r1=1897476&r2=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java Tue Jan 25 23:05:43 2022
@@ -26,9 +26,11 @@ import org.apache.poi.ss.formula.eval.Op
 import org.apache.poi.ss.formula.eval.ValueEval;
 
 /**
- * Implementation for Excel NORMDIST() function.<p>
-
- * https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+ * Implementation for Excel NORMDIST() and NORM.DIST() functions.<p>
+ * <ul>
+ *   <li>https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58</li>
+ *   <li>https://support.microsoft.com/en-us/office/norm-dist-function-edb1cc14-a21c-4e53-839d-8082074c9f8d</li>
+ * </ul>
  */
 public final class NormDist extends Fixed4ArgFunction implements FreeRefFunction {
 

Copied: poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java (from r1897476, poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java?p2=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java&p1=poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java&r1=1897476&r2=1897477&rev=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormDist.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/formula/functions/NormSDist.java Tue Jan 25 23:05:43 2022
@@ -17,7 +17,6 @@
 
 package org.apache.poi.ss.formula.functions;
 
-import org.apache.commons.math3.distribution.NormalDistribution;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
 import org.apache.poi.ss.formula.eval.ErrorEval;
 import org.apache.poi.ss.formula.eval.EvaluationException;
@@ -26,22 +25,18 @@ import org.apache.poi.ss.formula.eval.Op
 import org.apache.poi.ss.formula.eval.ValueEval;
 
 /**
- * Implementation for Excel NORMDIST() function.<p>
-
- * https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+ * Implementation for Excel NORMSDIST() function.<p>
+ * <ul>
+ *   <li>https://support.microsoft.com/en-us/office/normsdist-function-463369ea-0345-445d-802a-4ff0d6ce7cac</li>
+ *   <li>https://support.microsoft.com/en-us/office/norm-s-dist-function-1e787282-3832-4520-a9ae-bd2a8d99ba88</li>
+ * </ul>
  */
-public final class NormDist extends Fixed4ArgFunction implements FreeRefFunction {
-
-    public static final NormDist instance = new NormDist();
+public final class NormSDist extends Fixed1ArgFunction implements FreeRefFunction {
 
-    static double probability(double x, double mean, double stdev, boolean cumulative) {
-        NormalDistribution normalDistribution = new NormalDistribution(mean, stdev);
-        return cumulative ? normalDistribution.cumulativeProbability(x) : normalDistribution.density(x);
-    }
+    public static final NormSDist instance = new NormSDist();
 
     @Override
-    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2,
-                              ValueEval arg3, ValueEval arg4) {
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1) {
         try {
             Double xval = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
             if (xval == null) {
@@ -49,23 +44,8 @@ public final class NormDist extends Fixe
             } else if (xval < 0) {
                 return ErrorEval.NUM_ERROR;
             }
-            Double mean = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
-            if (mean == null) {
-                return ErrorEval.VALUE_INVALID;
-            }
-            Double stdev = evaluateValue(arg3, srcRowIndex, srcColumnIndex);
-            if (stdev == null) {
-                return ErrorEval.VALUE_INVALID;
-            } else if (stdev.doubleValue() <= 0) {
-                return ErrorEval.NUM_ERROR;
-            }
-            Boolean cumulative = OperandResolver.coerceValueToBoolean(arg4, false);
-            if (cumulative == null) {
-                return ErrorEval.VALUE_INVALID;
-            }
 
-            return new NumberEval(probability(
-                    xval.doubleValue(), mean.doubleValue(), stdev.doubleValue(), cumulative.booleanValue()));
+            return new NumberEval(NormDist.probability(xval.doubleValue(), 0, 1, true));
         } catch (EvaluationException e) {
             return e.getErrorEval();
         }
@@ -73,8 +53,8 @@ public final class NormDist extends Fixe
 
     @Override
     public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
-        if (args.length == 4) {
-            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1], args[2], args[3]);
+        if (args.length == 1) {
+            return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0]);
         }
 
         return ErrorEval.VALUE_INVALID;

Modified: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java?rev=1897477&r1=1897476&r2=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java Tue Jan 25 23:05:43 2022
@@ -63,6 +63,7 @@ final class TestNormDist {
     }
 
     //https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+    //https://support.microsoft.com/en-us/office/norm-dist-function-edb1cc14-a21c-4e53-839d-8082074c9f8d
     @Test
     void testMicrosoftExample1() throws IOException {
         try (HSSFWorkbook wb = new HSSFWorkbook()) {
@@ -74,6 +75,7 @@ final class TestNormDist {
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
             HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
             assertDouble(fe, cell, "NORMDIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
+            assertDouble(fe, cell, "NORM.DIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
         }
     }
 

Copied: poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java (from r1897476, poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java)
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java?p2=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java&p1=poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java&r1=1897476&r2=1897477&rev=1897477&view=diff
==============================================================================
--- poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormDist.java (original)
+++ poi/trunk/poi/src/test/java/org/apache/poi/ss/formula/functions/TestNormSDist.java Tue Jan 25 23:05:43 2022
@@ -19,10 +19,10 @@ package org.apache.poi.ss.formula.functi
 
 import org.apache.poi.hssf.usermodel.HSSFCell;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.formula.eval.BlankEval;
 import org.apache.poi.ss.formula.eval.BoolEval;
 import org.apache.poi.ss.formula.eval.ErrorEval;
 import org.apache.poi.ss.formula.eval.NumberEval;
@@ -37,67 +37,51 @@ import static org.apache.poi.ss.util.Uti
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 /**
- * Tests for {@link NormDist}
+ * Tests for {@link NormSDist}
  */
-final class TestNormDist {
+final class TestNormSDist {
 
     private static final OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
 
     @Test
     void testBasic() {
-        confirmValue("42", "40", "1.5", true, 0.9087888);
-        confirmValue("42", "40", "1.5", false, 0.10934);
+        confirmValue("1.333333", 0.908788726);
     }
 
     @Test
     void testInvalid() {
-        confirmInvalidError("A1","B2","C2", false);
+        confirmInvalidError("A1");
     }
 
-    @Test
-    void testNumError() {
-        confirmNumError("42","40","0", false);
-        confirmNumError("42","40","0", true);
-        confirmNumError("42","40","-0.1", false);
-        confirmNumError("42","40","-0.1", true);
-    }
-
-    //https://support.microsoft.com/en-us/office/normdist-function-126db625-c53e-4591-9a22-c9ff422d6d58
+    //https://support.microsoft.com/en-us/office/normsdist-function-463369ea-0345-445d-802a-4ff0d6ce7cac
+    //https://support.microsoft.com/en-us/office/norm-s-dist-function-1e787282-3832-4520-a9ae-bd2a8d99ba88
     @Test
     void testMicrosoftExample1() throws IOException {
         try (HSSFWorkbook wb = new HSSFWorkbook()) {
             HSSFSheet sheet = wb.createSheet();
-            addRow(sheet, 0, "Data", "Description");
-            addRow(sheet, 1, 42, "Value for which you want the distribution");
-            addRow(sheet, 2, 40, "Arithmetic mean of the distribution");
-            addRow(sheet, 3, 1.5, "Standard deviation of the distribution");
+            HSSFRow row = sheet.createRow(0);
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
-            HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
-            assertDouble(fe, cell, "NORMDIST(A2,A3,A4,TRUE)", 0.9087888, 0.000001);
+            HSSFCell cell = row.createCell(0);
+            assertDouble(fe, cell, "NORMSDIST(1.333333)", 0.908788726, 0.000001);
+            assertDouble(fe, cell, "NORM.S.DIST(1.333333)", 0.908788726, 0.000001);
         }
     }
 
-    private static ValueEval invokeValue(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), new StringEval(number3), BoolEval.valueOf(cumulative)};
-        return NormDist.instance.evaluate(args, ec);
+    private static ValueEval invokeValue(String number1) {
+        ValueEval[] args = new ValueEval[] { new StringEval(number1)};
+        return NormSDist.instance.evaluate(args, ec);
     }
 
-    private static void confirmValue(String number1, String number2, String number3, boolean cumulative, double expected) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
+    private static void confirmValue(String number1, double expected) {
+        ValueEval result = invokeValue(number1);
         assertEquals(NumberEval.class, result.getClass());
         assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0000001);
     }
 
-    private static void confirmInvalidError(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
+    private static void confirmInvalidError(String number1) {
+        ValueEval result = invokeValue(number1);
         assertEquals(ErrorEval.class, result.getClass());
         assertEquals(ErrorEval.VALUE_INVALID, result);
     }
 
-    private static void confirmNumError(String number1, String number2, String number3, boolean cumulative) {
-        ValueEval result = invokeValue(number1, number2, number3, cumulative);
-        assertEquals(ErrorEval.class, result.getClass());
-        assertEquals(ErrorEval.NUM_ERROR, result);
-    }
-
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org