mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* Add support for scopes with get and set methods * Fix build for node v12 * Fixup cli and parser tests * Add tests for simplify and evaluate * Add example for a custom scope object * Function calls need child scopes * Transitionary step: Separate Safe and Scope Property calls * Renamed identifiers in FunctionNode * Evaluate with ObjectScopeWrapper * Simplify tests passing * Assume all scopes are map-like. Except parser * Remove isMapLike check in customs.*SafeProperty() methods * Change MapLike to Map * Move keywords from an Object to a Set * Move ScopeProperty functions in to scope.js * Removed deprecation warning * Rename scope.js to map.js * Rename ScopeProperty to MapProperty * Add tests and docs for map.js * Put back the micro-optimization of function calls * Use Map in the parser * Called scope methods directly in cli.js * Coercing of scope into a Map is done in Node, not evaluate * Move createSubScope to its own file * Fixup following self-review * Add scope docs * Final self-review changes * Address reviewer comments * Remove MapProperty witness marks * Converted broken benchmark possibly lost in a rebase * Use bare map as scope in benchmark Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
88 lines
3.3 KiB
JavaScript
88 lines
3.3 KiB
JavaScript
import assert from 'assert'
|
|
import approx from '../../../../tools/approx.js'
|
|
import math from '../../../../src/defaultInstance.js'
|
|
import { createMap } from '../../../../src/utils/map.js'
|
|
|
|
const Complex = math.Complex
|
|
const Unit = math.Unit
|
|
const ResultSet = math.ResultSet
|
|
|
|
describe('evaluate', function () {
|
|
it('should evaluate expressions', function () {
|
|
approx.equal(math.evaluate('(2+3)/4'), 1.25)
|
|
assert.deepStrictEqual(math.evaluate('sqrt(-4)'), new Complex(0, 2))
|
|
})
|
|
|
|
it('should evaluate a list of expressions', function () {
|
|
assert.deepStrictEqual(math.evaluate(['1+2', '3+4', '5+6']), [3, 7, 11])
|
|
assert.deepStrictEqual(math.evaluate(['a=3', 'b=4', 'a*b']), [3, 4, 12])
|
|
assert.deepStrictEqual(math.evaluate(math.matrix(['a=3', 'b=4', 'a*b'])), math.matrix([3, 4, 12]))
|
|
assert.deepStrictEqual(math.evaluate(['a=3', 'b=4', 'a*b']), [3, 4, 12])
|
|
})
|
|
|
|
it('should evaluate a series of expressions', function () {
|
|
assert.deepStrictEqual(math.evaluate('a=3\nb=4\na*b'), new ResultSet([3, 4, 12]))
|
|
assert.deepStrictEqual(math.evaluate('f(x) = a * x; a=2; f(4)'), new ResultSet([8]))
|
|
assert.deepStrictEqual(math.evaluate('b = 43; b * 4'), new ResultSet([172]))
|
|
})
|
|
|
|
it('should throw an error if wrong number of arguments', function () {
|
|
assert.throws(function () { math.evaluate() }, /TypeError: Too few arguments/)
|
|
assert.throws(function () { math.evaluate('', {}, 3) }, /TypeError: Too many arguments/)
|
|
})
|
|
|
|
it('should throw an error with a unit', function () {
|
|
assert.throws(function () { math.evaluate(new Unit(5, 'cm')) }, /TypeError: Unexpected type of argument/)
|
|
})
|
|
|
|
it('should throw an error with a complex number', function () {
|
|
assert.throws(function () { math.evaluate(new Complex(2, 3)) }, /TypeError: Unexpected type of argument/)
|
|
})
|
|
|
|
it('should evaluate a boolean', function () {
|
|
// TODO: this is odd. Boolean is turned in to string and then evaluated
|
|
assert.strictEqual(math.evaluate(true), true)
|
|
})
|
|
|
|
it('should handle the given scope', function () {
|
|
const scope = {
|
|
a: 3,
|
|
b: 4
|
|
}
|
|
assert.deepStrictEqual(math.evaluate('a*b', scope), 12)
|
|
assert.deepStrictEqual(math.evaluate('c=5', scope), 5)
|
|
assert.deepStrictEqual(math.format(math.evaluate('f(x) = x^a', scope)), 'f(x)')
|
|
|
|
assert.deepStrictEqual(Object.keys(scope).length, 4)
|
|
assert.deepStrictEqual(scope.a, 3)
|
|
assert.deepStrictEqual(scope.b, 4)
|
|
assert.deepStrictEqual(scope.c, 5)
|
|
assert.deepStrictEqual(typeof scope.f, 'function')
|
|
|
|
assert.strictEqual(scope.f(3), 27)
|
|
scope.a = 2
|
|
assert.strictEqual(scope.f(3), 9)
|
|
scope.hello = function (name) {
|
|
return 'hello, ' + name + '!'
|
|
}
|
|
assert.deepStrictEqual(math.evaluate('hello("jos")', scope), 'hello, jos!')
|
|
})
|
|
|
|
it('should handle the given Map scope', function () {
|
|
const scope = createMap({
|
|
a: 3,
|
|
b: 4
|
|
})
|
|
assert.deepStrictEqual(math.evaluate('a*b', scope), 12)
|
|
assert.deepStrictEqual(math.evaluate('c=5', scope), 5)
|
|
})
|
|
|
|
it('should LaTeX evaluate', function () {
|
|
const expr1 = math.parse('evaluate(expr)')
|
|
const expr2 = math.parse('evaluate(expr,scope)')
|
|
|
|
assert.strictEqual(expr1.toTex(), '\\mathrm{evaluate}\\left( expr\\right)')
|
|
assert.strictEqual(expr2.toTex(), '\\mathrm{evaluate}\\left( expr, scope\\right)')
|
|
})
|
|
})
|