3.0.0 • Published 3 years ago

lmfit.js v3.0.0

Weekly downloads
2
License
BSD-2-Clause
Repository
github
Last release
3 years ago

lmfit.js

JavaScript/WebAssembly (emscripten) port of lmfit (lmcurve) library:

"a self-contained C library for Levenberg-Marquardt least-squares minimization and curve fitting" (https://jugit.fz-juelich.de/mlz/lmfit)

build & test

source {path to emsdk}/emsdk_env.sh
npm install
npm run dist
npm run test

usage

There is a browser (lm.js - WebWorker) and a node (lm.node.js + wasm file) version in the dist folder. The browser version does not require a wasm file since it is all bundled.

Read about the available options at src/lm.js and https://jugit.fz-juelich.de/mlz/lmfit/-/blob/master/lib/lmstruct.h

web

import lm from 'lmfit.js';

const options = {
    verbose: true
};

const data = {
    guess: [99],
    model: 'p[0] * Math.sqrt(x)',
    x, y
};

const worker = lm();
worker.onmessage = (ev) => {
    console.log(ev.data)
    if (ev.data.initialized) {
        worker.postMessage({ data, options })
    }
};

node

const lmPromised = require('lmfit.js');

lmPromised.then(lm => {

    const options = {
        verbose: true
    };

    const data = {
        guess: [1, 1, -1],
        model: function (x, p) {
            return p[0] * Math.pow(x, p[1]) * Math.exp(p[2] * x);
        },
        x, y
    };

    const ret = lm.fit(data, options);

});

missing features

  • surface fitting as example for minimization with lmmin()
  • nonlinear equations solving with lmmin()