TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Nonanti/mathcore/llms.txt
Use this file to discover all available pages before exploring further.
mathcore::calculus module provides symbolic differentiation, symbolic integration, and numerical integration over arbitrary expressions. It also exposes a limits submodule for computing one-sided and two-sided limits, evaluating continuity, and applying L’Hôpital’s rule. All operations work on the Expr AST type and return Result values carrying a MathError on failure.
pub struct Calculus
A zero-sized unit struct that acts as a namespace for all calculus operations. Every method is a free associated function — no instance is required.
differentiate
Symbolically differentiates expr with respect to the variable named var. The result is automatically simplified with basic algebraic identities (elimination of zero/one coefficients, double-negation collapse, etc.) before being returned.
Supported expression forms include constants, symbols, polynomials, quotients, the power rule (including the chain rule for composite exponents), negation, absolute value, and the standard transcendental functions sin, cos, tan, sec, ln, exp, and sqrt. Encountering an unsupported form returns Err(MathError::InvalidOperation).
The symbolic expression to differentiate. Must be a valid
Expr node; complex sub-expressions are handled recursively.The name of the differentiation variable. Any
Expr::Symbol whose string matches var is treated as the independent variable; all other symbols are treated as constants.Ok(Expr) containing the derivative, or Err(MathError) if the expression contains an unsupported operation.
integrate
Computes the symbolic (indefinite) integral of expr with respect to var. The following patterns are handled analytically:
| Input form | Result |
|---|---|
constant n | n * var |
var | var^2 / 2 |
var^n (n ≠ −1) | var^(n+1) / (n+1) |
var^(−1) | ln(var) |
sin(var) | −cos(var) |
cos(var) | sin(var) |
exp(var) | exp(var) |
| sum / difference | term-by-term integration |
| constant × f(var) | constant extracted outside |
Expr::Integral node so downstream code can represent the result symbolically.
The integrand expression. Must be a valid
Expr node.The integration variable name. Symbols matching this name are treated as the variable; all others are constants pulled outside the integral.
Ok(Expr) with the antiderivative (no constant of integration is appended), or Err(MathError) on failure.
numerical_integrate
Approximates the definite integral of expr from lower to upper using Simpson’s 3/8 composite rule (also known as the composite Simpson rule with midpoint correction). The interval is divided into steps equal sub-intervals; each sub-interval contributes h * (y1 + 4*y_mid + y2) / 6. The expression is evaluated numerically at each grid point using the engine’s variable substitution.
The integrand. Must evaluate to a numeric
Expr::Number at every sample point; non-numeric results produce Err(MathError::InvalidOperation).The integration variable. At each sample point the variable is bound to the current
x value before the expression is evaluated.Lower bound of integration.
Upper bound of integration.
Number of sub-intervals. Higher values increase accuracy. A value of
1000 is typically sufficient for smooth functions.Ok(f64) with the numerical approximation, or Err(MathError) if evaluation fails at any sample point.
pub mod limits
The limits submodule exposes LimitDirection, the Limits struct, and supporting helpers for numerically evaluating limits and checking continuity. Import it as mathcore::calculus::limits.
pub enum LimitDirection
Specifies the approach direction when computing a limit.
| Variant | Description |
|---|---|
Left | Uses sample points slightly below point (offset −1e-10 divided over 10 steps). |
Right | Uses sample points slightly above point (offset +1e-10 divided over 10 steps). |
Both | Computes left and right limits independently; returns the value when they agree within 1e-9, or Err when they diverge. |
pub struct Limits
A zero-sized unit struct namespacing all limit operations.
Limits::limit
Numerically approximates lim_{var → point} expr from the requested direction. The implementation evaluates the expression at up to ten probe points approaching point from the given side, then returns the converged numerical value (or the appropriate infinity symbol when divergent).
The expression whose limit is to be computed.
The variable approaching
point.The value that
var approaches.Whether to approach from the left, right, or verify both sides agree.
Ok(Expr::Number(n)) when the limit converges, Ok(Expr::Symbol("∞")) or Ok(Expr::Symbol("-∞")) when it diverges to infinity, or Err(MathError::InvalidOperation) when the limit does not exist or cannot be computed.
Limits::limit_at_infinity
Evaluates the limit of expr as var approaches positive or negative infinity by substituting the large proxy value 1e10 (positive) or -1e10 (negative) and calling the numerical limit engine.
The expression whose limit at infinity is to be evaluated.
The variable that tends to infinity.
Pass
true for var → +∞ or false for var → -∞.Ok(Expr::Number(n)) when the limit converges to a finite value, Ok(Expr::Symbol("∞")) or Ok(Expr::Symbol("-∞")) when it diverges, or Err(MathError) when the limit cannot be determined.
Limits::is_continuous_at
Checks whether expr is continuous at var = point by comparing the function’s value at that point to its two-sided limit. Returns true when both exist, are finite, and agree within 1e-9.
The expression to test for continuity.
The variable name at which continuity is checked.
The point at which continuity is tested.
Ok(true) if the function is continuous at point, Ok(false) if it has a discontinuity or the limit does not exist, or Err(MathError) if evaluation fails.
Limits::lhopital_rule
Applies L’Hôpital’s rule to the indeterminate form numerator / denominator at the given point. The method first checks whether both the numerator and denominator evaluate to zero (within tolerance 1e-10) at point. If so, it differentiates both expressions symbolically and evaluates the limit of the ratio of their derivatives. Otherwise it evaluates the limit of the original ratio directly.
The numerator expression. Symbolic differentiation is applied when a 0/0 form is detected.
The denominator expression. Must not be identically zero at all points.
The variable with respect to which the limit is taken and which is used for symbolic differentiation.
The point at which the limit is evaluated. Both expressions are checked for a 0/0 form here.
Ok(Expr) containing the limit value, or Err(MathError) if the limit cannot be computed.
