mathhook_core/calculus/
derivatives.rs

1//! Derivative computation and symbolic differentiation
2//!
3//! Provides symbolic differentiation capabilities including basic derivatives,
4//! chain rule, product rule, quotient rule, and higher-order derivatives.
5//! Utilizes the existing Expression::Calculus infrastructure.
6mod advanced_differentiation;
7mod basic;
8mod chain_rule;
9mod checker;
10pub mod educational;
11mod higher_order;
12mod partial;
13mod power_rule;
14mod product_rule;
15use crate::core::{Expression, Symbol};
16pub use advanced_differentiation::{
17    AdvancedDifferentiation, ImplicitCurveAnalysis, ImplicitDifferentiation,
18    ParametricCurveAnalysis, ParametricDifferentiation, VectorValuedDifferentiation,
19};
20pub use basic::BasicDerivatives;
21pub use chain_rule::{ChainRule, FunctionDerivatives};
22pub use checker::DifferentiabilityChecker;
23pub use educational::DerivativeWithSteps;
24pub use higher_order::HigherOrderDerivatives;
25pub use partial::{
26    ConservativeFields, DirectionalDerivatives, FluidDynamicsOperations, GradientOperations,
27    HessianOperations, JacobianDeterminant, JacobianOperations, MatrixUtils, PartialDerivatives,
28    PartialUtils, VectorFieldOperations,
29};
30pub use power_rule::PowerRule;
31pub use product_rule::{GeneralProductRule, ProductRule};
32/// Trait for derivative operations
33pub trait Derivative {
34    /// Compute the derivative with respect to a variable
35    ///
36    /// # Examples
37    ///
38    /// ```rust
39    /// use mathhook_core::{expr, symbol};
40    /// use mathhook_core::calculus::derivatives::Derivative;
41    ///
42    /// let x = symbol!(x);
43    /// let expr = expr!(x ^ 2);
44    /// let result = expr.derivative(x);
45    /// ```
46    fn derivative(&self, variable: Symbol) -> Expression;
47    /// Compute higher-order derivatives
48    ///
49    /// # Examples
50    ///
51    /// ```rust
52    /// use mathhook_core::{expr, symbol};
53    /// use mathhook_core::calculus::derivatives::Derivative;
54    ///
55    /// let x = symbol!(x);
56    /// let expr = expr!(x ^ 4);
57    /// let second_derivative = expr.nth_derivative(x, 2);
58    /// ```
59    fn nth_derivative(&self, variable: Symbol, order: u32) -> Expression;
60    /// Check if expression is differentiable with respect to variable
61    ///
62    /// # Examples
63    ///
64    /// ```rust
65    /// use mathhook_core::{expr, symbol};
66    /// use mathhook_core::calculus::derivatives::Derivative;
67    ///
68    /// let x = symbol!(x);
69    /// let expr = expr!(sin(x));
70    /// let is_diff = expr.is_differentiable(x);
71    /// ```
72    fn is_differentiable(&self, variable: Symbol) -> bool;
73}
74impl Derivative for Expression {
75    fn derivative(&self, variable: Symbol) -> Expression {
76        match self {
77            Expression::Calculus(data) => BasicDerivatives::handle_calculus(self, data, variable),
78            Expression::Number(_) | Expression::Constant(_) => Expression::integer(0),
79            Expression::Symbol(sym) => BasicDerivatives::handle_symbol(sym, &variable),
80            Expression::Add(terms) => BasicDerivatives::handle_sum(terms, &variable),
81            Expression::Mul(factors) => ProductRule::handle_product(factors, variable),
82            Expression::Pow(base, exponent) => PowerRule::apply(base, exponent, variable),
83            Expression::Function { name, args } => ChainRule::handle_function(name, args, variable),
84            _ => Expression::derivative(self.clone(), variable, 1),
85        }
86    }
87    fn nth_derivative(&self, variable: Symbol, order: u32) -> Expression {
88        HigherOrderDerivatives::compute(self, variable, order)
89    }
90    fn is_differentiable(&self, variable: Symbol) -> bool {
91        DifferentiabilityChecker::check(self, variable)
92    }
93}