ad_trait 0.3.1

Easy to use, efficient, and highly flexible automatic differentiation in Rust
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! # Introduction
//! This crate brings easy to use, efficient, and highly flexible automatic differentiation to the
//! Rust programming language. Utilizing Rust's extensive and expressive trait features, the several
//! types in this crate that implement the trait AD can be thought of as a drop-in replacement for an
//! f64 or f32 that affords forward mode or backwards mode automatic differentiation on any downstream
//! computation in Rust.
//!
//! # Key Features
//! - ad_trait supports reverse mode or forward mode automatic differentiation. The forward mode automatic
//! differentiation implementation can also take advantage of SIMD to compute multiple tangents simultaneously.
//! - The core rust f64 or f32 types also implement the AD trait, meaning any functions that take an AD
//! trait object as a generic type can handle either standard floating point computation or derivative
//! tracking automatic differentiation with essentially no overhead.
//! - The provided types that implement the AD trait also implement several useful traits that allow it
//! to operate almost exactly as a standard f64. For example, it even implements the `RealField` and
//! `ComplexField` traits, meaning it can be used in any `nalgebra` or `ndarray` computations.
//!
//! # Example
//! ```
//! use ad_trait::AD;
//! use ad_trait::function_engine::FunctionEngine;
//! use ad_trait::differentiable_function::{DifferentiableFunctionTrait, FiniteDifferencing, ForwardAD, ForwardADMulti, ReverseAD};
//! use ad_trait::forward_ad::adfn::adfn;
//! use ad_trait::reverse_ad::adr::adr;
//!
//! #[derive(Clone)]
//! pub struct Test<T: AD> {
//!     coeff: T
//! }
//! impl<T: AD> DifferentiableFunctionTrait<T> for Test<T> {
//!     const NAME: &'static str = "Test";
//!
//!     fn call(&self, inputs: &[T], _freeze: bool) -> Vec<T> {
//!         vec![ self.coeff*inputs[0].sin() + inputs[1].cos() ]
//!     }
//!
//!     fn num_inputs(&self) -> usize {
//!         2
//!     }
//!
//!     fn num_outputs(&self) -> usize {
//!         1
//!     }
//! }
//! impl<T: AD> Test<T> {
//!     pub fn to_other_ad_type<T2: AD>(&self) -> Test<T2> {
//!         Test { coeff: self.coeff.to_other_ad_type::<T2>() }
//!     }
//! }
//!
//!
//! fn main() {
//!     let inputs = vec![1., 2.];
//!
//!     // Reverse AD //////////////////////////////////////////////////////////////////////////////////
//!     let function_standard = Test { coeff: 2.0 };
//!     let function_derivative = function_standard.to_other_ad_type::<adr>();
//!     let differentiable_block = FunctionEngine::new(function_standard, function_derivative, ReverseAD::new());
//!
//!     let (f_res, derivative_res) = differentiable_block.derivative(&inputs);
//!     println!("Reverse AD: ");
//!     println!("  f_res: {}", f_res[0]);
//!     println!("  derivative: {}", derivative_res);
//!     println!("//////////////");
//!     println!();
//!
//!     // Forward AD //////////////////////////////////////////////////////////////////////////////////
//!     let function_standard = Test { coeff: 2.0 };
//!     let function_derivative = function_standard.to_other_ad_type::<adfn<1>>();
//!     let differentiable_block = FunctionEngine::new(function_standard, function_derivative, ForwardAD::new());
//!
//!     let (f_res, derivative_res) = differentiable_block.derivative(&inputs);
//!     println!("Forward AD: ");
//!     println!("  f_res: {}", f_res[0]);
//!     println!("  derivative: {}", derivative_res);
//!     println!("//////////////");
//!     println!();
//!
//!     // Forward AD Multi ////////////////////////////////////////////////////////////////////////////
//!     let function_standard = Test { coeff: 2.0 };
//!     let function_derivative = function_standard.to_other_ad_type::<adfn<2>>();
//!     let differentiable_block = FunctionEngine::new(function_standard, function_derivative, ForwardADMulti::new());
//!
//!     let (f_res, derivative_res) = differentiable_block.derivative(&inputs);
//!     println!("Forward AD Multi: ");
//!     println!("  f_res: {}", f_res[0]);
//!     println!("  derivative: {}", derivative_res);
//!     println!("//////////////");
//!     println!();
//!
//!     // Finite Differencing /////////////////////////////////////////////////////////////////////////
//!     let function_standard = Test { coeff: 2.0 };
//!     let function_derivative = function_standard.clone();
//!     let differentiable_block = FunctionEngine::new(function_standard, function_derivative, FiniteDifferencing::new());
//!
//!     let (f_res, derivative_res) = differentiable_block.derivative(&inputs);
//!     println!("Finite Differencing: ");
//!     println!("  f_res: {}", f_res[0]);
//!     println!("  derivative: {}", derivative_res);
//!     println!("//////////////");
//!     println!();
//!
//! }
//! ```
//! # Citation
//!
//! For more information about our work, refer to our paper:
//! <https://arxiv.org/abs/2504.15976>
//!
//! If you use this crate in your research, please cite:
//!
//! ```text
//! @article{liang2025ad,
//!   title={ad-trait: A Fast and Flexible Automatic Differentiation Library in Rust},
//!   author={Liang, Chen and Wang, Qian and Xu, Andy and Rakita, Daniel},
//!   journal={arXiv preprint arXiv:2504.15976},
//!   year={2025}
//! }
//! ```
//!

