当前位置: 首页>>代码示例>>Python>>正文


Python linalg.splu方法代码示例

本文整理汇总了Python中scipy.sparse.linalg.splu方法的典型用法代码示例。如果您正苦于以下问题:Python linalg.splu方法的具体用法?Python linalg.splu怎么用?Python linalg.splu使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.sparse.linalg的用法示例。


在下文中一共展示了linalg.splu方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_solve

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_solve(self):
        # Test whether the lu_solve command segfaults, as reported by Nils
        # Wagner for a 64-bit machine, 02 March 2005 (EJS)
        n = 20
        np.random.seed(0)  # make tests repeatable
        A = zeros((n,n), dtype=complex)
        x = np.random.rand(n)
        y = np.random.rand(n-1)+1j*np.random.rand(n-1)
        r = np.random.rand(n)
        for i in range(len(x)):
            A[i,i] = x[i]
        for i in range(len(y)):
            A[i,i+1] = y[i]
            A[i+1,i] = conjugate(y[i])
        A = self.spmatrix(A)
        x = splu(A).solve(r)
        assert_almost_equal(A*x,r) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:19,代码来源:test_base.py

示例2: test_expm

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_expm(self):
        M = array([[1, 0, 2], [0, 0, 3], [-4, 5, 6]], float)
        sM = self.spmatrix(M, shape=(3,3), dtype=float)
        Mexp = scipy.linalg.expm(M)

        N = array([[3., 0., 1.], [0., 2., 0.], [0., 0., 0.]])
        sN = self.spmatrix(N, shape=(3,3), dtype=float)
        Nexp = scipy.linalg.expm(N)

        with suppress_warnings() as sup:
            sup.filter(SparseEfficiencyWarning, "splu requires CSC matrix format")
            sup.filter(SparseEfficiencyWarning,
                       "spsolve is more efficient when sparse b is in the CSC matrix format")
            sup.filter(SparseEfficiencyWarning,
                       "spsolve requires A be CSC or CSR matrix format")
            sMexp = expm(sM).todense()
            sNexp = expm(sN).todense()

        assert_array_almost_equal((sMexp - Mexp), zeros((3, 3)))
        assert_array_almost_equal((sNexp - Nexp), zeros((3, 3))) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:22,代码来源:test_base.py

示例3: test_inv

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_inv(self):
        def check(dtype):
            M = array([[1, 0, 2], [0, 0, 3], [-4, 5, 6]], dtype)
            with suppress_warnings() as sup:
                sup.filter(SparseEfficiencyWarning,
                           "spsolve requires A be CSC or CSR matrix format")
                sup.filter(SparseEfficiencyWarning,
                           "spsolve is more efficient when sparse b is in the CSC matrix format")
                sup.filter(SparseEfficiencyWarning,
                           "splu requires CSC matrix format")
                sM = self.spmatrix(M, shape=(3,3), dtype=dtype)
                sMinv = inv(sM)
            assert_array_almost_equal(sMinv.dot(sM).todense(), np.eye(3))
            assert_raises(TypeError, inv, M)
        for dtype in [float]:
            check(dtype) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:18,代码来源:test_base.py

示例4: test_solve

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_solve(self):
        # Test whether the lu_solve command segfaults, as reported by Nils
        # Wagner for a 64-bit machine, 02 March 2005 (EJS)
        n = 20
        np.random.seed(0)  # make tests repeatable
        A = zeros((n,n), dtype=complex)
        x = np.random.rand(n)
        y = np.random.rand(n-1)+1j*np.random.rand(n-1)
        r = np.random.rand(n)
        for i in range(len(x)):
            A[i,i] = x[i]
        for i in range(len(y)):
            A[i,i+1] = y[i]
            A[i+1,i] = conjugate(y[i])
        A = self.spmatrix(A)
        with suppress_warnings() as sup:
            sup.filter(SparseEfficiencyWarning, "splu requires CSC matrix format")
            x = splu(A).solve(r)
        assert_almost_equal(A*x,r) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:21,代码来源:test_base.py

示例5: solve_system

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def solve_system(self, rhs, factor, u0, t):
        """
        Simple linear solver for (I+factor*A)u = rhs

        Args:
            rhs (dtype_f): right-hand side for the linear system
            factor (float) : abbrev. for the node-to-node stepsize (or any other factor required)
            u0 (dtype_u): initial guess for the iterative solver (not used here so far)
            t (float): current time (e.g. for time-dependent BCs)

        Returns:
            dtype_u: solution as mesh
        """

        me = self.dtype_u(self.init)
        L = splu(sp.eye(self.params.nvars, format='csc') + factor * self.A)
        me.values = L.solve(rhs.values)
        return me 
