sum-product
Advanced tools
Comparing version 0.0.3 to 1.0.0
47
index.js
@@ -1,19 +0,32 @@ | ||
function sumProduct (array) { // can use any number of arrays | ||
var sum = 0 | ||
var N = Array.isArray(array) ? array.length : 0 | ||
for (var i = 0, args = []; i < arguments.length; i++) { | ||
// if arrays don't have the same length, we use the shortest one | ||
if (!Array.isArray(arguments[i])) return NaN | ||
args[i] = arguments[i] | ||
if (args[i].length < N) N = args[i].length | ||
} | ||
module.exports = sumProduct | ||
for (i = 0; i < N; i++) { | ||
var prod = 1 | ||
for (var j = 0; j < args.length; j++) prod *= args[j][i] | ||
sum += prod | ||
} | ||
return Number(sum) | ||
/** | ||
* @param {array} array - first source array | ||
* @param {array} [other] - any number of additional arrays | ||
* @return {number} sum of the product | ||
*/ | ||
function sumProduct (array /*other arrays*/) { | ||
if (!arguments.length) return 0 | ||
if (!Array.isArray(array)) return NaN | ||
var dim = arguments.length, | ||
len = array.length, | ||
args = [array], | ||
sum = 0, | ||
err = 0, | ||
tot = 0 | ||
for (var i = 1; i < dim; ++i) { | ||
if (!Array.isArray(arguments[i]) || arguments[i].length !== len) return NaN | ||
args[i] = arguments[i] | ||
} | ||
//modified Kahan Sum | ||
for (i = 0; i < len; ++i) { | ||
var prod = 1 | ||
for (var j = 0; j < args.length; ++j) prod *= args[j][i] | ||
sum += prod | ||
err += (sum - tot) - prod | ||
tot = sum | ||
} | ||
return sum - err | ||
} | ||
module.exports = sumProduct |
{ | ||
"name": "sum-product", | ||
"version": "0.0.3", | ||
"description": "classic spreadsheet sum of array item products", | ||
"author": "Hugo Villeneuve", | ||
"license": "MIT", | ||
"keywords": [ | ||
"spreadsheet", | ||
"array", | ||
"statistics", | ||
"sum", | ||
"product", | ||
"sumproduct" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/hville/sum-product.git" | ||
}, | ||
"main": "./index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tt": "1.0.3", | ||
"untap": "0.0.4" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.0", | ||
"npm": "2.x" | ||
}, | ||
"files": [ | ||
"src/" | ||
], | ||
"scripts": { | ||
"test": "node test.js | untap" | ||
} | ||
"name": "sum-product", | ||
"version": "1.0.0", | ||
"description": "classic spreadsheet sum of array item products", | ||
"author": "Hugo Villeneuve", | ||
"license": "MIT", | ||
"keywords": [ | ||
"spreadsheet", | ||
"array", | ||
"statistics", | ||
"sum", | ||
"product", | ||
"sumproduct" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/hville/sum-product.git" | ||
}, | ||
"main": "./index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"cotest": "^1.0.1" | ||
}, | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org/" | ||
} | ||
} |
# sum-product | ||
1. [Introduction](#introduction) | ||
1. [Installation](#installation) | ||
1. [Usage](#usage) | ||
1. [Test](#test) | ||
1. [License](#license) | ||
• [Usage](#usage) • [Test](#test) • [License](#license) • | ||
## Introduction | ||
Simple and small standalone function to calculate the sum of the product of array items. | ||
`sumProduct([0,1], [2,3], [4,5]) // 0*2*4 + 1*3*5 = 15` | ||
`sumProduct([0,1], [2,3], [4,5]) // 0*2*4 + 1*3*5 = 15` | ||
## Installation | ||
Uses a modified Kahan sum to reduce floating point errors. | ||
In node, from the root of your project folder type `npm install --save sum-product`. | ||
## Usage | ||
@@ -24,19 +14,12 @@ | ||
```javascript | ||
var sumProduct = require('sum-product') | ||
var result = sumProduct([0,1], [2,3], [4,5]) // 0*2*4 + 1*3*5 = 15 | ||
result = sumProduct() // ==>0 | ||
result = sumProduct([]) // ==>0 | ||
result = sumProduct([a]) // ==>NaN | ||
``` | ||
var sumProduct = require('sum-product') | ||
var result = sumProduct([0,1], [2,3], [4,5]) // 0*2*4 + 1*3*5 = 15 | ||
result = sumProduct() // ==>0 | ||
result = sumProduct([]) // ==>0 | ||
result = sumProduct([a]) // ==>NaN | ||
``` | ||
## Test | ||
In node, from the root of your project folder type `npm test`. | ||
## License | ||
Released under the [MIT License](http://www.opensource.org/licenses/MIT) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
3443
1
5
55
1
25