// #![feature(min_specialization)]
// #![feature(portable_simd)]
// #![feature(trivial_bounds)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(
    feature = "nightly",
    feature(trivial_bounds, portable_simd, min_specialization)
)]

#[cfg(feature = "wasm_compat")]
pub extern crate nalgebra_compat as nalgebra;
#[cfg(all(feature = "latest_deps", not(feature = "wasm_compat")))]
pub extern crate nalgebra_latest as nalgebra;

#[cfg(all(feature = "bevy", feature = "wasm_compat"))]
pub extern crate bevy_reflect_compat as bevy_reflect;
#[cfg(all(feature = "bevy", feature = "latest_deps", not(feature = "wasm_compat")))]
pub extern crate bevy_reflect_latest as bevy_reflect;

extern crate alloc;
#[cfg(any(feature = "std", test))]
extern crate std;

pub mod differentiable_function;
pub mod forward_ad;
pub mod function_engine;
#[cfg(feature = "std")]
pub mod reverse_ad;
pub mod simd;

#[cfg(feature = "bevy")]
use bevy_reflect::Reflect;

#[cfg(feature = "bevy")]
pub trait MaybeReflect: Reflect {}
#[cfg(feature = "bevy")]
impl<T: Reflect> MaybeReflect for T {}

#[cfg(not(feature = "bevy"))]
pub trait MaybeReflect {}
#[cfg(not(feature = "bevy"))]
impl<T> MaybeReflect for T {}
use core::cmp::Ordering;
use core::fmt::{Debug, Display};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use nalgebra::{Dim, Matrix, RawStorageMut, Scalar};
use ndarray::{ArrayBase, Dimension, OwnedRepr, ScalarOperand};
use num_traits::Signed;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
use simba::scalar::{ComplexField, RealField};
use simba::simd::{SimdComplexField, SimdRealField};