开发者ID:Parallel-in-Time,项目名称:pySDC,代码行数:20,代码来源:AdvectionEquation_1D_FD_dirichlet.py

示例6: solve_system

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def solve_system(self, rhs, factor, u0, t):
        """
        Simple linear solver for (I+factor*A)u = rhs

        Args:
            rhs (dtype_f): right-hand side for the linear system
            factor (float) : abbrev. for the node-to-node stepsize (or any other factor required)
            u0 (dtype_u): initial guess for the iterative solver (not used here so far)
            t (float): current time (e.g. for time-dependent BCs)

        Returns:
            dtype_u: solution as mesh
        """

        me = self.dtype_u(self.init)
        L = splu(sp.eye(self.params.nvars, format='csc') - factor * self.A)
        me.values = L.solve(rhs.values)
        return me 
开发者ID:Parallel-in-Time,项目名称:pySDC,代码行数:20,代码来源:AdvectionEquation_1D_FD.py

示例7: solve_system

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def solve_system(self, rhs, factor, u0, t):
        """
        Simple linear solver for (I-factor*A)u = rhs

        Args:
            rhs (dtype_f): right-hand side for the linear system
            factor (float): abbrev. for the local stepsize (or any other factor required)
            u0 (dtype_u): initial guess for the iterative solver
            t (float): current time (e.g. for time-dependent BCs)

        Returns:
            dtype_u: solution as mesh
        """

        me = self.dtype_u(self.init)
        L = splu(sp.eye(self.params.nvars, format='csc') - factor * self.A)
        me.values = L.solve(rhs.values)
        return me 
开发者ID:Parallel-in-Time,项目名称:pySDC,代码行数:20,代码来源:HeatEquation_1D_FD_periodic.py

示例8: __init__

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def __init__(self, work):
        """
        Initialize structure for KKT system solution
        """
        # Construct reduced KKT matrix
        KKT = spspa.vstack([
              spspa.hstack([work.data.P + work.settings.sigma *
                            spspa.eye(work.data.n), work.data.A.T]),
              spspa.hstack([work.data.A, -spspa.diags(work.rho_inv_vec)])])

        # Initialize structure
        self.kkt_factor = spla.splu(KKT.tocsc())
        #  self.lu, self.piv = sp.linalg.lu_factor(KKT.todense()) 
开发者ID:oxfordcontrol,项目名称:osqp-python,代码行数:15,代码来源:_osqp.py

示例9: test_preconditioner

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_preconditioner(self):
        # Check that preconditioning works
        pc = splu(Am.tocsc())
        M = LinearOperator(matvec=pc.solve, shape=A.shape, dtype=A.dtype)

        x0, count_0 = do_solve()
        x1, count_1 = do_solve(M=M)

        assert_(count_1 == 3)
        assert_(count_1 < count_0/2)
        assert_(allclose(x1, x0, rtol=1e-14)) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:13,代码来源:test_lgmres.py

示例10: solve_nonlinear

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def solve_nonlinear(self, inputs, outputs):
        """
        Use numpy to solve Ax=b for x.

        Parameters
        ----------
        inputs : Vector
            unscaled, dimensional input variables read via inputs[key]
        outputs : Vector
            unscaled, dimensional output variables read via outputs[key]
        """
        # lu factorization for use with solve_linear
        K = self.assemble_CSC_K(inputs)
        self._lup = splu(K)
        outputs['disp_aug'] = self._lup.solve(inputs['forces']) 
开发者ID:mdolab,项目名称:OpenAeroStruct,代码行数:17,代码来源:fem.py

示例11: sparse_solve_kkt_inverse

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def sparse_solve_kkt_inverse(H_, A_, C_tilde, rx, rs, rz, ry, ns):
    nineq, nz, neq, nBatch = ns

    if neq > 0:
        g_ = torch.cat([rx, rs], 1).squeeze(0).numpy()
        h_ = torch.cat([rz, ry], 1).squeeze(0).numpy()
    else:
        g_ = torch.cat([rx, rs], 1).squeeze(0).numpy()
        h_ = rz.squeeze(0).numpy()

    full_mat = bmat([[H_, A_.transpose()],
                     [A_, C_tilde]], format='csc')
    full_res = np.concatenate([g_, h_], 0)
    sol = splu(full_mat).solve(full_res)
    # sol = spsolve(full_mat, full_res)

    dx = sol[:nz]
    ds = sol[nz:nz+nineq]
    dz = sol[nz+nineq:nz+nineq+nineq]
    dy = sol[nz+nineq+nineq:] if neq > 0 else None

    dx = torch.DoubleTensor(dx).unsqueeze(0)
    ds = torch.DoubleTensor(ds).unsqueeze(0)
    dz = torch.DoubleTensor(dz).unsqueeze(0)
    dy = torch.DoubleTensor(dy).unsqueeze(0) if neq > 0 else None

    return dx, ds, dz, dy 
