mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
* Add `.js` extension to source file imports * Specify package `exports` in `package.json` Specify package type as `commonjs` (It's good to be specific) * Move all compiled scripts into `lib` directory Remove ./number.js (You can use the compiled ones in `./lib/*`) Tell node that the `esm` directory is type `module` and enable tree shaking. Remove unused files from packages `files` property * Allow importing of package.json * Make library ESM first * - Fix merge conflicts - Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs` - Refactor unit tests to be able to run with plain nodejs (no transpiling) - Fix browser examples * Fix browser and browserstack tests * Fix running unit tests on Node 10 (which has no support for modules) * Fix node.js examples (those are still commonjs) * Remove the need for `browserBundle.cjs` * Generate minified bundle only * [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963) Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.** - [Release notes](https://github.com/bitinn/node-fetch/releases) - [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md) - [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * Cleanup console.log * Add integration tests to test the entry points (commonjs/esm, full/number only) * Create backward compatibility error messages in the files moved/removed since v8 * Describe breaking changes in HISTORY.md * Bump karma from 5.2.1 to 5.2.2 (#1965) Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2. - [Release notes](https://github.com/karma-runner/karma/releases) - [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md) - [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
388 lines
14 KiB
JavaScript
388 lines
14 KiB
JavaScript
import assert from 'assert'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
|
|
const math2 = math.create({ randomSeed: 'test2' })
|
|
const pickRandom = math2.pickRandom
|
|
|
|
describe('pickRandom', function () {
|
|
it('should have a function pickRandom', function () {
|
|
assert.strictEqual(typeof math.pickRandom, 'function')
|
|
})
|
|
|
|
it('should throw an error when providing a multi dimensional matrix', function () {
|
|
assert.throws(function () {
|
|
pickRandom(math.matrix([[1, 2], [3, 4]]))
|
|
}, /Only one dimensional vectors supported/)
|
|
})
|
|
|
|
it('should throw an error if the length of the weights does not match the length of the possibles', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4]
|
|
const number = 2
|
|
|
|
assert.throws(function () {
|
|
pickRandom(possibles, weights)
|
|
}, /Weights must have the same length as possibles/)
|
|
|
|
assert.throws(function () {
|
|
pickRandom(possibles, number, weights)
|
|
}, /Weights must have the same length as possibles/)
|
|
|
|
assert.throws(function () {
|
|
pickRandom(possibles, weights, number)
|
|
}, /Weights must have the same length as possibles/)
|
|
})
|
|
|
|
it('should throw an error if the weights array contains a non number or negative value', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
let weights = [1, 5, 2, -1, 6]
|
|
|
|
assert.throws(function () {
|
|
pickRandom(possibles, weights)
|
|
}, /Weights must be an array of positive numbers/)
|
|
|
|
weights = [1, 5, 2, 'stinky', 6]
|
|
|
|
assert.throws(function () {
|
|
pickRandom(possibles, weights)
|
|
}, /Weights must be an array of positive numbers/)
|
|
})
|
|
|
|
it('should return a single value if no number argument was passed', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
|
|
assert.notStrictEqual(possibles.indexOf(pickRandom(possibles)), -1)
|
|
assert.notStrictEqual(possibles.indexOf(pickRandom(possibles, weights)), -1)
|
|
})
|
|
|
|
it('should return a single value if no number argument was passed (2)', function () {
|
|
const possibles = [5]
|
|
|
|
assert.strictEqual(pickRandom(possibles), 5)
|
|
})
|
|
|
|
it('should return the given array if the given number is equal its length', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
const number = 5
|
|
|
|
assert.strictEqual(pickRandom(possibles, number), possibles)
|
|
assert.strictEqual(pickRandom(possibles, number, weights), possibles)
|
|
assert.strictEqual(pickRandom(possibles, weights, number), possibles)
|
|
})
|
|
|
|
it('should return the given array if the given number is greater than its length', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
const number = 6
|
|
|
|
assert.strictEqual(pickRandom(possibles, number), possibles)
|
|
assert.strictEqual(pickRandom(possibles, number, weights), possibles)
|
|
assert.strictEqual(pickRandom(possibles, weights, number), possibles)
|
|
})
|
|
|
|
it('should return an empty array if the given number is 0', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
const number = 0
|
|
|
|
assert.strictEqual(pickRandom(possibles, number).length, 0)
|
|
assert.strictEqual(pickRandom(possibles, number, weights).length, 0)
|
|
assert.strictEqual(pickRandom(possibles, weights, number).length, 0)
|
|
})
|
|
|
|
it('should return an array of length 1 if the number passed is 1', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
const number = 1
|
|
|
|
assert(Array.isArray(pickRandom(possibles, number)))
|
|
assert(Array.isArray(pickRandom(possibles, number, weights)))
|
|
assert(Array.isArray(pickRandom(possibles, weights, number)))
|
|
|
|
assert.strictEqual(pickRandom(possibles, number).length, 1)
|
|
assert.strictEqual(pickRandom(possibles, number, weights).length, 1)
|
|
assert.strictEqual(pickRandom(possibles, weights, number).length, 1)
|
|
})
|
|
|
|
it('should pick the given number of values from the given array', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 5, 2, 4, 6]
|
|
const number = 3
|
|
|
|
assert.strictEqual(pickRandom(possibles, number).length, number)
|
|
assert.strictEqual(pickRandom(possibles, number, weights).length, number)
|
|
assert.strictEqual(pickRandom(possibles, weights, number).length, number)
|
|
})
|
|
|
|
it('should pick a value from the given array following an uniform distribution if only possibles are passed', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push(pickRandom(possibles))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
})
|
|
|
|
it('should pick a value from the given matrix following an uniform distribution', function () {
|
|
const possibles = math.matrix([11, 22, 33, 44, 55])
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push(pickRandom(possibles))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
})
|
|
|
|
it('should pick a given number of values from the given array following an uniform distribution if no weights were passed', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const number = 2
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, number))
|
|
})
|
|
|
|
assert.strictEqual(picked.length, 2000)
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
})
|
|
|
|
it('should pick numbers from the given matrix following an uniform distribution', function () {
|
|
const possibles = math.matrix([11, 22, 33, 44, 55])
|
|
const number = 3
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, number))
|
|
})
|
|
|
|
assert.strictEqual(picked.length, 3000)
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
})
|
|
|
|
it('should pick a value from the given array following a weighted distribution', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 4, 0, 2, 3]
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push(pickRandom(possibles, weights))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
})
|
|
|
|
it('should pick a value from the given matrix following a weighted distribution', function () {
|
|
const possibles = math.matrix([11, 22, 33, 44, 55])
|
|
const weights = [1, 4, 0, 2, 3]
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push(pickRandom(possibles, weights))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
})
|
|
|
|
it('should return an array of values from the given array following a weighted distribution', function () {
|
|
const possibles = [11, 22, 33, 44, 55]
|
|
const weights = [1, 4, 0, 2, 3]
|
|
const number = 2
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, number, weights))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, weights, number))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
})
|
|
|
|
it('should return an array of values from the given matrix following a weighted distribution', function () {
|
|
const possibles = math.matrix([11, 22, 33, 44, 55])
|
|
const weights = [1, 4, 0, 2, 3]
|
|
const number = 2
|
|
const picked = []
|
|
let count
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, number, weights))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
|
|
times(1000, function () {
|
|
picked.push.apply(picked, pickRandom(possibles, weights, number))
|
|
})
|
|
|
|
count = picked.filter(function (val) { return val === 11 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
|
|
|
|
count = picked.filter(function (val) { return val === 22 }).length
|
|
assert.strictEqual(math.round((count) / picked.length, 1), 0.4)
|
|
|
|
count = picked.filter(function (val) { return val === 33 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0)
|
|
|
|
count = picked.filter(function (val) { return val === 44 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
|
|
|
|
count = picked.filter(function (val) { return val === 55 }).length
|
|
assert.strictEqual(math.round(count / picked.length, 1), 0.3)
|
|
})
|
|
|
|
it('should throw an error in case of wrong type of arguments', function () {
|
|
assert.throws(function () { pickRandom(23) }, /Unexpected type of argument/)
|
|
assert.throws(function () { pickRandom() }, /Too few arguments/)
|
|
assert.throws(function () { pickRandom([], 23, [], 9) }, /Too many arguments/)
|
|
|
|
// TODO: more type testing...
|
|
})
|
|
|
|
it('should LaTeX pickRandom', function () {
|
|
const expression = math.parse('pickRandom([1,2,3])')
|
|
assert.strictEqual(expression.toTex(), '\\mathrm{pickRandom}\\left(\\begin{bmatrix}1\\\\2\\\\3\\\\\\end{bmatrix}\\right)')
|
|
})
|
|
})
|
|
|
|
function times (n, callback) {
|
|
for (let i = 0; i < n; i++) {
|
|
callback()
|
|
}
|
|
}
|