Skip to main content

Convert latex to sympy with ANTLR and support Matrix, Linear Algebra and CAS functions.

Project description

Logo

latex2sympy2

About

latex2sympy2 parses LaTeX math expressions and converts it into the equivalent SymPy form. The latex2sympy2 is adapted from augustt198/latex2sympy and purdue-tlt / latex2sympy.

This project is a part of a VS Code extension called Latex Sympy Calculator. It is designed for providing people writing in latex or markdown a ability to calculate something when writing math expression.

ANTLR is used to generate the parser.

Features

  • Arithmetic: Add (+), Sub (-), Dot Mul (·), Cross Mul (×), Frac (/), Power (^), Abs (|x|), Sqrt (√), etc...
  • Alphabet: a - z, A - Z, α - ω, Subscript (x_1), Accent Bar(ā), etc...
  • Common Functions: gcd, lcm, floor, ceil, max, min, log, ln, exp, sin, cos, tan, csc, sec, cot, arcsin, sinh, arsinh, etc...
  • Funcion Symbol: f(x), f(x-1,), g(x,y), etc...
  • Calculous: Limit ($lim_{n\to\infty}$), Derivation ($\frac{d}{dx}(x^2+x)$), Integration ($\int xdx$), etc...
  • Linear Algebra: Matrix, Determinant, Transpose, Inverse, Elementary Transformation, etc...
  • Other: Binomial...

NOTICE: It will do some irreversible calculations when converting determinants, transposed matrixes and elementary transformations...

Installation

pip install latex2sympy2

Requirements: sympy and antlr4-python3-runtime packages.

Usage

Basic

In Python:

from latex2sympy2 import latex2sympy, latex2latex

tex = r"\frac{d}{dx}(x^{2}+x)"
# Or you can use '\mathrm{d}' to replace 'd'
latex2sympy(tex)
# => "Derivative(x**2 + x, x)"
latex2latex(tex)
# => "2 x + 1"

Examples

LaTeX Converted SymPy Calculated Latex
x^{3} $x^{3}$ x**3 x^{3} $x^{3}$
\frac{d}{dx} tx $\frac{d}{dx}tx$ Derivative(x*t, x) t $t$
\sum_{i = 1}^{n} i $\sum_{i = 1}^{n} i$ Sum(i, (i, 1, n)) \frac{n \left(n + 1\right)}{2} $\frac{n \left(n + 1\right)}{2}$
\int_{a}^{b} \frac{dt}{t} Integral(1/t, (t, a, b)) -\log{(a)} + \log{(b)} $-\log{(a)} + \log{(b)}$
`(2x^3 - x + z) {x=3}` $(2x^3 - x + z)|{x=3}$ z + 51

If you want to read the math formula, you can click GitNotes.

Solve Equation

# Before
x + y = 1

# After
[ y = 1 - x, \  x = 1 - y]

Eval At

# Before
(x+2)|_{x=y+1}

# After
y + 3

Matrix

Identity matrix

tex = r"\bm{I}_3"
latex2sympy(tex)
# => "Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])"

Determinant

from latex2sympy2 import latex2sympy

tex = r"\begin{vmatrix} x & 0 & 0 \\ 0 & x & 0 \\ 0 & 0 & x \end{vmatrix}"
latex2sympy(tex)
# => "x^{3}"

Transpose

from latex2sympy2 import latex2sympy

tex = r"\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}^T"
# Or you can use "\begin{pmatrix}1&2&3\\4&5&6\\7&8&9\end{pmatrix}'"
latex2sympy(tex)
# => "Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])"

Elementary Transformation

from latex2sympy2 import latex2sympy

matrix = r'''
    \begin{pmatrix}
        1 & 2 & 3 \\ 
        4 & 5 & 6 \\
        7 & 8 & 9 \\ 
    \end{pmatrix}
'''

# Scale the row with grammar "\xrightarrow{kr_n}"
tex = matrix + r'\xrightarrow{3r_1}'
latex2sympy(tex)
# => "Matrix([[3, 6, 9], [4, 5, 6], [7, 8, 9]])"

# Swap the cols with grammar "\xrightarrow{c_1<=>c_2}"
# Of course, you can use "\leftrightarrow" to replace "<=>" 
tex = matrix + r'\xrightarrow{c_1<=>c_2}'
latex2sympy(tex)
# => "Matrix([[2, 1, 3], [5, 4, 6], [8, 7, 9]])"

# Scale the second row and add it to the first row
# with grammar "\xrightarrow{r_1+kr_2}"
tex = matrix + r'\xrightarrow{r_1+kr_2}'
latex2sympy(tex)
# => "Matrix([[4*k + 1, 5*k + 2, 6*k + 3], [4, 5, 6], [7, 8, 9]])"

# You can compose the transform with comma ","
# and grammar "\xrightarrow[4r_3]{2r_1, 3r_2}"
# Remember the priority of "{}" is higher than "[]"
tex = matrix + r'\xrightarrow[4r_3]{2r_1, 3r_2}'
latex2sympy(tex)
# => "Matrix([[2, 4, 6], [12, 15, 18], [28, 32, 36]])"

Variances

from latex2sympy2 import latex2sympy, variances, var, set_variances

# Assign x a value of 1
latex2sympy(r"x = 1")

# Assign x a matrix symbol with dimension of n x m
latex2sympy(r"x \in \mathbb{R}^{n \times m}")

# Calculate x + y
latex2sympy(r"x + y")
# => "y + 1"

# Get all variances
print(variances)
# => "{x: 1}"

# Get variance of "x"
print(var["x"])
# => "1"

# Reset all variances
set_variances({})
latex2sympy(r"x + y")
# => "x + y"

Complex Number Support

from latex2sympy2 import set_real

set_real(False)

Contributing

If you want to add a new grammar, you can fork the code from OrangeX4/latex2sympy.

  • To modify parser grammar, view the existing structure in PS.g4.
  • To modify the action associated with each grammar, look into latex2sympy.py.

Contributors are welcome! Feel free to open a pull request or an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rlinf_latex2sympy2-1.9.0.tar.gz (82.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rlinf_latex2sympy2-1.9.0-py3-none-any.whl (90.9 kB view details)

Uploaded Python 3

File details

Details for the file rlinf_latex2sympy2-1.9.0.tar.gz.

File metadata

  • Download URL: rlinf_latex2sympy2-1.9.0.tar.gz
  • Upload date:
  • Size: 82.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rlinf_latex2sympy2-1.9.0.tar.gz
Algorithm Hash digest
SHA256 3033fc882e5a8e1fd67aeda8b31623d3375887c147ced7e47279e29aa40dbd01
MD5 c2de5b7e15186098a94d019eaf25ecaf
BLAKE2b-256 371ec56944cfbcceba7986ce1bed6fb77b3e60da4d25147a8d3fe35f4b8262eb

See more details on using hashes here.

File details

Details for the file rlinf_latex2sympy2-1.9.0-py3-none-any.whl.

File metadata

  • Download URL: rlinf_latex2sympy2-1.9.0-py3-none-any.whl
  • Upload date:
  • Size: 90.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rlinf_latex2sympy2-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2289af0b43b493878777388c46313edffbd0f68535b735c25193d915cc285f3d
MD5 c4e37cbef87dc512ec348293c549262d
BLAKE2b-256 d5a754cd35d6a8c4cc81a5b80ea9be2d6cf5be91accb0795e8661e2131e056ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page