/// The core trait for Automatic Differentiation (AD).
///
/// This trait defines the interface for types that can participate in automatic differentiation
/// computations. It combines numerical traits (like `RealField` and `ComplexField` from `simba`)
/// with AD-specific functionality.
///
/// Types implementing `AD` can represent:
/// -   Standard floating-point values (`f64`, `f32`) for non-derivative computations.
/// -   Forward-mode AD types (`adfn<K>`) for tangent propagation.
/// -   Reverse-mode AD types (`adr`) for gradient backpropagation.
pub trait AD :
    RealField +
    ComplexField +
    PartialOrd +
    PartialEq +
    Signed +
    Scalar +
    // Float +
    Clone +
    Copy +
    Debug +
    Display +
    Default +

    Add<F64, Output=Self> +
    AddAssign<F64> +
    Mul<F64, Output=Self> +
    MulAssign<F64> +
    Sub<F64, Output=Self> +
    SubAssign<F64> +
    Div<F64, Output=Self> +
    DivAssign<F64> +
    Rem<F64, Output=Self> +
    RemAssign<F64> +

    From<f32> +
    Into<f64> +

    SimdRealField +
    SimdComplexField +

    Serialize +
    DeserializeOwned +

    MaybeReflect +

    ScalarOperand
{
    /// Creates a constant value of this AD type from an `f64`.
    fn constant(constant: f64) -> Self;

    /// Converts the current AD value back to its base `f64` representation.
    /// This retrieves the value without its derivative information.
    fn to_constant(&self) -> f64;

    /// Returns the constant version of the current value as a new AD object.
    #[inline(always)]
    fn to_constant_ad(&self) -> Self {
        Self::constant(self.to_constant())
    }

    /// Returns the mode of automatic differentiation used by this type.
    fn ad_num_mode() -> ADNumMode;

    /// Returns the specific numerical type identifier for this AD type.
    fn ad_num_type() -> ADNumType;

    /// Scalar addition between an `f64` and an `AD` value.
    fn add_scalar(arg1: f64, arg2: Self) -> Self;

    /// Scalar subtraction: `arg1 (f64) - arg2 (AD)`.
    fn sub_l_scalar(arg1: f64, arg2: Self) -> Self;

    /// Scalar subtraction: `arg1 (AD) - arg2 (f64)`.
    fn sub_r_scalar(arg1: Self, arg2: f64) -> Self;

    /// Scalar multiplication between an `f64` and an `AD` value.
    fn mul_scalar(arg1: f64, arg2: Self) -> Self;

    /// Scalar division: `arg1 (f64) / arg2 (AD)`.
    fn div_l_scalar(arg1: f64, arg2: Self) -> Self;

    /// Scalar division: `arg1 (AD) / arg2 (f64)`.
    fn div_r_scalar(arg1: Self, arg2: f64) -> Self;

    /// Scalar remainder: `arg1 (f64) % arg2 (AD)`.
    fn rem_l_scalar(arg1: f64, arg2: Self) -> Self;

    /// Scalar remainder: `arg1 (AD) % arg2 (f64)`.
    fn rem_r_scalar(arg1: Self, arg2: f64) -> Self;

    /// Multiplies this scalar by an `nalgebra` matrix of AD values.
    fn mul_by_nalgebra_matrix<R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C>>(&self, other: Matrix<Self, R, C, S>) -> Matrix<Self, R, C, S>;

    /// Multiplies this scalar by a reference to an `nalgebra` matrix of AD values.
    fn mul_by_nalgebra_matrix_ref<'a, R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C>>(&'a self, other: &'a Matrix<Self, R, C, S>) -> Matrix<Self, R, C, S>;

    /// Multiplies this scalar by an `ndarray` matrix of AD values.
    fn mul_by_ndarray_matrix_ref<D: Dimension>(&self, other: &ArrayBase<OwnedRepr<Self>, D>) -> ArrayBase<OwnedRepr<Self>, D>;

    /// Converts this AD value to another AD type.
    /// This is useful for reparameterizing functions during the differentiation process.
    fn to_other_ad_type<T2: AD>(&self) -> T2 {
        T2::constant(self.to_constant())
    }
}

pub trait ObjectAD {
    fn to_constant(&self) -> f64;
}

/*
pub trait NalgebraMatMulAD<'a, R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C> + 'a>:
    AD +
    Mul<Matrix<Self, R, C, S>, Output=Matrix<Self, R, C, S>> +
    Mul<&'a Matrix<Self, R, C, S>, Output=Matrix<Self, R, C, S>> +
    Sized
{ }
*/

