spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[GEOMEAN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-04 16:57:36
stats
8 file(s) changed, 91 insertions(+), 5 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Statistical.js
src/Formulas/AllFormulas.ts
src/Formulas/Statistical.ts
tests/Formulas/StatisticalTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 1164e9c..ecdc272 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1792,6 +1792,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### GEOMEAN 
 11+
 12+```
 13+  Returns the geometric mean of a sample. 
 14+@param values - The numerical arguments or ranges that represent a random sample. 
 15+@returns {number} 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index 765bd85..32fd62d 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -62,7 +62,6 @@ For example 64 tbs to a qt.
 26 * COVAR
 27 * CRITBINOM
 28 * F.DIST.RT
 29-* GEOMEAN
 30 * HARMEAN
 31 * HYPGEOMDIST
 32 * LOGINV
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 6804a90..7e4c7d3 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -163,6 +163,7 @@ exports.NORMSDIST = Statistical_1.NORMSDIST;
 38 exports.NORMDIST = Statistical_1.NORMDIST;
 39 exports.NORMINV = Statistical_1.NORMINV;
 40 exports.NEGBINOMDIST = Statistical_1.NEGBINOMDIST;
 41+exports.GEOMEAN = Statistical_1.GEOMEAN;
 42 var Text_1 = require("./Text");
 43 exports.ARABIC = Text_1.ARABIC;
 44 exports.CHAR = Text_1.CHAR;
 45diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 46index 788813f..2ce18c9 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -1278,3 +1278,32 @@ var NEGBINOMDIST = function (k, r, p) {
 50     return _pdf(k, r, p);
 51 };
 52 exports.NEGBINOMDIST = NEGBINOMDIST;
 53+/**
 54+ * Returns the geometric mean of a sample.
 55+ * @param values - The numerical arguments or ranges that represent a random sample.
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var GEOMEAN = function () {
 60+    var values = [];
 61+    for (var _i = 0; _i < arguments.length; _i++) {
 62+        values[_i] = arguments[_i];
 63+    }
 64+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "GEOMEAN");
 65+    function _product(arr) {
 66+        var prod = 1;
 67+        var i = arr.length;
 68+        while (--i >= 0) {
 69+            prod *= arr[i];
 70+        }
 71+        return prod;
 72+    }
 73+    values = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber).map(function (value) {
 74+        if (value <= 0) {
 75+            throw new Errors_1.NumError("GEOMEAN requires inputs greater than 0, but one of the values entered is " + value + ".");
 76+        }
 77+        return value;
 78+    });
 79+    return Math.pow(_product(values), 1 / values.length);
 80+};
 81+exports.GEOMEAN = GEOMEAN;
 82diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 83index 883f92a..0021441 100644
 84--- a/src/Formulas/AllFormulas.ts
 85+++ b/src/Formulas/AllFormulas.ts
 86@@ -168,7 +168,8 @@ import {
 87   NORMSDIST,
 88   NORMDIST,
 89   NORMINV,
 90-  NEGBINOMDIST
 91+  NEGBINOMDIST,
 92+  GEOMEAN
 93 } from "./Statistical";
 94 import {
 95   ARABIC,
 96@@ -408,5 +409,6 @@ export {
 97   NORMSDIST,
 98   NORMDIST,
 99   NORMINV,
100-  NEGBINOMDIST
101+  NEGBINOMDIST,
102+  GEOMEAN
103 }
104\ No newline at end of file
105diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
106index df359e3..034b596 100644
107--- a/src/Formulas/Statistical.ts
108+++ b/src/Formulas/Statistical.ts
109@@ -1281,6 +1281,32 @@ var NEGBINOMDIST = function (k, r, p) {
110 };
111 
112 
113+/**
114+ * Returns the geometric mean of a sample.
115+ * @param values - The numerical arguments or ranges that represent a random sample.
116+ * @returns {number}
117+ * @constructor
118+ */
119+var GEOMEAN  = function (...values) {
120+  ArgsChecker.checkAtLeastLength(arguments, 1, "GEOMEAN");
121+  function _product(arr) {
122+    var prod = 1;
123+    var i = arr.length;
124+    while (--i >= 0) {
125+      prod *= arr[i];
126+    }
127+    return prod;
128+  }
129+  values = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber).map(function (value) {
130+    if (value <=0) {
131+      throw new NumError("GEOMEAN requires inputs greater than 0, but one of the values entered is " + value + ".");
132+    }
133+    return value;
134+  });
135+  return Math.pow(_product(values), 1 / values.length);
136+};
137+
138+
139 export {
140   AVERAGE,
141   AVERAGEA,
142@@ -1322,5 +1348,6 @@ export {
143   NORMSDIST,
144   NORMDIST,
145   NORMINV,
146-  NEGBINOMDIST
147+  NEGBINOMDIST,
148+  GEOMEAN
149 }
150\ No newline at end of file
151diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
152index a797fba..39710a4 100644
153--- a/tests/Formulas/StatisticalTest.ts
154+++ b/tests/Formulas/StatisticalTest.ts
155@@ -39,7 +39,8 @@ import {
156   NORMSDIST,
157   NORMDIST,
158   NORMINV,
159-  NEGBINOMDIST
160+  NEGBINOMDIST,
161+  GEOMEAN
162 } from "../../src/Formulas/Statistical";
163 import * as ERRORS from "../../src/Errors";
164 import {
165@@ -843,4 +844,17 @@ test("NEGBINOMDIST", function() {
166   catchAndAssertEquals(function() {
167     NEGBINOMDIST.apply(this, [0.2, 0.8, 6, 1]);
168   }, ERRORS.NA_ERROR);
169+});
170+
171+
172+test("GEOMEAN", function() {
173+  assertEquals(GEOMEAN(10, 4, 6, 3, 6, 7, 1, 1), 3.6313885790189477);
174+  assertEquals(GEOMEAN(10, 4, 6, [3, 6, [7]], 1, 1), 3.6313885790189477);
175+  assertEquals(GEOMEAN(10), 10);
176+  catchAndAssertEquals(function() {
177+    GEOMEAN(10, 2, 4, 5, 2, 0);
178+  }, ERRORS.NUM_ERROR);
179+  catchAndAssertEquals(function() {
180+    GEOMEAN.apply(this, []);
181+  }, ERRORS.NA_ERROR);
182 });
183\ No newline at end of file
184diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
185index c28333d..31af94d 100644
186--- a/tests/SheetFormulaTest.ts
187+++ b/tests/SheetFormulaTest.ts
188@@ -797,6 +797,10 @@ test("Sheet NEGBINOMDIST", function(){
189   assertFormulaEquals('=NEGBINOMDIST(5, 3, 0.2)', 0.05505024000000004);
190 });
191 
192+test("Sheet GEOMEAN", function(){
193+  assertFormulaEquals('=GEOMEAN(10, 4, 6, 3, 6, 7, 1, 1)', 3.6313885790189477);
194+});
195+
196 test("Sheet *", function(){
197   assertFormulaEquals('= 10 * 10', 100);
198   assertFormulaEquals('= 10 * 0', 0);