开发者ID:locuslab,项目名称:lcp-physics,代码行数:29,代码来源:dev_pdipm.py

示例12: sparse_factor_solve_kkt

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def sparse_factor_solve_kkt(Q_tilde, D_tilde, A_, C_tilde, rx, rs, rz, ry, ns):
    nineq, nz, neq, nBatch = ns

    # H_ = csc_matrix((nz + nineq, nz + nineq))
    # H_[:nz, :nz] = Q_tilde
    # H_[-nineq:, -nineq:] = D_tilde
    H_ = block_diag([Q_tilde, D_tilde], format='csc')
    if neq > 0:
        g_ = torch.cat([rx, rs], 1).squeeze(0).numpy()
        h_ = torch.cat([rz, ry], 1).squeeze(0).numpy()
    else:
        g_ = torch.cat([rx, rs], 1).squeeze(0).numpy()
        h_ = rz.squeeze(0).numpy()

    H_LU = splu(H_)

    invH_A_ = csc_matrix(H_LU.solve(A_.todense().transpose()))
    invH_g_ = H_LU.solve(g_)

    S_ = A_.dot(invH_A_) + C_tilde
    S_LU = splu(S_)
    # t_ = invH_g_[np.newaxis].dot(A_.transpose()).squeeze(0) - h_
    t_ = A_.dot(invH_g_) - h_
    w_ = -S_LU.solve(t_)
    # t_ = -g_ - w_[np.newaxis].dot(A_).squeeze(0)
    t_ = -g_ - A_.transpose().dot(w_)
    v_ = H_LU.solve(t_)

    dx = v_[:nz]
    ds = v_[nz:]
    dz = w_[:nineq]
    dy = w_[nineq:] if neq > 0 else None

    dx = torch.DoubleTensor(dx).unsqueeze(0)
    ds = torch.DoubleTensor(ds).unsqueeze(0)
    dz = torch.DoubleTensor(dz).unsqueeze(0)
    dy = torch.DoubleTensor(dy).unsqueeze(0) if neq > 0 else None

    return dx, ds, dz, dy 
开发者ID:locuslab,项目名称:lcp-physics,代码行数:41,代码来源:dev_pdipm.py

示例13: test_preconditioner

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def test_preconditioner(self):
        # Check that preconditioning works
        pc = splu(Am.tocsc())
        M = LinearOperator(matvec=pc.solve, shape=A.shape, dtype=A.dtype)

        x0, count_0 = do_solve()
        x1, count_1 = do_solve(M=M)

        assert_equal(count_1, 3)
        assert_(count_1 < count_0/2)
        assert_(allclose(x1, x0, rtol=1e-14)) 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:13,代码来源:test_gcrotmk.py

示例14: laplaceTria

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def laplaceTria(v, t, k=10):
    """
    Compute linear finite-element method Laplace-Beltrami spectrum
    """
    from scipy.sparse.linalg import LinearOperator, eigsh, splu
    useCholmod = True
    try:
        from sksparse.cholmod import cholesky
    except ImportError:
        useCholmod = False
    if useCholmod:
        print("Solver: cholesky decomp - performance optimal ...")
    else:
        print("Package scikit-sparse not found (Cholesky decomp)")
        print("Solver: spsolve (LU decomp) - performance not optimal ...")
    # import numpy as np
    # from shapeDNA import computeABtria
    A, M = computeABtria(v, t, lump=True)
    # turns out it is much faster to use cholesky and pass operator
    sigma = -0.01
    if useCholmod:
        chol = cholesky(A - sigma * M)
        OPinv = LinearOperator(matvec=chol, shape=A.shape, dtype=A.dtype)
    else:
        lu = splu(A - sigma * M)
        OPinv = LinearOperator(matvec=lu.solve, shape=A.shape, dtype=A.dtype)
    eigenvalues, eigenvectors = eigsh(A, k, M, sigma=sigma, OPinv=OPinv)
    return eigenvalues, eigenvectors 
开发者ID:Deep-MI,项目名称:FastSurfer,代码行数:30,代码来源:spherically_project.py

示例15: __init__

# 需要导入模块: from scipy.sparse import linalg [as 别名]
# 或者: from scipy.sparse.linalg import splu [as 别名]
def __init__(self, A):
    LOGGER.debug(
      'computing the LU decomposition of a %s by %s sparse matrix with %s '
      'nonzeros ' % (A.shape + (A.nnz,)))
    self.factor = spla.splu(A) 
开发者ID:treverhines,项目名称:RBF,代码行数:7,代码来源:linalg.py


注:本文中的scipy.sparse.linalg.splu方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。