New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
samueltlg opened this Issue Jun 16, 2018 · 7 comments

Comments

Projects
None yet
3 participants
@samueltlg

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

@josdejong

This comment has been minimized.

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.

@josdejong josdejong added the feature label Jun 24, 2018

@josdejong josdejong closed this in c3827d7 Jul 7, 2018

@josdejong

This comment has been minimized.

Owner

josdejong commented Jul 7, 2018

This should be fixed now in v5.0.2

@samueltlg

This comment has been minimized.

samueltlg commented Jul 8, 2018

Great! Thanks

@Radivarig

This comment has been minimized.

Radivarig commented Sep 30, 2018

Happens in 5.1.2

@josdejong

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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?

@josdejong josdejong reopened this Oct 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment