Quick Start
Get up and running with MathHook in 5 minutes. Learn basic expression creation, parsing, differentiation, and common operations across Rust, Python, and Node.js.
Code Examples
First Expression - Quadratic
Build and simplify x^2 + 2x + 1
use mathhook::prelude::*;
fn main() {
let x = symbol!(x);
let expr = expr!(add: (x ^ 2), (2 * x), 1);
let simplified = expr.simplify();
println!("Original: {}", expr);
println!("Simplified: {}", simplified);
}
Parsing LaTeX
Parse LaTeX notation into symbolic expression
let parser = Parser::new(ParserConfig::default());
let expr = parser.parse(r"\frac{x^2 + 1}{2}").unwrap();
println!("{}", expr);
Computing Derivatives
Compute first and second derivatives of x^3
use mathhook::prelude::*;
let x = symbol!(x);
let expr = expr!(x ^ 3);
let derivative = expr.derivative(x.clone());
let second_derivative = expr.nth_derivative(x, 2);
println!("f(x) = {}", expr);
println!("f'(x) = {}", derivative);
println!("f''(x) = {}", second_derivative);
Solving Equations
Solve x^2 = 4 symbolically
use mathhook::prelude::*;
let x = symbol!(x);
let mut solver = MathSolver::new();
let equation = Expression::equation(expr!(x ^ 2), expr!(4));
let solutions = solver.solve(&equation, &x);
println!("Solutions: {:?}", solutions);
// Output: [x = 2, x = -2]
Substitution
Substitute x = 3 into x^2 + 2x + 1
use mathhook::prelude::*;
use std::collections::HashMap;
let x = symbol!(x);
let expr = expr!(add: (x ^ 2), (2 * x), 1);
let mut vars = HashMap::new();
vars.insert("x".to_string(), Expression::integer(3));
let result = expr.substitute(&vars);
println!("Result: {}", result);
// Output: 16
Creating Expressions Programmatically
Use macros for compile-time values, explicit API for runtime
use mathhook::prelude::*;
let x = symbol!(x);
// Compile-time - use macros
let expr = expr!((x ^ 2) + 3);
// Runtime - use explicit API
let mut terms = Vec::new();
for i in 0..5 {
terms.push(Expression::mul(vec![
Expression::integer(i as i64),
Expression::pow(x.clone().into(), Expression::integer(i as i64))
]));
}
let polynomial = Expression::add(terms);