Ejemplo n.º 1
0
 def test_diag(self):
     a = (100 * get_mat(5)).astype('f')
     b = a.copy()
     for k in range(5):
         for l in range(k + 3, 5):
             b[k, l] = 0
     assert_equal(tril(a, k=2), b)
     b = a.copy()
     for k in range(5):
         for l in range(max((k - 1, 0)), 5):
             b[k, l] = 0
     assert_equal(tril(a, k=-2), b)
Ejemplo n.º 2
0
 def test_diag(self):
     a = (100*get_mat(5)).astype('f')
     b = a.copy()
     for k in range(5):
         for l in range(k+3,5):
             b[k,l] = 0
     assert_equal(tril(a,k=2),b)
     b = a.copy()
     for k in range(5):
         for l in range(max((k-1,0)),5):
             b[k,l] = 0
     assert_equal(tril(a,k=-2),b)
Ejemplo n.º 3
0
 def test_basic(self):
     a = (100 * get_mat(5)).astype('l')
     b = a.copy()
     for k in range(5):
         for l in range(k + 1, 5):
             b[k, l] = 0
     assert_equal(tril(a), b)
Ejemplo n.º 4
0
def iterative():
    N, h, neighs, atoms, nc, nv, vl = pre.run2()
    del atoms, nc, vl
    lp = pro.laplace(N, h, neighs)
    del neighs
    A = lp.tolil()
    del lp
    B = np.zeros(N)
    for i in range(N):
        A[0, i] = 0.0
        B[i] = -4 * np.pi * nv[i]
    del nv
    A[0, 0] = 1.0
    A = A.toarray()
    B[0] = 0.0
    Au = la.triu(A)
    Al = la.tril(A)
    Ad = np.diag(np.diag(A))
    ##    for i in range(N):
    ##        for j in range(N):
    ##            if A[i][j] != Au[i][j]+Al[i][j]-Ad[i][j]:
    ##                print(i, j)
    x = np.zeros(N)
    T = 1000
    RJ = np.dot(la.inv(Ad), A) + np.identity(N)
    RGS = -np.dot(la.inv(Al - Ad), Au)
    val, vec = la.eig(RGS)
    maxx = 0.0
    for i in range(len(val)):
        if abs(val[i]) > maxx:
            maxx = abs(val[i])
    print(maxx)
Ejemplo n.º 5
0
 def test_basic(self):
     a = (100*get_mat(5)).astype('l')
     b = a.copy()
     for k in range(5):
         for l in range(k+1,5):
             b[k,l] = 0
     assert_equal(tril(a),b)
Ejemplo n.º 6
0
    def schur(self, A, schur_list, full_matrix=True):
        """
        Setup the solver object to work in Schur complement mode

        full_matrix: boolean, default True. If full_matrix is False
        and a symmetric factorization is used, only the lower part of
        the Schur matrix is stored in self.S
        """

        self.A = A
        self.spmA = spm.spmatrix(A)
        if self.verbose:
            self.spmA.printInfo()

        self.schur_list = schur_list + self.spmA.findBase()
        setSchurUnknownList(self.pastix_data, self.schur_list)

        task_analyze(self.pastix_data, self.spmA)
        task_numfact(self.pastix_data, self.spmA)

        nschur = len(schur_list)
        self.S = np.zeros((nschur, nschur), order='F', dtype=A.dtype)
        getSchur(self.pastix_data, self.S)
        if full_matrix and (self.factotype != factotype.LU):
            self.S += la.tril(self.S, -1).T
Ejemplo n.º 7
0
def twogrid(FA,Fb,Fnu1,Fnu2,Fgamma,Fuu):
        nn = len(Fb)
        G = [[0.0] * (nn+1) for _ in [0.0]* (nn+1)]
#    ltemp = 0
        G = scipy.sparse.eye(nn,nn) - (inv(tril(FA))*FA)
#    G = scipy.sparse.eye(nn,nn) 
        print G
#   cG = inv(tril(FA))*Fb
        return Fuu
Ejemplo n.º 8
0
def LU(A):
	(m,n) = A.shape
	U = deepcopy(A)
	P = np.eye(m)
	L = np.eye(m)

	for i in range(m):
		# do the pivot
		max_j = i + np.argmax(np.abs(U[i:,i]))

		# make the pivot matrix
		Pi = np.eye(m)
		Pi[i,i] = 0
		Pi[max_j,max_j] = 0
		Pi[i,max_j] = 1
		Pi[max_j,i] = 1

		# do the pivot 
		U = Pi @ U

		# add the pivot to P
		P = P @ Pi

		# add the pivot to L
		L= Pi @ L @ Pi

		# make the eliminator Linv
		Li = np.eye(m)
		Li[(i+1):m,i] = -U[(i+1):m,i]/U[i,i]

		# make the inverse of the eliminator
		Linv = np.eye(m)
		Linv[(i+1):m,i] = U[(i+1):m,i]/U[i,i]

		# do the elimination (we already did the pivot)
		U = Li @ U

		# add the inverse-eliminator to L
		L = L @ Linv

	L = linalg.tril(L)
	U = linalg.triu(U)

	return (P,L,U)
