Doenet / math-expressions

Javascript library handling mathematical expressions involving transcendental functions
Other
49 stars 21 forks source link

Custom math functions #48

Open markhats opened 2 years ago

markhats commented 2 years ago

Hi

Just wondered if there was a way to add custom math functions to the parsing and evaluation of expressions?

Thanks.

dqnykamp commented 2 years ago

You can add new functions to the appliedFunctionSymbols argument when creating latexToAst or testToAst, which will parse "\myFunction(x)" (latex) or "myFunction(x)" (text) to the ast ["apply", "myFunction", "x"]. If that function is known to mathjs, then it will call the mathjs function when evaluating it. If producing latex, you also need to add it to allowedLatexSymbols of astToLatex so that the result going back to latex is "\myFunction(x)".

markhats commented 2 years ago

Thanks for the info.

I assume if it isn't a function known to mathjs, then I need to implement it as a custom mathjs function, as detailed here: https://mathjs.org/docs/core/extension.html ? Or is there a better way?

dqnykamp commented 2 years ago

That's right. That's what I did to add some functions from numeric.js, and it seemed to work without a hitch.