Example #1
0
def test_cholesky(size, n_threads):
    X = np.random.rand(size + 1, size)
    x_t_x = np.cov(X.T)
    x_t_x_2 = np.copy(x_t_x)
    print "About to start cholesky"
    start = time.time()
    cholesky.cholesky(x_t_x, n_threads)
    #ch_x = np.asarray(cholesky.cholesky(x_t_x, n_threads)).T
    print "Cholesky time for size: ", size, time.time() - start
    start = time.time()
    real_ch_x = np.linalg.cholesky(x_t_x_2)
    print "Theirs took: ", time.time() - start
    assert(np.allclose(x_t_x.T, real_ch_x))
Example #2
0
def parallel_cholesky_linear_regressiion(x_t_x,x_t_y):
    """
    calls the paralleized Cholesky written in Cython/OpenMP 
    """
    L = cholesky.cholesky(x_t_x)
    z = np.linalg.solve(L,x_t_y)    
    theta = np.linalg.solve(np.transpose(L),z)
    return theta
Example #3
0
 def runTest(self):
     aa = list(
         map(lambda xs: list(map(Fraction, xs)),
             [[5, 3, 3], [3, 3, 3], [3, 3, 8]]))
     m = np.array(aa)
     d, a = cholesky.cholesky(m)
     atma = a.transpose().dot(m).dot(a)
     self.assertEqual(sum((atma - d).ravel()), Fraction(0, 1))
def ResolCholesky(A, B):
    n, m = np.shape(A)
    n2, m2 = np.shape(B)
    L = cholesky.cholesky(A)
    L2 = L.T
    Laug = np.concatenate((L, B), axis=1)
    #print(Laug)
    Y = np.reshape(ResolutionSystTriInferieur.ResolutionSystTriInferieur(Laug),
                   (n2, 1))
    #print(Y)
    T = np.concatenate((L2, Y), axis=1)
    #print(T)
    X = ResolutionSystTriSup.ResolutionSystTriSup(T)
    return (X)
Example #5
0
    def predict(self, X_):
        kern = kernels.Kernels()

        K_a = kern.kern_matrix(self.X, self.X, self.func, self.lam)
        K_b = kern.kern_matrix(self.X, X_, self.func)
        K_c = kern.kern_matrix(X_, self.X, self.func)
        K_d = kern.kern_matrix(X_, X_, self.func, self.lam)

        m = np.dot(np.dot(K_c, np.linalg.inv(K_a)), self.Y)
        m = np.reshape(m, (np.size(m), 1))

        C = K_d - np.dot(np.dot(K_c, np.linalg.inv(K_a)), K_b)
        L = ch.cholesky(C)

        return m, L
Example #6
0
def differences_finies(N, a, b, f):

    #pas de subdivision
    h = (b - a) / N

    # segment x
    x = np.linspace(a, b, N + 1)

    # Discretisation
    xi = np.array([(a + (i * h)) for i in range(0, N)])

    #resoudre Au=b

    #construction matrice tridiagonale
    A = np.zeros((N - 1, N - 1))

    for i in range(N - 1):
        for j in range(N - 1):
            if (i == j):
                A[i][j] = 2
            if (j == i + 1):
                A[i][j] = -1
            if (i == j + 1):
                A[i][j] = -1

    #construction vecteur_f
    vecteur_f = np.zeros((N - 1, 1))
    for i in range(N - 1):
        vecteur_f[i] = f(xi[i])

    #Résolution de l'équation linéaire à l'aide de Cholesky
    t = time.time()
    U = cholesky.cholesky((1 / h**2) * A, vecteur_f)
    print(U)
    elapsed = time.time() - t
    print("temps écoulé Cholesky ", elapsed)

    #Résolution avec la factorisation LU
    t = time.time()
    U = lusolve.lusolve((1 / h**2) * A, vecteur_f)
    print(U)
    elapsed = time.time() - t
    print("temps écoulé méthode LU ", elapsed)
Example #7
0
def differences_finies(N, a, b, f):

    #pas de subdivision
    h = (b - a) / N

    # segment x
    x = np.linspace(a, b, N - 1)

    # Discretisation
    xi = np.array([(a + (i * h)) for i in range(0, N - 1)])

    #resoudre Au=b

    #construction matrice tridiagonale
    A = np.zeros((N - 1, N - 1))

    for i in range(N - 1):
        for j in range(N - 1):
            if (i == j):
                A[i][j] = 2 * (1 + h**2)
            elif (j == i + 1):
                A[i][j] = -1
            elif (i == j + 1):
                A[i][j] = -1

    #construction vecteur_f
    vecteur_f = np.zeros((N - 1, 1))
    for i in range(N - 1):
        if (i == 0):
            vecteur_f[i] = f(xi[i]) + 1 / h**2
        elif (i == N - 2):
            vecteur_f[i] = f(xi[i]) + exp(1) / h**2
        else:
            vecteur_f[i] = f(xi[i])

    #Résolution de l'équation linéaire à l'aide de Cholesky
    U = cholesky.cholesky((1 / h**2) * A, vecteur_f)
    #print(U)
    U_exact = [f(xi[i]) for i in range(0, N - 1)]
    #print(U_exact)
    return U, U_exact, x, xi
Example #8
0
            for k in range(n):
                if(k!=i):
                    sum += abs(A[i][k])
        if(sum > abs(A[i][i])):
                return False
    return True

testDiagonaleDominante = diagonaleDominante(A)
print("A est elle à diagonale dominante?")
print(testDiagonaleDominante)


