viktorstrate / algebra-latex

Parse and calculate latex formatted math
https://www.npmjs.com/package/algebra-latex
MIT License
28 stars 9 forks source link

why remove function parser in your last commit? #3

Closed pansitwattana closed 5 years ago

pansitwattana commented 6 years ago

When it will be fixed?

viktorstrate commented 6 years ago

The function parser, hasn't been removed? Last commit fixed a bug, it didn't change anything.

pansitwattana commented 6 years ago

I mean parsing function(sin, cos, tan, and etc..). I can't parse latex with that eg. \sin(0.5) or \sqrt(x)

viktorstrate commented 6 years ago

@pansitwattana: Are you sure you are running the latest version (0.5.3)? The following code:

const AlgebraLatex = require('algebra-latex')

const latex = '\\sin 0.7 + \\tan{32*4x} - \\sqrt {33} + \\cos (8)'
const algebra = new AlgebraLatex(latex)

console.log(algebra.toMath())

Returns

sin(0.7)+tan(32*4*x)-sqrt(33)+cos(8)

If your latex is correct, and you are using the latest version, please post a snippet that doesn't work

pansitwattana commented 6 years ago

Thanks for replying. I'm running with the latest version In my case, I use function toAlgebra(algebraJS) then I got result like this 128tanx + 7/10sin - 33sqrt + 8cos Here my full code.

const algebraObj = new AlgebraLatex('\\sin 0.7 + \\tan{32*4x} - \\sqrt {33} + \\cos (8)')
let result = algebraObj.toAlgebra(algebraJS)
console.log(result.toString())

but I tried function toMath() then It worked. I wonder why 2 function get different results.

viktorstrate commented 6 years ago

Ah okay, that's because Algebra.js doesn't support functions, so when algebra.js's parser tries to parse the math, it thinks that the functions are variables.

If you're not planning to use equations, and only expressions, then you can use mathjs, it supports functions.

Here is an example of how you might do this.

const AlgebraLatex = require('algebra-latex')
const math = require('mathjs')

const latex = '\\sin 0.7 + \\tan{32*4} - \\sqrt {33x} + \\cos (8)'

const algebra = new AlgebraLatex(latex)

const result = math.simplify(algebra.toMath())

console.log(result.toString()) // "-0.5418972380037778 - sqrt(33 * x)"
console.log(result.toTex()) // "-0.5418972380037778-\sqrt{33\cdot x}"

See this page for more information about algebra in mathjs.

pansitwattana commented 6 years ago

Do you have plan to support int function eg. \int_3^5x^2dx

viktorstrate commented 6 years ago

Maybe, as I know. Only Algebrite supports integrals, but maybe I will add support for integrals for that library in the future. You are welcome to send a pull request, if you want.

pansitwattana commented 6 years ago

4 I just pulled request for normal integral function \int x but don't know how to implement integral with closed interval (\int_3^5 x) or (\int_3^5 x dx) do you have any idea? How to structure the format?