Published
Edited
Nov 17, 2020
Importers
20 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chordAreaInverse = a => Math.pow(a, .6) - Math.pow(1 - a, .6)
Insert cell
Insert cell
chordAreaInverse2 = {
const nodes = [
0.5, 0, 2.3584407814e-1, 2.0697979931e-2, 2.0894975402e-3, 1.1722274484e-1,
2.1236567413e-4, 4.6305986706e-1, 5.3748971858e-5, 8.1099031276e-3, 5.8743883731e-2],
values = [
1.5874010464, 1.4053918380, 1.4973407222, 1.4208001108, 1.4086435353, 1.4585411281,
1.4060959864, 1.5737389975, 1.4056733185, 1.4135101277, 1.4374270332],
weights = [
2.5236473233e-1, 3.2702933141e-6, 2.1134869853e-1, -4.5296987518e-2,
-1.8210616414e-3, -2.2914836584e-1, 2.1728601712e-4, -3.3969424647e-1,
-6.8763426681e-5, 1.3707998140e-2, 1.3854310301e-1];
return function chordAreaInverse(a) {
var s = (a <= 0.5) - (a > 0.5), b = (a > 0.5) + s * a, c = Math.cbrt(b);
return s * (reval(nodes, values, weights, b) * c * c - 1);
}

function reval(nodes, values, weights, x) {
var n = nodes.length, p = 0, q = 0, f, j, xj;
for (j = 0; j < n; j++) {
q += xj = weights[j] / (x - nodes[j]);
p += xj * values[j]; }
f = p / q;
if (f !== f) {
for (j = 0; j < n; j++) {
if (x - nodes[j] === 0) {
f = values[j]; break; }}}
return f;
};
}
Insert cell
Insert cell
time(chordAreaInverse)
Insert cell
time(chordAreaInverse2)
Insert cell
error(chordAreaInverse)
Insert cell
error(chordAreaInverse2)
Insert cell
time = func => {
const N = 1e6;
const a = Array.from({length: N}, Math.random);
const t = performance.now();
for (let i = 0; i < N; i++) func(a[i]);
return md`${Math.round(performance.now() - t)} μs per evaluation`;
}
Insert cell
// worst case round-trip error
error = func => {
const N = 1e6;
let err = 0;
for (let i = 0; i <= N; i++) {
const a = i / N;
err = Math.max(Math.abs(chordArea(func(a) / 2 + 0.5) - a), err);
}
return err;
}
Insert cell
Insert cell
A = th => (th - sin(th)) / π / 2 // 0 to 1
Insert cell
chordArea = x => A(2 * acos(1 - 2 * x))
Insert cell
π = Math.PI
Insert cell
chordArea(.5)
Insert cell
DIFF = (x, e) => Math.abs(x - (1 + approx2(chordArea(x), e)) / 2)
Insert cell
approx2 = (t, e) => Math.pow(t, e) - Math.pow(1 - t, e)
Insert cell
d3.max(
Float64Array.from({ length: 100000 }, (_, i) => DIFF(i / (100000 - 1), 0.6))
)
Insert cell
d3.median(
Float64Array.from({ length: 100000 }, (_, i) => DIFF(i / (100000 - 1), 0.6))
)
Insert cell
Insert cell
Insert cell
Insert cell
f = x => sqrt(1 - x * x)
Insert cell
CDF = integral(f, -1, 1)
Insert cell
CDF(.3)
Insert cell
CDF.invert(0.6893155371739725)
Insert cell
CDF(0.1)
Insert cell
CDF.invert(CDF(0.1))
Insert cell
function integral(f, a, b) {
const n = 10000;
const step = (1 / (n - 1)) * (b - a);
const v = d3.cumsum(
Float32Array.from({ length: n }, (_, i) => f(a + i * step) * step)
);
const C = 1 / (v[v.length - 1] - v[0]);
for (let i = 0; i < n; ++i) v[i] = (v[i] - v[0]) * C;
const g = d3.scaleSequential(d3.piecewise(v)).domain([a, b]);
g.invert = x => {
const i = d3.bisect(v, x),
delta = (x - v[i]) / (v[i + 1] - v[i]);
return ((i + (isNaN(delta) ? 0 : delta)) / (n - 1)) * (b - a) + a;
};
return g;
}
Insert cell
import { acos, cos, sin, sqrt } from "@fil/math"
Insert cell
d3 = require("d3@6")
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more