latex2sympy2


Namelatex2sympy2 JSON
Version 1.9.1 PyPI version JSON
download
home_pagehttps://github.com/OrangeX4/latex2sympy
SummaryConvert latex to sympy with ANTLR and support Matrix, Linear Algebra and CAS functions.
upload_time2023-04-23 07:44:11
maintainer
docs_urlNone
authorOrangeX4
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements antlr4-python3-runtime mpmath sympy
Travis-CI No Travis.
coveralls test coverage
            ![Logo](https://picgo-1258602555.cos.ap-nanjing.myqcloud.com/icon.png)

# [latex2sympy2](https://github.com/OrangeX4/latex2sympy)

## About

`latex2sympy2` parses **LaTeX math expressions** and converts it into the equivalent **SymPy form**. The latex2sympy2 is adapted from [augustt198/latex2sympy](https://github.com/augustt198/latex2sympy) and [purdue-tlt / latex2sympy](https://github.com/purdue-tlt/latex2sympy).

This project is a part of a VS Code extension called [Latex Sympy Calculator](https://marketplace.visualstudio.com/items?itemName=OrangeX4.latex-sympy-calculator). It is designed for providing people writing in latex or markdown a ability to calculate something when writing math expression.

[ANTLR](http://www.antlr.org/) 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:

```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`| `z + 51` $z + 51$ |

If you want to read the math formula, you can click [GitNotes](https://notes.orangex4.cool/?git=github&github=OrangeX4/latex2sympy).

### Solve Equation

``` latex
# Before
x + y = 1

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

### Eval At

``` latex
# 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

``` python
from latex2sympy2 import latex2sympy

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

#### Transpose

``` python
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

``` python
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

``` python
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

``` python
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](https://github.com/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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/OrangeX4/latex2sympy",
    "name": "latex2sympy2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "OrangeX4",
    "author_email": "318483724@qq.com",
    "download_url": "",
    "platform": null,
    "description": "![Logo](https://picgo-1258602555.cos.ap-nanjing.myqcloud.com/icon.png)\n\n# [latex2sympy2](https://github.com/OrangeX4/latex2sympy)\n\n## About\n\n`latex2sympy2` parses **LaTeX math expressions** and converts it into the equivalent **SymPy form**. The latex2sympy2 is adapted from [augustt198/latex2sympy](https://github.com/augustt198/latex2sympy) and [purdue-tlt / latex2sympy](https://github.com/purdue-tlt/latex2sympy).\n\nThis project is a part of a VS Code extension called [Latex Sympy Calculator](https://marketplace.visualstudio.com/items?itemName=OrangeX4.latex-sympy-calculator). It is designed for providing people writing in latex or markdown a ability to calculate something when writing math expression.\n\n[ANTLR](http://www.antlr.org/) is used to generate the parser.\n\n## Features\n\n* **Arithmetic:** Add (+), Sub (-), Dot Mul (\u00b7), Cross Mul (\u00d7), Frac (/), Power (^), Abs (|x|), Sqrt (\u221a), etc...\n* **Alphabet:** a - z, A - Z, \u03b1 - \u03c9, Subscript (x_1), Accent Bar(\u0101), etc...\n* **Common Functions:** gcd, lcm, floor, ceil, max, min, log, ln, exp, sin, cos, tan, csc, sec, cot, arcsin, sinh, arsinh, etc...\n* **Funcion Symbol:** f(x), f(x-1,), g(x,y), etc...\n* **Calculous:** Limit ($lim_{n\\to\\infty}$), Derivation ($\\frac{d}{dx}(x^2+x)$), Integration ($\\int xdx$), etc...\n* **Linear Algebra:** Matrix, Determinant, Transpose, Inverse, Elementary Transformation, etc...\n* **Other:** Binomial...\n\n**NOTICE:** It will do some irreversible calculations when converting determinants, transposed matrixes and elementary transformations...\n\n## Installation\n\n```\npip install latex2sympy2\n```\n\n**Requirements:** `sympy` and `antlr4-python3-runtime` packages.\n\n## Usage\n\n### Basic\n\nIn Python:\n\n```python\nfrom latex2sympy2 import latex2sympy, latex2latex\n\ntex = r\"\\frac{d}{dx}(x^{2}+x)\"\n# Or you can use '\\mathrm{d}' to replace 'd'\nlatex2sympy(tex)\n# => \"Derivative(x**2 + x, x)\"\nlatex2latex(tex)\n# => \"2 x + 1\"\n```\n\n### Examples\n\n|LaTeX|Converted SymPy|Calculated Latex|\n|-----|-----|---------------|\n|`x^{3}` $x^{3}$| `x**3`|`x^{3}` $x^{3}$|\n|`\\frac{d}{dx} tx` $\\frac{d}{dx}tx$|`Derivative(x*t, x)`|`t` $t$|\n|`\\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}$|\n|`\\int_{a}^{b} \\frac{dt}{t}`|`Integral(1/t, (t, a, b))`|`-\\log{(a)} + \\log{(b)}` $-\\log{(a)} + \\log{(b)}$|\n|`(2x^3 - x + z)|_{x=3}` $(2x^3 - x + z)\\|_{x=3}$|`z + 51`| `z + 51` $z + 51$ |\n\nIf you want to read the math formula, you can click [GitNotes](https://notes.orangex4.cool/?git=github&github=OrangeX4/latex2sympy).\n\n### Solve Equation\n\n``` latex\n# Before\nx + y = 1\n\n# After\n[ y = 1 - x, \\  x = 1 - y]\n```\n\n### Eval At\n\n``` latex\n# Before\n(x+2)|_{x=y+1}\n\n# After\ny + 3\n```\n\n### Matrix\n\n#### Identity matrix\n\n```\ntex = r\"\\bm{I}_3\"\nlatex2sympy(tex)\n# => \"Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])\"\n```\n\n#### Determinant\n\n``` python\nfrom latex2sympy2 import latex2sympy\n\ntex = r\"\\begin{vmatrix} x & 0 & 0 \\\\ 0 & x & 0 \\\\ 0 & 0 & x \\end{vmatrix}\"\nlatex2sympy(tex)\n# => \"x^{3}\"\n```\n\n#### Transpose\n\n``` python\nfrom latex2sympy2 import latex2sympy\n\ntex = r\"\\begin{pmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\end{pmatrix}^T\"\n# Or you can use \"\\begin{pmatrix}1&2&3\\\\4&5&6\\\\7&8&9\\end{pmatrix}'\"\nlatex2sympy(tex)\n# => \"Matrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])\"\n```\n\n#### Elementary Transformation\n\n``` python\nfrom latex2sympy2 import latex2sympy\n\nmatrix = r'''\n    \\begin{pmatrix}\n        1 & 2 & 3 \\\\ \n        4 & 5 & 6 \\\\\n        7 & 8 & 9 \\\\ \n    \\end{pmatrix}\n'''\n\n# Scale the row with grammar \"\\xrightarrow{kr_n}\"\ntex = matrix + r'\\xrightarrow{3r_1}'\nlatex2sympy(tex)\n# => \"Matrix([[3, 6, 9], [4, 5, 6], [7, 8, 9]])\"\n\n# Swap the cols with grammar \"\\xrightarrow{c_1<=>c_2}\"\n# Of course, you can use \"\\leftrightarrow\" to replace \"<=>\" \ntex = matrix + r'\\xrightarrow{c_1<=>c_2}'\nlatex2sympy(tex)\n# => \"Matrix([[2, 1, 3], [5, 4, 6], [8, 7, 9]])\"\n\n# Scale the second row and add it to the first row\n# with grammar \"\\xrightarrow{r_1+kr_2}\"\ntex = matrix + r'\\xrightarrow{r_1+kr_2}'\nlatex2sympy(tex)\n# => \"Matrix([[4*k + 1, 5*k + 2, 6*k + 3], [4, 5, 6], [7, 8, 9]])\"\n\n# You can compose the transform with comma \",\"\n# and grammar \"\\xrightarrow[4r_3]{2r_1, 3r_2}\"\n# Remember the priority of \"{}\" is higher than \"[]\"\ntex = matrix + r'\\xrightarrow[4r_3]{2r_1, 3r_2}'\nlatex2sympy(tex)\n# => \"Matrix([[2, 4, 6], [12, 15, 18], [28, 32, 36]])\"\n```\n\n### Variances\n\n``` python\nfrom latex2sympy2 import latex2sympy, variances, var, set_variances\n\n# Assign x a value of 1\nlatex2sympy(r\"x = 1\")\n\n# Assign x a matrix symbol with dimension of n x m\nlatex2sympy(r\"x \\in \\mathbb{R}^{n \\times m}\")\n\n# Calculate x + y\nlatex2sympy(r\"x + y\")\n# => \"y + 1\"\n\n# Get all variances\nprint(variances)\n# => \"{x: 1}\"\n\n# Get variance of \"x\"\nprint(var[\"x\"])\n# => \"1\"\n\n# Reset all variances\nset_variances({})\nlatex2sympy(r\"x + y\")\n# => \"x + y\"\n```\n\n### Complex Number Support\n\n``` python\nfrom latex2sympy2 import set_real\n\nset_real(False)\n```\n\n\n## Contributing\n\nIf you want to add a new grammar, you can fork the code from [OrangeX4/latex2sympy](https://github.com/OrangeX4/latex2sympy).\n\n* To modify parser grammar, view the existing structure in `PS.g4`.\n* To modify the action associated with each grammar, look into `latex2sympy.py`.\n\nContributors are welcome! Feel free to open a pull request or an issue.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Convert latex to sympy with ANTLR and support Matrix, Linear Algebra and CAS functions.",
    "version": "1.9.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0c9e4520682ab29a9219f1845643fdc75f1453bebf4b602c6e4421579de1f05d",
                "md5": "666fe0c9d8fac3e38907a55ae655e90a",
                "sha256": "44f24d263d235164a91173167a30d449f4360e3f0a59239ce6b843c50a41c601"
            },
            "downloads": -1,
            "filename": "latex2sympy2-1.9.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "666fe0c9d8fac3e38907a55ae655e90a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 89798,
            "upload_time": "2023-04-23T07:44:11",
            "upload_time_iso_8601": "2023-04-23T07:44:11.064366Z",
            "url": "https://files.pythonhosted.org/packages/0c/9e/4520682ab29a9219f1845643fdc75f1453bebf4b602c6e4421579de1f05d/latex2sympy2-1.9.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-23 07:44:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "OrangeX4",
    "github_project": "latex2sympy",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "requirements": [
        {
            "name": "antlr4-python3-runtime",
            "specs": [
                [
                    "==",
                    "4.7.2"
                ]
            ]
        },
        {
            "name": "mpmath",
            "specs": [
                [
                    "==",
                    "1.1.0"
                ]
            ]
        },
        {
            "name": "sympy",
            "specs": [
                [
                    "==",
                    "1.4"
                ]
            ]
        }
    ],
    "lcname": "latex2sympy2"
}
        
Elapsed time: 0.21539s