mathhook_core/calculus/derivatives/
partial.rs

1//! Partial derivative operations for multivariate calculus
2//!
3//! Provides comprehensive partial differentiation capabilities organized
4//! into focused modules for better maintainability and performance.
5mod gradient;
6mod hessian;
7mod jacobian;
8mod utils;
9mod vector_fields;
10use crate::calculus::derivatives::Derivative;
11use crate::core::{Expression, Symbol};
12use crate::simplify::Simplify;
13pub use gradient::{DirectionalDerivatives, GradientOperations};
14pub use hessian::HessianOperations;
15pub use jacobian::{JacobianDeterminant, JacobianOperations};
16pub use utils::{MatrixUtils, PartialUtils};
17pub use vector_fields::{ConservativeFields, FluidDynamicsOperations, VectorFieldOperations};
18/// Main partial derivatives interface
19pub struct PartialDerivatives;
20impl PartialDerivatives {
21    /// Compute mixed partial derivative ∂ⁿf/∂x₁∂x₂...∂xₙ
22    ///
23    /// # Examples
24    ///
25    /// ```rust
26    /// use mathhook_core::simplify::Simplify;
27    /// use mathhook_core::calculus::derivatives::Derivative;
28    /// use mathhook_core::{Expression};
29    /// use mathhook_core::symbol;
30    /// use mathhook_core::calculus::derivatives::PartialDerivatives;
31    ///
32    /// let x = symbol!(x);
33    /// let y = symbol!(y);
34    /// let expr = Expression::mul(vec![
35    ///     Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
36    ///     Expression::pow(Expression::symbol(y.clone()), Expression::integer(3))
37    /// ]);
38    /// let mixed = PartialDerivatives::mixed_partial(&expr, vec![x, y]);
39    /// ```
40    pub fn mixed_partial(expr: &Expression, variables: Vec<Symbol>) -> Expression {
41        let mut result = expr.clone();
42        for var in variables {
43            result = result.derivative(var);
44        }
45        result.simplify()
46    }
47    /// Compute gradient vector - delegates to specialized module
48    ///
49    /// # Examples
50    ///
51    /// ```rust
52    /// use mathhook_core::{Expression};
53    /// use mathhook_core::symbol;
54    /// use mathhook_core::calculus::derivatives::PartialDerivatives;
55    ///
56    /// let x = symbol!(x);
57    /// let y = symbol!(y);
58    /// let expr = Expression::add(vec![
59    ///     Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
60    ///     Expression::pow(Expression::symbol(y.clone()), Expression::integer(2))
61    /// ]);
62    /// let grad = PartialDerivatives::gradient(&expr, vec![x, y]);
63    /// ```
64    pub fn gradient(expr: &Expression, variables: Vec<Symbol>) -> Vec<Expression> {
65        GradientOperations::compute(expr, variables)
66    }
67    /// Compute Hessian matrix - delegates to specialized module
68    ///
69    /// # Examples
70    ///
71    /// ```rust
72    /// use mathhook_core::{Expression};
73    /// use mathhook_core::symbol;
74    /// use mathhook_core::calculus::derivatives::PartialDerivatives;
75    ///
76    /// let x = symbol!(x);
77    /// let y = symbol!(y);
78    /// let expr = Expression::add(vec![
79    ///     Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
80    ///     Expression::mul(vec![Expression::symbol(x.clone()), Expression::symbol(y.clone())]),
81    ///     Expression::pow(Expression::symbol(y.clone()), Expression::integer(2))
82    /// ]);
83    /// let hessian = PartialDerivatives::hessian(&expr, vec![x, y]);
84    /// ```
85    pub fn hessian(expr: &Expression, variables: Vec<Symbol>) -> Vec<Vec<Expression>> {
86        HessianOperations::compute(expr, &variables)
87    }
88    /// Compute directional derivative - delegates to specialized module
89    ///
90    /// # Examples
91    ///
92    /// ```rust
93    /// use mathhook_core::{Expression};
94    /// use mathhook_core::symbol;
95    /// use mathhook_core::calculus::derivatives::PartialDerivatives;
96    ///
97    /// let x = symbol!(x);
98    /// let y = symbol!(y);
99    /// let expr = Expression::add(vec![
100    ///     Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
101    ///     Expression::pow(Expression::symbol(y.clone()), Expression::integer(2))
102    /// ]);
103    /// let direction = vec![Expression::integer(1), Expression::integer(1)];
104    /// let dir_deriv = PartialDerivatives::directional_derivative(&expr, vec![x, y], direction);
105    /// ```
106    pub fn directional_derivative(
107        expr: &Expression,
108        variables: Vec<Symbol>,
109        direction: Vec<Expression>,
110    ) -> Expression {
111        DirectionalDerivatives::compute(expr, variables, direction)
112    }
113    /// Compute Jacobian matrix - delegates to specialized module
114    pub fn jacobian(functions: &[Expression], variables: &[Symbol]) -> Vec<Vec<Expression>> {
115        JacobianOperations::compute(functions, variables)
116    }
117    /// Compute Jacobian determinant - delegates to specialized module
118    pub fn jacobian_determinant(functions: &[Expression], variables: &[Symbol]) -> Expression {
119        JacobianDeterminant::compute(functions, variables)
120    }
121    /// Compute divergence - delegates to specialized module
122    pub fn divergence(vector_field: &[Expression], variables: Vec<Symbol>) -> Expression {
123        VectorFieldOperations::divergence(vector_field, variables)
124    }
125    /// Compute curl - delegates to specialized module
126    pub fn curl(vector_field: &[Expression], variables: Vec<Symbol>) -> Vec<Expression> {
127        VectorFieldOperations::curl(vector_field, &variables)
128    }
129    /// Compute Laplacian - delegates to specialized module
130    pub fn laplacian(expr: &Expression, variables: Vec<Symbol>) -> Expression {
131        VectorFieldOperations::laplacian(expr, variables)
132    }
133    /// Check if vector field is conservative - delegates to specialized module
134    pub fn is_conservative(vector_field: &[Expression], variables: Vec<Symbol>) -> bool {
135        ConservativeFields::is_conservative(vector_field, variables)
136    }
137    /// Find potential function - delegates to specialized module
138    pub fn find_potential(
139        vector_field: &[Expression],
140        variables: Vec<Symbol>,
141    ) -> Option<Expression> {
142        ConservativeFields::find_potential(vector_field, &variables)
143    }
144}