forked from WebMemex/webmemex-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-range-transform.js
More file actions
22 lines (20 loc) · 897 Bytes
/
Copy pathmake-range-transform.js
File metadata and controls
22 lines (20 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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))
}
}