A mathematical expression parser supporting arithmetic, functions, and complex numbers
Project description
parsimathious
parsimathious is a simple mathematical expression parser implemented with parsimonious. It supports basic arithmetic operations, parentheses, unary functions, constants, and complex numbers.
Installation
You can install parsimathious using pip:
pip install parsimathious
Usage
Import the ExpressionParser and create an instance:
from parsimathious import ExpressionParser
parser = ExpressionParser()
Then you can parse and evaluate expressions:
result = parser("sin(pi / 2) + 1")
print(result) # Output: 2.0
Supported functions and constants
On top of basic arithmetic operations, parsimathious supports the following unary functions and constants by default:
| Name | Python Implementation | Description |
|---|---|---|
sin |
math.sin | Sine |
cos |
math.cos | Cosine |
tan |
math.tan | Tangent |
log |
math.log | Natural logarithm (base e) |
sqrt |
math.sqrt | Square root |
exp |
math.exp | Exponential (e^x) |
log10 |
math.log10 | Logarithm base 10 |
abs |
abs | Absolute value |
floor |
math.floor | Floor (round down) |
ceil |
math.ceil | Ceiling (round up) |
round |
round | Round to nearest integer |
sinh |
math.sinh | Hyperbolic sine |
cosh |
math.cosh | Hyperbolic cosine |
tanh |
math.tanh | Hyperbolic tangent |
asin |
math.asin | Arc sine |
acos |
math.acos | Arc cosine |
atan |
math.atan | Arc tangent |
asinh |
math.asinh | Inverse hyperbolic sine |
acosh |
math.acosh | Inverse hyperbolic cosine |
atanh |
math.atanh | Inverse hyperbolic tangent |
sec |
lambda x: 1 / math.cos(x) | Secant |
csc |
lambda x: 1 / math.sin(x) | Cosecant |
cot |
lambda x: 1 / math.tan(x) | Cotangent |
Constants
| Name | Value | Description |
|---|---|---|
pi |
math.pi | The mathematical constant π |
e |
math.e | The mathematical constant e |
i |
1j | The imaginary unit |
Custom Unary Functions
It's also possible to support custom unary functions by passing a dictionary of function names to their implementations when creating the ExpressionParser:
import math
from parsimathious import ExpressionParser, UnaryFunctionMap
custom_functions: UnaryFunctionMap = {
"log2": math.log2, # Logarithm base 2
"cube": lambda x: x ** 3, # Cube function
}
parser = ExpressionParser(unary_functions=custom_functions)
result = parser("log2(8) + cube(3)")
print(result) # Output: 35.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parsimathious-0.1.1.tar.gz.
File metadata
- Download URL: parsimathious-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa07dc9c62dba84098b04445372ec6c5e1c8b6200b9304e41c99abbcf6f507ff
|
|
| MD5 |
5aa8462fc89a54ecc2ba95486ec63e5a
|
|
| BLAKE2b-256 |
ef38a832963cfc3567e71c02d57221057b3106cfb22371aee0e07d3acc7890a3
|
Provenance
The following attestation bundles were made for parsimathious-0.1.1.tar.gz:
Publisher:
publish.yml on stur86/parsimathious
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parsimathious-0.1.1.tar.gz -
Subject digest:
aa07dc9c62dba84098b04445372ec6c5e1c8b6200b9304e41c99abbcf6f507ff - Sigstore transparency entry: 1399417367
- Sigstore integration time:
-
Permalink:
stur86/parsimathious@8c44abe7d91d726020328220705bcc78c8e1ec4d -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/stur86
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c44abe7d91d726020328220705bcc78c8e1ec4d -
Trigger Event:
release
-
Statement type:
File details
Details for the file parsimathious-0.1.1-py3-none-any.whl.
File metadata
- Download URL: parsimathious-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efcfaa968f23ccca9951fb15b353754a3f0f926f2efd164fb8a67f0dcbc21f1e
|
|
| MD5 |
d5542f6de29a32d0ec6e8bdfd67000d0
|
|
| BLAKE2b-256 |
35731c43d5ac13691f8754445d5367f9b48e977b7d78bb965d097d7f6e8bc15d
|
Provenance
The following attestation bundles were made for parsimathious-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on stur86/parsimathious
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
parsimathious-0.1.1-py3-none-any.whl -
Subject digest:
efcfaa968f23ccca9951fb15b353754a3f0f926f2efd164fb8a67f0dcbc21f1e - Sigstore transparency entry: 1399417370
- Sigstore integration time:
-
Permalink:
stur86/parsimathious@8c44abe7d91d726020328220705bcc78c8e1ec4d -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/stur86
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8c44abe7d91d726020328220705bcc78c8e1ec4d -
Trigger Event:
release
-
Statement type: