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
use Vec;
/*
use std::ops::{Add, AddAssign, Sub};
#[macro_export]
macro_rules! add_output_value {
($self: ident, $rhs: ident) => { $self.value + $rhs.value }
}
#[macro_export]
macro_rules! add_derivative1_value {
($self: ident, $rhs: ident) => { 1.0 }
}
#[macro_export]
macro_rules! add_derivative2_value {
($self: ident, $rhs: ident) => { 1.0 }
}
#[macro_export]
macro_rules! sub_output_value {
($self: ident, $rhs: ident) => { $self.value - $rhs.value }
}
#[macro_export]
macro_rules! sub_derivative1_value {
($self: ident, $rhs: ident) => { 1.0 }
}
#[macro_export]
macro_rules! sub_derivative2_value {
($self: ident, $rhs: ident) => { -1.0 }
}
#[macro_export]
macro_rules! mul_output_value {
() => { self.value * rhs.value }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Copy)]
pub struct adf {
pub (crate) value: f64,
pub (crate) tangent: f64
}
impl adf {
pub fn new(value: f64, tangent: f64) -> Self {
Self {
value,
tangent
}
}
pub fn new_constant(value: f64) -> Self {
Self {
value,
tangent: 0.0
}
}
#[inline(always)]
pub fn value(&self) -> f64 {
self.value
}
#[inline(always)]
pub fn tangent(&self) -> f64 {
self.tangent
}
}
/*
impl Add<Self> for adf {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let output_value = self.value + rhs.value;
let d_add_d_arg1 = 1.0;
let d_add_d_arg2 = 1.0;
let output_tangent = d_add_d_arg1*self.tangent + d_add_d_arg2*rhs.tangent;
adf {
value: output_value,
tangent: output_tangent
}
}
}
*/
impl AddAssign<Self> for adf {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
#[macro_export]
macro_rules! forward_ad_standard_two_input_trait_adf {
($trait_name: tt, $function_name: tt, $output_value_macro: tt, $derivative1: tt, $derivative2: tt) => {
impl $trait_name<Self> for adf {
type Output = Self;
fn $function_name(self, rhs: Self) -> Self::Output {
let output_value = $output_value_macro!(self, rhs);
let d1 = $derivative1!(self, rhs);
let d2 = $derivative2!(self, rhs);
let output_tangent = d1*self.tangent + d2*rhs.tangent;
adf {
value: output_value,
tangent: output_tangent
}
}
}
}
}
// forward_ad_standard_two_input_trait_adf!(Add, add, add_output_value, add_derivative1_value, add_derivative2_value);
// forward_ad_standard_two_input_trait_adf!(Sub, sub, sub_output_value, sub_derivative1_value, sub_derivative2_value);
#[macro_export]
macro_rules! forward_ad_standard_two_input_trait_nongeneric_wrapper {
($t: tt, $inner_macro: tt, $trait_name: tt, $function_name: tt, $output_value_macro: tt, $derivative1: tt, $derivative2: tt) => {
impl $trait_name<Self> for $t {
$inner_macro!($trait_name, $function_name, $output_value_macro, $derivative1, $derivative2);
}
}
}
#[macro_export]
macro_rules! forward_ad_standard_two_input_trait_inner_macro {
($trait_name: tt, $function_name: tt, $output_value_macro: tt, $derivative1: tt, $derivative2: tt) => {
type Output = Self;
fn $function_name(self, rhs: Self) -> Self::Output {
let output_value = $output_value_macro!(self, rhs);
let d1 = $derivative1!(self, rhs);
let d2 = $derivative2!(self, rhs);
let output_tangent = d1*self.tangent + d2*rhs.tangent;
adf {
value: output_value,
tangent: output_tangent
}
}
}
}
forward_ad_standard_two_input_trait_nongeneric_wrapper!(adf, forward_ad_standard_two_input_trait_inner_macro, Add, add, add_output_value, add_derivative1_value, add_derivative2_value);
*/