Exemplo n.º 1
0
 def test_qr_solve(self):
     nrows = 3
     rhs = np.random.normal(0., 1., (nrows, 1))
     A = np.random.normal(0., 1., (nrows, nrows))
     A = A.T.dot(A)
     Q, R = qr_factorization(A)
     sol = qr_solve(Q, R, rhs)
     assert np.allclose(sol, np.linalg.solve(A, rhs))
Exemplo n.º 2
0
 def get_implicit_timestep_matrix_inverse_factors(self, matrix):
     identity = np.eye(matrix.shape[0])
     if (self.time_step_method == "backward-euler"):
         matrix = identity-self.time_step_size*matrix
     elif (self.time_step_method == "crank-nicholson"):
         matrix = identity-self.time_step_size/2.*matrix
     else:
         raise Exception('incorrect timestepping method specified')
     self.apply_boundary_conditions_to_matrix(matrix)
     return qr_factorization(matrix)
Exemplo n.º 3
0
def get_fekete_samples(generate_basis_matrix,
                       generate_candidate_samples,
                       num_candidate_samples,
                       preconditioning_function=None,
                       precond_opts=dict()):
    """
    Generate Fekete samples using QR factorization. 

    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,samples)
        precondition a basis matrix to improve stability.
        samples are the samples used to build the basis matrix. They must
        be in the same order as they were used to create the rows of the basis 
        matrix. Note if using probability density function for 
        preconditioning make sure density has been mapped to canonical domain

    TODO unfortunately some preconditioing_functions need only basis matrix
    or samples, but cant think of a better way to generically pass in function
    here other than to require functions that use both arguments

    Returns
    -------
    fekete_samples : np.ndarray (num_vars, num_indices)
        The Fekete samples

    data_structures : tuple
        (Q,R,p) the QR factors and pivots. This can be useful for
        quickly building an interpolant from the samples

    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
    """
    candidate_samples = generate_candidate_samples(num_candidate_samples)
    basis_matrix = generate_basis_matrix(candidate_samples)
    if preconditioning_function is not None:
        weights = np.sqrt(
            preconditioning_function(basis_matrix, candidate_samples))
        basis_matrix = np.dot(np.diag(weights), basis_matrix)
    else:
        weights = None
    Q, R, p = qr_factorization(basis_matrix.T, pivoting=True)
    p = p[:basis_matrix.shape[1]]
    fekete_samples = candidate_samples[:, p]
    data_structures = (Q, R[:, :basis_matrix.shape[1]], p, weights[p])
    return fekete_samples, data_structures