This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function iterations2spec(iterations) { | |
// Because the initial guess can be changed now we need to scale equation line to iterations | |
const max_x = Math.max(...iterations.map(d => d.x)); | |
const min_x = Math.min(...iterations.map(d => d.x)); | |
const equation_line = { | |
"width": 800, | |
"height": 600, | |
"data": { | |
"sequence": { | |
// Draw equation within -4 to 4 window, if iterations are inside of window |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [result, setResult] = React.useState({ root: undefined, iterations: [] }); | |
function handleSubmit(event) { | |
event.preventDefault(); | |
const worker = new Worker('worker.js'); | |
worker.onmessage = function (message) { | |
if (message.data.type === 'RESULT') { | |
const result = message.data.payload; | |
setResult(result); | |
worker.terminate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this JavaScript snippet is stored as webassembly/worker.js | |
importScripts('newtonraphson.js'); | |
// this JavaScript snippet is later referred to as <<worker-provider-onmessage>> | |
onmessage = function(message) { | |
// this JavaScript snippet is before referred to as <<handle-message>> | |
if (message.data.type === 'CALCULATE') { | |
createModule().then(({NewtonRaphson}) => { | |
// this JavaScript snippet is before referred to as <<perform-calc-in-worker>> | |
const tolerance = message.data.payload.tolerance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const spec = { | |
"$schema": "https://vega.github.io/schema/vega-lite/v4.json", | |
"width": 800, | |
"height": 600, | |
"title": {"text": "Iterative root finding", "fontSize": 20, "fontWeight": "normal"}, | |
"layer": [ | |
equation_line, | |
root_rule, | |
iterations_scatter | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const root_rule = { | |
"data": {"values": [{"x": -1}]}, | |
"encoding": {"x": {"field": "x", "type": "quantitative"}}, | |
"layer": [ | |
{"mark": {"type": "rule", "strokeDash": [4, 8]}}, | |
{"mark": | |
{ | |
"type": "text", | |
"align": "right", | |
"baseline": "bottom", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const root_rule = { | |
"data": {"values": [{"x": -1}]}, | |
"encoding": {"x": {"field": "x", "type": "quantitative"}}, | |
"layer": [ | |
{"mark": {"type": "rule", "strokeDash": [4, 8]}}, | |
{"mark": | |
{ | |
"type": "text", | |
"align": "right", | |
"baseline": "bottom", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const equation_line = { | |
"data": {"sequence": {"start": -4, "stop": 4, "step": 0.1, "as": "x"}}, | |
"transform": [ | |
{ | |
"calculate": "2 * datum.x * datum.x * datum.x - 4 * datum.x * datum.x + 6", | |
"as": "y" | |
} | |
], | |
"mark": "line", | |
"encoding": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="newtonraphson.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
</head> | |
<body> | |
<div id="plot"></div> | |
<script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="newtonraphson.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | |
</head> | |
<body> | |
<div id="plot"></div> | |
<script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const spec = { | |
"$schema": "https://vega.github.io/schema/vega-lite/v4.json", | |
"width": 800, | |
"height": 600, | |
"title": { | |
<stuff related to the title> | |
}, | |
"data": { | |
<holds the iterations we want to plot as an array of iteration objects> | |
}, |