// @HEADER // ***************************************************************************** // Stokhos Package // // Copyright 2009 NTESS and the Stokhos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "Sacado_DynamicArrayTraits.hpp" #include "Teuchos_TimeMonitor.hpp" #include "Stokhos_ConstantOrthogPolyExpansion.hpp" namespace Sacado { namespace PCE { template OrthogPoly:: OrthogPoly() : expansion_(), th(new Stokhos::OrthogPolyApprox) { const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion); expansion_ = const_expansion_; } template OrthogPoly:: OrthogPoly(const typename OrthogPoly::value_type& x) : expansion_(), th(new Stokhos::OrthogPolyApprox(Teuchos::null, 1, &x)) { const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion); expansion_ = const_expansion_; } template OrthogPoly:: OrthogPoly(const Teuchos::RCP& expansion) : expansion_(expansion), th(new Stokhos::OrthogPolyApprox(expansion_->getBasis())) { const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion); } template OrthogPoly:: OrthogPoly(const Teuchos::RCP& expansion, ordinal_type sz) : expansion_(expansion), th(new Stokhos::OrthogPolyApprox(expansion_->getBasis(), sz)) { const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion); } template OrthogPoly:: OrthogPoly(const OrthogPoly& x) : expansion_(x.expansion_), th(x.th) { const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion); } template OrthogPoly:: ~OrthogPoly() { } template void OrthogPoly:: reset(const Teuchos::RCP& expansion) { expansion_ = expansion; th->reset(expansion_->getBasis()); } template void OrthogPoly:: reset(const Teuchos::RCP& expansion, ordinal_type sz) { expansion_ = expansion; th->reset(expansion_->getBasis(), sz); } template typename OrthogPoly::value_type OrthogPoly:: evaluate(const Teuchos::Array::value_type>& point) const { return th->evaluate(point); } template typename OrthogPoly::value_type OrthogPoly:: evaluate( const Teuchos::Array::value_type>& point, const Teuchos::Array::value_type>& bvals) const { return th->evaluate(point, bvals); } template bool OrthogPoly:: isEqualTo(const OrthogPoly& x) const { typedef IsEqual IE; if (x.size() != this->size()) return false; // Allow expansions to be different if their size is 1 and one // of them is a constant expansion if (expansion_ != x.expansion_) { if (x.size() != 1) return false; if ((expansion_ != const_expansion_) && (x.expansion_ != x.const_expansion_)) return false; } bool eq = true; for (int i=0; isize(); i++) eq = eq && IE::eval(x.coeff(i), this->coeff(i)); return eq; } template OrthogPoly& OrthogPoly:: operator=(const typename OrthogPoly::value_type& v) { th.makeOwnCopy(); *th = v; return *this; } template OrthogPoly& OrthogPoly:: operator=(const OrthogPoly& x) { expansion_ = x.expansion_; th = x.th; return *this; } template OrthogPoly OrthogPoly:: operator+() const { return *this; } template OrthogPoly OrthogPoly:: operator-() const { OrthogPoly x(expansion_); expansion_->unaryMinus(*(x.th), *th); return x; } template OrthogPoly& OrthogPoly:: operator+=(const typename OrthogPoly::value_type& v) { th.makeOwnCopy(); expansion_->plusEqual(*th, v); return *this; } template OrthogPoly& OrthogPoly:: operator-=(const typename OrthogPoly::value_type& v) { th.makeOwnCopy(); expansion_->minusEqual(*th, v); return *this; } template OrthogPoly& OrthogPoly:: operator*=(const typename OrthogPoly::value_type& v) { th.makeOwnCopy(); expansion_->timesEqual(*th, v); return *this; } template OrthogPoly& OrthogPoly:: operator/=(const typename OrthogPoly::value_type& v) { th.makeOwnCopy(); expansion_->divideEqual(*th, v); return *this; } template OrthogPoly& OrthogPoly:: operator+=(const OrthogPoly& x) { th.makeOwnCopy(); Teuchos::RCP::expansion_type> e = expansion_; if (x.size() > size()) { e = x.expansion(); reset(e, size()); } e->plusEqual(*th, *x.th); return *this; } template OrthogPoly& OrthogPoly:: operator-=(const OrthogPoly& x) { th.makeOwnCopy(); Teuchos::RCP::expansion_type> e = expansion_; if (x.size() > size()) { e = x.expansion(); reset(e, size()); } e->minusEqual(*th, *x.th); return *this; } template OrthogPoly& OrthogPoly:: operator*=(const OrthogPoly& x) { th.makeOwnCopy(); Teuchos::RCP::expansion_type> e = expansion_; if (x.size() > size()) { e = x.expansion(); reset(e, size()); } e->timesEqual(*th, *x.th); return *this; } template OrthogPoly& OrthogPoly:: operator/=(const OrthogPoly& x) { th.makeOwnCopy(); Teuchos::RCP::expansion_type> e = expansion_; if (x.size() > size()) { e = x.expansion(); reset(e, size()); } e->divideEqual(*th, *x.th); return *this; } template OrthogPoly operator+(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->plus(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly operator+(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->plus(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly operator+(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->plus(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template OrthogPoly operator-(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->minus(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly operator-(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->minus(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly operator-(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->minus(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template OrthogPoly operator*(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->times(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly operator*(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->times(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly operator*(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->times(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template OrthogPoly operator/(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->divide(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly operator/(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->divide(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly operator/(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->divide(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template OrthogPoly exp(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->exp(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly log(const OrthogPoly& a) { TEUCHOS_FUNC_TIME_MONITOR("LOG"); OrthogPoly c(a.expansion(), 0); { TEUCHOS_FUNC_TIME_MONITOR("OPA LOG"); a.expansion()->log(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); } OrthogPoly d(c); return d; } template void log(OrthogPoly& c, const OrthogPoly& a) { OrthogPoly d(a.expansion(), 0); a.expansion()->log(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); } template OrthogPoly log10(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->log10(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly sqrt(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->sqrt(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly cbrt(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->cbrt(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly pow(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->pow(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly pow(const T& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->pow(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly pow(const OrthogPoly& a, const T& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->pow(c.getOrthogPolyApprox(),a.getOrthogPolyApprox(), b); return c; } template OrthogPoly sin(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->sin(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly cos(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->cos(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly tan(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->tan(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly sinh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->sinh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly cosh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->cosh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly tanh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->tanh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly acos(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->acos(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly asin(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->asin(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly atan(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->atan(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly acosh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->acosh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly asinh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->asinh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly atanh(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->atanh(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly fabs(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->fabs(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly abs(const OrthogPoly& a) { OrthogPoly c(a.expansion(), 0); a.expansion()->abs(c.getOrthogPolyApprox(), a.getOrthogPolyApprox()); return c; } template OrthogPoly max(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->max(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly max(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->max(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly max(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->max(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template OrthogPoly min(const OrthogPoly& a, const OrthogPoly& b) { // Get expansion typedef typename OrthogPoly::ordinal_type ordinal_type; ordinal_type da = a.size(); ordinal_type db = b.size(); Teuchos::RCP::expansion_type> e = a.expansion(); if (da == db || da > 1) e = a.expansion(); else e = b.expansion(); OrthogPoly c(e, 0); e->min(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b.getOrthogPolyApprox()); return c; } template OrthogPoly min(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { OrthogPoly c(b.expansion(), 0); b.expansion()->min(c.getOrthogPolyApprox(), a, b.getOrthogPolyApprox()); return c; } template OrthogPoly min(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { OrthogPoly c(a.expansion(), 0); a.expansion()->min(c.getOrthogPolyApprox(), a.getOrthogPolyApprox(), b); return c; } template bool operator==(const OrthogPoly& a, const OrthogPoly& b) { int n = std::max(a.size(), b.size()); for (int i=0; i bool operator==(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { if (a != b.coeff(0)) return false; for (int i=1; i bool operator==(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { if (a.coeff(0) != b) return false; for (int i=1; i bool operator!=(const OrthogPoly& a, const OrthogPoly& b) { return !(a == b); } template bool operator!=(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { return !(a == b); } template bool operator!=(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { return !(a == b); } template bool operator<=(const OrthogPoly& a, const OrthogPoly& b) { return a.two_norm() <= b.two_norm(); } template bool operator<=(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { return a <= b.two_norm(); } template bool operator<=(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { return a.two_norm() <= b; } template bool operator>=(const OrthogPoly& a, const OrthogPoly& b) { return a.two_norm() >= b.two_norm(); } template bool operator>=(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { return a >= b.two_norm(); } template bool operator>=(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { return a.two_norm() >= b; } template bool operator<(const OrthogPoly& a, const OrthogPoly& b) { return a.two_norm() < b.two_norm(); } template bool operator<(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { return a < b.two_norm(); } template bool operator<(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { return a.two_norm() < b; } template bool operator>(const OrthogPoly& a, const OrthogPoly& b) { return a.two_norm() > b.two_norm(); } template bool operator>(const typename OrthogPoly::value_type& a, const OrthogPoly& b) { return a > b.two_norm(); } template bool operator>(const OrthogPoly& a, const typename OrthogPoly::value_type& b) { return a.two_norm() > b; } template bool toBool(const OrthogPoly& x) { bool is_zero = true; for (int i=0; i inline bool operator && (const OrthogPoly& x1, const OrthogPoly& x2) { return toBool(x1) && toBool(x2); } template inline bool operator && (const typename OrthogPoly::value_type& a, const OrthogPoly& x2) { return a && toBool(x2); } template inline bool operator && (const OrthogPoly& x1, const typename OrthogPoly::value_type& b) { return toBool(x1) && b; } template inline bool operator || (const OrthogPoly& x1, const OrthogPoly& x2) { return toBool(x1) || toBool(x2); } template inline bool operator || (const typename OrthogPoly::value_type& a, const OrthogPoly& x2) { return a || toBool(x2); } template inline bool operator || (const OrthogPoly& x1, const typename OrthogPoly::value_type& b) { return toBool(x1) || b; } template std::ostream& operator << (std::ostream& os, const OrthogPoly& a) { typedef typename OrthogPoly::ordinal_type ordinal_type; os << "[ "; for (ordinal_type i=0; i std::istream& operator >> (std::istream& is, OrthogPoly& a) { typedef typename OrthogPoly::ordinal_type ordinal_type; // Read in the opening "[" char bracket; is >> bracket; for (ordinal_type i=0; i> a.fastAccessCoeff(i); } // Read in the closing "]" is >> bracket; return is; } } // namespace PCE } // namespace Sacado