Python API Guide

Complete guide to using MathHook from Python via PyO3 bindings. Provides comprehensive documentation for the Python API including installation, quick start, API reference, performance comparisons with SymPy, and integration patterns.

Code Examples

Basic Symbol Creation and Expression Building

Create symbols and build expressions using operator overloading

use mathhook::symbol;
use mathhook::expr;

let x = symbol!(x);
let y = symbol!(y);

// Build expressions
let expr = expr!(x^2 + 2*x + 1);
let expr2 = expr!((x + 1) * (x - 1));
let expr3 = expr!(x / (x + 1));
let expr4 = expr!(-x);

Expression Simplification

Simplify algebraic expressions using MathHook

use mathhook::{expr, symbol, simplify};

let x = symbol!(x);
let expr = expr!(x + x);
let result = simplify(expr);  // 2*x

let expr2 = expr!((x + 1) * (x - 1));
let result2 = simplify(expr2);  // x^2 - 1

Symbolic Differentiation

Compute derivatives symbolically

use mathhook::{expr, symbol, derivative};

let x = symbol!(x);
let expr = expr!(x^3);

// First derivative
let df = derivative(&expr, &x, 1);
// Result: 3*x^2

// Second derivative
let d2f = derivative(&expr, &x, 2);
// Result: 6*x

// Partial derivatives
let y = symbol!(y);
let expr2 = expr!(x^2 * y);
let df_dx = derivative(&expr2, &x, 1);  // 2*x*y
let df_dy = derivative(&expr2, &y, 1);  // x^2

Equation Solving

Solve algebraic equations symbolically

use mathhook::{expr, symbol, solve};

let x = symbol!(x);

// Linear equation: 2*x + 3 = 7
let solutions = solve(expr!(2*x + 3), expr!(7), &x);
// Result: [x = 2]

// Quadratic equation: x^2 - 5*x + 6 = 0
let solutions = solve(expr!(x^2 - 5*x + 6), expr!(0), &x);
// Result: [x = 2, x = 3]

Integration with NumPy

Convert symbolic expressions to NumPy functions for numerical evaluation

Evaluation with Context

Advanced evaluation with custom contexts and variable substitutions

🔗 Related Topics