Exemplo n.º 1
0
def get_oli_leja_samples(pce,
                         generate_candidate_samples,
                         num_candidate_samples,
                         num_leja_samples,
                         preconditioning_function=None,
                         basis_generator=total_degree_basis_generator,
                         initial_samples=None):
    """
    Generate Leja samples using orthogonal least interpolation. 

    The number of samples is determined by the number of basis functions.


    Parameters
    ----------
    generate_basis_matrix : callable
        basis_matrix = generate_basis_matrix(candidate_samples)
        Function to evaluate a basis at a set of samples

    generate_candidate_samples : callable
        candidate_samples = generate_candidate_samples(num_candidate_samples)
        Function to generate candidate samples. This can siginficantly effect
        the fekete samples generated

    num_candidate_samples : integer
        The number of candidate_samples

    preconditioning_function : callable
        basis_matrix = preconditioning_function(basis_matrix)
        precondition a basis matrix to improve stability

    num_leja_samples : integer
        The number of desired leja samples. Must be <= num_indices

    Returns
    -------
    laja_samples : np.ndarray (num_vars, num_indices)
        The samples of the Leja sequence

    data_structures : tuple
        (oli_solver,) the final state of the othogonal least interpolation 
        solver. This is useful for quickly building an interpolant

    Notes
    -----
    Should use basis_generator=canonical_basis_matrix here. Thus 
    generate_candidate_samples must generate samples in the canonical domain
    and leja samples are returned in the canonical domain
    """
    oli_opts = dict()
    oli_solver = LeastInterpolationSolver()
    oli_solver.configure(oli_opts)
    oli_solver.set_pce(pce)

    if preconditioning_function is not None:
        oli_solver.set_preconditioning_function(preconditioning_function)

    oli_solver.set_basis_generator(basis_generator)

    num_vars = pce.num_vars()
    max_degree = get_total_degree(num_vars, num_leja_samples)
    indices = compute_hyperbolic_indices(num_vars, max_degree, 1.)
    # warning this assumes basis generator is always compute_hyperbolic_indices
    # with p=1
    assert indices.shape[1] >= num_leja_samples
    pce.set_indices(indices)

    assert num_leja_samples <= num_candidate_samples

    candidate_samples = generate_candidate_samples(num_candidate_samples)

    oli_solver.factorize(candidate_samples,
                         initial_samples,
                         num_selected_pts=num_leja_samples)

    leja_samples = oli_solver.get_current_points()

    data_structures = (oli_solver, )

    return leja_samples, data_structures
Exemplo n.º 2
0
    def test_least_interpolation_lu_equivalence_in_1d(self):
        num_vars = 1
        alpha_stat = 2; beta_stat  = 5
        max_num_pts = 100
        
        var_trans = define_iid_random_variable_transformation(
            beta(alpha_stat,beta_stat),num_vars)
        pce_opts = {'alpha_poly':beta_stat-1,'beta_poly':alpha_stat-1,
                    'var_trans':var_trans,'poly_type':'jacobi',}

        # Set oli options
        oli_opts = {'verbosity':0,
                    'assume_non_degeneracy':False}

        basis_generator = \
          lambda num_vars,degree: (degree+1,compute_hyperbolic_level_indices(
              num_vars,degree,1.0))

        pce = PolynomialChaosExpansion()
        pce.configure(pce_opts)

        oli_solver = LeastInterpolationSolver()
        oli_solver.configure(oli_opts)
        oli_solver.set_pce(pce)

        # univariate_beta_pdf = partial(beta.pdf,a=alpha_stat,b=beta_stat)
        # univariate_pdf = lambda x: univariate_beta_pdf(x)
        # preconditioning_function = partial(
        #     tensor_product_pdf,univariate_pdfs=univariate_pdf)
        from pyapprox.indexing import get_total_degree
        max_degree = get_total_degree(num_vars,max_num_pts)
        indices = compute_hyperbolic_indices(num_vars, max_degree, 1.)
        pce.set_indices(indices)
        
        from pyapprox.polynomial_sampling import christoffel_function
        preconditioning_function = lambda samples: 1./christoffel_function(
            samples,pce.basis_matrix)
    
        oli_solver.set_preconditioning_function(preconditioning_function)
        oli_solver.set_basis_generator(basis_generator)
        
        initial_pts = None
        candidate_samples = np.linspace(0.,1.,1000)[np.newaxis,:]

        oli_solver.factorize(
            candidate_samples, initial_pts,
            num_selected_pts = max_num_pts)

        oli_samples = oli_solver.get_current_points()

        from pyapprox.utilities import truncated_pivoted_lu_factorization
        pce.set_indices(oli_solver.selected_basis_indices)
        basis_matrix = pce.basis_matrix(candidate_samples)
        weights = np.sqrt(preconditioning_function(candidate_samples))
        basis_matrix = np.dot(np.diag(weights),basis_matrix)
        L,U,p = truncated_pivoted_lu_factorization(
            basis_matrix,max_num_pts)
        assert p.shape[0]==max_num_pts
        lu_samples = candidate_samples[:,p]

        assert np.allclose(lu_samples,oli_samples)

        L1,U1,H1 = oli_solver.get_current_LUH_factors()
        
        true_permuted_matrix = (pce.basis_matrix(lu_samples).T*weights[p]).T
        assert np.allclose(np.dot(L,U),true_permuted_matrix)
        assert np.allclose(np.dot(L1,np.dot(U1,H1)),true_permuted_matrix)