Public
Edited
Mar 12, 2024
Importers
1 star
Chandrupatla’s root-finding method
Sidi’s root-finding method
Regular numbersDruidJS workerNatural breaksDistance to a segmentRay out of a convex hullWord Tour: 40k words and their friendsHello, @thi.ng/grid-iteratorsHead/tail breaksPseudo-blue noise shaderHow fast does walk-on-spheres converge?AoC 12: shortest path under constraintsKDE estimationPlot: Correlation heatmapPoisson Finish 2Poisson disk sampling functionsWoS with transportSimple and surprising sortLocal medianTime series topological subsamplingUnion-FindLevel set experiment 1Mean value coordinatesPoisson potentialMiddle-squareWorld of squares (spherical)World of squaresLargest Inscribed SquareHello, PyWaveletsGeothmetic meandianHello, Reorder.jsGeometric MedianImage FFTTransport to a mapDisc TransportTP3: Power Diagram and Semi-Discrete Optimal TransportThe blue waveHello, genetic-jsSliced Optimal TransportDruidJSSelf-Organizing Maps meet DelaunayHello, polygon-clippingseedrandom, minimalWalk on Spheres 2Walk on SpheresHello, AutoencoderKaprekar’s numberVoronoiMap2DHello, ccwt.jsPolygon TriangulationQuantile.invert?Linear congruential generatorHue blurNeedle in a haystackMoving average blurApollo 11 implementation of trigonometric functions, by Margaret H. Hamilton (march 1969)2D curves intersectionThe 2D approximate Newton-Raphson methodInverting Lee’s Tetrahedral projectionLinde–Buzo–Gray stipplingMean shift clustering with kd-tree2D point distributionsShortest pathKahan SummationHello, delatinDijkstra’s algorithm in gpu.jsLloyd’s relaxation on a graphManhattan DiameterManhattan VoronoiMobility landscapes — an introductionDijkstra’s shortest-path treeH3 odditiesProtein MatrixConvex Spectral WeightsSort stuff by similarityKrigingDelaunay.findTrianglen-dimensions binning?Travelling with a self-organizing mapUMAP-o-MaticMNIST & UMAP-jsHello UMAP-jsMean shift clusteringLevenshtein transitionRd quasi-random sequencesAutomated label placement (countries)Phyllotaxis explainedMotionrugsPlanar hull (Andrew’s monotone chain algorithm)South Africa’s medial axisTravelling salesperson approximation with t-SNEDistance to shoreWorkerngraph: pagerank, louvain…t-SNE VoronoiCloud ContoursCircular function drawingKruskal MazeMyceliumTravelling salesperson approximation on the globe, with t-SNEtsne.jstsne.js & worker
Insert cell
Insert cell
f = x => (x + 3) * (x - 1) ** 3
Insert cell
function updateDividedDiffs(xvals, divdiffs, x, fx, k) {
xvals.unshift(x);
divdiffs.unshift(fx);
for (let i = 1; i <= xvals.length; i++)
divdiffs[i] = (divdiffs[i] - divdiffs[i - 1]) / (xvals[i] - xvals[0]);
if (xvals.length > k + 1) {
xvals.pop();
divdiffs.pop();
}
}
Insert cell
function sidi(f, k) {
const xvals = [0, 1],
divdiffs = [f(xvals[1])];

updateDividedDiffs(xvals, divdiffs, xvals.shift(), f(xvals[0]), k);

return { divdiffs };
}
Insert cell
sidi(f, 2)
Insert cell
Sidi_method(f, -100, 1000)
Insert cell
Sidi_method(Math.cos, 0, 1, 0.99)
Insert cell
// https://github.com/susiang100/cejs/blob/master/data/math.js#L1302
/**
* 求根/求取反函數 equation^-1(y)。 using Sidi's generalized secant method.
*
* @param {Function}equation
* 演算式, mapping function
* @param {Number}x0
* 求值之自變數 variable 下限,設定初始近似值。
* @param {Number}x1
* 求值之自變數 variable 上限,設定初始近似值。
* @param {Number}[y]
* 目標值。default: 0. get (equation^-1)(y)
* @param {Object}[options]
* options 設定特殊功能:<br />
*
* @returns {Number}root: equation(root)≈y
*
* @see https://en.wikipedia.org/wiki/Root-finding_algorithm
* @see https://en.wikipedia.org/wiki/Sidi's_generalized_secant_method
*/
function Sidi_method(equation, x0 = 0, x1 = 1, y, options) {
// default error, accuracy, stopping tolerance, 容許誤差
var error = Number.EPSILON;
if (!options) options = {};
else if (options > 0) error = options;
else if (options.error > 0) error = Math.abs(options.error);

y = +y || 0;

var count = (options.count || 40) | 0,
// assert: y0 = equation(x0)
y0 = 'y0' in options ? options.y0 : equation(x0),
// assert: y1 = equation(x1)
y1 = 'y1' in options ? options.y1 : equation(x1);

if (
typeof options.start_OK === 'function' &&
// 初始測試: Invalid initial value, 不合理的初始值,因此毋須繼續。
!options.start_OK(y0, y1)
)
return;

// initialization
var x2 = x1 - ((x1 - x0) * (y1 - y)) / (y1 - y0),
//
y2 = equation(x2),
x3 = x2,
y3 = y2,
// divided differences, 1階差商
y10 = (y1 - y0) / (x1 - x0),
y21 = (y2 - y1) / (x2 - x1),
// 2階差商
y210 = (y21 - y10) / (x2 - x0),
// 暫時使用。
denominator;

// main loop of Sidi's generalized secant method (take k = 2)
while (
error < Math.abs(y3 - y) &&
count-- > 0 &&
// 檢查是否兩個差距極小的不同輸入,獲得相同輸出。
y21 !== 0 &&
// 分母不應為 0 或 NaN。
(denominator = y21 + y210 * (x2 - x1)) &&
// Avram Sidi (2008), "Generalization Of The Secant Method For Nonlinear
// Equations"
// 可能需要考量會不會有循環的問題。
((x3 = x2 - (y2 - y) / denominator) !== x2 || x2 !== x1 || x1 !== x0)
) {
// evaluate result
y3 = equation(x3);
// console.log(count + ': ' + x3 + ',' + y3 + ' → error ' + (y3 - y));
// shift items
(x0 = x1), (y0 = y1);
(x1 = x2), (y1 = y2);
(x2 = x3), (y2 = y3);
// reckon divided differences
y10 = y21;
y21 = (y2 - y1) / (x2 - x1);
// y210 = (y21 - y10) / (x2 - x0);
// incase y21 === y10
if ((y210 = y21 - y10)) y210 /= x2 - x0;
// console.log('divided differences: ' + [ y10, y21, y210 ]);
}

return { x: x3, count: 40 - count, fm: y3 - y };
}
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