/*
pub trait NalgebraMatMulAD<R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C>>:
    Sized
{
    fn mul_by_nalgebra_matrix(&self, other: Matrix<Self, R, C, S>) -> Matrix<Self, R, C, S>;
    fn mul_by_nalgebra_matrix_ref<'a>(&'a self, other: &'a Matrix<Self, R, C, S>) -> Matrix<Self, R, C, S>;
}

impl<T: AD, R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C>> NalgebraMatMulAD<R, C, S> for T
    where T: Mul<Matrix<Self, R, C, S>, Output=Matrix<Self, R, C, S>> + for<'a> Mul<&'a Matrix<Self, R, C, S>, Output=Matrix<Self, R, C, S>>
{
    fn mul_by_nalgebra_matrix(&self, other: Matrix<T, R, C, S>) -> Matrix<T, R, C, S> {
        *self * other
    }
    fn mul_by_nalgebra_matrix_ref<'b>(&'b self, other: &'b Matrix<T, R, C, S>) -> Matrix<T, R, C, S> {
        *self * other
    }
}
*/

/*
pub trait NalgebraPointMulAD<'a, D: DimName>:
    AD +
    Mul<OPoint<Self, D>, Output=OPoint<Self, D>> +
    Mul<&'a OPoint<Self, D>, Output=OPoint<Self, D>> +
    Sized where DefaultAllocator: nalgebra::allocator::Allocator<Self, D>
{ }
*/
// pub type OVector<T, D> = Matrix<T, D, U1, Owned<T, D, U1>>;
// <DefaultAllocator as Allocator<T, R, C>>::Buffer

/*
pub trait NalgebraMatMulAD3<R: Clone + Dim>:
    AD +
    Mul<Matrix<Self, R, U1, Owned<Self, R, U1>>, Output=Matrix<Self, R, U1, Owned<Self, R, U1>>> +
    Sized
    where DefaultAllocator: Allocator<Self, R>
{
    fn mul(&self, other: Matrix<Self, R, U1, Owned<Self, R, U1>>) -> Matrix<Self, R, U1, Owned<Self, R, U1>>;
    fn mul_by_ref(&self, other: &Matrix<Self, R, U1, Owned<Self, R, U1>>) -> Matrix<Self, R, U1, Owned<Self, R, U1>>;
}
*/

/*
pub trait NalgebraPointMulAD2<D: DimName>:
    AD +
    // Mul<OPoint<Self, D>, Output=OPoint<Self, D>> +
    Sized
    where DefaultAllocator: Allocator<Self, D>
{
    fn mul(&self, other: OPoint<Self, D>) -> OPoint<Self, D>;
    fn mul_by_ref(&self, other: &OPoint<Self, D>) -> OPoint<Self, D>;
}
*/

/*
pub trait NalgebraMatMulNoRefAD<R: Clone + Dim, C: Clone + Dim, S: Clone + RawStorageMut<Self, R, C>>:
    AD +
    Mul<Matrix<Self, R, C, S>, Output=Matrix<Self, R, C, S>> +
    Sized
{ }
*/

/*
pub trait NalgebraPointMulNoRefAD<D: DimName>:
    AD +
    Mul<OPoint<Self, D>, Output=OPoint<Self, D>> +
    Sized where DefaultAllocator: nalgebra::allocator::Allocator<Self, D>
{ }
*/

#[macro_export]
macro_rules! ad_setup {
    ($($T: ident),*) => {
        $(
        ad_setup_f64!($T);
        // ad_setup_any_nalgebra_dmatrix!($T);
        )*
    }
}
ad_setup!(f64, f32);

/// Categorizes the mode of automatic differentiation used by a type.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ADNumMode {
    /// Standard floating point computation without differentiation metadata.
    Float,
    /// Forward-mode automatic differentiation (tangent propagation).
    ForwardAD,
    #[cfg(feature = "std")]
    /// Reverse-mode automatic differentiation (gradient backpropagation).
    ReverseAD,
    /// SIMD-accelerated numerical computation.
    SIMDNum,
}

