Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_LFad_LogicalSparse.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_LFAD_LOGICALSPARSE_HPP
11 #define SACADO_LFAD_LOGICALSPARSE_HPP
12 
16 
17 namespace Sacado {
18 
20  namespace LFad {
21 
23 
27  template <typename ExprT> class Expr {};
28 
30 
35  template <typename T>
36  struct ExprLevel {
37  static const unsigned value = 0;
38  };
39 
40  template <typename T>
41  struct ExprLevel< Expr<T> > {
42  static const unsigned value =
44  };
45 
47  template <typename T>
48  struct IsFadExpr {
49  static const bool value = false;
50  };
51 
52  template <typename T>
53  struct IsFadExpr< Expr<T> > {
54  static const bool value = true;
55  };
56 
57  // Forward declaration
58  template <typename ValT, typename LogT> class LogicalSparse;
59 
64  template <typename ValT, typename LogT>
66  public Fad::DynamicStorage<ValT,LogT> {
67 
69 
70  public:
71 
73  typedef ValT value_type;
74 
77 
79  typedef LogT logical_type;
80 
85 
88 
90 
93  template <typename S>
95  Storage(x) {}
96 
98 
101  LogicalSparseImp(const int sz, const value_type & x) :
102  Storage(sz, x, InitDerivArray) {}
103 
105 
110  LogicalSparseImp(const int sz, const int i, const value_type & x) :
111  Storage(sz, x, InitDerivArray) {
112  this->fastAccessDx(i)=logical_type(1);
113  }
114 
117  Storage(x) {}
118 
120  template <typename S>
122  Storage(value_type(0)) {
123  int sz = x.size();
124 
125  if (sz != this->size())
126  this->resize(sz);
127 
128  if (sz) {
129  if (x.hasFastAccess())
130  for(int i=0; i<sz; ++i)
131  this->fastAccessDx(i) = x.fastAccessDx(i);
132  else
133  for(int i=0; i<sz; ++i)
134  this->fastAccessDx(i) = x.dx(i);
135  }
136 
137  this->val() = x.val();
138  }
139 
142 
144 
150  void diff(const int ith, const int n) {
151  if (this->size() != n)
152  this->resize(n);
153 
154  this->zero();
155  this->fastAccessDx(ith) = logical_type(1);
156  }
157 
159  template <typename S>
160  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
161  typedef IsEqual<value_type> IE;
162  if (x.size() != this->size()) return false;
163  bool eq = IE::eval(x.val(), this->val());
164  for (int i=0; i<this->size(); i++)
165  eq = eq && IE::eval(x.dx(i), this->dx(i));
166  return eq;
167  }
168 
170 
175 
177  bool hasFastAccess() const { return this->size()!=0;}
178 
180  bool isPassive() const { return this->size()!=0;}
181 
183  void setIsConstant(bool is_const) {
184  if (is_const && this->size()!=0)
185  this->resize(0);
186  }
187 
189 
194 
196  template <typename S>
197  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator=(const S& v) {
198  this->val() = v;
199  if (this->size()) {
200  this->zero();
201  this->resize(0);
202  }
203  return *this;
204  }
205 
208  // Copy value & dx_
210 
211  return *this;
212  }
213 
215  template <typename S>
216  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator=(const Expr<S>& x) {
217  int sz = x.size();
218 
219  if (sz != this->size())
220  this->resize(sz);
221 
222  if (sz) {
223  if (x.hasFastAccess())
224  for(int i=0; i<sz; ++i)
225  this->fastAccessDx(i) = x.fastAccessDx(i);
226  else
227  for(int i=0; i<sz; ++i)
228  this->fastAccessDx(i) = x.dx(i);
229  }
230 
231  this->val() = x.val();
232 
233  return *this;
234  }
235 
237 
242 
244  template <typename S>
245  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator += (const S& v) {
246  this->val() += v;
247  return *this;
248  }
249 
251  template <typename S>
252  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator -= (const S& v) {
253  this->val() -= v;
254  return *this;
255  }
256 
258  template <typename S>
259  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator *= (const S& v) {
260  this->val() *= v;
261  return *this;
262  }
263 
265  template <typename S>
266  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator /= (const S& v) {
267  this->val() /= v;
268  return *this;
269  }
270 
272  LogicalSparseImp& operator += (const LogicalSparseImp& x) {
273  int xsz = x.size(), sz = this->size();
274 
275 #ifdef SACADO_DEBUG
276  if ((xsz != sz) && (xsz != 0) && (sz != 0))
277  throw "LFad Error: Attempt to assign with incompatible sizes";
278 #endif
279 
280  if (xsz) {
281  if (sz) {
282  for (int i=0; i<xsz; ++i)
283  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
284  }
285  else {
286  this->resize(xsz);
287  for (int i=0; i<xsz; ++i)
288  this->fastAccessDx(i) = x.fastAccessDx(i);
289  }
290  }
291 
292  this->val() += x.val();
293 
294  return *this;
295  }
296 
298  LogicalSparseImp& operator -= (const LogicalSparseImp& x) {
299  int xsz = x.size(), sz = this->size();
300 
301 #ifdef SACADO_DEBUG
302  if ((xsz != sz) && (xsz != 0) && (sz != 0))
303  throw "LFad Error: Attempt to assign with incompatible sizes";
304 #endif
305 
306  if (xsz) {
307  if (sz) {
308  for (int i=0; i<xsz; ++i)
309  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
310  }
311  else {
312  this->resize(xsz);
313  for (int i=0; i<xsz; ++i)
314  this->fastAccessDx(i) = x.fastAccessDx(i);
315  }
316  }
317 
318  this->val() -= x.val();
319 
320 
321  return *this;
322  }
323 
325  LogicalSparseImp& operator *= (const LogicalSparseImp& x) {
326  int xsz = x.size(), sz = this->size();
327 
328 #ifdef SACADO_DEBUG
329  if ((xsz != sz) && (xsz != 0) && (sz != 0))
330  throw "LFad Error: Attempt to assign with incompatible sizes";
331 #endif
332 
333  if (xsz) {
334  if (sz) {
335  for (int i=0; i<xsz; ++i)
336  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
337  }
338  else {
339  this->resize(xsz);
340  for (int i=0; i<xsz; ++i)
341  this->fastAccessDx(i) = x.fastAccessDx(i);
342  }
343  }
344 
345  this->val() *= x.val();
346 
347  return *this;
348  }
349 
351  LogicalSparseImp& operator /= (const LogicalSparseImp& x) {
352  int xsz = x.size(), sz = this->size();
353 
354 #ifdef SACADO_DEBUG
355  if ((xsz != sz) && (xsz != 0) && (sz != 0))
356  throw "LFad Error: Attempt to assign with incompatible sizes";
357 #endif
358 
359  if (xsz) {
360  if (sz) {
361  for (int i=0; i<xsz; ++i)
362  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
363  }
364  else {
365  this->resize(xsz);
366  for (int i=0; i<xsz; ++i)
367  this->fastAccessDx(i) = x.fastAccessDx(i);
368  }
369  }
370 
371  this->val() /= x.val();
372 
373  return *this;
374  }
375 
377  template <typename S>
378  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator += (const Expr<S>& x){
379  int xsz = x.size(), sz = this->size();
380 
381 #ifdef SACADO_DEBUG
382  if ((xsz != sz) && (xsz != 0) && (sz != 0))
383  throw "LFad Error: Attempt to assign with incompatible sizes";
384 #endif
385 
386  if (xsz) {
387  if (sz) {
388  if (x.hasFastAccess())
389  for (int i=0; i<xsz; ++i)
390  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
391  else
392  for (int i=0; i<xsz; ++i)
393  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
394  }
395  else {
396  this->resize(xsz);
397  if (x.hasFastAccess())
398  for (int i=0; i<xsz; ++i)
399  this->fastAccessDx(i) = x.fastAccessDx(i);
400  else
401  for (int i=0; i<xsz; ++i)
402  this->fastAccessDx(i) = x.dx(i);
403  }
404  }
405 
406  this->val() += x.val();
407 
408  return *this;
409  }
410 
412  template <typename S>
413  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator -= (const Expr<S>& x){
414  int xsz = x.size(), sz = this->size();
415 
416 #ifdef SACADO_DEBUG
417  if ((xsz != sz) && (xsz != 0) && (sz != 0))
418  throw "LFad Error: Attempt to assign with incompatible sizes";
419 #endif
420 
421  if (xsz) {
422  if (sz) {
423  if (x.hasFastAccess())
424  for (int i=0; i<xsz; ++i)
425  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
426  else
427  for (int i=0; i<xsz; ++i)
428  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
429  }
430  else {
431  this->resize(xsz);
432  if (x.hasFastAccess())
433  for (int i=0; i<xsz; ++i)
434  this->fastAccessDx(i) = x.fastAccessDx(i);
435  else
436  for (int i=0; i<xsz; ++i)
437  this->fastAccessDx(i) = x.dx(i);
438  }
439  }
440 
441  this->val() -= x.val();
442 
443 
444  return *this;
445  }
446 
448  template <typename S>
449  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator *= (const Expr<S>& x){
450  int xsz = x.size(), sz = this->size();
451 
452 #ifdef SACADO_DEBUG
453  if ((xsz != sz) && (xsz != 0) && (sz != 0))
454  throw "LFad Error: Attempt to assign with incompatible sizes";
455 #endif
456 
457  if (xsz) {
458  if (sz) {
459  if (x.hasFastAccess())
460  for (int i=0; i<xsz; ++i)
461  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
462  else
463  for (int i=0; i<xsz; ++i)
464  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
465  }
466  else {
467  this->resize(xsz);
468  if (x.hasFastAccess())
469  for (int i=0; i<xsz; ++i)
470  this->fastAccessDx(i) = x.fastAccessDx(i);
471  else
472  for (int i=0; i<xsz; ++i)
473  this->fastAccessDx(i) = x.dx(i);
474  }
475  }
476 
477  this->val() *= x.val();
478 
479  return *this;
480  }
481 
483  template <typename S>
484  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator /= (const Expr<S>& x){
485  int xsz = x.size(), sz = this->size();
486 
487 #ifdef SACADO_DEBUG
488  if ((xsz != sz) && (xsz != 0) && (sz != 0))
489  throw "LFad Error: Attempt to assign with incompatible sizes";
490 #endif
491 
492  if (xsz) {
493  if (sz) {
494  if (x.hasFastAccess())
495  for (int i=0; i<xsz; ++i)
496  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
497  else
498  for (int i=0; i<xsz; ++i)
499  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
500  }
501  else {
502  this->resize(xsz);
503  if (x.hasFastAccess())
504  for (int i=0; i<xsz; ++i)
505  this->fastAccessDx(i) = x.fastAccessDx(i);
506  else
507  for (int i=0; i<xsz; ++i)
508  this->fastAccessDx(i) = x.dx(i);
509  }
510  }
511 
512  this->val() /= x.val();
513 
514  return *this;
515  }
516 
518 
519  }; // class LogicalSparseImp
520 
522  template <typename ValT, typename LogT>
523  class Expr< LogicalSparseImp<ValT,LogT> > :
524  public LogicalSparseImp<ValT,LogT> {
525 
526  public:
527 
530 
533 
536 
538  Expr() :
539  LogicalSparseImp<ValT,LogT>() {}
540 
542 
545  template <typename S>
547  LogicalSparseImp<ValT,LogT>(x) {}
548 
550 
553  Expr(const int sz, const ValT & x) :
554  LogicalSparseImp<ValT,LogT>(sz,x) {}
555 
557 
562  Expr(const int sz, const int i, const ValT & x) :
563  LogicalSparseImp<ValT,LogT>(sz,i,x) {}
564 
566  Expr(const Expr& x) :
567  LogicalSparseImp<ValT,LogT>(x) {}
568 
570  template <typename S>
572  LogicalSparseImp<ValT,LogT>(x) {}
573 
575  ~Expr() {}
576 
577  }; // class Expr<LogicalSparseImp>
578 
583  template <typename ValT, typename LogT >
584  class LogicalSparse : public Expr< LogicalSparseImp<ValT,LogT > > {
585 
586  public:
587 
591 
593  typedef typename ExprType::value_type value_type;
594 
596  typedef typename ExprType::scalar_type scalar_type;
597 
599  template <typename T, typename U = LogT>
600  struct apply {
602  };
603 
608 
610 
614  ExprType() {}
615 
617 
620  template <typename S>
622  ExprType(x) {}
623 
625 
628  LogicalSparse(const int sz, const ValT& x) :
629  ExprType(sz,x) {}
630 
632 
637  LogicalSparse(const int sz, const int i, const ValT & x) :
638  ExprType(sz,i,x) {}
639 
642  ExprType(x) {}
643 
645  template <typename S>
647  ExprType(x) {}
648 
650 
653 
655  template <typename S>
656  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator=(const S& v) {
658  return *this;
659  }
660 
663  ImplType::operator=(static_cast<const ImplType&>(x));
664  return *this;
665  }
666 
668  template <typename S>
669  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator=(const Expr<S>& x)
670  {
672  return *this;
673  }
674 
676  template <typename S>
677  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator += (const S& x) {
678  ImplType::operator+=(x);
679  return *this;
680  }
681 
683  template <typename S>
684  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator -= (const S& x) {
685  ImplType::operator-=(x);
686  return *this;
687  }
688 
690  template <typename S>
691  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator *= (const S& x) {
692  ImplType::operator*=(x);
693  return *this;
694  }
695 
697  template <typename S>
698  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator /= (const S& x) {
699  ImplType::operator/=(x);
700  return *this;
701  }
702 
704  LogicalSparse& operator += (const LogicalSparse& x) {
705  ImplType::operator+=(static_cast<const ImplType&>(x));
706  return *this;
707  }
708 
710  LogicalSparse& operator -= (const LogicalSparse& x) {
711  ImplType::operator-=(static_cast<const ImplType&>(x));
712  return *this;
713  }
714 
716  LogicalSparse& operator *= (const LogicalSparse& x) {
717  ImplType::operator*=(static_cast<const ImplType&>(x));
718  return *this;
719  }
720 
722  LogicalSparse& operator /= (const LogicalSparse& x) {
723  ImplType::operator/=(static_cast<const ImplType&>(x));
724  return *this;
725  }
726 
728  template <typename S>
729  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator += (const Expr<S>& x) {
730  ImplType::operator+=(x);
731  return *this;
732  }
733 
735  template <typename S>
736  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator -= (const Expr<S>& x) {
737  ImplType::operator-=(x);
738  return *this;
739  }
740 
742  template <typename S>
743  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator *= (const Expr<S>& x) {
744  ImplType::operator*=(x);
745  return *this;
746  }
747 
749  template <typename S>
750  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator /= (const Expr<S>& x) {
751  ImplType::operator/=(x);
752  return *this;
753  }
754 
755  }; // class LogicalSparse<ValT,LogT>
756 
757  template <typename T, typename L>
758  struct ExprLevel< LogicalSparse<T,L> > {
759  static const unsigned value =
761  };
762 
763  } // namespace LFad
764 
765  template <typename T>
766  struct IsExpr< LFad::Expr<T> > {
767  static const bool value = true;
768  };
769 
770  template <typename T>
771  struct BaseExprType< LFad::Expr<T> > {
773  };
774 
775  template <typename T, typename L>
776  struct IsExpr< LFad::LogicalSparse<T,L> > {
777  static const bool value = true;
778  };
779 
780  template <typename T, typename L>
781  struct BaseExprType< LFad::LogicalSparse<T,L> > {
783  };
784 
785 } // namespace Sacado
786 
788 
789 #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.
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.
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
LogicalSparse(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
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:553
SACADO_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &x)
Assignment.
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 ValT &x)
Constructor with size sz and value x.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
Base template specification for testing equivalence.
SACADO_INLINE_FUNCTION LogT & fastAccessDx(int i)
Returns derivative component i without bounds checking.
LFad::LogicalSparse< T, L >::base_expr_type type
Get the base Fad type from a view/expression.
LogicalSparseImp(const LogicalSparseImp &x)
Copy constructor.
LogT logical_type
Logical type (i.e., type for derivative array components (e.g., bool)
Wrapper for a generic expression template.
SACADO_INLINE_FUNCTION const ValT & val() const
Returns value.
LogicalSparseImp< ValT, LogT >::value_type value_type
Typename of values.
bool isPassive() const
Returns true if derivative array is empty.
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.
Expr(const int sz, const int i, const ValT &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
Turn LogicalSparse into a meta-function class usable with mpl::apply.
ExprType::value_type value_type
Typename of values.
Initialize the derivative array.
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.
Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
LogicalSparseImp(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
LogicalSparseImp(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
bool hasFastAccess() const
Returns true if derivative array is not empty.
void setIsConstant(bool is_const)
Set whether variable is constant.
SACADO_INLINE_FUNCTION const LogT * dx() const
Returns derivative array.