use of org.apache.poi.ss.formula.eval.ErrorEval in project poi by apache.
the class LinearRegressionFunction method evaluateInternal.
private double evaluateInternal(ValueVector x, ValueVector y, int size) throws EvaluationException {
// error handling is as if the x is fully evaluated before y
ErrorEval firstXerr = null;
ErrorEval firstYerr = null;
boolean accumlatedSome = false;
// first pass: read in data, compute xbar and ybar
double sumx = 0.0, sumy = 0.0;
for (int i = 0; i < size; i++) {
ValueEval vx = x.getItem(i);
ValueEval vy = y.getItem(i);
if (vx instanceof ErrorEval) {
if (firstXerr == null) {
firstXerr = (ErrorEval) vx;
continue;
}
}
if (vy instanceof ErrorEval) {
if (firstYerr == null) {
firstYerr = (ErrorEval) vy;
continue;
}
}
// only count pairs if both elements are numbers
if (vx instanceof NumberEval && vy instanceof NumberEval) {
accumlatedSome = true;
NumberEval nx = (NumberEval) vx;
NumberEval ny = (NumberEval) vy;
sumx += nx.getNumberValue();
sumy += ny.getNumberValue();
} else {
// all other combinations of value types are silently ignored
}
}
double xbar = sumx / size;
double ybar = sumy / size;
// second pass: compute summary statistics
double xxbar = 0.0, xybar = 0.0;
for (int i = 0; i < size; i++) {
ValueEval vx = x.getItem(i);
ValueEval vy = y.getItem(i);
if (vx instanceof ErrorEval) {
if (firstXerr == null) {
firstXerr = (ErrorEval) vx;
continue;
}
}
if (vy instanceof ErrorEval) {
if (firstYerr == null) {
firstYerr = (ErrorEval) vy;
continue;
}
}
// only count pairs if both elements are numbers
if (vx instanceof NumberEval && vy instanceof NumberEval) {
NumberEval nx = (NumberEval) vx;
NumberEval ny = (NumberEval) vy;
xxbar += (nx.getNumberValue() - xbar) * (nx.getNumberValue() - xbar);
xybar += (nx.getNumberValue() - xbar) * (ny.getNumberValue() - ybar);
} else {
// all other combinations of value types are silently ignored
}
}
double beta1 = xybar / xxbar;
double beta0 = ybar - beta1 * xbar;
if (firstXerr != null) {
throw new EvaluationException(firstXerr);
}
if (firstYerr != null) {
throw new EvaluationException(firstYerr);
}
if (!accumlatedSome) {
throw new EvaluationException(ErrorEval.DIV_ZERO);
}
if (function == FUNCTION.INTERCEPT) {
return beta0;
} else {
return beta1;
}
}
use of org.apache.poi.ss.formula.eval.ErrorEval in project poi by apache.
the class TestFixed method testOptionalParams.
@Test
public void testOptionalParams() {
Fixed fixed = new Fixed();
ValueEval evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789));
assertTrue(evaluate instanceof StringEval);
assertEquals("1,234.57", ((StringEval) evaluate).getStringValue());
evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1));
assertTrue(evaluate instanceof StringEval);
assertEquals("1,234.6", ((StringEval) evaluate).getStringValue());
evaluate = fixed.evaluate(0, 0, new NumberEval(1234.56789), new NumberEval(1), BoolEval.TRUE);
assertTrue(evaluate instanceof StringEval);
assertEquals("1234.6", ((StringEval) evaluate).getStringValue());
evaluate = fixed.evaluate(new ValueEval[] {}, 1, 1);
assertTrue(evaluate instanceof ErrorEval);
evaluate = fixed.evaluate(new ValueEval[] { new NumberEval(1), new NumberEval(1), new NumberEval(1), new NumberEval(1) }, 1, 1);
assertTrue(evaluate instanceof ErrorEval);
}
use of org.apache.poi.ss.formula.eval.ErrorEval in project poi by apache.
the class TestEDate method testEDateInvalidValues.
@Test
public void testEDateInvalidValues() {
EDate eDate = new EDate();
ErrorEval result = (ErrorEval) eDate.evaluate(new ValueEval[] { new NumberEval(1000) }, null);
assertEquals(FormulaError.VALUE.getCode(), result.getErrorCode(), 0);
}
use of org.apache.poi.ss.formula.eval.ErrorEval in project poi by apache.
the class TestEDate method testEDateInvalidValueEval.
@Test
public void testEDateInvalidValueEval() {
ValueEval evaluate = new EDate().evaluate(new ValueEval[] { new ValueEval() {
}, new NumberEval(0) }, null);
assertTrue(evaluate instanceof ErrorEval);
assertEquals(ErrorEval.VALUE_INVALID, evaluate);
}
use of org.apache.poi.ss.formula.eval.ErrorEval in project poi by apache.
the class TestEOMonth method testEOMonthInvalidArguments.
@Test
public void testEOMonthInvalidArguments() {
ValueEval result = eOMonth.evaluate(new ValueEval[] { new NumberEval(DATE_1902_09_26) }, ec);
assertTrue(result instanceof ErrorEval);
assertEquals(FormulaError.VALUE.getCode(), ((ErrorEval) result).getErrorCode(), 0);
result = eOMonth.evaluate(new ValueEval[] { new StringEval("a"), new StringEval("b") }, ec);
assertTrue(result instanceof ErrorEval);
assertEquals(FormulaError.VALUE.getCode(), ((ErrorEval) result).getErrorCode(), 0);
}
Aggregations