| 1 |
// Emacs will be in -*- Mode: c++ -*- |
| 2 |
// @HEADER |
| 3 |
// ***************************************************************************** |
| 4 |
// Sacado Package |
| 5 |
// |
| 6 |
// Copyright 2006 NTESS and the Sacado contributors. |
| 7 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 8 |
// ***************************************************************************** |
| 9 |
// @HEADER |
| 10 |
|
| 11 |
// ********** DO NOT REMOVE THIS BANNER ********** |
| 12 |
// |
| 13 |
// SUMMARY: Tools for Automatic Differentiaton (order 1) |
| 14 |
// RELEASE: 0.1 |
| 15 |
// USAGE : You may copy freely these files and use it for |
| 16 |
// teaching or research. These or part of these may |
| 17 |
// not be sold or used for a commercial purpose with- |
| 18 |
// out our consent : fax (33)1 44 27 72 00 |
| 19 |
// |
| 20 |
// AUTHOR : Nicolas Di cesare |
| 21 |
// ORG : |
| 22 |
// E-MAIL : [email protected] |
| 23 |
// |
| 24 |
// ORIG-DATE: September 97 |
| 25 |
// LAST-MOD : 28/07/98 |
| 26 |
// ******************************************************** |
| 27 |
// FILE : tinyfadbin.h |
| 28 |
// ******************************************************** |
| 29 |
#ifndef _tinyfadbin_h_ |
| 30 |
#define _tinyfadbin_h_ |
| 31 |
|
| 32 |
|
| 33 |
template <int Num, class L, class R> inline |
| 34 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 35 |
operator +(const TinyFad<Num,L>& un, const TinyFad<Num,R>& deux) { |
| 36 |
|
| 37 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 38 |
|
| 39 |
No_Initialization nothing; |
| 40 |
TinyFad<Num,value_type> tmp( nothing ); |
| 41 |
|
| 42 |
for (int i=0; i<Num; ++i) |
| 43 |
tmp.dx(i) = un.dx(i) + deux.dx(i); |
| 44 |
|
| 45 |
tmp.val() = un.val() + deux.val(); |
| 46 |
|
| 47 |
return tmp; |
| 48 |
} |
| 49 |
|
| 50 |
template <int Num, class L, class R> inline |
| 51 |
TinyFad<Num,typename NumericalTraits<L,R>::promote> |
| 52 |
operator +(const TinyFad<Num,L>& un, const R& deux) { |
| 53 |
|
| 54 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 55 |
|
| 56 |
No_Initialization nothing; |
| 57 |
TinyFad<Num,value_type> tmp( nothing ); |
| 58 |
|
| 59 |
for (int i=0; i<Num; ++i) |
| 60 |
tmp.dx(i) = un.dx(i); |
| 61 |
|
| 62 |
tmp.val() = un.val() + deux; |
| 63 |
|
| 64 |
return tmp; |
| 65 |
} |
| 66 |
|
| 67 |
template <int Num, class L, class R> inline |
| 68 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 69 |
operator +(const L& un, const TinyFad<Num,R>& deux) { |
| 70 |
return operator +(deux,un); |
| 71 |
} |
| 72 |
|
| 73 |
template <int Num, class L, class R> inline |
| 74 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 75 |
operator *(const TinyFad<Num,L>& un, const TinyFad<Num,R>& deux) { |
| 76 |
|
| 77 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 78 |
|
| 79 |
No_Initialization nothing; |
| 80 |
TinyFad<Num,value_type> tmp( nothing ); |
| 81 |
|
| 82 |
for (int i=0; i<Num; ++i) |
| 83 |
tmp.dx(i) = un.dx(i)*deux.val() + un.val() * deux.dx(i); |
| 84 |
|
| 85 |
tmp.val() = un.val() * deux.val(); |
| 86 |
|
| 87 |
return tmp; |
| 88 |
} |
| 89 |
|
| 90 |
template <int Num, class L, class R> inline |
| 91 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 92 |
operator *(const TinyFad<Num,L>& un, const R& deux) { |
| 93 |
|
| 94 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 95 |
|
| 96 |
No_Initialization nothing; |
| 97 |
TinyFad<Num,value_type> tmp( nothing ); |
| 98 |
|
| 99 |
for (int i=0; i<Num; ++i) |
| 100 |
tmp.dx(i) = un.dx(i)*deux; |
| 101 |
|
| 102 |
tmp.val() = un.val() * deux; |
| 103 |
|
| 104 |
|
| 105 |
return tmp; |
| 106 |
} |
| 107 |
|
| 108 |
template <int Num, class L, class R> inline |
| 109 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 110 |
operator *(const L& un, const TinyFad<Num,R>& deux) { |
| 111 |
|
| 112 |
return operator *(deux,un); |
| 113 |
} |
| 114 |
|
| 115 |
//------------------------------------------------------- |
| 116 |
template <int Num, class L, class R> inline |
| 117 |
TinyFad<Num,typename NumericalTraits<L,R>::promote > |
| 118 |
operator -(const TinyFad<Num,L> & un, const TinyFad<Num,R> & deux) { |
| 119 |
|
| 120 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 121 |
|
| 122 |
No_Initialization nothing; |
| 123 |
TinyFad<Num,value_type> tmp( nothing ); |
| 124 |
|
| 125 |
for (int i=0; i<Num; ++i) |
| 126 |
tmp.dx(i) = un.dx(i) - deux.dx(i); |
| 127 |
|
| 128 |
tmp.val() = un.val() - deux.val(); |
| 129 |
|
| 130 |
return tmp; |
| 131 |
} |
| 132 |
|
| 133 |
template <int Num, class L, class R> inline |
| 134 |
TinyFad<Num,typename NumericalTraits<L,R>::promote> |
| 135 |
operator -(const L & un, const TinyFad<Num,R> & deux) { |
| 136 |
|
| 137 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 138 |
|
| 139 |
No_Initialization nothing; |
| 140 |
TinyFad<Num,value_type> tmp( nothing ); |
| 141 |
|
| 142 |
for (int i=0; i<Num; ++i) |
| 143 |
tmp.dx(i) -= deux.dx(i); |
| 144 |
|
| 145 |
tmp.val() = un - deux.val(); |
| 146 |
|
| 147 |
return tmp; |
| 148 |
} |
| 149 |
|
| 150 |
template <int Num, class L, class R> inline |
| 151 |
TinyFad<Num, typename NumericalTraits<L,R>::promote > |
| 152 |
operator -(const TinyFad<Num,L> & un, const R & deux) { |
| 153 |
|
| 154 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 155 |
|
| 156 |
No_Initialization nothing; |
| 157 |
TinyFad<Num,value_type> tmp( nothing ); |
| 158 |
|
| 159 |
for (int i=0; i<Num; ++i) |
| 160 |
tmp.dx(i) = un.dx(i); |
| 161 |
|
| 162 |
tmp.val() = un.val() - deux; |
| 163 |
|
| 164 |
return tmp; |
| 165 |
} |
| 166 |
|
| 167 |
template <int Num, class L, class R> inline |
| 168 |
TinyFad<Num, typename NumericalTraits<L,R>::promote > |
| 169 |
operator /(const TinyFad<Num,L> & un, const TinyFad<Num,R> & deux) { |
| 170 |
|
| 171 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 172 |
|
| 173 |
if (deux.val() == 0.) error("TinyFad & TinyFad::operator /(const TinyFad<Num,L> & un, const TinyFad<Num,R> & deux), dividing by 0"); |
| 174 |
|
| 175 |
No_Initialization nothing; |
| 176 |
TinyFad<Num,value_type> tmp( nothing ); |
| 177 |
value_type dval = deux.val(); |
| 178 |
|
| 179 |
for (int i=0; i<Num; ++i) |
| 180 |
tmp.dx(i) = ( un.dx(i)* deux.val() - un.val() * deux.dx(i) ) / dval / dval ; |
| 181 |
|
| 182 |
tmp.val() = un.val() / dval; |
| 183 |
|
| 184 |
return tmp; |
| 185 |
} |
| 186 |
|
| 187 |
template <int Num, class L, class R> inline |
| 188 |
TinyFad<Num, typename NumericalTraits<L,R>::promote > |
| 189 |
operator /(const L & un, const TinyFad<Num,R> & deux) { |
| 190 |
|
| 191 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 192 |
|
| 193 |
if (deux.val() == 0.) error("TinyFad & TinyFad::operator /(const L & un, const TinyFad<Num,R> & deux), dividing by 0"); |
| 194 |
|
| 195 |
No_Initialization nothing; |
| 196 |
TinyFad<Num,value_type> tmp( nothing ); |
| 197 |
value_type dval = deux.val(); |
| 198 |
|
| 199 |
for (int i=0; i<Num; ++i) |
| 200 |
tmp.dx(i) = - un * deux.dx(i) / dval / dval ; |
| 201 |
|
| 202 |
tmp.val() = un / dval; |
| 203 |
|
| 204 |
return tmp; |
| 205 |
} |
| 206 |
|
| 207 |
template <int Num, class L, class R> inline |
| 208 |
TinyFad<Num, typename NumericalTraits<L,R>::promote > |
| 209 |
operator /(const TinyFad<Num,L> & un, const R & deux) { |
| 210 |
|
| 211 |
typedef typename NumericalTraits<L,R>::promote value_type; |
| 212 |
|
| 213 |
if (deux == 0.) error("TinyFad & TinyFad::operator /(const TinyFad<Num,L> & un, const R & deux), dividing by 0"); |
| 214 |
|
| 215 |
No_Initialization nothing; |
| 216 |
TinyFad<Num,value_type> tmp( nothing ); |
| 217 |
|
| 218 |
for (int i=0; i<Num; ++i) |
| 219 |
tmp.dx(i) = un.dx(i) / deux; |
| 220 |
|
| 221 |
tmp.val() = un.val() / deux; |
| 222 |
|
| 223 |
return tmp; |
| 224 |
} |
| 225 |
|
| 226 |
#endif |