function-plot
function-plot copied to clipboard
How to use Math.js evaluator
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
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.
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?
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
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.
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.