/// Identifies the specific numerical type used in AD computations.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(non_camel_case_types)]
pub enum ADNumType {
    /// Standard 64-bit float.
    F64,
    /// Standard 32-bit float.
    F32,
    #[cfg(feature = "std")]
    /// Reverse-mode AD type (`adr`).
    ADR,
    /// Forward-mode AD type (`adfn`).
    ADFN,
    /// Alternative forward-mode AD type.
    ADF,
    /// SIMD-based 64-bit float vector.
    F64XN,
    #[cfg(feature = "hessian")]
    /// Hyper-dual forward-mode AD type.
    HYPER_ADFN,
    #[cfg(feature = "hessian")]
    /// Forward-over-reverse AD type.
    HYPER_ADR,
}

////////////////////////////////////////////////////////////////////////////////////////////////////

pub trait FloatADTrait: AD {}

impl AD for f64 {
    fn constant(v: f64) -> Self {
        return v;
    }

    fn to_constant(&self) -> f64 {
        *self
    }

    fn ad_num_mode() -> ADNumMode {
        ADNumMode::Float
    }

    fn ad_num_type() -> ADNumType {
        ADNumType::F64
    }

    fn add_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 + arg2
    }

    fn sub_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 - arg2
    }

    fn sub_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 - arg2
    }

    fn mul_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 * arg2
    }

    fn div_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 / arg2
    }

    fn div_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 / arg2
    }

    fn rem_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 % arg2
    }

    fn rem_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 % arg2
    }

    fn mul_by_nalgebra_matrix<
        R: Clone + Dim,
        C: Clone + Dim,
        S: Clone + RawStorageMut<Self, R, C>,
    >(
        &self,
        other: Matrix<Self, R, C, S>,
    ) -> Matrix<Self, R, C, S> {
        let mut out = other.clone();
        out.iter_mut().for_each(|x| *x *= *self);
        out
    }

    fn mul_by_nalgebra_matrix_ref<
        'a,
        R: Clone + Dim,
        C: Clone + Dim,
        S: Clone + RawStorageMut<Self, R, C>,
    >(
        &'a self,
        other: &'a Matrix<Self, R, C, S>,
    ) -> Matrix<Self, R, C, S> {
        let mut out = other.clone();
        out.iter_mut().for_each(|x| *x *= *self);
        out
    }

    fn mul_by_ndarray_matrix_ref<D: Dimension>(
        &self,
        other: &ArrayBase<OwnedRepr<Self>, D>,
    ) -> ArrayBase<OwnedRepr<Self>, D> {
        other * *self
    }
}
impl FloatADTrait for f64 {}

impl AD for f32 {
    fn constant(v: f64) -> Self {
        return v as f32;
    }

    fn to_constant(&self) -> f64 {
        *self as f64
    }

    fn ad_num_mode() -> ADNumMode {
        ADNumMode::Float
    }

    fn ad_num_type() -> ADNumType {
        ADNumType::F32
    }

    fn add_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 as f32 + arg2
    }

    fn sub_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 as f32 - arg2
    }

    fn sub_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 - arg2 as f32
    }

    fn mul_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 as f32 * arg2
    }

    fn div_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 as f32 / arg2
    }

    fn div_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 / arg2 as f32
    }

    fn rem_l_scalar(arg1: f64, arg2: Self) -> Self {
        arg1 as f32 % arg2
    }

    fn rem_r_scalar(arg1: Self, arg2: f64) -> Self {
        arg1 % arg2 as f32
    }

    fn mul_by_nalgebra_matrix<
        R: Clone + Dim,
        C: Clone + Dim,
        S: Clone + RawStorageMut<Self, R, C>,
    >(
        &self,
        other: Matrix<Self, R, C, S>,
    ) -> Matrix<Self, R, C, S> {
        let mut out = other.clone();
        out.iter_mut().for_each(|x| *x *= *self);
        out
    }

    fn mul_by_nalgebra_matrix_ref<
        'a,
        R: Clone + Dim,
        C: Clone + Dim,
        S: Clone + RawStorageMut<Self, R, C>,
    >(
        &'a self,
        other: &'a Matrix<Self, R, C, S>,
    ) -> Matrix<Self, R, C, S> {
        let mut out = other.clone();
        out.iter_mut().for_each(|x| *x *= *self);
        out
    }

    fn mul_by_ndarray_matrix_ref<D: Dimension>(
        &self,
        other: &ArrayBase<OwnedRepr<Self>, D>,
    ) -> ArrayBase<OwnedRepr<Self>, D> {
        other * *self
    }
}
impl FloatADTrait for f32 {}