def GaussSeidel(A, b, tolerance = 1.e-10, MaxSteps = 100):
    """Solve the linear system A x = b using the Gauss-Seidel method, 
    starting from the trivial initial guess."""
    
    x = np.zeros_like(b)
    
    Anorm = A.copy()
    bnorm = b.copy()
    n = len(b)
    
    for i in range(n):
        bnorm[i] /= A[i, i]
        Anorm[i, :] /= A[i, i]
    
    # Compute the split
    D = np.eye(n)
    AL = la.tril(D - Anorm)
    AU = la.triu(D - Anorm)
    N = np.eye(n) - AL
    P = AU
    
    # Compute the convergence matrix and check its spectral radius
    M = np.dot(la.inv(N), P)
    eigenvalues, eigenvectors = la.eig(M)
    rho = np.amax(np.absolute(eigenvalues))
    if (rho > 1):
        print("Gauss-Seidel will not converge as the"\
              " largest eigenvalue of the convergence matrix is {}".format(rho))
    
    for j in range(MaxSteps):
        x_old = x.copy()
        for i in range(n):
            x[i] = bnorm[i] + np.dot(AL[i, :], x) + np.dot(AU[i, :], x_old)
        if (la.norm(x - x_old) < tolerance):
            print("Gauss-Seidel converged in {} iterations.".format(j))
            break
    
    return x
Ejemplo n.º 10
0
def GaussSeidel(A, b, tolerance=1.e-10, MaxSteps=100):
    """Solve the linear system A x = b using the Gauss-Seidel method, 
    starting from the trivial initial guess."""

    x = np.zeros_like(b)

    Anorm = A.copy()
    bnorm = b.copy()
    n = len(b)

    for i in range(n):
        bnorm[i] /= A[i, i]
        Anorm[i, :] /= A[i, i]

    # Compute the split
    D = np.eye(n)
    AL = la.tril(D - Anorm)
    AU = la.triu(D - Anorm)
    N = np.eye(n) - AL
    P = AU

    # Compute the convergence matrix and check its spectral radius
    M = np.dot(la.inv(N), P)
    eigenvalues, eigenvectors = la.eig(M)
    rho = np.amax(np.absolute(eigenvalues))
    if (rho > 1):
        print("Gauss-Seidel will not converge as the"\
              " largest eigenvalue of the convergence matrix is {}".format(rho))

    for j in range(MaxSteps):
        x_old = x.copy()
        for i in range(n):
            x[i] = bnorm[i] + np.dot(AL[i, :], x) + np.dot(AU[i, :], x_old)
        if (la.norm(x - x_old) < tolerance):
            print("Gauss-Seidel converged in {} iterations.".format(j))
            break

    return x
def Jacobi(A, b, tolerance = 1.e-10, MaxSteps = 100):
    """Solve the linear system A x = b using Jacobi's method, 
    starting from the trivial initial guess."""
    
    x = np.zeros_like(b)
    
    Anorm = A.copy()
    bnorm = b.copy()
    n = len(b)
    
    for i in range(n):
        bnorm[i] /= A[i, i]
        Anorm[i, :] /= A[i, i]
    
    # Compute the split
    N = np.eye(n)
    P = N - Anorm
    AL = la.tril(P)
    AU = la.triu(P)
    
    # Compute the convergence matrix and check its spectral radius
    M = np.dot(la.inv(N), P)
    eigenvalues, eigenvectors = la.eig(M)
    rho = np.amax(np.absolute(eigenvalues))
    if (rho > 1):
        print("Jacobi will not converge as the"\
            " largest eigenvalue of the convergence matrix is {}".format(rho))
    
    for j in range(MaxSteps):
        x_old = x.copy()
        x = bnorm + np.dot(AL + AU, x)
        if (la.norm(x - x_old) < tolerance):
            print "Jacobi converged in ", j, " iterations."
            break
    
    return x
