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
Permanent Custom Functions
We have existing custom functions likeiterate(), 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 solvinginterpolate()- Linear and non-linear interpolationsolveSecant()- 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
Decision Tree
Performance Considerations
Best Practices
- Start Simple: Always check if MathJS already has what you need
- Minimize Solver Calls: Batch operations when possible
- Document Complex Functions: Add descriptions and author notes
- Consider User Experience: Balance accuracy with performance
- 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