function-plot icon indicating copy to clipboard operation
function-plot copied to clipboard

How to use Math.js evaluator

Open nikolayg opened this issue 10 months ago • 4 comments

I saw that support for math.js was removed a while ago in this commit. The comment says "users that want to use need to register an evaluator.".

However, I can't find any docs how to register an evaluator that overrides the default build in one. How can this be done?

Thanks! N

nikolayg avatar Jan 29 '25 10:01 nikolayg

Hey, thank you for looking at the commit history. Yes I removed support for it because in the initial API loading MathJS was done automatically and while it was easier for developers it wasn't clear how to use the builtIn evaluator instead. In the new API version new evaluators would need to be registered with a line that's like const interval = registerEvaluator('<new evaluator name>', evaluatorCompilerFunction).

This is not documented yet but it's already exposed as part of the public exports.

This will eventually work:

import { registerEvaluator } from 'function-plot'
registerEvaluator('mathjs', mathJSEvaluatorFunction)

functionPlot({
  data: [{ fn: 'foo', graphType: 'polyline', sampler: 'mathjs' }]
})

There's missing implementation to make that work in many places including this line which is hardcoded to use the builtIn evaluator (instead it should use the builtIn evaluator by default but if asked use the one requested in the datum) https://github.com/mauriciopoppe/function-plot/blob/5ad80cc99ae90cd9a32003bccbe06ffe37550642/src/graph-types/polyline.ts#L15.

mauriciopoppe avatar Feb 04 '25 02:02 mauriciopoppe

Thanks @mauriciopoppe

The reason I can't use the builtIn evaluator is because I'll be deploying into an iframe. The security policy doesn't allow eval, which is used by the builtIn evaluator.

I've worked around it by specifying the function directly, as in:

functionPlot({
            ...
            data: [
              {
                graphType: 'polyline',
                fn: scope => evaluate('x^2', scope),
                nSamples: undefined,
                range: [-10, 10]
              }
})

Which sampler will functionPlot use in this scenario?

nikolayg avatar Feb 06 '25 11:02 nikolayg

The default evaluator depends on the graphType, in your example because it's set to graphType: 'polyline' then the evaluator is builtIn

https://github.com/mauriciopoppe/function-plot/blob/5ad80cc99ae90cd9a32003bccbe06ffe37550642/src/datum-defaults.ts#L12

Both evaluators use a utility library that compiles the string expression to a function that can be evaluated later, in that library I used eval as the name of the function.

https://github.com/mauriciopoppe/math-codegen/blob/46174b92624a40c04a92ef7b906700fa8cd89b12/lib/CodeGenerator.js#L75-L84

To change it to evaluate (which is a good suggestion) would take:

  • Update the API https://github.com/mauriciopoppe/math-codegen
  • Make a new major version (because the API changed)
  • Adopt it in https://github.com/mauriciopoppe/built-in-math-eval and https://github.com/mauriciopoppe/interval-arithmetic-eval
  • Make a new major version of both (because the API changed)
  • Adopt it in function-plot

mauriciopoppe avatar Feb 08 '25 19:02 mauriciopoppe

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 25 '25 22:04 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 26 '25 22:06 stale[bot]