Published unlisted
Edited
Jul 30, 2022
Insert cell
Insert cell
function sine_arclength(x) {
let i = Math.round(x * (1/Math.PI)); x -= i*Math.PI; // reduce to [-π/2, π/2]
let sx = (x > 0) - (x < 0); x *= sx; // reduce to [0, π/2]
let y = cheb_eval(sine_arclength_coeffs, x * (4/Math.PI) - 1);
const K = 3.820197789027712;
return i * K + sx * y;
}
Insert cell
Insert cell
Insert cell
f = (x) =>
sine_arclength(x) - x*(3.820197789027712/Math.PI);
Insert cell
// Coeffs generated using Chebfun (http://www.chebfun.org) using the Matlab code:
// f = cumsum(chebfun(@(x) sqrt(1 + cos(x)*cos(x)), [0, pi/2])); format longe; f.coeffs
sine_arclength_coeffs = [
1.003779909381942e+00, 9.563086185904272e-01, -5.148678032547906e-02, -1.469577505554187e-03,
2.794604256600180e-03, 2.223165035001421e-04, -3.636212528869967e-05, -1.195702400746026e-05,
-2.042301587639539e-06, 1.421301353688803e-08, 1.159950864889720e-07, 3.388424989626866e-08,
2.975891659682502e-09, -1.314232608846369e-09, -6.149778187104962e-10, -1.019788962754806e-10,
1.210888156113980e-11, 1.153819027032262e-11, 2.849307759807778e-12, 4.221699706939989e-14,
-2.112279791468886e-13, -7.398458675657313e-14, -7.696536772197088e-15, 3.487971687135836e-15,
1.827243706664117e-15, 3.310356733875579e-16,
]
Insert cell
// Clenshaw's method.
cheb_eval = function cheb_eval(coeffs, x) {
const x2 = 2 * x;
let d = coeffs.length - 1, b2 = 0, b1 = (d % 2) ? coeffs[d--] : 0;
for (let i = d; i >= 2; i -= 2) {
b2 = coeffs[i] + x2 * b1 - b2;
b1 = coeffs[i-1] + x2 * b2 - b1;
}
return coeffs[0] + x * b1 - b2;
}
Insert cell
{
const n = 1e6;
const inputs = [...Array(n)].map(_ => Math.random() * 20);
const outputs = Array(n);
const t0 = +new Date;
for (let i = 0; i < n; ++i) {
outputs[i] = sine_arclength(inputs[i]);
}
return (+new Date) - t0;
}

Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more