cortex-js / compute-engine

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON
https://cortexjs.io
MIT License
356 stars 42 forks source link

BoxedExpression isComplex doesn't seem to be working #149

Closed rwmorton closed 6 months ago

rwmorton commented 6 months ago

On the compute-engine demo plugging in sqrt(-2) gives 2i which is correct. However when testing the BoxedExpression.isComplex value it returns false despite the evaluated value being ["Complex", 0, 1].

Here is a code sample to demonstate:

import { ComputeEngine } from '@cortex-js/compute-engine'

const ce = new ComputeEngine()

let latex = '\\sqrt{4}'
let expr = ce.parse(latex)
let x = expr.N().value

console.log('expr.latex =', expr.latex)
console.log('expr =', JSON.stringify(expr.json))
console.log(`expr.N().value = ${x}`)
console.log('expr.isComplex =', expr.isComplex)

latex = '\\sqrt{-1}'
expr = ce.parse(latex)
x = expr.N().value

console.log('-'.repeat(30))

console.log('expr.latex =', expr.latex)
console.log('expr =', JSON.stringify(expr.json))
console.log(`expr.N().value = ${x}`)
console.log('expr.isComplex =', expr.isComplex)

and the output is:

image

arnog commented 6 months ago

The expression \sqrt{-1} is a Number. It needs to be evaluated to be recognized as a complex number. The domain of an expression can be observed with the .domain property.

const expr = ce.parse("\\sqrt{-1}")
console.log('expr.isComplex =', expr.isComplex)
// -> false
console.log('expr.domain =', expr.domain)
// -> Numbers
console.log(`expr.N().value = ${x}`)
// ["Complex", 0, 1]
console.log(`expr.N().isComplex = ${expr.N().isComplex}`)
// -> true
console.log(`expr.N().domain = ${expr.N().domain}`)
// ComplexNumbers
rwmorton commented 6 months ago

Wow that's awesome, thank you!