hello I am trying to apply the example "Custom relational functions" in the browser #2675
-
my adaptation in browser does not solve the errors Please can you guide me how I can solve this problem this example mathjs/examples/advanced/custom_relational_functions.js const { create, all, factory } = require('../..')
// First let's see what the default behavior is:
// strings are compared by their numerical value
console.log('default (compare string by their numerical value)')
const { evaluate } = create(all)
evaluateAndLog(evaluate, '2 < 10') // true
evaluateAndLog(evaluate, '"2" < "10"') // true
evaluateAndLog(evaluate, '"a" == "b"') // Error: Cannot convert "a" to a number
evaluateAndLog(evaluate, '"a" == "a"') // Error: Cannot convert "a" to a number
console.log('')
// Suppose we want different behavior for string comparisons. To achieve
// this we can replace the factory functions for all relational functions
// with our own. In this simple example we use the JavaScript implementation.
console.log('custom (compare strings lexically)')
const allWithCustomFunctions = {
...all,
createEqual: factory('equal', [], () => function equal (a, b) {
return a === b
}),
createUnequal: factory('unequal', [], () => function unequal (a, b) {
return a !== b
}),
createSmaller: factory('smaller', [], () => function smaller (a, b) {
return a < b
}),
createSmallerEq: factory('smallerEq', [], () => function smallerEq (a, b) {
return a <= b
}),
createLarger: factory('larger', [], () => function larger (a, b) {
return a > b
}),
createLargerEq: factory('largerEq', [], () => function largerEq (a, b) {
return a >= b
}),
createCompare: factory('compare', [], () => function compare (a, b) {
return a > b ? 1 : a < b ? -1 : 0
})
}
const evaluateCustom = create(allWithCustomFunctions).evaluate
evaluateAndLog(evaluateCustom, '2 < 10') // true
evaluateAndLog(evaluateCustom, '"2" < "10"') // false
evaluateAndLog(evaluateCustom, '"a" == "b"') // false
evaluateAndLog(evaluateCustom, '"a" == "a"') // true
// helper function to evaluate an expression and print the results
function evaluateAndLog (evaluate, expression) {
try {
console.log(expression, evaluate(expression))
} catch (err) {
console.error(expression, err.toString())
}
} my adaptation in browser // First let's see what the default behavior is:
// strings are compared by their numerical value
console.log('default (compare string by their numerical value)')
const { evaluate } = math.create()
evaluateAndLog(evaluate, '2 < 10') // true
evaluateAndLog(evaluate, '"2" < "10"') // true
evaluateAndLog(evaluate, '"a" == "b"') // Error: Cannot convert "a" to a number
evaluateAndLog(evaluate, '"a" == "a"') // Error: Cannot convert "a" to a number
console.log('')
// Suppose we want different behavior for string comparisons. To achieve
// this we can replace the factory functions for all relational functions
// with our own. In this simple example we use the JavaScript implementation.
console.log('custom (compare strings lexically)')
const allWithCustomFunctions = {
//...all,
createEqual: math.factory('equal', [], () => function equal (a, b) {
return a === b
}),
createUnequal: math.factory('unequal', [], () => function unequal (a, b) {
return a !== b
}),
createSmaller: math.factory('smaller', [], () => function smaller (a, b) {
return a < b
}),
createSmallerEq: math.factory('smallerEq', [], () => function smallerEq (a, b) {
return a <= b
}),
createLarger: math.factory('larger', [], () => function larger (a, b) {
return a > b
}),
createLargerEq: math.factory('largerEq', [], () => function largerEq (a, b) {
return a >= b
}),
createCompare: math.factory('compare', [], () => function compare (a, b) {
return a > b ? 1 : a < b ? -1 : 0
})
}
const evaluateCustom = math.create(allWithCustomFunctions).evaluate
evaluateAndLog(evaluateCustom, '2 < 10') // true
evaluateAndLog(evaluateCustom, '"2" < "10"') // false
evaluateAndLog(evaluateCustom, '"a" == "b"') // Error: Cannot convert "a" to a number
evaluateAndLog(evaluateCustom, '"a" == "a"') // Error: Cannot convert "a" to a number
// helper function to evaluate an expression and print the results
function evaluateAndLog (evaluate, expression) {
try {
console.log(expression, evaluate(expression))
} catch (err) {
console.error(expression, err.toString())
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This customization is unfortunately not possible to do from a given const { create, all, factory } = require('mathjs')
// the static function has syntax: create(factories, config)
const math = create(all)
// the instance method has syntax: math.create(config) This is confusing, and a limitation in the browser bundle. It may be possible to expose the static function too, so you can do that too from the browser bundle. |
Beta Was this translation helpful? Give feedback.
This customization is unfortunately not possible to do from a given
mathjs
instance. Thecreate
function on a math instance differs from the static one available in the ES library:This is confusing, and a limitation in the browser bundle. It may be possible to expose the static function too, so you can do that too from the browser bundle.