@jrc03c/js-math-tools
Version:
some math tools for JS
83 lines (70 loc) • 1.76 kB
JavaScript
import { argmin } from "./argmin.mjs"
import { DataFrame, Series } from "./dataframe/index.mjs"
import { expect, test } from "@jrc03c/fake-jest"
import { forEach } from "./for-each.mjs"
import { normal } from "./normal.mjs"
import { range } from "./range.mjs"
import { shuffle } from "./shuffle.mjs"
test("gets the argmin of various kinds of containers", () => {
const a = shuffle(range(0, 100).toArray())
expect(argmin(a)).toStrictEqual(a.indexOf(0))
const b = normal([50, 50])
let minRow = 0
let minCol = 0
let min = Infinity
forEach(b, (row, i) => {
forEach(row, (v, j) => {
if (v < min) {
min = v
minRow = i
minCol = j
}
})
})
expect(argmin(b)).toStrictEqual([minRow, minCol])
const c = new Series(normal(100))
expect(argmin(c)).toBe(c.index[c.values.indexOf(Math.min(...c.values))])
const d = new DataFrame(normal([50, 50]))
minRow = 0
minCol = 0
min = Infinity
forEach(d.values, (row, i) => {
forEach(row, (v, j) => {
if (v < min) {
min = v
minRow = i
minCol = j
}
})
})
expect(argmin(d)).toStrictEqual([d.index[minRow], d.columns[minCol]])
expect(argmin([432n, 324n, 342n, 234n, 243n, 423n])).toBe(3)
expect(argmin([-2n, -3n, -4n, -234n, -23n])).toBe(3)
const e = normal(100)
e[0] = "uh-oh!"
expect(argmin(e)).toBe(undefined)
expect(argmin(e, true)).not.toBe(undefined)
const wrongs = [
0,
1,
2.3,
-2.3,
Infinity,
-Infinity,
NaN,
"foo",
true,
false,
null,
undefined,
Symbol.for("Hello, world!"),
x => x,
function (x) {
return x
},
{ hello: "world" },
]
forEach(wrongs, v => {
expect(() => argmin(v)).toThrow()
})
})