add COVARIANCE.S function

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901687 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-06-05 19:55:50 +00:00
parent f0b78f9e3b
commit 41dfbd67c5
4 changed files with 17 additions and 2 deletions

View File

@ -91,6 +91,7 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "COUPNUM", null);
r(m, "COUPPCD", null);
r(m, "COVARIANCE.P", Covar.instanceP);
r(m, "COVARIANCE.S", Covar.instanceS);
r(m, "CUBEKPIMEMBER", null);
r(m, "CUBEMEMBER", null);
r(m, "CUBEMEMBERPROPERTY", null);

View File

@ -42,6 +42,8 @@ public class Correl extends TwoArrayFunction {
public static final Correl instance = new Correl();
private Correl() {}
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
try {

View File

@ -37,7 +37,14 @@ import java.util.List;
*/
public class Covar extends TwoArrayFunction implements FreeRefFunction {
public static final Covar instanceP = new Covar();
public static final Covar instanceP = new Covar(false);
public static final Covar instanceS = new Covar(true);
private final boolean sampleBased;
private Covar(boolean sampleBased) {
this.sampleBased = sampleBased;
}
@Override
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
@ -45,7 +52,7 @@ public class Covar extends TwoArrayFunction implements FreeRefFunction {
final List<DoubleList> arrays = getNumberArrays(arg0, arg1);
final Covariance covar = new Covariance();
final double result = covar.covariance(
arrays.get(0).toArray(), arrays.get(1).toArray(), false);
arrays.get(0).toArray(), arrays.get(1).toArray(), sampleBased);
return new NumberEval(result);
} catch (EvaluationException e) {
return e.getErrorEval();

View File

@ -46,6 +46,7 @@ final class TestCovar {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
assertDouble(fe, cell, "COVAR(A2:A6,B2:B6)", 5.2, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.P(A2:A6,B2:B6)", 5.2, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.S(A2:A6,B2:B6)", 6.5, 0.0000000005);
}
}
@ -58,6 +59,7 @@ final class TestCovar {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
assertDouble(fe, cell, "COVAR(A2:A6,B2:B6)", 5.5625, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.P(A2:A6,B2:B6)", 5.5625, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.S(A2:A6,B2:B6)", 7.416666666666666, 0.0000000005);
}
}
@ -70,6 +72,7 @@ final class TestCovar {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
assertDouble(fe, cell, "COVAR(A2:A6,B2:B6)", 5.5625, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.P(A2:A6,B2:B6)", 5.5625, 0.0000000005);
assertDouble(fe, cell, "COVARIANCE.S(A2:A6,B2:B6)", 7.416666666666666, 0.0000000005);
}
}
@ -82,8 +85,10 @@ final class TestCovar {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
assertError(fe, cell, "COVAR(A2:A6,B2:B5)", FormulaError.NA);
assertError(fe, cell, "COVARIANCE.P(A2:A6,B2:B5)", FormulaError.NA);
assertError(fe, cell, "COVARIANCE.S(A2:A6,B2:B5)", FormulaError.NA);
assertError(fe, cell, "COVAR(A2:B6,B2:B6)", FormulaError.NA);
assertError(fe, cell, "COVARIANCE.P(A2:B6,B2:B6)", FormulaError.NA);
assertError(fe, cell, "COVARIANCE.S(A2:B6,B2:B6)", FormulaError.NA);
}
}