Gaussian Elimination Row operations


In [12]:
import numpy as np
from functools import reduce

In [180]:
a = np.array([
    [0, 1, -1, -1],
    [3, -1, 1, 4],
    [1, 1, -2, -3]
])

In [11]:
p @ a


Out[11]:
array([[ 1,  1, -2, -3],
       [ 0,  1, -1, -1],
       [ 3, -1,  1,  4]])

In [88]:
def basis_vector(size, index):
    return np.eye(1, size, index).T

def multiply(ops, a):
    return reduce(lambda x, y: x @ y, ops + [a])

In [158]:
ops = [
    np.diag([1, 1, 1/3]),
    np.eye(3) + 2 / 3 * basis_vector(size=3, index=0) @ basis_vector(size=3, index=2).T,  # I - 3e1e3
    np.eye(3) - 1 * basis_vector(size=3, index=0) @ basis_vector(size=3, index=1).T,  # I - 3e1e2
    np.eye(3) + 1 / 3 * basis_vector(size=3, index=1) @ basis_vector(size=3, index=2).T,  # I - 3e2e3
    np.eye(3) + 4 * basis_vector(size=3, index=2) @ basis_vector(size=3, index=1).T,  # I - 3e3e2
    np.eye(3) - 3 * basis_vector(size=3, index=2) @ basis_vector(size=3, index=0).T,  # I - 3e3e1
    p
]

In [161]:
np.round(multiply(ops, a), 2)


Out[161]:
array([[ 1.,  0., -0.,  1.],
       [ 0.,  1.,  0.,  2.],
       [ 0.,  0.,  1.,  3.]])

In [162]:
# matrix inverse
multiply(ops, np.eye(3))


Out[162]:
array([[ 3.33333333e-01,  3.33333333e-01,  5.55111512e-17],
       [ 2.33333333e+00,  3.33333333e-01, -1.00000000e+00],
       [ 1.33333333e+00,  3.33333333e-01, -1.00000000e+00]])

In [ ]:


In [ ]:


In [201]:
p = np.array([
    [0, 1, 0],
    [0, 0, 1],
    [1, 0, 0]
])

In [202]:
l = np.eye(3) - 1 * basis_vector(size=3, index=1) @ basis_vector(size=3, index=0).T
l


Out[202]:
array([[ 1.,  0.,  0.],
       [-1.,  1.,  0.],
       [ 0.,  0.,  1.]])

In [203]:
l @ np.diag([1/3, 1, 1]) @ p @ a


Out[203]:
array([[ 1.        , -0.33333333,  0.33333333,  1.33333333],
       [ 0.        ,  1.33333333, -2.33333333, -4.33333333],
       [ 0.        ,  1.        , -1.        , -1.        ]])

In [206]:
a = np.array([
    [1, 0, 0],
    [-1, 1, 0],
    [0, 0, 1]
])

b = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [-2, 0, 1]
])

a @ b


Out[206]:
array([[ 1,  0,  0],
       [-1,  1,  0],
       [-2,  0,  1]])

In [207]:
i = 0
j = 1
k = 2

In [208]:
P_jk = np.array([
    [1, 0, 0],
    [0, 0, 1],
    [0, 1, 0]
])

In [212]:
e_k = basis_vector(3, k)
e_i = basis_vector(3, i).T
e_j = basis_vector(3, j)

In [213]:
e_k.shape


Out[213]:
(3, 1)

In [216]:
e_k @ e_i


Out[216]:
array([[0., 0., 0.],
       [0., 0., 0.],
       [1., 0., 0.]])

In [217]:
e_j @ e_i


Out[217]:
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 0., 0.]])

In [218]:
P_jk


Out[218]:
array([[1, 0, 0],
       [0, 0, 1],
       [0, 1, 0]])

In [214]:
P_jk @ e_k @ e_i


Out[214]:
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 0., 0.]])

In [215]:
e_j @ e_i @ P_jk


Out[215]:
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 0., 0.]])

In [221]:
np.linalg.inv(p) == p.T


Out[221]:
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]])

In [222]:
p


Out[222]:
array([[0, 1, 0],
       [0, 0, 1],
       [1, 0, 0]])

In [223]:
p2 = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])

p3 = np.array([
    [0, 0, 1],
    [1, 0, 0],
    [0, 1, 0]
])

In [227]:
px = p @ p2 @ p3
px.T == np.linalg.inv(px)


Out[227]:
array([[ True,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]])

In [231]:
np.linalg.inv(np.eye(3) + e_j @ e_i)


Out[231]:
array([[ 1.,  0.,  0.],
       [-1.,  1.,  0.],
       [ 0.,  0.,  1.]])

Positive semi-definite matrix


In [280]:
# Columns not linearly independent
A = np.array([
    [1, 0, 0],
    [0, 0, 0],
    [0, 1, 1]
])

In [281]:
A.T @ A


Out[281]:
array([[1, 0, 0],
       [0, 1, 1],
       [0, 1, 1]])

In [284]:
x = np.array([0, 1, -1]).reshape(3, 1)
x.T @ A.T @ A @ x


Out[284]:
array([[0]])