/*
#[macro_export]
macro_rules! nalgebra_mat_mul_ad_setup {
    ($t1: tt, $t2: tt; $(($x:tt, $y:tt)),*) => {
        $(
            impl NalgebraMatMulAD2<Const<$x>, Const<$y>, ArrayStorage<$t1, $x, $y>> for $t1 {
                fn mul_by_nalgebra_matrix(&self, other: Matrix<$t1, Const<$x>, Const<$y>, ArrayStorage<$t1, $x, $y>>) -> Matrix<$t1, Const<$x>, Const<$y>, ArrayStorage<$t1, $x, $y>> {
                    *self * other
                }
                fn mul_by_nalgebra_matrix_ref(&self, other: &Matrix<$t1, Const<$x>, Const<$y>, ArrayStorage<$t1, $x, $y>>) -> Matrix<$t1, Const<$x>, Const<$y>, ArrayStorage<$t1, $x, $y>> {
                    *self * other
                }
            }
            impl NalgebraMatMulAD2<Const<$x>, Const<$y>, ArrayStorage<$t2, $x, $y>> for $t2 {
                fn mul_by_nalgebra_matrix(&self, other: Matrix<$t2, Const<$x>, Const<$y>, ArrayStorage<$t2, $x, $y>>) -> Matrix<$t2, Const<$x>, Const<$y>, ArrayStorage<$t2, $x, $y>> {
                    *self * other
                }
                fn mul_by_nalgebra_matrix_ref(&self, other: &Matrix<$t2, Const<$x>, Const<$y>, ArrayStorage<$t2, $x, $y>>) -> Matrix<$t2, Const<$x>, Const<$y>, ArrayStorage<$t2, $x, $y>> {
                    *self * other
                }
            }
        )*
    }
}
nalgebra_mat_mul_ad_setup!(f64, f32; (1, 2), (2, 1), (1, 3), (3, 1), (2, 3), (3, 2), (1, 1), (2, 2), (3, 3), (4, 4));
*/

// impl<'a> NalgebraMatMulAD<'a, Const<3>, Const<1>, ArrayStorage<f64, 3, 1>> for f64 { }
// impl<'a> NalgebraMatMulAD<'a, Const<3>, Const<3>, ArrayStorage<f64, 3, 3>> for f64 { }

// impl<'a> NalgebraMatMulAD<'a, Const<3>, Const<1>, ArrayStorage<f32, 3, 1>> for f32 { }
// impl<'a> NalgebraMatMulAD<'a, Const<3>, Const<1>, ArrayStorage<f32, 3, 1>> for f32 { }

////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Clone, Debug, Copy)]
pub struct F64(pub f64);

impl<T: AD> Add<T> for F64 {
    type Output = T;

    #[inline]
    fn add(self, rhs: T) -> Self::Output {
        AD::add_scalar(self.0, rhs)
    }
}
impl<T: AD> Mul<T> for F64 {
    type Output = T;

    fn mul(self, rhs: T) -> Self::Output {
        AD::mul_scalar(self.0, rhs)
    }
}
impl<T: AD> Sub<T> for F64 {
    type Output = T;

    fn sub(self, rhs: T) -> Self::Output {
        AD::sub_l_scalar(self.0, rhs)
    }
}
impl<T: AD> Div<T> for F64 {
    type Output = T;

    fn div(self, rhs: T) -> Self::Output {
        AD::div_l_scalar(self.0, rhs)
    }
}
impl<T: AD> Rem<T> for F64 {
    type Output = T;

    fn rem(self, rhs: T) -> Self::Output {
        AD::rem_l_scalar(self.0, rhs)
    }
}

