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 //
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) :
122  Storage(sz, x, InitDerivArray) {}
123 
125 
130  LogicalSparseImp(const int sz, const int i, const value_type & x) :
131  Storage(sz, x, InitDerivArray) {
132  this->fastAccessDx(i)=logical_type(1);
133  }
134 
137  Storage(x) {}
138 
140  template <typename S>
142  Storage(value_type(0)) {
143  int sz = x.size();
144 
145  if (sz != this->size())
146  this->resize(sz);
147 
148  if (sz) {
149  if (x.hasFastAccess())
150  for(int i=0; i<sz; ++i)
151  this->fastAccessDx(i) = x.fastAccessDx(i);
152  else
153  for(int i=0; i<sz; ++i)
154  this->fastAccessDx(i) = x.dx(i);
155  }
156 
157  this->val() = x.val();
158  }
159 
162 
164 
170  void diff(const int ith, const int n) {
171  if (this->size() != n)
172  this->resize(n);
173 
174  this->zero();
175  this->fastAccessDx(ith) = logical_type(1);
176  }
177 
179  template <typename S>
180  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
181  typedef IsEqual<value_type> IE;
182  if (x.size() != this->size()) return false;
183  bool eq = IE::eval(x.val(), this->val());
184  for (int i=0; i<this->size(); i++)
185  eq = eq && IE::eval(x.dx(i), this->dx(i));
186  return eq;
187  }
188 
190 
195 
197  bool hasFastAccess() const { return this->size()!=0;}
198 
200  bool isPassive() const { return this->size()!=0;}
201 
203  void setIsConstant(bool is_const) {
204  if (is_const && this->size()!=0)
205  this->resize(0);
206  }
207 
209 
214 
216  template <typename S>
217  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator=(const S& v) {
218  this->val() = v;
219  if (this->size()) {
220  this->zero();
221  this->resize(0);
222  }
223  return *this;
224  }
225 
228  // Copy value & dx_
230 
231  return *this;
232  }
233 
235  template <typename S>
236  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator=(const Expr<S>& x) {
237  int sz = x.size();
238 
239  if (sz != this->size())
240  this->resize(sz);
241 
242  if (sz) {
243  if (x.hasFastAccess())
244  for(int i=0; i<sz; ++i)
245  this->fastAccessDx(i) = x.fastAccessDx(i);
246  else
247  for(int i=0; i<sz; ++i)
248  this->fastAccessDx(i) = x.dx(i);
249  }
250 
251  this->val() = x.val();
252 
253  return *this;
254  }
255 
257 
262 
264  template <typename S>
265  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator += (const S& v) {
266  this->val() += v;
267  return *this;
268  }
269 
271  template <typename S>
272  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator -= (const S& v) {
273  this->val() -= v;
274  return *this;
275  }
276 
278  template <typename S>
279  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator *= (const S& v) {
280  this->val() *= v;
281  return *this;
282  }
283 
285  template <typename S>
286  SACADO_ENABLE_VALUE_FUNC(LogicalSparseImp&) operator /= (const S& v) {
287  this->val() /= v;
288  return *this;
289  }
290 
292  LogicalSparseImp& operator += (const LogicalSparseImp& x) {
293  int xsz = x.size(), sz = this->size();
294 
295 #ifdef SACADO_DEBUG
296  if ((xsz != sz) && (xsz != 0) && (sz != 0))
297  throw "LFad Error: Attempt to assign with incompatible sizes";
298 #endif
299 
300  if (xsz) {
301  if (sz) {
302  for (int i=0; i<xsz; ++i)
303  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
304  }
305  else {
306  this->resize(xsz);
307  for (int i=0; i<xsz; ++i)
308  this->fastAccessDx(i) = x.fastAccessDx(i);
309  }
310  }
311 
312  this->val() += x.val();
313 
314  return *this;
315  }
316 
318  LogicalSparseImp& operator -= (const LogicalSparseImp& x) {
319  int xsz = x.size(), sz = this->size();
320 
321 #ifdef SACADO_DEBUG
322  if ((xsz != sz) && (xsz != 0) && (sz != 0))
323  throw "LFad Error: Attempt to assign with incompatible sizes";
324 #endif
325 
326  if (xsz) {
327  if (sz) {
328  for (int i=0; i<xsz; ++i)
329  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
330  }
331  else {
332  this->resize(xsz);
333  for (int i=0; i<xsz; ++i)
334  this->fastAccessDx(i) = x.fastAccessDx(i);
335  }
336  }
337 
338  this->val() -= x.val();
339 
340 
341  return *this;
342  }
343 
345  LogicalSparseImp& operator *= (const LogicalSparseImp& x) {
346  int xsz = x.size(), sz = this->size();
347 
348 #ifdef SACADO_DEBUG
349  if ((xsz != sz) && (xsz != 0) && (sz != 0))
350  throw "LFad Error: Attempt to assign with incompatible sizes";
351 #endif
352 
353  if (xsz) {
354  if (sz) {
355  for (int i=0; i<xsz; ++i)
356  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
357  }
358  else {
359  this->resize(xsz);
360  for (int i=0; i<xsz; ++i)
361  this->fastAccessDx(i) = x.fastAccessDx(i);
362  }
363  }
364 
365  this->val() *= x.val();
366 
367  return *this;
368  }
369 
371  LogicalSparseImp& operator /= (const LogicalSparseImp& x) {
372  int xsz = x.size(), sz = this->size();
373 
374 #ifdef SACADO_DEBUG
375  if ((xsz != sz) && (xsz != 0) && (sz != 0))
376  throw "LFad Error: Attempt to assign with incompatible sizes";
377 #endif
378 
379  if (xsz) {
380  if (sz) {
381  for (int i=0; i<xsz; ++i)
382  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
383  }
384  else {
385  this->resize(xsz);
386  for (int i=0; i<xsz; ++i)
387  this->fastAccessDx(i) = x.fastAccessDx(i);
388  }
389  }
390 
391  this->val() /= x.val();
392 
393  return *this;
394  }
395 
397  template <typename S>
398  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator += (const Expr<S>& x){
399  int xsz = x.size(), sz = this->size();
400 
401 #ifdef SACADO_DEBUG
402  if ((xsz != sz) && (xsz != 0) && (sz != 0))
403  throw "LFad Error: Attempt to assign with incompatible sizes";
404 #endif
405 
406  if (xsz) {
407  if (sz) {
408  if (x.hasFastAccess())
409  for (int i=0; i<xsz; ++i)
410  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
411  else
412  for (int i=0; i<xsz; ++i)
413  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
414  }
415  else {
416  this->resize(xsz);
417  if (x.hasFastAccess())
418  for (int i=0; i<xsz; ++i)
419  this->fastAccessDx(i) = x.fastAccessDx(i);
420  else
421  for (int i=0; i<xsz; ++i)
422  this->fastAccessDx(i) = x.dx(i);
423  }
424  }
425 
426  this->val() += x.val();
427 
428  return *this;
429  }
430 
432  template <typename S>
433  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator -= (const Expr<S>& x){
434  int xsz = x.size(), sz = this->size();
435 
436 #ifdef SACADO_DEBUG
437  if ((xsz != sz) && (xsz != 0) && (sz != 0))
438  throw "LFad Error: Attempt to assign with incompatible sizes";
439 #endif
440 
441  if (xsz) {
442  if (sz) {
443  if (x.hasFastAccess())
444  for (int i=0; i<xsz; ++i)
445  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
446  else
447  for (int i=0; i<xsz; ++i)
448  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
449  }
450  else {
451  this->resize(xsz);
452  if (x.hasFastAccess())
453  for (int i=0; i<xsz; ++i)
454  this->fastAccessDx(i) = x.fastAccessDx(i);
455  else
456  for (int i=0; i<xsz; ++i)
457  this->fastAccessDx(i) = x.dx(i);
458  }
459  }
460 
461  this->val() -= x.val();
462 
463 
464  return *this;
465  }
466 
468  template <typename S>
469  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator *= (const Expr<S>& x){
470  int xsz = x.size(), sz = this->size();
471 
472 #ifdef SACADO_DEBUG
473  if ((xsz != sz) && (xsz != 0) && (sz != 0))
474  throw "LFad Error: Attempt to assign with incompatible sizes";
475 #endif
476 
477  if (xsz) {
478  if (sz) {
479  if (x.hasFastAccess())
480  for (int i=0; i<xsz; ++i)
481  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
482  else
483  for (int i=0; i<xsz; ++i)
484  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
485  }
486  else {
487  this->resize(xsz);
488  if (x.hasFastAccess())
489  for (int i=0; i<xsz; ++i)
490  this->fastAccessDx(i) = x.fastAccessDx(i);
491  else
492  for (int i=0; i<xsz; ++i)
493  this->fastAccessDx(i) = x.dx(i);
494  }
495  }
496 
497  this->val() *= x.val();
498 
499  return *this;
500  }
501 
503  template <typename S>
504  SACADO_ENABLE_EXPR_FUNC(LogicalSparseImp&) operator /= (const Expr<S>& x){
505  int xsz = x.size(), sz = this->size();
506 
507 #ifdef SACADO_DEBUG
508  if ((xsz != sz) && (xsz != 0) && (sz != 0))
509  throw "LFad Error: Attempt to assign with incompatible sizes";
510 #endif
511 
512  if (xsz) {
513  if (sz) {
514  if (x.hasFastAccess())
515  for (int i=0; i<xsz; ++i)
516  this->fastAccessDx(i) = this->fastAccessDx(i) || x.fastAccessDx(i);
517  else
518  for (int i=0; i<xsz; ++i)
519  this->fastAccessDx(i) = this->fastAccessDx(i) || x.dx(i);
520  }
521  else {
522  this->resize(xsz);
523  if (x.hasFastAccess())
524  for (int i=0; i<xsz; ++i)
525  this->fastAccessDx(i) = x.fastAccessDx(i);
526  else
527  for (int i=0; i<xsz; ++i)
528  this->fastAccessDx(i) = x.dx(i);
529  }
530  }
531 
532  this->val() /= x.val();
533 
534  return *this;
535  }
536 
538 
539  }; // class LogicalSparseImp
540 
542  template <typename ValT, typename LogT>
543  class Expr< LogicalSparseImp<ValT,LogT> > :
544  public LogicalSparseImp<ValT,LogT> {
545 
546  public:
547 
550 
553 
556 
558  Expr() :
559  LogicalSparseImp<ValT,LogT>() {}
560 
562 
565  template <typename S>
567  LogicalSparseImp<ValT,LogT>(x) {}
568 
570 
573  Expr(const int sz, const ValT & x) :
574  LogicalSparseImp<ValT,LogT>(sz,x) {}
575 
577 
582  Expr(const int sz, const int i, const ValT & x) :
583  LogicalSparseImp<ValT,LogT>(sz,i,x) {}
584 
586  Expr(const Expr& x) :
587  LogicalSparseImp<ValT,LogT>(x) {}
588 
590  template <typename S>
592  LogicalSparseImp<ValT,LogT>(x) {}
593 
595  ~Expr() {}
596 
597  }; // class Expr<LogicalSparseImp>
598 
603  template <typename ValT, typename LogT >
604  class LogicalSparse : public Expr< LogicalSparseImp<ValT,LogT > > {
605 
606  public:
607 
611 
613  typedef typename ExprType::value_type value_type;
614 
616  typedef typename ExprType::scalar_type scalar_type;
617 
619  template <typename T, typename U = LogT>
620  struct apply {
622  };
623 
628 
630 
634  ExprType() {}
635 
637 
640  template <typename S>
642  ExprType(x) {}
643 
645 
648  LogicalSparse(const int sz, const ValT& x) :
649  ExprType(sz,x) {}
650 
652 
657  LogicalSparse(const int sz, const int i, const ValT & x) :
658  ExprType(sz,i,x) {}
659 
662  ExprType(x) {}
663 
665  template <typename S>
667  ExprType(x) {}
668 
670 
673 
675  template <typename S>
676  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator=(const S& v) {
678  return *this;
679  }
680 
683  ImplType::operator=(static_cast<const ImplType&>(x));
684  return *this;
685  }
686 
688  template <typename S>
689  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator=(const Expr<S>& x)
690  {
692  return *this;
693  }
694 
696  template <typename S>
697  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator += (const S& x) {
698  ImplType::operator+=(x);
699  return *this;
700  }
701 
703  template <typename S>
704  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator -= (const S& x) {
705  ImplType::operator-=(x);
706  return *this;
707  }
708 
710  template <typename S>
711  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator *= (const S& x) {
712  ImplType::operator*=(x);
713  return *this;
714  }
715 
717  template <typename S>
718  SACADO_ENABLE_VALUE_FUNC(LogicalSparse&) operator /= (const S& x) {
719  ImplType::operator/=(x);
720  return *this;
721  }
722 
724  LogicalSparse& operator += (const LogicalSparse& x) {
725  ImplType::operator+=(static_cast<const ImplType&>(x));
726  return *this;
727  }
728 
730  LogicalSparse& operator -= (const LogicalSparse& x) {
731  ImplType::operator-=(static_cast<const ImplType&>(x));
732  return *this;
733  }
734 
736  LogicalSparse& operator *= (const LogicalSparse& x) {
737  ImplType::operator*=(static_cast<const ImplType&>(x));
738  return *this;
739  }
740 
742  LogicalSparse& operator /= (const LogicalSparse& x) {
743  ImplType::operator/=(static_cast<const ImplType&>(x));
744  return *this;
745  }
746 
748  template <typename S>
749  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator += (const Expr<S>& x) {
750  ImplType::operator+=(x);
751  return *this;
752  }
753 
755  template <typename S>
756  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator -= (const Expr<S>& x) {
757  ImplType::operator-=(x);
758  return *this;
759  }
760 
762  template <typename S>
763  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator *= (const Expr<S>& x) {
764  ImplType::operator*=(x);
765  return *this;
766  }
767 
769  template <typename S>
770  SACADO_ENABLE_EXPR_FUNC(LogicalSparse&) operator /= (const Expr<S>& x) {
771  ImplType::operator/=(x);
772  return *this;
773  }
774 
775  }; // class LogicalSparse<ValT,LogT>
776 
777  template <typename T, typename L>
778  struct ExprLevel< LogicalSparse<T,L> > {
779  static const unsigned value =
781  };
782 
783  } // namespace LFad
784 
785  template <typename T>
786  struct IsExpr< LFad::Expr<T> > {
787  static const bool value = true;
788  };
789 
790  template <typename T>
791  struct BaseExprType< LFad::Expr<T> > {
793  };
794 
795  template <typename T, typename L>
796  struct IsExpr< LFad::LogicalSparse<T,L> > {
797  static const bool value = true;
798  };
799 
800  template <typename T, typename L>
801  struct BaseExprType< LFad::LogicalSparse<T,L> > {
803  };
804 
805 } // namespace Sacado
806 
808 
809 #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:573
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.