I want to reproduce the example in this npm library in an Observable notebook. I run the following in a cell and a block:

fit_data = {
  let data = {
    x: [0, 1, 2],
    y: [1, 1, 1]
  }
  return data
}
{
    const LM = require('[email protected]/lib/index.js').catch(() => window["_interopDefault"]);
    function sinFunction([a, b]) {
    return (t) => a * Math.sin(b * t);
  }

  const options = {
    damping: 1.5,
    gradientDifference: 10e-2,
    maxIterations: 100,
    errorTolerance: 10e-3
  };

  let fittedParams = levenbergMarquardt(fit_data, sinFunction, options);
  return fittedParams
}

and I get the error message TypeError: isArray is not a function, which I suspect is this function failing to be imported from the library's dependency.

I am importing the library by following this guide.

1

There are 1 best solutions below

0
calmar On

Everything looks fine, I guess you misspelled the import method name. Another suggestion is to break into cells. Here are the cells that worked for me

Here is how you import it

LM = await require('[email protected]/lib/index.js').catch(
  () => window["_interopDefault"]
)

Here is how you use it

{
  const options = {
    damping: 1.5,
    gradientDifference: 10e-2,
    maxIterations: 100,
    errorTolerance: 10e-3
  };

  let fittedParams = LM(fit_data, sinFunction, options);
  return fittedParams;
}
fit_data = {
  let data = {
    x: [0, 1, 2],
    y: [1, 1, 1]
  };
  return data;
}
function sinFunction([a, b]) {
  return t => a * Math.sin(b * t);
}