Skip to main content
When facing complex mathematical challenges in template development, you have several powerful tools at your disposal. This guide helps you choose the right approach for your specific problem.

Existing MathJS Functions

It’s always worth triple-checking what already exists in the MathJS Functions Reference.
Some functions have unexpected names - for example, compareNatural() can do matrix comparisons. Always search thoroughly!

Pros

  • No new code required
  • More functions exist than you probably think!
  • Well-tested and documented

Cons

  • Sometimes weird or hard-to-read function names (add a description or authorNote!)
  • Can be awkward making a function work that isn’t quite intended for your use case
  • Your specific solution may not exist

Best Suited For

Being your first port of call for mild-to-moderate complexity problems.

Temporary Custom Functions

MathJS allows you to define temporary custom functions within your templates.

Pros

  • Programming with no dev help required 🙂
  • Particularly useful when creating or processing matrices
  • Works across multiple widgets in the same template

Cons

  • Limited to MathJS capabilities
  • Doesn’t render correctly in LaTeX (can be fixed if needed)

Best Suited For

Doing weird manipulation of matrices or creating simple custom operations.

Examples

Custom functions DO work over multiple widgets! Define once, use anywhere in the template.

Permanent Custom Functions

We have existing custom functions like iterate(), interpolate(), and more. New ones can be added as needed.

Pros

  • Very fast (executed in users’ web browsers)
  • Can use both MathJS and native JavaScript libraries
  • Minimal code required
  • No network latency

Cons

  • Engineers may be less familiar with JavaScript
  • Not well suited for calling external libraries or APIs
  • Bad for memory-intensive work (e.g., huge matrices)

Best Suited For

Operations that require less than ~5 parameters of data and nothing outside MathJS or native JavaScript.

Existing Custom Functions

  • iterate() - Iterative solving
  • interpolate() - Linear and non-linear interpolation
  • solveSecant() - Secant method solver
  • See full documentation

Python Solvers

The big Python scripts that handle complex engineering calculations.

Pros

  • Lots of external libraries available (check licenses for commercial use!)
  • Python is similar to Matlab and familiar to engineers
  • Few restrictions on memory or CPU
  • Can handle complex FEA and matrix operations

Cons

  • Every solver call requires a network roundtrip (~100-300ms each)
  • Must always support history - hard to change once written
  • Requires careful consideration to write good-quality code

Best Suited For

Super-complex calculations:
  • Finite Element Analysis (FEA)
  • Huge matrices
  • External library dependencies
  • API integrations
Solver calls add up quickly! Each call is 100-300ms, so minimize the number of calls in your template.

Decision Tree

Performance Considerations

Best Practices

  1. Start Simple: Always check if MathJS already has what you need
  2. Minimize Solver Calls: Batch operations when possible
  3. Document Complex Functions: Add descriptions and author notes
  4. Consider User Experience: Balance accuracy with performance
  5. Test Edge Cases: Ensure your solution handles all scenarios

Getting Help

  • Check existing functions documentation
  • Ask in engineering Slack channels
  • Review similar templates for inspiration
  • Consult with dev team for custom function needs