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