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
[ | |
{ | |
"index": 0, | |
"x": -4, | |
"y": -186, | |
"slope": 128, | |
"delta_x": -1.453125 | |
}, | |
{ | |
"index": 1, |
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 initial_guess = -4; | |
const tolerance = 0.001; | |
const newtonraphson = new NewtonRaphson(tolerance); | |
newtonraphson.solve(initial_guess); | |
// newtonraphson.iterations is a vector object, which is not | |
// consumeable by Vega, so we need to convert Emscripten's | |
// vector of objects to a JavaScript array of objects first | |
const iterations = new Array( | |
newtonraphson.iterations.size() | |
).fill().map( |
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
#include <emscripten/bind.h> | |
#include "newtonraphson.hpp" | |
using namespace emscripten; | |
EMSCRIPTEN_BINDINGS(newtonraphson) { | |
class_<NewtonRaphson>("NewtonRaphson") | |
.constructor<float>() | |
.function("solve", &NewtonRaphson::solve) | |
.property("iterations", &NewtonRaphson::iterations) |
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
#include <iostream> | |
#include <iomanip> | |
#include "newtonraphson.hpp" | |
int main() { | |
float initial_guess = -4; | |
float tolerance = 0.001; | |
NewtonRaphson newtonraphson(tolerance); | |
newtonraphson.solve(initial_guess); |
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
#include "newtonraphson.hpp" | |
#include "problem.hpp" | |
#include <cmath> | |
// Define the constructor method of NewtonRaphson instances | |
NewtonRaphson::NewtonRaphson(float tolerance_in) : tolerance(tolerance_in) {} | |
// Define the 'solve' method of NewtonRaphson instances | |
float NewtonRaphson::solve(float initial_guess) { | |
float x = initial_guess; |
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
#ifndef H_NEWTONRAPHSON_HPP | |
#define H_NEWTONRAPHSON_HPP | |
#include <vector> | |
struct Iteration { | |
int index; | |
float x; | |
float y; | |
float slope; |
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
ReactDOM.render( | |
<App/>, | |
document.getElementById('container') | |
); |
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 Result(props) { | |
const root = props.root; | |
let message = 'Not submitted'; | |
if (root !== undefined) { | |
message = 'Function root is approximately at x = ' + root.toFixed(2); | |
} | |
return <div>{message}</div>; | |
} |
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, setRoot] = React.useState(undefined); |
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 handleSubmit(event) { | |
event.preventDefault(); | |
// Wait for module to initialize, | |
createModule().then(({NewtonRaphson}) => { | |
// Perform computation | |
const newtonraphson = new NewtonRaphson(tolerance); | |
const root = newtonraphson.solve(initial_guess); | |
setRoot(root); | |
}); | |
} |