This file contains hidden or 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
<div id="container"></div> |
This file contains hidden or 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
<head> | |
<title>Example React application</title> | |
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script> | |
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script> | |
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> | |
</head> |
This file contains hidden or 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
importScripts('newtonraphson.js'); | |
onmessage = function(message) { | |
if (message.data.type === 'CALCULATE') { | |
createModule().then(({NewtonRaphson}) => { | |
const tolerance = message.data.payload.tolerance; | |
const finder = new NewtonRaphson(tolerance); | |
const initial_guess = message.data.payload.initial_guess; | |
const root = finder.solve(initial_guess); | |
postMessage({ |
This file contains hidden or 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Web worker example</title> | |
<script> | |
const worker = new Worker('worker.js'); | |
worker.postMessage({ | |
type: 'CALCULATE', | |
payload: { tolerance: 0.001, initial_guess: -4.0 } | |
}); |
This file contains hidden or 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> | |
<!-- Load WebAssembly module --> | |
<script type="text/javascript" src="newtonraphson.js"></script> | |
</head> | |
<body> | |
<div> | |
Function root is approximately at x = | |
<span id="answer"/> | |
</div> |
This file contains hidden or 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) | |
; |
This file contains hidden or 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); | |
float root = newtonraphson.solve(initial_guess); |
This file contains hidden or 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 | |
class NewtonRaphson { | |
public: | |
NewtonRaphson(float tolerance_in); | |
float solve(float initial_guess); | |
private: | |
float tolerance; | |
}; |
This file contains hidden or 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 hidden or 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
// An example equation | |
float equation(float x) { | |
return 2 * x * x * x - 4 * x * x + 6; | |
} | |
// Derivative of the above equation | |
float derivative(float x) { | |
return 6 * x * x - 8 * x; | |
} |