value_type Stokhos::KL::OneDExponentialCovarianceFunction<value_type>:: bisection(const Func& func, const value_type& a, const value_type& b, magnitude_type tol, int max_num_its) { value_type low, hi; value_type fa = func.eval(a); value_type fb = func.eval(b); TEUCHOS_TEST_FOR_EXCEPTION(fa*fb > value_type(0.0), std::logic_error, "Bounds [" << a << "," << b << "] must bracket the root!" << std::endl << "f(a) = " << fa << ", f(b) = " << fb << std::endl) if (fa <= 0.0) { low = a; hi = b; } else { low = b; hi = a; } int nit = 0; value_type u = low + (hi - low)/2.0; value_type f = func.eval(u); while ((Teuchos::ScalarTraits<value_type>::magnitude(hi - low) > 2.0*tol || Teuchos::ScalarTraits<value_type>::magnitude(f) > tol) && nit < max_num_its) { //std::cout << "u = " << u << " f = " << f << std::endl; if (f <= 0.0) low = u; else hi = u; u = low + (hi - low)/2.0; f = func.eval(u); ++nit; } TEUCHOS_TEST_FOR_EXCEPTION(nit >= max_num_its, std::logic_error, "Nonlinear solver did not converge!" << std::endl); return u; }
value_type Stokhos::KL::OneDExponentialCovarianceFunction<value_type>:: newton(const Func& func, const value_type& a, const value_type& b, magnitude_type tol, int max_num_its) { value_type u = (a+b)/2.0; value_type f = func.eval(u); int nit = 0; while (Teuchos::ScalarTraits<value_type>::magnitude(f) > tol && nit < max_num_its) { std::cout << "u = " << u << " f = " << f << std::endl; value_type dfdu = func.deriv(u); u -= f / dfdu; f = func.eval(u); ++nit; } TEUCHOS_TEST_FOR_EXCEPTION(nit >= max_num_its, std::logic_error, "Nonlinear solver did not converge!" << std::endl); return u; }
Lanczos_PCE_Setup(bool normalize, bool project) : func() { rtol = 1e-8; atol = 1e-12; const OrdinalType d = 3; const OrdinalType p = 5; // Create product basis Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<OrdinalType,ValueType> > > bases(d); for (OrdinalType i=0; i<d; i++) bases[i] = Teuchos::rcp(new Stokhos::LegendreBasis<OrdinalType,ValueType>(p)); basis = Teuchos::rcp(new Stokhos::CompletePolynomialBasis<OrdinalType,ValueType>(bases)); // Create approximation sz = basis->size(); Stokhos::OrthogPolyApprox<OrdinalType,ValueType> x(basis); for (OrdinalType i=0; i<d; i++) x.term(i, 1) = 1.0; // Tensor product quadrature Teuchos::RCP<const Stokhos::Quadrature<OrdinalType,ValueType> > quad = Teuchos::rcp(new Stokhos::TensorProductQuadrature<OrdinalType,ValueType>(basis, 4*p)); // Triple product tensor Teuchos::RCP<Stokhos::Sparse3Tensor<int,double> > Cijk = basis->computeTripleProductTensor(basis->size()); // Quadrature expansion exp = Teuchos::rcp(new Stokhos::QuadOrthogPolyExpansion<OrdinalType,ValueType>(basis, Cijk, quad)); // Compute PCE via quadrature expansion u.reset(basis); v.reset(basis); func.eval(*exp, x, u); exp->times(v,u,u); // Compute Lanczos basis st_bases.resize(1); if (project) { st_1d_proj_basis = Teuchos::rcp(new Stokhos::LanczosProjPCEBasis<OrdinalType,ValueType>( p, Teuchos::rcp(&u,false), Cijk, normalize)); st_bases[0] = st_1d_proj_basis; } else { st_1d_basis = Teuchos::rcp(new Stokhos::LanczosPCEBasis<OrdinalType,ValueType>( p, Teuchos::rcp(&u,false), quad, normalize, false)); st_bases[0] = st_1d_basis; } st_basis = Teuchos::rcp(new Stokhos::CompletePolynomialBasis<OrdinalType,ValueType>(st_bases, 1e-15)); st_sz = st_basis->size(); u_st.reset(st_basis); v_st.reset(st_basis); if (project) { u_st[0] = st_1d_proj_basis->getNewCoeffs(0); u_st[1] = st_1d_proj_basis->getNewCoeffs(1); } else { u_st[0] = st_1d_basis->getNewCoeffs(0); u_st[1] = st_1d_basis->getNewCoeffs(1); } // Tensor product quadrature st_quad = Teuchos::rcp(new Stokhos::TensorProductQuadrature<OrdinalType,ValueType>(st_basis)); // Triple product tensor Teuchos::RCP<Stokhos::Sparse3Tensor<int,double> > st_Cijk = st_basis->computeTripleProductTensor(st_basis->size()); // Quadrature expansion Stokhos::QuadOrthogPolyExpansion<OrdinalType,ValueType> st_exp(st_basis, st_Cijk, st_quad); st_exp.times(v_st, u_st, u_st); }