Skip to main content

expr

Macro expr 

Source
macro_rules! expr {
    (input -> $($rest:tt)*) => { ... };
    (inputs: [$($input:ident),*] $($rest:tt)*) => { ... };
    (@build_single $graph:ident, $node:ident, Add -> $($rest:tt)*) => { ... };
    (@build_single $graph:ident, $node:ident, Mul -> $($rest:tt)*) => { ... };
    (@build_single $graph:ident, $node:ident, $op:ident -> $($rest:tt)*) => { ... };
    (@build_single $graph:ident, $node:ident, $op:ident ( $($op_args:tt)* ) -> $($rest:tt)*) => { ... };
    (@build_single $graph:ident, $node:ident, output) => { ... };
    (@build_multi $graph:ident, $node:ident -> Add -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, $node:ident -> Mul -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, $node:ident -> Add ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, $node:ident -> Mul ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, $node:ident -> $op:ident -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, $node:ident -> $op:ident ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> Sin -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> Cos -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> Scale ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> Pow ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( @ $node:ident ) -> Add -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( @ $node:ident ) -> Mul -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> $op:ident -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, ( $( @ $node:ident ),+ ) -> $op:ident ( $($op_args:tt)* ) -> @ $result:ident $($rest:tt)*) => { ... };
    (@build_multi $graph:ident, output @ $node:ident) => { ... };
    (@build_multi $graph:ident, output) => { ... };
}
Expand description

Macro for building differentiable expressions.

§Examples

Single input expression:

let expr = expr! {
    input -> Sin -> Cos -> output
};

Multi-input expression:

let expr = expr! {
    inputs: [x, y]
    x -> Pow(2) -> @x_sq
    y -> Sin -> @y_sin
    (@x_sq, @y_sin) -> Add -> @result
    output @result
};

Mixed expression (operations without intermediate names):

let expr = expr! {
    inputs: [x, y]
    x -> Pow(2) -> @temp1
    y -> Cos -> @temp2
    (@temp1, @temp2) -> Mul -> @res
    output @res
};

§Performance Notes

The default eval path allocates a fresh ReverseTape each call for purity. When you need to reuse buffers, create a tape with expr.tape() (or expr.reverse_tape()) and call eval_with_tape to keep allocations off the hot path. Operation arity is validated at runtime.