Skip to main content

Crate sim_lib_numbers_cas_diff

Crate sim_lib_numbers_cas_diff 

Source
Expand description

Symbolic differentiation and integration over the numbers/cas expression tree: the diff and integrate-sym functions plus an extensible registry of per-operator differentiation rules.

§Examples

Teach the differentiator a new operator through the extensible rule registry, then look the rule up. Rules map an operator’s arguments and the variable of differentiation to a derivative CasExpr:

use sim_kernel::Symbol;
use sim_lib_numbers_cas::CasExpr;
use sim_lib_numbers_cas_diff::{global_diff_registry, register_diff_rule};

// d/dx ln(x) = 1/x, expressed structurally as (/ 1 x).
register_diff_rule(
    Symbol::new("ln-doctest"),
    Box::new(|args: &[CasExpr], _var: &Symbol| {
        let [arg] = args else { return None };
        Some(CasExpr::Op(
            Symbol::qualified("math", "div"),
            vec![CasExpr::Var(Symbol::new("1")), arg.clone()],
        ))
    }),
);

let registry = global_diff_registry().read().unwrap();
let derivative = registry.apply(
    &Symbol::new("ln-doctest"),
    &[CasExpr::Var(Symbol::new("x"))],
    &Symbol::new("x"),
);
assert!(matches!(derivative, Some(CasExpr::Op(_, _))));

Structs§

CasDiffLib
The CAS differentiation and integration library.
CasDiffRegistry
A registry of per-operator differentiation rules.

Functions§

diff_cas
Differentiate a CasExpr with respect to var, returning a simplified derivative tree.
diff_symbol
The diff symbol: the symbolic differentiation entry point.
global_diff_registry
Access the process-global differentiation-rule registry.
integrate_cas
Integrate a CasExpr with respect to var, returning a simplified antiderivative tree (without constant of integration).
integrate_sym_symbol
The integrate-sym symbol: the symbolic integration entry point.
register_diff_rule
Register rule for symbol in the global registry, returning any rule it replaced.

Type Aliases§

DiffRule
A custom differentiation rule: maps an operator’s arguments and the variable of differentiation to a derivative tree, or None to decline.