Skip to main content

A symbolic differentiation library.

Project description

diffstep

diffstep is a small Python library for math derivatives.

It helps you:

  • Find the derivative of math expressions.
  • Get the derivative value at a given x value.
  • Print a tree view of the expression.
  • Print the steps used to get the derivative.

Installation

pip install diffstep

Quick Start

import diffstep

diffstep.diffstep("x^2 + 3x + 2")
# 2*x + 3

diffstep.gradient("x^2 + 3x + 2", 1)
# 5

diffstep.diffstep("x^x")
# x^x*(log(x) + 1)

diffstep.parse_expression("sin(x) + cos(x)")
# Prints pretty AST once and returns None

diffstep.trace("x^2 + sin(x^2)")
# Prints rendered trace text once and returns None

Public API

  • diffstep(expression: str) Returns the simplified symbolic derivative as a SymPy expression.

  • differentiate(expression: str) Alias of diffstep().

  • gradient(expression: str, x_value: float) Differentiates, then evaluates at x = x_value.

  • normalise_expression(expression: str) Validates and normalizes expression text (spaces, implicit multiplication, casing).

  • tokenise_expression(expression: str) Returns the token list after normalization.

  • parse_expression(expression: str, pretty: bool | None = None, emit: bool = True) If pretty=False, it returns the raw parse tree object. In other cases, it prints a readable tree and returns None. If you set emit=False, it does not print and returns the tree as text.

  • pretty_parse_expression(expression: str, emit: bool = True) Convenience wrapper for parse_expression(expression, pretty=True).

  • derived_ast(expression: str) Returns unsimplified derivative AST.

  • trace(expression: str) Prints rendered differentiation trace text and returns None.

  • DiffStep(expression: str) Deprecated alias of diffstep() (emits DeprecationWarning).

Supported Expressions

  • Variables: x, e
  • Numeric constants (integers and decimals)
  • Operators: +, -, *, /, ^
  • Parentheses and implicit multiplication (for example 2x, x(x+1))
  • Power forms including:
    • x^n
    • a^f(x)
    • e^f(x)
    • General u(x)^v(x) (for example x^x)
  • Functions:
    • sin, cos, tan
    • sec, cosec, cot
    • arcsin, arccos, arctan
    • ln, sqrt

Project Layout

  • diffstep/core/: normalization, tokenization, parsing, AST nodes
  • diffstep/differentiation/: derivative and trace rule engines
  • diffstep/simplify/: SymPy-backed simplification
  • diffstep/printer/: AST and trace rendering utilities
  • diffstep/pipeline.py: user-facing orchestration API

License

MIT License. See LICENSE.txt.

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

diffstep-1.4.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

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

diffstep-1.4.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file diffstep-1.4.0.tar.gz.

File metadata

  • Download URL: diffstep-1.4.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for diffstep-1.4.0.tar.gz
Algorithm Hash digest
SHA256 e964cf6fb47e062227cf7d875848e3ef04730e662de37238b1d680a005118808
MD5 0b7e0b15675fbb299272f44e055d1687
BLAKE2b-256 22c281cb3987b02579ce663b8ad55dc143c275c5c7ceb95a6f82ca43c51d12ec

See more details on using hashes here.

File details

Details for the file diffstep-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: diffstep-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for diffstep-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32586efd9280e35b4873c81669375256282e2d806081640123ccecfaf9234dc8
MD5 734c537b3e5f02b98abb5458c44e43e2
BLAKE2b-256 468b030f0df8462095267f4f259f41e327567c0551bbc8d6bcb536d50b02dd5a

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