Search code examples
mathartificial-intelligencelinear-algebra

To understand how Gram-Schmidt Process is translated into this piece of code as the implementation


Trying to understand Gram-Schmidt process from this explanation:

http://mlwiki.org/index.php/Gram-Schmidt_Process

The steps of the calculation make sense to me. However the Python implementation included in the same article doesn't seem to be aligned.

def normalize(v):
    return v / np.sqrt(v.dot(v))

n = len(A)

A[:, 0] = normalize(A[:, 0])

for i in range(1, n):
    Ai = A[:, i]
    for j in range(0, i):
        Aj = A[:, j]
        t = Ai.dot(Aj)
        Ai = Ai - t * Aj
    A[:, i] = normalize(Ai)

From above code, we see it does dot product for V1 and b, however the (V1,V1) part is not done as the denominator (refer to below equation). I wonder how below equation is translated into code residing in the for loop?

enter image description here


Solution

  • This is what the code does exactly

    enter image description here

    Basically it normalize the previous vector (column in A) and project the current one to it and to be subtracted by the current one.

    Normalization happens with every vector for neat calculation.

    The V2 equation above doesn't normalize the previous vector hence the difference.