Ejemplo n.º 1
0
def user_venue_scores(usr_location_matrix):
    M = usr_location_matrix.as_matrix()
    user_hub__initial_score = usr_location_matrix.sum(axis=1).values
    user_hub_score = power_method.power_method(np.dot(M, M.T), user_hub__initial_score, 10)

    venue_auth_initial_score = usr_location_matrix.sum(axis=0).values
    venue_hub_score = power_method.power_method(np.dot(M.T, M), venue_auth_initial_score, 10)

    return user_hub_score, venue_hub_score
    def testPowerMethod(self):
        # expected value
        eigenvalue = 2 + math.sqrt(13)

        # tolerance
        e = 0.00005
        MAX_ITERATIONS = 20
        result = power_method.power_method(self.A, [1,1], e, MAX_ITERATIONS)
        self.assertTrue(result)
        self.assertTrue(abs(result['value'] - eigenvalue) <= e)
        self.assertTrue(result['iterations'] < MAX_ITERATIONS)
Ejemplo n.º 3
0
def rayleigh_method(matrix, sigma=None, eps=10 ** -6, steps=10 ** 5):
    if sigma is None:
        _, sigma = power_method(matrix, steps=10)
    vector = np.random.rand(matrix.shape[0])
    k = 0
    while k < steps:
        next_vect = scipy.linalg.inv(matrix - sigma * np.identity(matrix.shape[0])) @ vector
        next_vect = norm(next_vect)
        sigma = (next_vect.T @ matrix @ next_vect) / (next_vect.T @ next_vect)
        if np.linalg.norm(abs(vector - next_vect)) < eps:
            break
        vector = next_vect
        k += 1
    return vector, sigma
Ejemplo n.º 4
0
def inv_pow_method(matrix, sigma=None, eps=10**-9, steps=10**9):
    if sigma is None:
        _, sigma = power_method(matrix, steps=10)
    vector = np.random.rand(matrix.shape[0])
    vector = norm(vector)
    k = 0
    A = (matrix - sigma * np.identity(matrix.shape[0]))
    lu_piv = lu_factor(A)
    while k < steps:
        next_vect = lu_solve(lu_piv, vector)
        next_vect = norm(next_vect)
        if np.linalg.norm(abs(next_vect) - abs(vector)) < eps:
            break
        vector = next_vect
        k += 1
        sigma = vector.T @ matrix @ vector
    return norm(vector), sigma
Ejemplo n.º 5
0
from power_method import power_method


def inv_pow_method(matrix, sigma=None, eps=10**-9, steps=10**9):
    if sigma is None:
        _, sigma = power_method(matrix, steps=10)
    vector = np.random.rand(matrix.shape[0])
    vector = norm(vector)
    k = 0
    A = (matrix - sigma * np.identity(matrix.shape[0]))
    lu_piv = lu_factor(A)
    while k < steps:
        next_vect = lu_solve(lu_piv, vector)
        next_vect = norm(next_vect)
        if np.linalg.norm(abs(next_vect) - abs(vector)) < eps:
            break
        vector = next_vect
        k += 1
        sigma = vector.T @ matrix @ vector
    return norm(vector), sigma


if __name__ == '__main__':
    matrix = rand_sym_matrix(100)
    _, sigma = power_method(matrix, steps=10)

    vector, dominant = inv_pow_method(matrix, sigma)
    print(vector)
    print(dominant)
    lib_vector, lib_dominant = lib_solution(matrix)
    check_correct(vector, dominant, lib_vector, lib_dominant)
Ejemplo n.º 6
0
from DecomposicaoLU import pivot_matrix
import DecomposicaoCholesky
from power_method import power_method
from power_method import power_method_inverse
from power_method import power_method_shifted
from QRdecomposition import QRdecomposition
from QRdecomposition import QReigenvalues

import numpy as np
import os
import scipy.linalg

#A = np.asarray([[6.0, 3, 4, 8], [3, 6, 5,1], [4, 5, 10, 7], [8, 1, 7, 25]])
#A = np.asarray([ [7.0, 3, -1, 2], [3, 8, 1, -4], [9, 9, 4, -1], [2, 10, -1, 6] ])
#A = np.asarray([ [7.0, 3, -1, 2], [3, 8, 1, -4], [-1, 1, 4, -1]])
#A = np.asarray([ [7.0, 3, -1, 2, 5], [3, 8, 1, -4,6], [-1,8, 1, 4, -1]])
#A = np.asarray([[4.0, 1, -1], [3, 2, -3], [1, 3, 0]])
A = np.asarray([[6.0, 3, 4, 8], [3, 6, 5, 1], [4, 5, 10, 7], [8, 1, 7, 25]])
#A = np.asarray([[0,1.0],[-2,-3]])
L,U,P = LU(A)


C,Ct = DecomposicaoCholesky.cholesky(A)
eigenvectormax,maxeigenvalue = power_method(A)
#eigenvalues = power_method_shifted(A)

eigenvectormin,mineigenvalue = power_method_inverse(A)

Q,R = QRdecomposition(A)
eigenvaluesA = QReigenvalues(A)