##Résolution de Ax=b avec la méthode Cholesky

print("Méthode de Cholesky")
x = cholesky.cholesky(A,b)
print("valeur obtenue de x")
print(x)

##Vérification résultat
test = np.linalg.solve(A, b)
print("valeur réelle résolution linéaire")
print(test)

##Résolution de Ax=b avec la méthode de Gauss-Seidel
print("Méthode de Gauss-Seidel")
x0 = np.ones((10,1))
x = gaussseidel(A,b,Imax,errSeuil,x0)
print("valeur obtenue")
print(x)
Example #9
0
def stiffnessmatrix(element, node, support, supportinfo):
    #หา existnode

    constrained = []
    unconstrained = []
    a = []
    n = []
    equation2 = {}
    numsupportforce = 0

    degreefreedom = len(node) * 2
    g = (len(node) - len(support)) * 2
    z = len(node)
    L = [[0] * 2 for i in range(z)]
    k = 0
    print(len(node), "len all node")
    for i in range(len(node)):
        if node[i] not in support:
            L[i][0] = k * 2
            L[i][1] = k * 2 + 1
            k = k + 1
    for x in range(len(support)):
        if supportinfo["type"][x] == 2:
            numsupportforce = numsupportforce + 1
            if supportinfo["constrained"][x] == "xr" or supportinfo[
                    "constrained"][x] == "xl":
                constrained.append((x, 0))
                unconstrained.append((x, 1))
            elif supportinfo["constrained"][x] == "y":
                unconstrained.append((x, 0))
                constrained.append((x, 1))
        elif supportinfo["type"][x] == 1:
            numsupportforce = numsupportforce + 2
            constrained.append((x, 0))
            constrained.append((x, 1))
    for z, b in unconstrained:
        y = node.index(support[z])
        L[y][b] = g
        g = g + 1
    for j, r in constrained:
        y = node.index(support[j])
        L[y][r] = g
        g = g + 1

    print(L, "L")

    for x in range(degreefreedom):
        for i in range(degreefreedom):
            equation2.setdefault((x, i), []).append(0)

    print(equation2, "equation2")
    for q in range(len(element)):
        print(q, "q")
        Nearendnode = element[q][0]
        Farendnode = element[q][1]

        Nx = L[Nearendnode][0]
        Ny = L[Nearendnode][1]
        Fx = L[Farendnode][0]
        Fy = L[Farendnode][1]

        X = node[Farendnode][0] - node[Nearendnode][0]
        Y = node[Farendnode][1] - node[Nearendnode][1]
        Magnitude = ((X * X) + (Y * Y))**0.5
        lambdaX = X / Magnitude
        lambdaY = Y / Magnitude
        print(Fx, "Fx")
        print(Fy, "Fy")
        print(Nx, "Nx")
        print(Ny, "Ny")
        print(lambdaX, "lamdaX value")
        print(lambdaY, "lambdaY value")
        print(Magnitude, "Magnitude value")
        equation2[(
            Nx, Nx)][-1] = equation2[(Nx, Nx)][-1] + (lambdaX**2) / Magnitude
        equation2[(Ny, Nx)][-1] = equation2[
            (Ny, Nx)][-1] + (lambdaX * lambdaY) / Magnitude
        equation2[(
            Fx, Nx)][-1] = equation2[(Fx, Nx)][-1] - (lambdaX**2) / Magnitude
        equation2[(Fy, Nx)][-1] = equation2[
            (Fy, Nx)][-1] - (lambdaX * lambdaY) / Magnitude
        equation2[(Nx, Ny)][-1] = equation2[
            (Nx, Ny)][-1] + (lambdaX * lambdaY) / Magnitude
        equation2[(
            Ny, Ny)][-1] = equation2[(Ny, Ny)][-1] + (lambdaY**2) / Magnitude
        equation2[(Fx, Ny)][-1] = equation2[
            (Fx, Ny)][-1] - (lambdaX * lambdaY) / Magnitude
        equation2[(
            Fy, Ny)][-1] = equation2[(Fy, Ny)][-1] - (lambdaY**2) / Magnitude
        equation2[(
            Nx, Fx)][-1] = equation2[(Nx, Fx)][-1] - (lambdaX**2) / Magnitude
        equation2[(Ny, Fx)][-1] = equation2[
            (Ny, Fx)][-1] - (lambdaX * lambdaY) / Magnitude
        equation2[(
            Fx, Fx)][-1] = equation2[(Fx, Fx)][-1] + (lambdaX**2) / Magnitude
        equation2[(Fy, Fx)][-1] = equation2[
            (Fy, Fx)][-1] + (lambdaX * lambdaY) / Magnitude
        equation2[(Nx, Fy)][-1] = equation2[
            (Nx, Fy)][-1] - (lambdaX * lambdaY) / Magnitude
        equation2[(
            Ny, Fy)][-1] = equation2[(Ny, Fy)][-1] - (lambdaY**2) / Magnitude
        equation2[(Fx, Fy)][-1] = equation2[
            (Fx, Fy)][-1] + (lambdaX * lambdaY) / Magnitude
        equation2[(
            Fy, Fy)][-1] = equation2[(Fy, Fy)][-1] + (lambdaY**2) / Magnitude

        #ตัดให้เหลือ K11 อิงจา่ก จำนวน reactionatsupport

    i = len(node) * 2 - numsupportforce

    for row in range(i):
        s = []
        for column in range(i):
            s.append(equation2[row, column][-1])
        a.append(s)

    print(a, "a")
    result = cholesky.cholesky(a)
    return result