@@ -64,8 +64,7 @@ fn main() {
6464
6565 let dataset = Dataset :: new (x , y );
6666
67- let operators = Operators :: <D >:: from_names_by_arity :: <BuiltinOpsF32 >(& [" cos" , " exp" , " sin" ], & [" +" , " -" , " *" , " /" ], & [])
68- . unwrap ();
67+ let operators = BuiltinOpsF32 :: from_names ([" cos" , " exp" , " sin" , " +" , " sub" , " *" , " /" ]). unwrap ();
6968
7069 let options = Options :: <f32 , D > {
7170 operators ,
@@ -99,42 +98,46 @@ fn main() {
9998
10099## Custom operators
101100
102- Define light-weight operator sets with inline evaluation and derivatives using the ` custom_opset! ` macro (re-exported from ` symbolic_regression ` ) :
101+ Define custom operators with ` op! ` , then build an operator set with ` opset! ` :
103102
104103``` rust
105- use symbolic_regression :: custom_opset;
106104use symbolic_regression :: prelude :: * ;
107105
108- custom_opset! {
109- pub struct CustomOps <f64 > {
110- 1 {
111- square {
112- eval (args ) { args [0 ] * args [0 ] },
113- partial (args , _idx ) { 2.0 * args [0 ] },
114- }
115- exp {
116- eval (args ) { args [0 ]. exp () },
117- partial (args , _idx ) { args [0 ]. exp () },
118- }
119- }
120- 2 {
121- add {
122- eval (args ) { args [0 ] + args [1 ] },
123- partial (_args , _idx ) { 1.0 },
124- }
125- sub {
126- infix : " -" , // optional
127- complexity : 2 , // optional
128- eval (args ) { args [0 ] - args [1 ] },
129- partial (_args , idx ) {
130- if idx == 0 { 1.0 } else { - 1.0 }
131- },
132- }
133- }
106+ op! (Square for f64 {
107+ eval : | [x ]| { x * x },
108+ partial : | [x ], _idx | { 2.0 * x },
109+ });
110+
111+ op! (Exp for f64 {
112+ eval : | [x ]| { x . exp () },
113+ partial : | [x ], _idx | { x . exp () },
114+ });
115+
116+ op! (Add for f64 {
117+ infix : " +" ,
118+ commutative : true ,
119+ associative : true ,
120+ eval : | [x , y ]| { x + y },
121+ partial : | [_x , _y ], _idx | { 1.0 },
122+ });
123+
124+ op! (Sub for f64 {
125+ infix : " -" , // optional
126+ complexity : 2 , // optional
127+ eval : | [x , y ]| { x - y },
128+ partial : | [_x , _y ], idx | { if idx == 0 { 1.0 } else { - 1.0 } },
129+ });
130+
131+ opset! {
132+ pub CustomOps for f64 {
133+ Square ,
134+ Exp ,
135+ Add ,
136+ Sub ,
134137 }
135138}
136139
137- let operators = CustomOps :: from_names_by_arity ( & [" square" , " exp" ], & [ " add" , " sub" ], & [ ]). unwrap ();
140+ let operators = CustomOps :: from_names ( [" square" , " exp" , " add" , " sub" ]). unwrap ();
138141let options = Options :: <f64 , _ > { operators , .. Default :: default () };
139142```
140143
0 commit comments