Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
adexpr.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2008-02-14
7 
8  Copyright (C) 2008 Université Joseph Fourier (Grenoble I)
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 3.0 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #ifndef __ADExpr_H
30 #define __ADExpr_H 1
31 
32 #include <boost/type_traits/is_fundamental.hpp>
33 
34 namespace Feel
35 {
43 template< typename Expr>
44 class ADExpr
45 {
46 public:
47 
48 
52  enum { nvar = Expr::nvar };
53 
54  typedef Expr expression_type;
55  typedef typename expression_type::value_type value_type;
56 
57  typedef ADExpr<Expr> This;
58 
60 
64 
65  explicit ADExpr( expression_type const & expr )
66  :
67  __expression( expr )
68  {
69 
70  }
71  ~ADExpr() {}
72 
74 
78 
79 
81 
85 
86  value_type value() const
87  {
88  return __expression.value();
89  }
90 
91  value_type grad( int __ith ) const
92  {
93  return __expression.grad( __ith );
94  }
95 
96  value_type hessian( int __i, int __j ) const
97  {
98  return __expression.hessian( __i, __j );
99  }
100 
101  bool deps( int i ) const
102  {
103  return __expression.deps( i );
104  }
105 
106  operator bool() const
107  {
108  return __expression.value() != value_type( 0 );
109  }
110 
112 
116 
117 
119 
123 
124 
126 
127 
128 
129 protected:
130 
131  ADExpr()
132  {
133  ;
134  }
135 
136  expression_type __expression;
137 
138 private:
139 
140 };
141 
142 //------------------------------- AD constant ------------------------------------------
143 template < class T >
144 class ADCst
145 {
146 public:
147 
148  typedef T value_type;
149 
150 protected:
151  ADCst() {}
152 
153  const T constant_;
154 
155 public:
156  explicit ADCst( const T& value ) : constant_( value )
157  {
158  ;
159  }
160 
161  value_type value() const
162  {
163  return constant_;
164  }
165  value_type grad( int __i ) const
166  {
167  return 0;
168  }
169  value_type hessian( int __i, int __j ) const
170  {
171  return 0;
172  }
173 
174  bool deps( int ) const
175  {
176  return false;
177  }
178 
179 };
180 
181 //------------------------------- AD unary + ------------------------------------------
182 template < class T >
183 class ADUnaryPlus
184 {
185 public:
186 
187  enum { nvar = T::nvar };
188  typedef typename T::value_type value_type;
189 
190 protected:
191  ADUnaryPlus() {}
192 
193  const T& expr_;
194 
195 public:
196  ADUnaryPlus( const T& value ) : expr_( value )
197  {
198  ;
199  }
200 
201  value_type value() const
202  {
203  return expr_.value();
204  }
205  value_type grad( int __i ) const
206  {
207  return expr_.grad( __i );
208  }
209  value_type hessian( int __i, int __j ) const
210  {
211  return expr_.hessian( __i, __j );
212  }
213 
214  bool deps( int i ) const
215  {
216  return expr_.deps( i );
217  }
218 };
219 template <class T> inline
220 ADExpr< ADUnaryPlus< ADExpr<T> > >
221 operator + ( const ADExpr<T>& expr )
222 {
223  typedef ADUnaryPlus< ADExpr<T> > expr_t;
224 
225  return ADExpr< expr_t >( expr_t( expr ) );
226 }
227 
228 
229 //------------------------------- AD unary - ------------------------------------------
230 template < class T >
231 class ADUnaryMinus
232 {
233 public:
234  enum { nvar = T::nvar };
235  typedef typename T::value_type value_type;
236 protected:
237  ADUnaryMinus() {}
238 
239  const T& expr_;
240 
241 public:
242  ADUnaryMinus( const T& value ) : expr_( value )
243  {
244  ;
245  }
246 
247  value_type value() const
248  {
249  return - expr_.value();
250  }
251  value_type grad( int __i ) const
252  {
253  return - expr_.grad( __i );
254  }
255  value_type hessian( int __i, int __j ) const
256  {
257  return - expr_.hessian( __i, __j );
258  }
259 
260  bool deps( int i ) const
261  {
262  return expr_.deps( i );
263  }
264 };
265 
266 
267 
268 template <class T> inline
269 ADExpr< ADUnaryMinus< ADExpr<T> > >
270 operator - ( const ADExpr<T>& expr )
271 {
272  typedef ADUnaryMinus< ADExpr<T> > expr_t;
273 
274  return ADExpr< expr_t >( expr_t( expr ) );
275 }
276 
277 }
278 
279 
280 
281 #endif /* __ADExpr_H */
282 

Generated on Sun Oct 20 2013 08:24:54 for Feel++ by doxygen 1.8.4