X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fsolver%2Fformula%2FGeomeanFormulaFunction.java;fp=bundles%2Forg.simantics.spreadsheet%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fsolver%2Fformula%2FGeomeanFormulaFunction.java;h=25f3f5814f9287f96f6a81c638e220d64e276532;hb=758f56e06b3849ded15ca0c1f842ecf083705ad2;hp=0000000000000000000000000000000000000000;hpb=2e3ff6ba450ca80d7cb4522cf6fa3a99587a484b;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/formula/GeomeanFormulaFunction.java b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/formula/GeomeanFormulaFunction.java new file mode 100644 index 000000000..25f3f5814 --- /dev/null +++ b/bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/formula/GeomeanFormulaFunction.java @@ -0,0 +1,45 @@ +package org.simantics.spreadsheet.solver.formula; + +import org.simantics.spreadsheet.SpreadsheetMatrix; +import org.simantics.spreadsheet.Spreadsheets; +import org.simantics.spreadsheet.solver.formula.parser.ast.AstArgList; +import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue; + +public class GeomeanFormulaFunction implements CellFormulaFunction { + + @Override + public Object evaluate(CellValueVisitor visitor, AstArgList args) { + if (args.values.size() == 0) + throw new IllegalStateException(); + double result = 1.0; + double count = 0.0; + for (AstValue value : args.values){ + Object r = value.accept(visitor); + if (r instanceof SpreadsheetMatrix) { + Object v = ((SpreadsheetMatrix) r).productWithFormulaError(); + if(v instanceof String) return v; + double dval = ((Number)v).doubleValue(); + if(dval!=0.0){ + result = result*dval; + count += ((SpreadsheetMatrix) r).countOfActualDoubleValues(); + } + } else { + FormulaError2 error = FormulaError2.forObject(r); + if(error!=null) return error.getString(); + + Number vNum = Spreadsheets.asValidNumber(r); + Double v = null; + if(vNum!=null) v = vNum.doubleValue(); + + if(v!=null){ + if(v<=0) return FormulaError2.NUM.getString(); + result = result*v; + count++; + } + } + } + if(result==0.0 || count==0.0) return FormulaError2.NUM.getString(); + return Double.valueOf(Math.pow(result, (1.0/count))); + } + +}