Skip to content

Instantly share code, notes, and snippets.

View sverhoeven's full-sized avatar

Stefan Verhoeven sverhoeven

View GitHub Profile
@sverhoeven
sverhoeven / iterations.json
Created October 2, 2020 09:36
run-cpp-on-web: vega
[
{
"index": 0,
"x": -4,
"y": -186,
"slope": 128,
"delta_x": -1.453125
},
{
"index": 1,
@sverhoeven
sverhoeven / iterations.js
Created October 2, 2020 09:36
run-cpp-on-web: vega
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(
@sverhoeven
sverhoeven / bindings.cpp
Created October 2, 2020 09:35
run-cpp-on-web: vega
#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)
@sverhoeven
sverhoeven / cli.cpp
Created October 2, 2020 09:33
run-cpp-on-web: vega
#include <iostream>
#include <iomanip>
#include "newtonraphson.hpp"
int main() {
float initial_guess = -4;
float tolerance = 0.001;
NewtonRaphson newtonraphson(tolerance);
newtonraphson.solve(initial_guess);
@sverhoeven
sverhoeven / newtonraphson.cpp
Last active October 7, 2020 12:09
run-cpp-on-web: vega
#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;
@sverhoeven
sverhoeven / newtonraphson.hpp
Created October 2, 2020 09:32
run-cpp-on-web: vega
#ifndef H_NEWTONRAPHSON_HPP
#define H_NEWTONRAPHSON_HPP
#include <vector>
struct Iteration {
int index;
float x;
float y;
float slope;
@sverhoeven
sverhoeven / render-app.jsx
Created October 2, 2020 09:18
run-cpp-on-web: react
ReactDOM.render(
<App/>,
document.getElementById('container')
);
@sverhoeven
sverhoeven / result.jsx
Last active October 7, 2020 11:04
run-cpp-on-web: react
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>;
}
@sverhoeven
sverhoeven / setroot.js
Created October 2, 2020 09:16
run-cpp-on-web: react
const [root, setRoot] = React.useState(undefined);
@sverhoeven
sverhoeven / submit.js
Created October 2, 2020 09:16
run-cpp-on-web: react
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);
});
}