forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-range-transform.js
More file actions
31 lines (29 loc) · 931 Bytes
/
make-range-transform.js
File metadata and controls
31 lines (29 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import clamp from 'lodash/fp/clamp'
import compose from 'lodash/fp/compose'
// Create an affine transformation to map numbers from one range to another.
export function makeRangeTransform({
domain: [minX, maxX],
range: [minY, maxY],
clampOutput = false,
}) {
const scale = (maxY - minY) / (maxX - minX)
const bias = minY - minX * scale
const transform = x => bias + x * scale
return clampOutput ? compose(clamp(minY, maxY), transform) : transform
}
// Create a transformation that passes the input through a monotonic nonlinearity
export function makeNonlinearTransform({
domain,
range,
nonlinearity,
clampOutput = false,
}) {
return function nonLinearTransform(input) {
const toOutputRange = makeRangeTransform({
domain: domain.map(nonlinearity),
range,
clampOutput,
})
return toOutputRange(nonlinearity(input))
}
}