# Hide implicit multiplication (symbols) when using math.simplify to evaluate custom functions #1137

Open
opened this Issue Jun 16, 2018 · 7 comments

Projects
None yet
3 participants

### samueltlg commented Jun 16, 2018

 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
Owner

### josdejong commented Jun 24, 2018

 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.

Owner

### josdejong commented Jul 7, 2018

 This should be fixed now in v5.0.2

### samueltlg commented Jul 8, 2018

 Great! Thanks

### Radivarig commented Sep 30, 2018

 Happens in 5.1.2
Owner

### josdejong commented Sep 30, 2018

 @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'

### Radivarig commented Sep 30, 2018

 @josdejong $node -v v9.10.1$ cat package.json | grep mathjs "mathjs": "^5.1.2" // 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" })) }) $ 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
Owner

### josdejong commented Oct 3, 2018

 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?