Example #1
0
def solve_gen_eig_prob(A, B, eps=1e-6):
    """
    Solves the generalised eigenvalue problem of the form:
    Aw = \lambda*Bw

    Note: can be validated against `scipy.linalg.eig(A, b=B)`

    Ref:
    'Eigenvalue and Generalized Eigenvalue Problems: Tutorial (2019)'
    Benyamin Ghojogh and Fakhri Karray and Mark Crowley
    arXiv 1903.11240

    """
    Lam_b, Phi_b = np.linalg.eig(B)  # eig decomp of B alone
    Lam_b = np.eye(
        len(Lam_b)) * Lam_b  # convert to diagonal matrix of eig vals

    Lam_b_sq = replace_nan(Lam_b**0.5) + np.eye(len(Lam_b)) * eps
    Phi_b_hat = np.dot(Phi_b, np.linalg.inv(Lam_b_sq))
    A_hat = np.dot(np.dot(Phi_b_hat.transpose(), A), Phi_b_hat)
    Lam_a, Phi_a = np.linalg.eig(A_hat)
    Lam_a = np.eye(len(Lam_a)) * Lam_a

    Lam = Lam_a
    Phi = np.dot(Phi_b_hat, Phi_a)

    return np.diag(Lam), Phi
Example #2
0
def solve_eig_qr(A, iterations=30):
    """
    Use the QR iteration algorithm to iteratively solve for the eigenvectors and eigenvalues
    of a matrix A. Note: only guaranteed to recover exactly for symmetric matrices
    with real eigenvalues. May work partially for asymmetric matrices (no complex support yet).

    Returns:
        `lam`: vector of eigenvalues
        `Q_bar`: matrix of eigenvectors (columns)
    """

    Ak = A
    Q_bar = np.eye(len(Ak))

    for _ in range(iterations):
        Qk, Rk = np.linalg.qr(Ak)
        Ak = np.dot(Rk, Qk)
        Q_bar = np.dot(Q_bar, Qk)

    lam = np.diag(Ak)
    return lam, Q_bar
Example #3
0
from ulab import numpy as np

print(np.eye(3))
Example #4
0
c = np.array([[19, 22], [43, 50]])
matrix_is_close(np.dot(a, b), c, 2)

c = np.array([[26, 30], [38, 44]])
matrix_is_close(np.dot(a.transpose(), b), c, 2)

c = np.array([[17, 23], [39, 53]])
matrix_is_close(np.dot(a, b.transpose()), c, 2)

c = np.array([[23, 31], [34, 46]])
matrix_is_close(np.dot(a.transpose(), b.transpose()), c, 2)

a = np.array([[1., 2.], [3., 4.]])
b = np.linalg.inv(a)
ab = np.dot(a, b)
c = np.eye(2)
matrix_is_close(ab, c, 2)

a = np.array([[1, 2, 3, 4], [4, 5, 6, 4], [7, 8.6, 9, 4], [3, 4, 5, 6]])
b = np.linalg.inv(a)
ab = np.dot(a, b)
c = np.eye(4)
matrix_is_close(ab, c, 4)

a = np.array([[1, 2, 3, 4], [4, 5, 6, 4], [7, 8.6, 9, 4], [3, 4, 5, 6]])
result = (np.linalg.det(a))
ref_result = 7.199999999999995
print(math.isclose(result, ref_result, rel_tol=1E-9, abs_tol=1E-9))

a = np.array([1, 2, 3])
w, v = np.linalg.eig(np.diag(a))
Example #5
0
from ulab import numpy as np

print('isinf(0): ', np.isinf(0))

a = np.array([1, 2, np.nan])
print('\n' + '=' * 20)
print('a:\n', a)
print('\nisinf(a):\n', np.isinf(a))

b = np.array([1, 2, np.inf])
print('\n' + '=' * 20)
print('b:\n', b)
print('\nisinf(b):\n', np.isinf(b))

c = np.array([1, 2, 3], dtype=np.uint16)
print('\n' + '=' * 20)
print('c:\n', c)
print('\nisinf(c):\n', np.isinf(c))

d = np.eye(5) * 1e999
print('\n' + '=' * 20)
print('d:\n', d)
print('\nisinf(d):\n', np.isinf(d))
Example #6
0
try:
    from ulab import numpy as np
except:
    import numpy as np


def print_as_buffer(a):
    print(len(memoryview(a)), list(memoryview(a)))


print_as_buffer(np.ones(3))
print_as_buffer(np.zeros(3))
print_as_buffer(np.eye(4))
print_as_buffer(np.ones(1, dtype=np.int8))
print_as_buffer(np.ones(2, dtype=np.uint8))
print_as_buffer(np.ones(3, dtype=np.int16))
print_as_buffer(np.ones(4, dtype=np.uint16))
print_as_buffer(np.ones(5, dtype=np.float))
print_as_buffer(np.linspace(0, 1, 9))
Example #7
0
print(np.arange(2, 10))
print(np.arange(2, 10, 3))
print(np.arange(2, 10, 3, dtype=np.float))
print("Array concatenation:")
a = np.array([1,2,3], dtype=np.float)
b = np.array([4,5,6], dtype=np.float)
print(np.concatenate((a,b)))
print(np.concatenate((a,b), axis=0))
a = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float)
b = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=np.float)
print(np.concatenate((a,b), axis=0))
print(np.concatenate((a,b), axis=1))
print(np.concatenate((b,a), axis=0))
print(np.concatenate((b,a), axis=1))
print("Identity array creation:")
print(np.eye(3))
print(np.eye(3, M=4))
print(np.eye(3, M=4, k=0))
print(np.eye(3, M=4, k=-1))
print(np.eye(3, M=4, k=-2))
print(np.eye(3, M=4, k=-3))
print(np.eye(3, M=4, k=1))
print(np.eye(3, M=4, k=2))
print(np.eye(3, M=4, k=3))
print(np.eye(4, M=4))
print(np.eye(4, M=3, k=0))
print(np.eye(4, M=3, k=-1))
print(np.eye(4, M=3, k=-2))
print(np.eye(4, M=3, k=-3))
print(np.eye(4, M=3, k=1))
print(np.eye(4, M=3, k=2))
Example #8
0
        if i == j:
            print(math.isclose(v[i][j], 1.0, rel_tol=1E-9, abs_tol=1E-9))
        else:
            print(math.isclose(v[i][j], 0.0, rel_tol=1E-9, abs_tol=1E-9))

a = np.array([[25, 15, -5], [15, 18, 0], [-5, 0, 11]])
result = (np.linalg.cholesky(a))
ref_result = np.array([[5., 0., 0.], [3., 3., 0.], [-1., 1., 3.]])
for i in range(3):
    for j in range(3):
        print(
            math.isclose(result[i][j],
                         ref_result[i][j],
                         rel_tol=1E-9,
                         abs_tol=1E-9))

a = np.array([1, 2, 3, 4, 5], dtype=np.float)
result = (np.linalg.norm(a))
ref_result = 7.416198487095663
print(math.isclose(result, ref_result, rel_tol=1E-9, abs_tol=1E-9))

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
result = (np.linalg.norm(a))  ## Here is a problem
ref_result = 16.881943016134134
print(math.isclose(result, ref_result, rel_tol=1E-6, abs_tol=1E-6))

if use_ulab:
    print(np.linalg.trace(np.eye(3)))
else:
    print(np.trace(np.eye(3)))