Published
Edited
Apr 11, 2021
Importers
9 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Clenshaw's method.
evaluate = function evaluate(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
// definite integral from -1 to 1
sum = function sum(coeffs) {
let s = 0, i = coeffs.length;
i = i + i % 2;
while ((i -= 2) >= 0) s += 2 * coeffs[i] / (1 - i*i);
return s;
}
Insert cell
// indefinite integral
cumsum = function cumsum(coeffs) {
let c = coeffs.slice(), i = c.push(0, 0) - 1, k = 0, newcoeffs = [];
c[0] *= 2;
while (--i) k = (newcoeffs[i] = (c[i-1] - c[i+1]) / (2*i)) - k;
newcoeffs[0] = (1 - 2 * coeffs.length % 2) * k;
return newcoeffs;
}
Insert cell
// derivative
diff = function diff(coeffs) {
let newcoeffs = [], i = coeffs.length;
newcoeffs[i--] = newcoeffs[i] = 0;
while (i--) newcoeffs[i] = newcoeffs[i+2] + 2*(i+1)*coeffs[i+1];
newcoeffs[0] *= .5;
newcoeffs.splice(-2);
return newcoeffs;
}
Insert cell
// Chebyshev nodes of the 2nd kind in [-1, 1]
chebpts = function chebpts (n) {
let pts = [], scale = Math.PI * .5 / (n - 1);
for (let i = n - 1; i >= 0; i--) {
pts[i] = Math.sin((1 - n + 2*i) * scale);
}
return pts;
}
Insert cell
Insert cell
coeffs2vals = function coeffs2vals (coeffs) {
// for now, only deal with real-valued coefficients.
coeffs = coeffs.slice();
const n = coeffs.length - 1;
if (n <= 0) return coeffs;
for (let i = 1; i < n; i++) {
coeffs[2*n - i] = coeffs[i] *= .5;
}
let imag = []; imag.length = 2*n; imag.fill(0);
fft_in_place(coeffs, imag);
let values = [];
for (let i = n; i >=0; i--) {
values[i] = coeffs[n-i];
}
return values;
}
Insert cell
vals2coeffs = function vals2coeffs (vals) {
const n = vals.length - 1;
if (n <= 0) return vals.slice();

let coeffs = [];
for (let i = n - 1; i >= 1; i--) {
coeffs[n + i] = coeffs[n - i] = vals[i];
}
coeffs[0] = vals[n]; coeffs[n] = vals[0];
let imag = []; imag.length = 2*n; imag.fill(0);
fft_in_place(imag, coeffs);
const scale = 1/n;
for (let i = 1; i <= n; i++) {
coeffs[i] *= scale;
}
coeffs[0] *= scale * .5; coeffs[n] *= scale * .5;
coeffs.splice(n+1);
return coeffs;
}
Insert cell
// Either truncate the array or pad it with zeros to match the desired length.
fixlength = function fixlength(array, n) {
const k = array.length;
if (k < n) { array.length = n; array.fill(0, k); }
else { array.splice(n); }
return array
}
Insert cell
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