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§
- CasDiff
Lib - The CAS differentiation and integration library.
- CasDiff
Registry - A registry of per-operator differentiation rules.
Functions§
- diff_
cas - Differentiate a
CasExprwith respect tovar, returning a simplified derivative tree. - diff_
symbol - The
diffsymbol: the symbolic differentiation entry point. - global_
diff_ registry - Access the process-global differentiation-rule registry.
- integrate_
cas - Integrate a
CasExprwith respect tovar, returning a simplified antiderivative tree (without constant of integration). - integrate_
sym_ symbol - The
integrate-symsymbol: the symbolic integration entry point. - register_
diff_ rule - Register
ruleforsymbolin the global registry, returning any rule it replaced.
Type Aliases§
- Diff
Rule - A custom differentiation rule: maps an operator’s arguments and the variable
of differentiation to a derivative tree, or
Noneto decline.