Ejemplo n.º 12
0
def Jacobi(A, b, tolerance=1.e-10, MaxSteps=100):
    """Solve the linear system A x = b using Jacobi's method, 
    starting from the trivial initial guess."""

    x = np.zeros_like(b)

    Anorm = A.copy()
    bnorm = b.copy()
    n = len(b)

    for i in range(n):
        bnorm[i] /= A[i, i]
        Anorm[i, :] /= A[i, i]

    # Compute the split
    N = np.eye(n)
    P = N - Anorm
    AL = la.tril(P)
    AU = la.triu(P)

    # Compute the convergence matrix and check its spectral radius
    M = np.dot(la.inv(N), P)
    eigenvalues, eigenvectors = la.eig(M)
    rho = np.amax(np.absolute(eigenvalues))
    if (rho > 1):
        print("Jacobi will not converge as the"\
            " largest eigenvalue of the convergence matrix is {}".format(rho))

    for j in range(MaxSteps):
        x_old = x.copy()
        x = bnorm + np.dot(AL + AU, x)
        if (la.norm(x - x_old) < tolerance):
            print "Jacobi converged in ", j, " iterations."
            break

    return x
Ejemplo n.º 13
0
x = np.linspace(0.0, 1.0, np1)
y = np.linspace(0.0, 1.0, np1)

F = np.zeros((nm1, nm1))
for i in range(nm1):
    F[0, i] += left(y[i + 1]) / h2
    F[nm2, i] += right(y[i + 1]) / h2
    F[i, 0] += bottom(x[i + 1]) / h2
    F[i, nm2] += top(x[i + 1]) / h2
    for j in range(nm2):
        F[i, j] += f(x[i + 1], y[i + 1])

F *= h2

D = np.ones((nm1, nm1))
D = 5 * np.eye(nm1) - sl.triu(sl.tril(D, 1), -1)

start = t.time()
U = sweep(nm1, D, F).transpose()
end = t.time()
print('time = ', end - start, sep='')


def ua(x, y):
    return x * x * y + y * y * x


Ua = np.zeros((np1, np1))
for i in range(np1):
    for j in range(np1):
        Ua[i, j] = ua(x[i], y[j])
Ejemplo n.º 14
0
import numpy as np
import scipy.linalg as sl

A = np.arange(16).reshape(4, 4)
print('A: \n', A)
# k = 0 时,保留对角线元素,对角线上方的所有元素格式化为0
print('tril(A, 0): \n', sl.tril(A))
# k = -1 时,包括对角线元素都被格式化为0
print('tril(A, -1): \n', sl.tril(A, -1))
# k = 1 时,边界上移到对角线上方的一格
print('tril(A, 1): \n', sl.tril(A, 1))

# triu 与 tril相反
# k = 0 时,保留对角线元素,对角线下方的所有元素格式化为0
print('triu(A, 0): \n', sl.triu(A))
# k = 1 时,包括对角线元素都被格式化为0
print('triu(A, 1): \n', sl.triu(A, 1))
# k = -1 时,边界下移到对角线下方的一格
print('triu(A, -1): \n', sl.triu(A, -1))

# 不管是tril还是triu,k的正值永远是把边界往上移,为负时往下移
Ejemplo n.º 15
0
## Kronecker product
## =================

kron = linalg.kron(a, a_inv)
print(kron)

##
## Others
## ======

# (1): tril
# ---

# Make a copy of a matrix with elements above the k-th diagonal zeroed.
# k == 0 is the main diagonal, k < 0 subdiagonal and k > 0 superdiagonal.
print(linalg.tril(kron))

print(linalg.tril(kron, k=-1))
print(linalg.tril(kron, k=1))

# (2): triu
# ---

# Make a copy of a matrix with elements above the k-th diagonal zeroed.
# k == 0 is the main diagonal, k < 0 subdiagonal and k > 0 superdiagonal.
print(linalg.triu(kron))

print(linalg.triu(kron, k=-1))
print(linalg.triu(kron, k=1))

###################################################################################################
Ejemplo n.º 16
0
schurlist = np.arange(nschur) + spmA.findBase()
pastix.setSchurUnknownList(pastix_data, schurlist)

# Perform analyze
pastix.task_analyze(pastix_data, spmA)

# Perform numerical factorization
pastix.task_numfact(pastix_data, spmA)

# Get the Schur complement
S = np.array(np.zeros((nschur, nschur)), order='F', dtype=spmA.dtype)
pastix.getSchur(pastix_data, S)

# Store both sides for linalg
if factotype != pastix.factotype.LU:
    S += la.tril(S, -1).T

# 1- Apply P to b
pastix.subtask_applyorder(pastix_data, pastix.dir.Forward, x)

if factotype == pastix.factotype.LU:
    # 2- Forward solve on the non Schur complement part of the system
    pastix.subtask_trsm(pastix_data, pastix.side.Left, pastix.uplo.Lower,
                        pastix.trans.NoTrans, pastix.diag.Unit, x)

    # 3- Solve the Schur complement part
    x[-nschur:] = la.solve(S, x[-nschur:], sym_pos=False)

    # 4- Backward solve on the non Schur complement part of the system
    pastix.subtask_trsm(pastix_data, pastix.side.Left, pastix.uplo.Upper,
                        pastix.trans.NoTrans, pastix.diag.NonUnit, x)