Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_LFad_LogicalSparse.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // 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 St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_LFAD_LOGICALSPARSE_HPP
31 #define SACADO_LFAD_LOGICALSPARSE_HPP
32 
36 
37 namespace Sacado {
38 
40  namespace LFad {
41 
43 
47  template <typename ExprT> class Expr {};
48 
50 
55  template <typename T>
56  struct ExprLevel {
57  static const unsigned value = 0;
58  };
59 
60  template <typename T>
61  struct ExprLevel< Expr<T> > {
62  static const unsigned value =
64  };
65 
67  template <typename T>
68  struct IsFadExpr {
69  static const bool value = false;
70  };
71 
72  template <typename T>
73  struct IsFadExpr< Expr<T> > {
74  static const bool value = true;
75  };
76 
77  // Forward declaration
78  template <typename ValT, typename LogT> class LogicalSparse;
79 
84  template <typename ValT, typename LogT>
86  public Fad::DynamicStorage<ValT,LogT> {
87 
89 
90  public:
91 
93  typedef ValT value_type;
94 
97 
99  typedef LogT logical_type;
100 
105 
108 
110 
113  template <typename S>
115  Storage(x) {}
116 
118 
121  LogicalSparseImp(const int sz, const value_type & x) : Storage(sz, x) {}
122 
124 
129  LogicalSparseImp(const int sz, const int i, const value_type & x) :
130  Storage(sz, x) {
131  this->fastAccessDx(i)=logical_type(1);
132  }
133 
136  Storage(x) {}
137 
139  template <typename S>
141  Storage(value_type(0)) {
142  int sz = x.size();
143 
144  if (sz != this->size())
145  this->resize(sz);
146 
147  if (sz) {
148  if (x.hasFastAccess())
149  for(int i=0; i<sz; ++i)
150  this->fastAccessDx(i) = x.fastAccessDx(i);
151  else
152  for(int i=0; i<sz; ++i)
153  this->fastAccessDx(i) = x.dx(i);
154  }
155 
156  this->val() = x.val();
157  }
158 
161 
163 
169  void diff(const int ith, const int n) {
170  if (this->size() != n)
171  this->resize(n);
172 
173  this->zero();
174  this->fastAccessDx(ith) = logical_type(1);
175  }
176 
178  template <typename S>
179  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
180  typedef IsEqual<value_type> IE;
181  if (x.size() != this->size()) return false;
182  bool eq = IE::eval(x.val(), this->val());
183  for (int i=0; i<this->size(); i++)
184  eq = eq && IE::eval(x.dx(i), this->dx(i));
185  return eq;
186  }
187 
189 
194 
196  bool hasFastAccess() const { return this->size()!=0;}
197 
199  bool isPassive() const { return this->size()!=0;}
200 
202  void setIsConstant(bool is_const) {
203  if (is_const && this->size()!=0)
204  this->resize(0);
205  }
206 
208 
213 
215  template <typename S>
217  this->val() = v;
218  if (this->size()) {
219  this->zero();
220  this->resize(0);
221  }
222  return *this;
223  }
224 
227  // Copy value & dx_
229 
230  return *this;
231  }
232 
234  template <typename S>
236  int sz = x.size();
237 
238  if (sz != this->size())
239  this->resize(sz);
240 
241  if (sz) {
242  if (x.hasFastAccess())
243  for(int i=0; i<sz; ++i)
244  this->fastAccessDx(i) = x.fastAccessDx(i);
245  else
246  for(int i=0; i<sz; ++i)
247  this->fastAccessDx(i) = x.dx(i);
248  }
249 
250  this->val() = x.val();
251 
252  return *this;
253  }
254 
256 
261 
263  template <typename S>
264  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator += (const S& v) {
265  this->val() += v;
266  return *this;
267  }
268 
270  template <typename S>
271  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator -= (const S& v) {
272  this->val() -= v;
273  return *this;
274  }
275 
277  template <typename S>
278  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator *= (const S& v) {
279  this->val() *= v;
280  return *this;
281  }
282 
284  template <typename S>
285  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator /= (const S& v) {
286  this->val() /= v;
287  return *this;
288  }
289 
291  LogicalSparseImp& operator += (const LogicalSparseImp& x) {
292  int xsz = x.size(), sz = this->size();
293 
294 #ifdef SACADO_DEBUG
295  if ((xsz != sz) && (xsz != 0) && (sz != 0))
296  throw "LFad Error: Attempt to assign with incompatible sizes";
297 #endif
298 
299  if (xsz) {
300  if (sz) {
301  for (int i=0; i<xsz; ++i)
302  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
303  }
304  else {
305  this->resize(xsz);
306  for (int i=0; i<xsz; ++i)
307  this->fastAccessDx(i) = x.fastAccessDx(i);
308  }
309  }
310 
311  this->val() += x.val();
312 
313  return *this;
314  }
315 
317  LogicalSparseImp& operator -= (const LogicalSparseImp& x) {
318  int xsz = x.size(), sz = this->size();
319 
320 #ifdef SACADO_DEBUG
321  if ((xsz != sz) && (xsz != 0) && (sz != 0))
322  throw "LFad Error: Attempt to assign with incompatible sizes";
323 #endif
324 
325  if (xsz) {
326  if (sz) {
327  for (int i=0; i<xsz; ++i)
328  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
329  }
330  else {
331  this->resize(xsz);
332  for (int i=0; i<xsz; ++i)
333  this->fastAccessDx(i) = x.fastAccessDx(i);
334  }
335  }
336 
337  this->val() -= x.val();
338 
339 
340  return *this;
341  }
342 
344  LogicalSparseImp& operator *= (const LogicalSparseImp& x) {
345  int xsz = x.size(), sz = this->size();
346 
347 #ifdef SACADO_DEBUG
348  if ((xsz != sz) && (xsz != 0) && (sz != 0))
349  throw "LFad Error: Attempt to assign with incompatible sizes";
350 #endif
351 
352  if (xsz) {
353  if (sz) {
354  for (int i=0; i<xsz; ++i)
355  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
356  }
357  else {
358  this->resize(xsz);
359  for (int i=0; i<xsz; ++i)
360  this->fastAccessDx(i) = x.fastAccessDx(i);
361  }
362  }
363 
364  this->val() *= x.val();
365 
366  return *this;
367  }
368 
370  LogicalSparseImp& operator /= (const LogicalSparseImp& x) {
371  int xsz = x.size(), sz = this->size();
372 
373 #ifdef SACADO_DEBUG
374  if ((xsz != sz) && (xsz != 0) && (sz != 0))
375  throw "LFad Error: Attempt to assign with incompatible sizes";
376 #endif
377 
378  if (xsz) {
379  if (sz) {
380  for (int i=0; i<xsz; ++i)
381  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
382  }
383  else {
384  this->resize(xsz);
385  for (int i=0; i<xsz; ++i)
386  this->fastAccessDx(i) = x.fastAccessDx(i);
387  }
388  }
389 
390  this->val() /= x.val();
391 
392  return *this;
393  }
394 
396  template <typename S>
397  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator += (const Expr<S>& x){
398  int xsz = x.size(), sz = this->size();
399 
400 #ifdef SACADO_DEBUG
401  if ((xsz != sz) && (xsz != 0) && (sz != 0))
402  throw "LFad Error: Attempt to assign with incompatible sizes";
403 #endif
404 
405  if (xsz) {
406  if (sz) {
407  if (x.hasFastAccess())
408  for (int i=0; i<xsz; ++i)
409  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
410  else
411  for (int i=0; i<xsz; ++i)
412  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
413  }
414  else {
415  this->resize(xsz);
416  if (x.hasFastAccess())
417  for (int i=0; i<xsz; ++i)
418  this->fastAccessDx(i) = x.fastAccessDx(i);
419  else
420  for (int i=0; i<xsz; ++i)
421  this->fastAccessDx(i) = x.dx(i);
422  }
423  }
424 
425  this->val() += x.val();
426 
427  return *this;
428  }
429 
431  template <typename S>
432  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator -= (const Expr<S>& x){
433  int xsz = x.size(), sz = this->size();
434 
435 #ifdef SACADO_DEBUG
436  if ((xsz != sz) && (xsz != 0) && (sz != 0))
437  throw "LFad Error: Attempt to assign with incompatible sizes";
438 #endif
439 
440  if (xsz) {
441  if (sz) {
442  if (x.hasFastAccess())
443  for (int i=0; i<xsz; ++i)
444  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
445  else
446  for (int i=0; i<xsz; ++i)
447  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
448  }
449  else {
450  this->resize(xsz);
451  if (x.hasFastAccess())
452  for (int i=0; i<xsz; ++i)
453  this->fastAccessDx(i) = x.fastAccessDx(i);
454  else
455  for (int i=0; i<xsz; ++i)
456  this->fastAccessDx(i) = x.dx(i);
457  }
458  }
459 
460  this->val() -= x.val();
461 
462 
463  return *this;
464  }
465 
467  template <typename S>
468  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator *= (const Expr<S>& x){
469  int xsz = x.size(), sz = this->size();
470 
471 #ifdef SACADO_DEBUG
472  if ((xsz != sz) && (xsz != 0) && (sz != 0))
473  throw "LFad Error: Attempt to assign with incompatible sizes";
474 #endif
475 
476  if (xsz) {
477  if (sz) {
478  if (x.hasFastAccess())
479  for (int i=0; i<xsz; ++i)
480  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
481  else
482  for (int i=0; i<xsz; ++i)
483  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
484  }
485  else {
486  this->resize(xsz);
487  if (x.hasFastAccess())
488  for (int i=0; i<xsz; ++i)
489  this->fastAccessDx(i) = x.fastAccessDx(i);
490  else
491  for (int i=0; i<xsz; ++i)
492  this->fastAccessDx(i) = x.dx(i);
493  }
494  }
495 
496  this->val() *= x.val();
497 
498  return *this;
499  }
500 
502  template <typename S>
503  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator /= (const Expr<S>& x){
504  int xsz = x.size(), sz = this->size();
505 
506 #ifdef SACADO_DEBUG
507  if ((xsz != sz) && (xsz != 0) && (sz != 0))
508  throw "LFad Error: Attempt to assign with incompatible sizes";
509 #endif
510 
511  if (xsz) {
512  if (sz) {
513  if (x.hasFastAccess())
514  for (int i=0; i<xsz; ++i)
515  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
516  else
517  for (int i=0; i<xsz; ++i)
518  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
519  }
520  else {
521  this->resize(xsz);
522  if (x.hasFastAccess())
523  for (int i=0; i<xsz; ++i)
524  this->fastAccessDx(i) = x.fastAccessDx(i);
525  else
526  for (int i=0; i<xsz; ++i)
527  this->fastAccessDx(i) = x.dx(i);
528  }
529  }
530 
531  this->val() /= x.val();
532 
533  return *this;
534  }
535 
537 
538  }; // class LogicalSparseImp
539 
541  template <typename ValT, typename LogT>
542  class Expr< LogicalSparseImp<ValT,LogT> > :
543  public LogicalSparseImp<ValT,LogT> {
544 
545  public:
546 
549 
552 
555 
557  Expr() :
558  LogicalSparseImp<ValT,LogT>() {}
559 
561 
564  template <typename S>
566  LogicalSparseImp<ValT,LogT>(x) {}
567 
569 
572  Expr(const int sz, const ValT & x) :
573  LogicalSparseImp<ValT,LogT>(sz,x) {}
574 
576 
581  Expr(const int sz, const int i, const ValT & x) :
582  LogicalSparseImp<ValT,LogT>(sz,i,x) {}
583 
585  Expr(const Expr& x) :
586  LogicalSparseImp<ValT,LogT>(x) {}
587 
589  template <typename S>
591  LogicalSparseImp<ValT,LogT>(x) {}
592 
594  ~Expr() {}
595 
596  }; // class Expr<LogicalSparseImp>
597 
602  template <typename ValT, typename LogT >
603  class LogicalSparse : public Expr< LogicalSparseImp<ValT,LogT > > {
604 
605  public:
606 
610 
612  typedef typename ExprType::value_type value_type;
613 
615  typedef typename ExprType::scalar_type scalar_type;
616 
618  template <typename T, typename U = LogT>
619  struct apply {
621  };
622 
627 
629 
633  ExprType() {}
634 
636 
639  template <typename S>
641  ExprType(x) {}
642 
644 
647  LogicalSparse(const int sz, const ValT& x) :
648  ExprType(sz,x) {}
649 
651 
656  LogicalSparse(const int sz, const int i, const ValT & x) :
657  ExprType(sz,i,x) {}
658 
661  ExprType(x) {}
662 
664  template <typename S>
666  ExprType(x) {}
667 
669 
672 
674  template <typename S>
677  return *this;
678  }
679 
682  ImplType::operator=(static_cast<const ImplType&>(x));
683  return *this;
684  }
685 
687  template <typename S>
689  {
691  return *this;
692  }
693 
695  template <typename S>
696  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator += (const S& x) {
697  ImplType::operator+=(x);
698  return *this;
699  }
700 
702  template <typename S>
703  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator -= (const S& x) {
704  ImplType::operator-=(x);
705  return *this;
706  }
707 
709  template <typename S>
710  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator *= (const S& x) {
711  ImplType::operator*=(x);
712  return *this;
713  }
714 
716  template <typename S>
717  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator /= (const S& x) {
718  ImplType::operator/=(x);
719  return *this;
720  }
721 
723  LogicalSparse& operator += (const LogicalSparse& x) {
724  ImplType::operator+=(static_cast<const ImplType&>(x));
725  return *this;
726  }
727 
729  LogicalSparse& operator -= (const LogicalSparse& x) {
730  ImplType::operator-=(static_cast<const ImplType&>(x));
731  return *this;
732  }
733 
735  LogicalSparse& operator *= (const LogicalSparse& x) {
736  ImplType::operator*=(static_cast<const ImplType&>(x));
737  return *this;
738  }
739 
741  LogicalSparse& operator /= (const LogicalSparse& x) {
742  ImplType::operator/=(static_cast<const ImplType&>(x));
743  return *this;
744  }
745 
747  template <typename S>
748  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator += (const Expr<S>& x) {
749  ImplType::operator+=(x);
750  return *this;
751  }
752 
754  template <typename S>
755  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator -= (const Expr<S>& x) {
756  ImplType::operator-=(x);
757  return *this;
758  }
759 
761  template <typename S>
762  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator *= (const Expr<S>& x) {
763  ImplType::operator*=(x);
764  return *this;
765  }
766 
768  template <typename S>
769  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator /= (const Expr<S>& x) {
770  ImplType::operator/=(x);
771  return *this;
772  }
773 
774  }; // class LogicalSparse<ValT,LogT>
775 
776  template <typename T, typename L>
777  struct ExprLevel< LogicalSparse<T,L> > {
778  static const unsigned value =
780  };
781 
782  } // namespace LFad
783 
784  template <typename T>
785  struct IsExpr< LFad::Expr<T> > {
786  static const bool value = true;
787  };
788 
789  template <typename T>
790  struct BaseExprType< LFad::Expr<T> > {
792  };
793 
794  template <typename T, typename L>
795  struct IsExpr< LFad::LogicalSparse<T,L> > {
796  static const bool value = true;
797  };
798 
799  template <typename T, typename L>
800  struct BaseExprType< LFad::LogicalSparse<T,L> > {
802  };
803 
804 } // namespace Sacado
805 
807 
808 #endif // SACADO_LFAD_LOGICALSPARSE_HPP
LogicalSparseImp< ValT, LogT > ImplType
Base classes.
Implementation class for computing the logical sparsity of a derivative using forward-mode AD...
LogicalSparse(const int sz, const int i, const ValT &x)
Constructor with size sz, index i, and value x.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
Determine whether a given type is an expression.
Derivative array storage class using dynamic memory allocation.
#define SACADO_ENABLE_VALUE_CTOR_DECL
LogicalSparseImp(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
LogicalSparse(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x of type ValueT.
bool isPassive() const
Returns true if derivative array is empty.
ValT value_type
Typename of values (e.g., double)
LogicalSparse(const int sz, const ValT &x)
Constructor with size sz and value x.
static const bool value
#define SACADO_ENABLE_EXPR_CTOR_DECL
Fad::DynamicStorage< ValT, LogT > Storage
KOKKOS_INLINE_FUNCTION const ValT & val() const
Returns value.
LogicalSparse(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
Is a type an expression.
void diff(const int ith, const int n)
Set LogicalSparseImp object as the ith independent variable.
LogicalSparse< ValT, LogT > base_expr_type
Typename of base-expressions.
LogicalSparseImp(const int sz, const value_type &x)
Constructor with size sz and value x.
#define T
Definition: Sacado_rad.hpp:573
Expr(const int sz, const ValT &x)
Constructor with size sz and value x.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
KOKKOS_INLINE_FUNCTION const LogT * dx() const
Returns derivative array.
Base template specification for testing equivalence.
LFad::LogicalSparse< T, L >::base_expr_type type
Get the base Fad type from a view/expression.
LogicalSparseImp(const LogicalSparseImp &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION void zero()
Zero out derivative array.
LogT logical_type
Logical type (i.e., type for derivative array components (e.g., bool)
Wrapper for a generic expression template.
LogicalSparseImp< ValT, LogT >::value_type value_type
Typename of values.
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from ValT)
User inteface class for computing the logical sparsity pattern of a derivative via forward-mode AD...
Meta-function for determining nesting with an expression.
SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
Expr(const int sz, const int i, const ValT &x)
Constructor with size sz, index i, and value x.
Turn LogicalSparse into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
ExprType::value_type value_type
Typename of values.
LogicalSparseImp< ValT, LogT >::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
LogicalSparse(const LogicalSparse &x)
Copy constructor.
ExprType::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
bool hasFastAccess() const
Returns true if derivative array is not empty.
Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
KOKKOS_INLINE_FUNCTION LogT & fastAccessDx(int i)
Returns derivative component i without bounds checking.
LogicalSparseImp(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
LogicalSparseImp(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
void setIsConstant(bool is_const)
Set whether variable is constant.