Skip to main content

A mathematical expression parser supporting arithmetic, functions, and complex numbers

Project description

parsimathious

PyPI Python Tests License: MIT

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


Download files

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

Source Distribution

parsimathious-0.2.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

parsimathious-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file parsimathious-0.2.0.tar.gz.

File metadata

  • Download URL: parsimathious-0.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for parsimathious-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0b18a28fa6f4824b09dd0e0b75b44419c28aa2641badab4ea1fa3fa312354369
MD5 e435e0f836be9de8e89d9082b0b80584
BLAKE2b-256 9aff2435c544ea84920903ce1484ca7c94e653aee8d0c40542deafcaeee828b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsimathious-0.2.0.tar.gz:

Publisher: publish.yml on stur86/parsimathious

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parsimathious-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: parsimathious-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for parsimathious-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfc0f71ea5c6d4a6f59c164aef1e4e227e7c33618d7dad14210a30d8bec9fd56
MD5 c1d1ca2fdb6f996f0d462f3fc9528f44
BLAKE2b-256 e0222ef49ad35aab0256aac140d01fd446131cd5fd529f9e87e9b5c5512d61ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for parsimathious-0.2.0-py3-none-any.whl:

Publisher: publish.yml on stur86/parsimathious

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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