#[macro_export]
macro_rules! ad_setup_f64 {
    ($T: ident) => {
        impl Add<F64> for $T {
            type Output = $T;

            #[inline]
            fn add(self, rhs: F64) -> Self::Output {
                AD::add_scalar(rhs.0, self)
            }
        }

        impl AddAssign<F64> for $T {
            #[inline]
            fn add_assign(&mut self, rhs: F64) {
                *self = *self + rhs;
            }
        }

        impl Mul<F64> for $T {
            type Output = $T;

            #[inline]
            fn mul(self, rhs: F64) -> Self::Output {
                AD::mul_scalar(rhs.0, self)
            }
        }

        impl MulAssign<F64> for $T {
            #[inline]
            fn mul_assign(&mut self, rhs: F64) {
                *self = *self * rhs;
            }
        }

        impl Sub<F64> for $T {
            type Output = $T;

            #[inline]
            fn sub(self, rhs: F64) -> Self::Output {
                AD::sub_r_scalar(self, rhs.0)
            }
        }

        impl SubAssign<F64> for $T {
            #[inline]
            fn sub_assign(&mut self, rhs: F64) {
                *self = *self - rhs;
            }
        }

        impl Div<F64> for $T {
            type Output = $T;

            #[inline]
            fn div(self, rhs: F64) -> Self::Output {
                AD::div_r_scalar(self, rhs.0)
            }
        }

        impl DivAssign<F64> for $T {
            #[inline]
            fn div_assign(&mut self, rhs: F64) {
                *self = *self / rhs;
            }
        }

        impl Rem<F64> for $T {
            type Output = $T;

            #[inline]
            fn rem(self, rhs: F64) -> Self::Output {
                AD::rem_r_scalar(self, rhs.0)
            }
        }

        impl RemAssign<F64> for $T {
            #[inline]
            fn rem_assign(&mut self, rhs: F64) {
                *self = *self % rhs;
            }
        }
    };
}

////////////////////////////////////////////////////////////////////////////////////////////////////

impl<T: AD> ObjectAD for T {
    fn to_constant(&self) -> f64 {
        self.to_constant()
    }
}

impl PartialEq<f64> for dyn ObjectAD {
    fn eq(&self, other: &f64) -> bool {
        self.to_constant().eq(other)
    }
}

impl PartialOrd<f64> for dyn ObjectAD {
    fn partial_cmp(&self, other: &f64) -> Option<Ordering> {
        self.to_constant().partial_cmp(other)
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////

// Custom serializer for the AD trait
pub fn ad_custom_serialize<S, T: AD>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_f64(value.to_constant())
}

// Custom deserializer for the AD trait
pub fn ad_custom_deserialize<'de, D, T: AD>(deserializer: D) -> Result<T, D::Error>
where
    D: Deserializer<'de>,
{
    let constant = f64::deserialize(deserializer)?;
    Ok(T::constant(constant))
}
pub struct SerdeAD<T: AD>(pub T);

impl<T: AD> SerializeAs<T> for SerdeAD<T> {
    fn serialize_as<S>(source: &T, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        ad_custom_serialize(source, serializer)
    }
}
impl<'de, T: AD> DeserializeAs<'de, T> for SerdeAD<T> {
    fn deserialize_as<D>(deserializer: D) -> Result<T, D::Error>
    where
        D: Deserializer<'de>,
    {
        ad_custom_deserialize(deserializer)
    }
}

pub trait ADConvertableTrait {
    type ConvertableType<T: AD>;

    fn convert_to_other_ad_type<T1: AD, T2: AD>(
        input: &Self::ConvertableType<T1>,
    ) -> Self::ConvertableType<T2>;
}
impl ADConvertableTrait for () {
    type ConvertableType<T: AD> = ();

    fn convert_to_other_ad_type<T1: AD, T2: AD>(
        _input: &Self::ConvertableType<T1>,
    ) -> Self::ConvertableType<T2> {
        ()
    }
}

#[cfg(feature = "hessian")]
pub mod hyper_ad;