Hi there,
I have this piece of code:
function genRandomInt(min, max) {
return min + Math.floor(Math.random() * (max - min + 1))
}
genRandomInt.transform = function(min, max) {
const res = genRandomInt(min, max)
return res
}
math.import({
r: genRandomInt
})
const node = math.parse('r(1,4)x')
const rules = [
]
const simplified = math.simplify(node)
console.log(simplified.toTex({implicit: 'hide'}))
katex.render(simplified.toTex(), elem)
I am looking to use the simplify function in order to evaluate _custom functions only_ ; hence I am not using math.eval. However, it seems that using math.simplify seems to make multiplication explicit, hence the { implicit: 'hide' } option belonging to toTex has no effect. Is there a way to retain this implicit multiplication when using simplify?
Also, I was looking for a way to perhaps use \times and \cdot in TeX output, does toTex cater for this? I cannot seem to find this anywhere.
Furthermore, after looking at the documentation for simplify, it does state that you can use rules, but I am unable to find a way to use it such that, for example, only custom functions are evaluated, is this also a possibility?
Many thanks,
Sam
Thanks for reporting!
A simpler example demonstrating the issue is:
const f = math.parse('2x')
console.log(f.toString({implicit: 'hide'})) // '2 x'
const simplified = math.simplify(f)
console.log(simplified.toString({implicit: 'hide'})) // '2 * x' instead of '2 x'
The simplify function somewhere loses the implicit property of the original OperatorNode *, it would be nice if it can keep track on implicit multiplication when multiplications are left unchanged.
This should be fixed now in v5.0.2
Great! Thanks
Happens in 5.1.2
@Radivarig can you give an example of when this still happens? It works as expected for example in the following case:
math.simplify('2 x').toString() // '2 x'
@josdejong
$ node -v
v9.10.1
$ cat package.json | grep mathjs
"mathjs": "^5.1.2"
```js
// index.js
const math = require ("mathjs")
const expr = "2 x + 3 x"
const n0 = math.parse (expr) // works
const n1 = math.simplify(n0) // does not work
;[n0, n1].map (function(n, i) {
const ni = n${i}
console.log ("\n" + ni,n.toString ())
console.log (ni, n.toString ({ "implicit": "hide" }))
console.log (ni, n.toString ({ "implicit": "show" }))
console.log (ni, n.toTex ({ "implicit": "hide" }))
console.log (ni, n.toTex ({ "implicit": "show" }))
})
```bash
$ node index.js
n0 2 x + 3 x
n0 2 x + 3 x
n0 2 * x + 3 * x
n0 2~ x+3~ x
n0 2\cdot x+3\cdot x
n1 5 * x
n1 5 * x
n1 5 * x
n1 5\cdot x
n1 5\cdot x
Ah ok.
Right now implicit multiplication is only retained in parts of the expression that are not changed when simplified, like 2 x. When replacing parts of the expression, output is always explicit multiplication.
I guess we could improve simplify to check cases like 2x + 3x where both sides of the expression have an implicit multiplication and if so keep the implicit multiplication in place.
Anyone interested in picking this up?
Most helpful comment
This should be fixed now in
v5.0.2