1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use crate;
use Vec;
use DMatrix;
/*
pub struct DifferentiableBlock<'a, D: DifferentiableFunctionTrait, E: DerivativeMethodTrait, AP: DifferentiableBlockArgPrepTrait<'a, D, E>> {
function_standard_args: D::ArgsType<'a, f64>,
function_derivative_args: D::ArgsType<'a, E::T>,
derivative_method_data: E::DerivativeMethodData,
args_prep: AP
}
impl<'a, D: DifferentiableFunctionTrait, E: DerivativeMethodTrait, AP: DifferentiableBlockArgPrepTrait<'a, D, E>> DifferentiableBlock<'a, D, E, AP> {
pub fn new(function_standard_args: D::ArgsType<'a, f64>,
function_derivative_args: D::ArgsType<'a, E::T>,
derivative_method_data: E::DerivativeMethodData,
args_prep: AP) -> Self {
Self {
function_standard_args,
function_derivative_args,
derivative_method_data,
args_prep,
}
}
pub fn call(&self, inputs: &[f64]) -> Vec<f64> {
self.prep_args(inputs);
D::call(inputs, &self.function_standard_args)
}
pub fn derivative(&self, inputs: &[f64]) -> (Vec<f64>, DMatrix<f64>) {
self.prep_args(inputs);
D::derivative::<E>(inputs, &self.function_derivative_args, &self.derivative_method_data)
}
pub (crate) fn prep_args(&self, inputs: &[f64]) {
// AP::prep_args(inputs, &mut self.function_standard_args, &mut self.function_derivative_args);
self.args_prep.prep_args(inputs, &self.function_standard_args, &self.function_derivative_args);
}
pub fn update_args<U: Fn(&mut D::ArgsType<'_, f64>, &mut D::ArgsType<'_, E::T>) >(&mut self, update_fn: U) {
(update_fn)(&mut self.function_standard_args, &mut self.function_derivative_args)
}
/*
pub fn update_args2<A: DifferentiableBlockUpdateArgs<'a, D>>(&mut self, u: A) {
u.update_args(&mut self.function_standard_args);
u.update_args(&mut self.function_derivative_args);
}
*/
pub fn function_standard_args(&self) -> &D::ArgsType<'a, f64> {
&self.function_standard_args
}
pub fn function_derivative_args(&self) -> &D::ArgsType<'a, E::T> {
&self.function_derivative_args
}
pub fn derivative_method_data(&self) -> &E::DerivativeMethodData {
&self.derivative_method_data
}
}
*/
/*
/// Has to be separate so that both the standard args and derivative args can be updated at the same time
pub trait DifferentiableBlockArgPrepTrait<'a, D: DifferentiableFunctionTrait, E: DerivativeMethodTrait> {
fn prep_args(&self, inputs: &[f64], function_standard_args: &D::ArgsType<'a, f64>, function_derivative_args: &D::ArgsType<'a, E::T>);
}
impl<'a, D: DifferentiableFunctionTrait, E: DerivativeMethodTrait> DifferentiableBlockArgPrepTrait<'a, D, E> for () {
fn prep_args(&self, _inputs: &[f64], _function_standard_args: &D::ArgsType<'a, f64>, _function_derivative_args: &D::ArgsType<'a, E::T>) { }
}
*/
/*
pub struct DifferentiableBlock2<'a, E: DerivativeMethodTrait2> {
function_standard: Box<dyn DifferentiableFunctionTrait2<'a, f64> + 'a>,
function_derivative: Box<dyn DifferentiableFunctionTrait2<'a, E::T> + 'a>,
derivative_method: E
}
impl<'a, E: DerivativeMethodTrait2> DifferentiableBlock2<'a, E> {
pub fn new<D1, D2>(derivative_method: E, function_standard: D1, function_derivative: D2) -> Self
where D1: DifferentiableFunctionTrait2<'a, f64> + 'a,
D2: DifferentiableFunctionTrait2<'a, E::T> + 'a {
assert_eq!(function_standard.type_string(), function_derivative.type_string(), "must be the same type");
Self {
function_standard: Box::new(function_standard),
function_derivative: Box::new(function_derivative),
derivative_method,
}
}
#[inline]
pub fn call(&self, inputs: &[f64]) -> Vec<f64> {
self.function_standard.call(inputs)
}
#[inline]
pub fn derivative(&self, inputs: &[f64]) -> (Vec<f64>, DMatrix<f64>) {
self.derivative_method.derivative(inputs, &*self.function_derivative)
}
pub fn update_args<DC: DifferentiableFunctionClass + 'static, U: Fn(&mut DC::FunctionType<'a, f64>, &mut DC::FunctionType<'a, E::T>) >(&'a mut self, update_fn: U) {
// let mut fs = self.function_standard.as_mut().
// (update_fn)(&mut self.function_standard_args, &mut self.function_derivative_args)
}
}
*/
/*
pub struct DifferentiableBlock2<'a, E: DerivativeMethodTrait2, D1: DifferentiableFunctionTrait2<'a, f64>, D2: DifferentiableFunctionTrait2<'a, E::T>> {
function_standard: D1,
function_derivative: D2,
derivative_method: E,
phantom_data: PhantomData<&'a ()>
}
impl<'a, E: DerivativeMethodTrait2, D1: DifferentiableFunctionTrait2<'a, f64>, D2: DifferentiableFunctionTrait2<'a, E::T>> DifferentiableBlock2<'a, E, D1, D2> {
pub fn new(function_standard: D1, function_derivative: D2, derivative_method: E) -> Self {
assert_eq!(function_derivative.type_string(), function_standard.type_string(), "differentiable block must have functions of the same type");
Self { function_standard, function_derivative, derivative_method, phantom_data: Default::default() }
}
#[inline]
pub fn call(&self, inputs: &[f64]) -> Vec<f64> {
self.function_standard.call(inputs)
}
#[inline]
pub fn derivative(&self, inputs: &[f64]) -> (Vec<f64>, DMatrix<f64>) {
self.derivative_method.derivative(inputs, &self.function_derivative)
}
pub fn update_function<U: Fn(&D1, &D2) >(&'a self, update_fn: U) {
update_fn(&self.function_standard, &self.function_derivative)
}
}
*/
/*
#[derive(Clone)]
pub struct DifferentiableBlock<DC: DifferentiableFunctionClass, E: DerivativeMethodTrait> {
function_standard: DC::FunctionType<f64>,
function_derivative: DC::FunctionType<E::T>,
derivative_method: E
}
impl<DC: DifferentiableFunctionClass, E: DerivativeMethodTrait> DifferentiableBlock<DC, E> {
pub fn new(derivative_method: E, function_standard: DC::FunctionType<f64>, function_derivative: DC::FunctionType<E::T>) -> Self {
Self {
function_standard,
function_derivative,
derivative_method,
}
}
pub fn new_with_tag(_differentiable_function_class: DC, derivative_method: E, function_standard: DC::FunctionType<f64>, function_derivative: DC::FunctionType<E::T>) -> Self {
Self {
function_standard,
function_derivative,
derivative_method,
}
}
#[inline(always)]
pub fn num_inputs(&self) -> usize {
self.function_standard.num_inputs()
}
#[inline(always)]
pub fn num_outputs(&self) -> usize {
self.function_standard.num_outputs()
}
#[inline]
pub fn call(&self, inputs: &[f64]) -> Vec<f64> {
self.function_standard.call(inputs, false)
}
#[inline]
pub fn call_ad_values(&self, inputs: &[E::T]) -> Vec<E::T> {
self.function_derivative.call(inputs, false)
}
#[inline]
pub fn derivative(&self, inputs: &[f64]) -> (Vec<f64>, DMatrix<f64>) {
self.derivative_method.derivative(inputs, &self.function_derivative)
}
pub fn update_function<U: Fn(&DC::FunctionType<f64>, &DC::FunctionType<E::T>) >(&self, update_fn: U) {
update_fn(&self.function_standard, &self.function_derivative)
}
#[inline]
pub fn function_standard(&self) -> &DC::FunctionType<f64> {
&self.function_standard
}
#[inline]
pub fn function_derivative(&self) -> &DC::FunctionType<E::T> {
&self.function_derivative
}
#[inline]
pub fn derivative_method(&self) -> &E {
&self.derivative_method
}
}
pub type DifferentiableBlockEmpty = DifferentiableBlock<(), ()>;
impl DifferentiableBlockEmpty {
pub fn new_empty() -> Self {
Self::new((), (), ())
}
}
pub type DifferentiableBlockZero<E> = DifferentiableBlock<DifferentiableFunctionClassZero, E>;
impl<E: DerivativeMethodTrait> DifferentiableBlockZero<E> {
pub fn new_zero(derivative_method: E, num_inputs: usize, num_outputs: usize) -> Self {
Self::new(derivative_method, DifferentiableFunctionZero::new(num_inputs, num_outputs), DifferentiableFunctionZero::new(num_inputs, num_outputs))
}
}
*/
/// A wrapper that combines a differentiable function with a specific derivative method.
///
/// `FunctionEngine` simplifies the process of evaluating a function or computing its derivative
/// by encapsulating both the base function (operating on `f64`) and the derivative-tracking
/// version of that function (operating on an `AD` type).
/*
#[allow(dead_code)]
pub struct DifferentiableBlock2<'a, E: DerivativeMethodTrait> {
functions_standard: Vec<Box<dyn DifferentiableFunctionTrait2<'a, f64>>>,
functions_derivative: Vec<Box<dyn DifferentiableFunctionTrait2<'a, E::T>>>,
weights_standard: Vec<f64>,
weights_derivative: Vec<E::T>,
derivative_method: E
}
#[allow(unused_variables)]
impl<'a, E: DerivativeMethodTrait> DifferentiableBlock2<'a, E> {
pub fn new_empty(derivative_method: E) -> Self {
Self {
functions_standard: vec![],
functions_derivative: vec![],
weights_standard: vec![],
weights_derivative: vec![],
derivative_method,
}
}
pub fn insert_function<F1: DifferentiableFunctionTrait2<'a, f64> + 'a, F2: DifferentiableFunctionTrait2<'a, E::T> + 'a>(mut self, function_standard: F1, function_derivative: F2, weight: f64) -> Self {
assert_eq!(function_standard.name(), function_derivative.name());
self.functions_standard.push(Box::new(function_standard));
self.functions_derivative.push(Box::new(function_derivative));
self.weights_standard.push(weight);
self.weights_derivative.push(weight.to_other_ad_type::<E::T>());
self
}
pub fn update_weight(&mut self, function_idx: usize, weight: f64) {
self.weights_standard[function_idx] = weight;
self.weights_derivative[function_idx] = weight.to_other_ad_type::<E::T>();
}
pub fn update_function<F1: DifferentiableFunctionTrait2<'a, f64>, F2: DifferentiableFunctionTrait2<'a, E::T>>(&self, function_idx: usize) {
// self.functions_standard[function_idx].downcast_ref::<F1>();
todo!()
}
}
*/
/*
pub trait DifferentiableBlockArgPrepTrait2<'a, DC: DifferentiableFunctionClass, E: DerivativeMethodTrait2> {
fn prep_args(&self, inputs: &[f64], function_standard_args: &DC::FunctionType<'a, f64>, function_derivative_args: &DC::FunctionType<'a, E::T>);
}
impl<'a, DC: DifferentiableFunctionClass, E: DerivativeMethodTrait2> DifferentiableBlockArgPrepTrait2<'a, DC, E> for () {
fn prep_args(&self, _inputs: &[f64], _function_standard_args: &DC::FunctionType<'a, f64>, _function_derivative_args: &DC::FunctionType<'a, E::T>) { }
}
*/
/*
pub trait DifferentiableBlockUpdateArgs<'a, D: DifferentiableFunctionTrait> {
fn update_args<T: AD>(&self, args: &mut D::ArgsType<'_, T>);
}
*/