mathjs/examples/advanced/custom_evaluate_using_import.js
Vrushaket Chaudhari 8f8e506166
feat: implement subtractScalar (#3081, #2643)
* added subtractScaler

* added subtractScaler missing entries

* added test cases for 2 or more parameters, test for subtractScalar instead fo subtract

* replaced subtract with subtractScalar whereever possible

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2023-10-25 13:46:58 +02:00

22 lines
853 B
JavaScript

// we use the number only implementation in order to not pull in
// the `Unit` class for example. when using as library,
// use require('mathjs/number')
const { create, evaluateDependencies } = require('../../lib/cjs/number.js')
// custom implementations of all functions you want to support
const add = (a, b) => a + b
const subtract = (a, b) => a - b
const multiply = (a, b) => a * b
const divide = (a, b) => a / b
// create a mathjs instance with hardly any functions
// there are some functions created which are used internally by evaluate though,
// for example by the Unit class which has dependencies on addScalar, subtractScalar,
// multiplyScalar, etc.
const math = create(evaluateDependencies)
// import your own functions
math.import({ add, subtract, multiply, divide }, { override: true })
console.log(math.evaluate('2 + 3 * 4')) // 14