rustad 0.0.0

A rust Automatic Differentiation library
Documentation
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2025 Bradley M. Bell
// ---------------------------------------------------------------------------
//
//! This module defines the automatic differentiation class `AD<V>`.
//!
//! Link to [parent module](super)
//!
// ---------------------------------------------------------------------------
use std::thread::LocalKey;
use std::cell::RefCell;
//
use crate::IndexT;
use crate::tape::Tape;
use crate::tape::sealed::ThisThreadTape;
use crate::op::id;
// ---------------------------------------------------------------------------
/// Documentation for the rustad generic type parameter V.
///
/// The generic parameter *V* , in ``AD`` < *V* > and other generic types ,
/// is the type used for calculating values.
/// It does not have dependency information that represents
/// how each value is related to the domain variables (independent variables).
pub fn doc_generic_v() {}
//
// AD
//
/// AD acts like V but in addition can record a function evaluation.
///
/// * V : see [doc_generic_v]
///
/// * variable :
/// An AD object is a variable if it one of the domain variables
/// or its value depends on the value of the domain variables.
///
/// * constant :
/// If an AD object is not a variable it is referred to as a constant.
///
#[derive(Clone, Debug)]
pub struct AD<V> {
    //
    // tape_id
    ///
    /// An AD object is a variable if the following two conditions hold:
    /// 1. This threads tape is currently recording.
    /// 2. This threads tape and the AD object have the same *tape_id* .
    pub(crate) tape_id   : usize,
    //
    // var_index
    /// If this AD object is a variable, *var_index* is its index in the tape.
    pub(crate) var_index : usize,
    //
    // value
    /// is the value of this AD variable or constant.
    pub(crate) value     : V,
}
//
// new
impl<V> AD<V> {
    //
    /// Create an arbitrary new AD object.
    ///
    /// * new_tape_id : is the [AD::tape_id] for the new object.
    ///
    /// * new_var_index : is the [AD::var_index] for the new object.
    ///
    /// * new_value : is the [AD::value}} for the new object.
    pub(crate) fn new(
        new_tape_id: usize, new_var_index: usize, new_value: V )-> Self {
        Self {
            tape_id   : new_tape_id,
            var_index : new_var_index,
            value     : new_value,
        }
    }
}
//
// to_value
impl<V> AD<V> {
    //
    /// Convert an AD object to a value
    /// (its the variable information is lost).
    ///
    /// **See Also** : example in [ad_from_value]
    ///
    /// # Example using NumVec
    /// ```
    /// use rustad::AD;
    /// use rustad::ad_from_value;
    /// use rustad::NumVec;
    /// let v   : Vec<f64>    = vec![ 2.0, 3.0 ];
    /// let nv                = NumVec::new(v);
    /// let av                = ad_from_value(nv);
    /// let nv                = av.to_value();
    /// assert_eq!( nv.get(0), 2.0 );
    /// assert_eq!( nv.get(1), 3.0 );
    /// ```
    pub fn to_value(self) -> V {
        self.value
    }
}
// ---------------------------------------------------------------------------
// Display
//
/// Display only shows the value and ignores the variable information.
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// let x  : f64  = 5.0;
/// let ax        = ad_from_value(x);
/// let s         = format!( "{ax}" );
/// assert_eq!(s, "5");
/// ```
///
/// # Example using NumVec
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// use rustad::NumVec;
/// let x  : Vec<f64>  = vec![ 5.0, 6.0 ];
/// let x_nv           = NumVec::new(x);
/// let ax             = ad_from_value(x_nv);
/// let s              = format!( "{ax}" );
/// assert_eq!(s, "[ 5, 6, ]");
/// ```
impl<V : std::fmt::Display> std::fmt::Display for AD<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.value)
    }
}
// ---------------------------------------------------------------------------
/// Binary `AD<V>` operators.
///
/// * Syntax :
/// ```text
///        az = &ax Op &ay
///        az = &ay Op &y
/// ```
///
/// * V : see [doc_generic_v]
///
/// * Op : is the source code token for this binary operator;
/// i.e., `+` , `-` , `*` , or `/` .
///
/// * ax : left hand side `AD<V>` object
/// * ay : right hand side `AD<V>` object
/// * az : result `AD<V>` object
///
/// * x  : left hand side *V* object
/// * y  : right hand side *V* object
///
/// # Example
///```
/// use rustad::AD;
/// use rustad::ad_from_value;
///
/// let ax  = ad_from_value( 3.0f32 );
/// let y   = 4.0f32;
/// let az  = &ax * &y;
/// assert_eq!( az.to_value(), 12.0f32 );
///
/// let x  = 3.0f32;
/// let ay = ad_from_value(4.0f32 );
/// let az  = &x * &ay;
/// assert_eq!( az.to_value(), 12.0f32 );
/// ```
///
/// # Example using NumVec
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// use rustad::NumVec;
///
/// let x     : Vec<f64> = vec![ 1.0, 4.0 ];
/// let y     : Vec<f64> = vec![ 2.0, 2.0 ];
/// let x_nv             = NumVec::new(x);
/// let y_nv             = NumVec::new(y);
/// let ax               = ad_from_value(x_nv);
/// let ay               = ad_from_value(y_nv);
/// let az               = &ax / &ay;
/// let z_nv             = az.to_value();
/// assert_eq!( z_nv.get(0), 0.5f64 );
/// assert_eq!( z_nv.get(1), 2.0f64 );
/// ```
pub fn doc_ad_binary_op() { }
//
/// Add one binary operator to the `AD<V>` class;
//
/// * V : see [doc_generic_v]
/// * Name : is the operator name; i.e., Add, Sub, Mul, or Div.
/// * Op : is the operator token; i.e., +, -, *, or /.
///
/// see [doc_ad_binary_op]
macro_rules! ad_binary_op { ($Name:ident, $Op:tt) => { paste::paste! {
    // -----------------------------------------------------------------------
    fn [< record_ $Name:lower _aa >]<V> (
        tape: &mut Tape<V> ,
        lhs:       &AD<V>  ,
        rhs:       &AD<V>  ,
    ) -> (usize, usize)
    where
        V : Clone ,
    {
        let mut new_tape_id   = 0;
        let mut new_var_index = 0;
        if tape.recording {
            let var_lhs    = lhs.tape_id == tape.tape_id;
            let var_rhs    = rhs.tape_id == tape.tape_id;
            if var_lhs || var_rhs {
                new_tape_id   = tape.tape_id;
                new_var_index = tape.n_var;
                tape.n_var   += 1;
                tape.op2arg.push( tape.arg_all.len() as IndexT );
                if var_lhs && var_rhs {
                    tape.id_all.push( id::[< $Name:upper _VV_OP >] );
                    tape.arg_all.push( lhs.var_index as IndexT );
                    tape.arg_all.push( rhs.var_index as IndexT );
                } else if var_lhs {
                    tape.id_all.push( id::[< $Name:upper _VC_OP >] );
                    tape.arg_all.push( lhs.var_index as IndexT );
                    tape.arg_all.push( tape.con_all.len() as IndexT );
                    tape.con_all.push( rhs.value.clone() );
                } else {
                    tape.id_all.push( id::[< $Name:upper _CV_OP >] );
                    tape.arg_all.push( tape.con_all.len() as IndexT );
                    tape.con_all.push( lhs.value.clone() );
                    tape.arg_all.push( rhs.var_index as IndexT );
                }
            }
        }
        ( new_tape_id, new_var_index )
    }
    //
    #[doc = concat!(
        "& `AD<V>` ", stringify!($Op), " & `AD<V>`",
        "; see [doc_ad_binary_op]"
    )]
    impl<V> std::ops::$Name< &AD<V> > for &AD<V>
    where
        for<'a> &'a V: std::ops::$Name<&'a V, Output=V>,
        V    : Clone + crate::ThisThreadTapePublic ,
    {   type Output = AD<V>;
        //
        fn [< $Name:lower >](self , rhs : &AD<V> ) -> AD<V>
        {
            // new_value
            let new_value     = &self.value  $Op &rhs.value;
            //
            // local_key
            let local_key : &LocalKey< RefCell< Tape<V> > > =
                ThisThreadTape::get();
            //
            // new_tape_id, new_var_index
            let (new_tape_id, new_var_index) =
                local_key.with_borrow_mut( |tape|
                    [< record_ $Name:lower _aa >]::<V> ( tape, &self, &rhs )
            );
            //
            // result
            AD::new(new_tape_id, new_var_index, new_value)
        }
    }
    // -----------------------------------------------------------------------
    fn [< record_ $Name:lower _av >]<V> (
        tape: &mut Tape<V> ,
        lhs:       &AD<V>  ,
        rhs:       &V      ,
    ) -> (usize, usize)
    where
        V : Clone ,
    {
        let mut new_tape_id   = 0;
        let mut new_var_index = 0;
        if tape.recording {
            let var_lhs    = lhs.tape_id == tape.tape_id;
            if var_lhs {
                new_tape_id   = tape.tape_id;
                new_var_index = tape.n_var;
                tape.n_var   += 1;
                tape.op2arg.push( tape.arg_all.len() as IndexT );
                tape.id_all.push( id::[< $Name:upper _VC_OP >] );
                tape.arg_all.push( lhs.var_index as IndexT );
                tape.arg_all.push( tape.con_all.len() as IndexT );
                tape.con_all.push( rhs.clone() );
            }
        }
        (new_tape_id, new_var_index)
    }
    //
    #[doc = concat!(
        "& `AD<V>` ", stringify!($Op), " & V`",
        "; see [doc_ad_binary_op]"
    )]
    impl<V> std::ops::$Name< &V> for &AD<V>
    where
        for<'a> &'a V: std::ops::$Name<&'a V, Output=V>,
        V            : Clone + crate::ThisThreadTapePublic ,
    {   type Output = AD<V>;
        //
        fn [< $Name:lower >](self , rhs : &V ) -> AD<V>
        {
            // new_value
            let new_value     = &self.value  $Op &rhs;
            //
            // local_key
            let local_key : &LocalKey< RefCell< Tape<V> > > =
                ThisThreadTape::get();
            //
            // new_tape_id, new_var_index
            let (new_tape_id, new_var_index) =
                local_key.with_borrow_mut( |tape|
                    [< record_ $Name:lower _av >]::<V> ( tape, &self, &rhs )
            );
            //
            // result
            AD::new(new_tape_id, new_var_index, new_value)
        }
    }
} } }
//
ad_binary_op!(Add, +);
ad_binary_op!(Sub, -);
ad_binary_op!(Mul, *);
ad_binary_op!(Div, /);
// ---------------------------------------------------------------------------
/// Compound Assignment `AD<V>` operators.
///
/// Syntax :
/// ```text
///     ax Op &ay
///     ax Op &y
/// ```
///
/// * V : see [doc_generic_v]
///
/// * Op : is the source code token for this binary operator;
/// i.e., `+=` , `-=` , `*=` , or `/=` .
///
/// * ax : left hand side `AD<V>` object.
/// * ay : right hand size `AD<V>` object
/// * y  : right hand size *V* object
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
///
/// let mut ax   = ad_from_value( 3.0f64 );
/// let y        = 4.0f64;
/// ax          -= &y;
/// assert_eq!( ax.to_value(), -1.0f64 );
/// ```
///
/// # Example using NumVec
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// use rustad::NumVec;
///
/// let x     : Vec<f32>  = vec![ 1.0, 4.0 ];
/// let y     : Vec<f32>  = vec![ 2.0, 2.0 ];
/// let x_nv              = NumVec::new(x);
/// let y_nv              = NumVec::new(y);
/// let mut ax            = ad_from_value(x_nv);
/// let ay                = ad_from_value(y_nv);
/// ax                   *= &ay;
/// let x_nv              = ax.to_value();
/// assert_eq!( x_nv.get(0), 2.0f32 );
/// assert_eq!( x_nv.get(1), 8.0f32 );
/// ```
pub fn doc_ad_compound_op() { }
//
/// Add one compound assignment operator to the `AD<V>` class;
///
/// * V : see [doc_generic_v]
///
/// * Name : is the operator name with Assign at end;
/// i.e., Add, Sub, Mul, or Div.
///
/// * Op : is the operator token; i.e., +=, -=, *=, or /= .
///
/// see [doc_ad_compound_op]
//
/// see [doc_ad_compound_op]
macro_rules! ad_compound_op { ($Name:ident, $Op:tt) => { paste::paste! {
    // ------------------------------------------------------------------------
    fn [< record_ $Name:lower _assign_aa >]<V> (
         tape: &mut Tape<V> ,
         lhs:  &mut AD<V>   ,
         rhs:  &    AD<V>   )
     where
        V : Clone,
     {
         if tape.recording {
             let var_lhs    = lhs.tape_id == tape.tape_id;
             let var_rhs    = rhs.tape_id == tape.tape_id;
             if var_lhs || var_rhs {
                 tape.op2arg.push( tape.arg_all.len() as IndexT );
                 if var_lhs && var_rhs {
                     tape.id_all.push( id::[< $Name:upper _VV_OP >] );
                     tape.arg_all.push( lhs.var_index as IndexT);
                     tape.arg_all.push( rhs.var_index as IndexT);
                 } else if var_lhs {
                     tape.id_all.push( id::[< $Name:upper _VC_OP >] );
                     tape.arg_all.push( lhs.var_index as IndexT);
                     tape.arg_all.push( tape.con_all.len() as IndexT );
                     tape.con_all.push( rhs.value.clone() );
                 } else {
                     tape.id_all.push( id::[< $Name:upper _CV_OP >] );
                     tape.arg_all.push( tape.con_all.len() as IndexT );
                     tape.con_all.push( lhs.value.clone() );
                     tape.arg_all.push( rhs.var_index as IndexT);
                 }
                 lhs.tape_id   = tape.tape_id;
                 lhs.var_index = tape.n_var;
                 tape.n_var   += 1;
             }
         }
     }
    //
    #[doc = concat!(
        "`AD<V>` ", stringify!($Op), " & `AD<V>`",
        "; see [doc_ad_compound_op]"
    )]
    impl<V> std::ops::[< $Name Assign >] < &AD<V> > for AD<V>
    where
        V: Clone +
            for<'a> std::ops::[< $Name Assign >] <&'a V> +
            crate::ThisThreadTapePublic  ,
    {   //
        fn [< $Name:lower _assign >] (&mut self, rhs : &AD<V> )
        {   //
            //
            // local_key
            let local_key : &LocalKey< RefCell< Tape<V> > > =
                ThisThreadTape::get();
            //
            // tape, self.tape_id, self.var_index
            local_key.with_borrow_mut( |tape|
                [< record_ $Name:lower _assign_aa >]::<V> ( tape, self, rhs )
            );
            //
            // self.value
            // record above assuees that self.value is its value before this op
            self.value $Op &rhs.value;
        }
    }
    // ------------------------------------------------------------------------
     fn [< record_ $Name:lower _assign_av >]<V> (
         tape: &mut Tape<V> ,
         lhs:  &mut AD<V>   ,
         rhs:  &    V       )
     where
        V : Clone,
     {
         if tape.recording {
             let var_lhs    = lhs.tape_id == tape.tape_id;
             if var_lhs {
                 tape.op2arg.push( tape.arg_all.len() as IndexT );
                 tape.id_all.push( id::[< $Name:upper _VC_OP >] );
                 tape.arg_all.push( lhs.var_index as IndexT);
                 tape.arg_all.push( tape.con_all.len() as IndexT );
                 tape.con_all.push( rhs.clone() );
                 //
                 lhs.var_index = tape.n_var;
                 tape.n_var   += 1;
             }
         }
     }
    //
    #[doc = concat!(
        "`AD<V>` ", stringify!($Op), " & V; see [doc_ad_compound_op]"
    )]
    impl<V> std::ops::[< $Name Assign >] <&V> for AD<V>
    where
        V: Clone +
            for<'a> std::ops::[< $Name Assign >] <&'a V> +
            crate::ThisThreadTapePublic  ,
    {   //
        fn [< $Name:lower _assign >] (&mut self, rhs : &V)
        {   //
            // local_key
            let local_key : &LocalKey< RefCell< Tape<V> > > =
                ThisThreadTape::get();
            //
            // tape, self.tape_id, self.var_index
            local_key.with_borrow_mut( |tape|
                [< record_ $Name:lower _assign_av >]::<V> ( tape, self, rhs )
            );
            //
            // self.value
            // record above assuees that self.value is its value before this op
            self.value $Op &rhs;
        }
    }
} } }
//
ad_compound_op!(Add, +=);
ad_compound_op!(Sub, -=);
ad_compound_op!(Mul, *=);
ad_compound_op!(Div, /=);
// ---------------------------------------------------------------------------
// record_value_op_ad!
//
/// Create function that records
/// one binary operation where lhs is *V* and rhs is `AD<V>` .
///
/// * Name         : is the operator name; i.e., Add, Sub, Mul, or Div.
///
/// * Op           : is the operator token; i.e., +, -, *, or /.
///
/// * Function Name: `record_value_` *name* `_ad` where *name* is
///  a lower case version of Name.
///
macro_rules! record_value_op_ad{ ($Name:ident, $Op:tt) => { paste::paste! {
    #[doc = concat!( "record one ", stringify!($Name),
        " where lhs is a value and rhs is a variable"
    ) ]
    pub(crate) fn [< record_value_ $Name:lower _ad >]<V> (
        tape: &mut Tape<V> ,
        lhs:       &V      ,
        rhs:       &AD<V>  ,
    ) -> (usize, usize)
    where
        V : Clone ,
    {
        let mut new_tape_id   = 0;
        let mut new_var_index = 0;
        if tape.recording {
            let var_rhs    = rhs.tape_id == tape.tape_id;
            if var_rhs {
                new_tape_id   = tape.tape_id;
                new_var_index = tape.n_var;
                tape.n_var   += 1;
                tape.op2arg.push( tape.arg_all.len() as IndexT );
                tape.id_all.push( id::[< $Name:upper _CV_OP >] );
                tape.arg_all.push( tape.con_all.len() as IndexT );
                tape.con_all.push( lhs.clone() );
                tape.arg_all.push( rhs.var_index as IndexT );
            }
        }
        (new_tape_id, new_var_index)
    }
} } }
record_value_op_ad!(Add, +=);
record_value_op_ad!(Sub, -=);
record_value_op_ad!(Mul, *=);
record_value_op_ad!(Div, /=);
// ---------------------------------------------------------------------------
// impl_value_op_ad!
//
// If you try to make this implementation generic w.r.t V,
// you get a message saying that f32 and f64 must be covered
// because they are not local types.
//
/// Implement one binary `AD<V>` operator where lhs is a *V* object.
///
/// * V : see [doc_generic_v]
/// * Name : is the operator name; i.e., Add, Sub, Mul, or Div.
/// * Op : is the operator token; i.e., +, -, *, or /.
///
/// If *V* is the only argument to this macro, it will invoke itself
/// with *Op* equal to +, -, *, / and the corresponding *Name* .
///
/// see [doc_ad_binary_op]
///
/// This macro can be invoked from anywhere given the following use statements:
/// ```text
///     use std::thread::LocalKey;
///     use std::cell::RefCell;
///     use crate::ad::AD;
/// ```
macro_rules! impl_value_op_ad{
    ($V:ty)                      => {
        crate::ad::impl_value_op_ad!($V, Add, +);
        crate::ad::impl_value_op_ad!($V, Sub, -);
        crate::ad::impl_value_op_ad!($V, Mul, *);
        crate::ad::impl_value_op_ad!($V, Div, /);
    };
    ($V:ty, $Name:ident, $Op:tt) => { paste::paste! {
        #[doc =
        "see [doc_ad_binary_op](crate::ad::doc_ad_binary_op)"
        ]
        impl std::ops::$Name< &AD<$V> > for & $V
        where
            for <'a> &'a $V : std::ops::$Name<&'a $V, Output=$V>,
        {   type Output = AD<$V>;
            //
            #[ doc = concat!(
                "compute & `", stringify!($V), "` ",
                stringify!($Op), " & `AD<", stringify!($f1), ">` "
            ) ]
            fn [< $Name:lower >]
                (self , rhs : &AD<$V>
            ) -> AD<$V> {
                //
                // new_value
                let new_value = self $Op &rhs.value;
                //
                // local_key
                let local_key : &LocalKey<
                    RefCell< crate::tape::Tape<$V> >
                > = crate::tape::sealed::ThisThreadTape::get();
                //
                // new_tape_id, new_var_index
                let (new_tape_id, new_var_index) = local_key.with_borrow_mut(
                    |tape|
                    crate::ad::[< record_value_ $Name:lower _ad >]::<$V>
                            ( tape, &self, &rhs )
                );
                //
                // result
                AD::new(new_tape_id, new_var_index, new_value)
            }
        }
    } }
}
pub(crate) use impl_value_op_ad;
// ---------------------------------------------------------------------------
// ad_from_value
/// Convert a value to an AD object with no variable information;
/// i.e., a constant
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// let x  : f32  = 3.0;
/// let ax        = ad_from_value(x);
/// assert_eq!( ax.to_value(), 3.0 );
/// ```
pub fn ad_from_value<V>(value : V) -> AD<V> {
    let tape_id   = 0;
    let var_index = 0;
    AD::new(tape_id, var_index, value)
}
// ---------------------------------------------------------------------------
// ad_from_vector
/// Convert a vector to an vector of AD objects with no variable information;
/// i.e., a vector of constants.
///
/// **See Also** : example in [ad_from_value]
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::ad_from_vector;
/// let x  : Vec<f64>  = vec![ 3.0, 4.0 ];
/// let ax             = ad_from_vector(x);
/// assert_eq!( ax[0].clone().to_value(), 3.0 );
/// assert_eq!( ax[1].clone().to_value(), 4.0 );
/// ```
pub fn ad_from_vector<V> ( vec : Vec<V> ) -> Vec< AD<V> > {
    assert_ne!( vec.len() , 0 );
    let tape_id   = 0;
    let var_index = 0;
    let avec      = vec.into_iter().map(
        |value| AD::new(tape_id, var_index, value)
    ).collect();
    avec
}
// ---------------------------------------------------------------------------
// ad_to_vector
/// Convert a vector of AD object to a vector of values
/// (any variable information is lost).
///
/// **See Also** : example in [ad_from_vector], [AD::to_value]
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::ad_from_value;
/// use rustad::ad_to_vector;
/// let ax    = vec![ ad_from_value(3f64), ad_from_value(4f64) ];
/// let y     = ad_to_vector(ax);
/// assert_eq!( y , vec![ 3f64, 4f64 ] );
/// ```
///
/// # Example using NumVec
/// ```
/// use rustad::AD;
/// use rustad::NumVec;
/// use rustad::ad_from_vector;
/// use rustad::ad_to_vector;
/// let v_0  : Vec<f32>   = vec![ 2.0, 3.0 ];
/// let nv_0              = NumVec::new(v_0);
/// let v_1  : Vec<f32>   = vec![ 4.0, 5.0 ];
/// let nv_1              = NumVec::new(v_1);
/// let av                = ad_from_vector( vec![nv_0, nv_1] );
/// let v                 = ad_to_vector(av);
/// assert_eq!( v[0].get(0), 2.0 );
/// assert_eq!( v[0].get(1), 3.0 );
/// assert_eq!( v[1].get(0), 4.0 );
/// assert_eq!( v[1].get(1), 5.0 );
/// ```
pub fn ad_to_vector<V> ( avec : Vec< AD<V> > ) -> Vec<V> {
    assert_ne!( avec.len() , 0 );
    let vec  = avec.into_iter().map( |ad| ad.value).collect();
    vec
}
// -------------------------------------------------------------------------
// impl_ad_from_f32
/// Convert an f32 value to an AD object with no variable information;
/// i.e., constant.
///
/// **See Also** : example in [ad_from_value], [ad_from_vector]
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::NumVec;
/// let ax : AD< NumVec<f64> >  = (3.0 as f32).into();
/// let x                       = ax.to_value();
/// assert_eq!( x.get(0), 3.0 as f64);
/// ```
pub fn doc_impl_ad_from_f32() { }
//
/// Implement from f32 for `AD<V>` .
///
/// * V : see [doc_generic_v]
///
/// This macro must be executed once for any type *V*  where
/// `AD<V>` is used. The rustad package automatically executes it
/// for the following types: `f32` , `f64` , `NumVec<f32>`, `NumVec<f64>`.
///
/// This macro can be invoked from anywhere.
macro_rules! impl_ad_from_f32{ ($V:ty) => {
    impl From<f32> for crate::AD<$V> {
        fn from( f32_value : f32 ) -> crate::AD<$V> {
            let tape_id         = 0;
            let var_index       = 0;
            let value      : $V = f32_value.into();
            crate::AD::new(tape_id, var_index, value)
        }
    }
} }
pub(crate) use impl_ad_from_f32;
// -------------------------------------------------------------------------
// impl_ad_from_f64
/// Convert an f64 value to an AD object with no variable information;
/// i.e., constant.
///
/// Only AD objects with f64 precision are supported; e.g.,
/// `AD<f32>` is not supported.
///
/// **See Also** : example in [ad_from_value], [ad_from_vector]
///
/// # Example
/// ```
/// use rustad::AD;
/// use rustad::NumVec;
/// let ax : AD< NumVec<f64> >  = (3.0 as f64).into();
/// let x                       = ax.to_value();
/// assert_eq!( x.get(0), 3.0 as f64);
/// ```
pub fn doc_impl_ad_from_f64() { }
//
/// Implement from f64 for `AD<V>` .
///
/// * V : see [doc_generic_v]
///
/// This macro must be executed once for any type *V*  where
/// `AD<V>` is used. The rustad package automatically executes it
/// for the following types: `f64` , `NumVec<f64>` .
///
/// This macro can be invoked from anywhere.
macro_rules! impl_ad_from_f64{ ($V:ty) => {
    impl From<f64> for crate::AD<$V> {
        fn from( f64_value : f64 ) -> crate::AD<$V> {
            let tape_id         = 0;
            let var_index       = 0;
            let value      : $V = f64_value.into();
            crate::AD::new(tape_id, var_index, value)
        }
    }
} }
pub(crate) use impl_ad_from_f64;