Skip to main content

2.9 Gram-Schmidt Orthogolization

Gram-Schdmit method allows us to constructively transform any basis $(b_1,b_2,....,b_n)$ of an $n$-dimensional vector space $V$ into an orthogonal/orthonormal basis $(u_1,u_2,....u_n)$ of $V$. The method interactively constructs an orthogonal basis as follows

$u_1=b_1$

$u_2=b_2- \frac{u_1.u_1^T b_2}{u_1^T.u_1}$

..

$u_k=b_k - $ projection of $u_k$ onto the span of $(u_1,u_2,...u_{k-1})$

The $k$ th basis vector $b_k$ is projected onto the subspace spanned by the first $k-1$ constructed orthogonal vectors $u_1,u_2,......u_{k-1}$. This projection is then subtracted from $b_k$ and yields a vector $u_k$ that is orthogonal to the $(k-1)$ dimensional subspace spanned by $u_1,u_2,......u_{k-1}$.Repeating this procedure for all $n$ basis vectors $(b_1,b_2,....,b_n)$ yields an orthogonal basis $(u_1,u_2,....u_n)$ of V. If we normalize $u_k$, we obtain a ortho normal basis where $||u_k||=1$ for $k=1\ldots n $

Consider a simple example



import numpy as np
b1=np.array([2,0])
b2=np.array([1,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
print("b1")
print(b1)
print("b2")
print(b2)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("dot product of u1 and u2")
print(u1.dot(u2))

o/p
b1 
[2 0] 
b2 
[1 1] 
orthogonal vectors 
u1 
[2 0]
 u2 
[0. 1.]
dot product of u1 and u2 
0.0

Orthogonalization in three dimension
import numpy as np
np.set_printoptions(precision=4)
b1=np.array([1,2,2])
b2=np.array([-1,0,2])
b3=np.array([0,0,1])
u1=b1
u2=b2-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b2))
u3=b3-((np.outer(u1,u1)/np.dot(u1,u1)).dot(b3))-((np.outer(u2,u2)/np.dot(u2,u2)).dot(b3))
print("b1")
print(b1)
print("b2")
print(b2)
print("b3")
print(b3)
print("orthogonal vectors")
print("u1")
print(u1)
print("u2")
print(u2)
print("u3")
print(u3)
print("length of u1 u2 u3")
lu1=np.sqrt(u1.dot(u1))
lu2=np.sqrt(u2.dot(u2))
lu3=np.sqrt(u3.dot(u3))
print(lu1,lu2,lu3)
print("normalized vectors u1 u2 u3")
u1=u1/lu1
u2=u2/lu2
u3=u3/lu3
print(u1)
print(u2)
print(u3)
np.set_printoptions(suppress=True)
print("dot product of u1 and u2")
display(round(u1.dot(u2)))
print("dot product of u1 and u3")
print(round(u1.dot(u3)))
print("dot product of u2 and u3")
display(round(u2.dot(u3)))

o/p
b1 
[1 2 2] 
b2
 [-1 0 2]
 b3 
[0 0 1] 
orthogonal vectors 
u1 
[1 2 2]
u2 
[-1.3333 -0.6667 1.3333] 
u3 
[ 0.2222 -0.2222 0.1111] 
length of u1 u2 u3 
3.0 2.0 0.3333333333333333
 normalized vectors u1 u2 u3
 [0.3333 0.6667 0.6667] 
[-0.6667 -0.3333 0.6667] 
[ 0.6667 -0.6667 0.3333] 
dot product of u1 and u2
0
dot product of u1 and u3 
dot product of u2 and u3
0
Compact code
import numpy as np
def gram_schmidt(A):
    """Orthogonalize a set of vectors stored as the columns of matrix A."""
    # Get the number of vectors.
    n = A.shape[1]
    for j in range(n):
        # To orthogonalize the vector in column j with respect to the
        # previous vectors, subtract from it its projection onto
        # each of the previous vectors.
        for k in range(j):
            A[:, j] -= np.dot(A[:, k], A[:, j]) * A[:, k]
        A[:, j] = A[:, j] / np.linalg.norm(A[:, j])
    return A

if __name__ == '__main__':
    A = np.array([[1.0, 0.0, 1.0], [1.0, 0.0, -1.0], [0.0, 3.0, 4.0]])
    print(gram_schmidt(A))

Example ( university question)
Use the Gram-Schmidt process to find an orthogonal basis for the column space of the following matrix.

$\begin{bmatrix}
2 & 1& 0\\
1 & -1&1 \\
0 & 3 & 1\\
1 & 1 & 1
\end{bmatrix}$

here
$u_1=b_1=\begin{bmatrix}
2\\
1\\
0\\
1
\end{bmatrix}$

$u_2=b_2-\frac{(u_1u_1^T)}{||u_1||^2}.b_2$
$u_2=\begin{bmatrix}
1\\
-1\\
3\\
1
\end{bmatrix}-\frac{1}{6}\begin{bmatrix}
4 & 2 & 0 & 2\\
2 & 1 & 0 & 1\\
0& 0 & 0 & 0\\
2 & 1 & 0 & 1
\end{bmatrix}\begin{bmatrix}
1\\
-1\\
3\\
1
\end{bmatrix}$
$u_2=\begin{bmatrix}
0.3333\\
-1.3333\\
3.\\
0.6667
\end{bmatrix}$

$u_3=b_3-\frac{(u_1u_1^T)}{||u_1||^2}.b_3-\frac{(u_2u_2^T)}{||u_2||^2}.b_3$
$u_3=\begin{bmatrix}
-0.7353\\
0.9412\\
0.3824\\
0.5294
\end{bmatrix}$

Example ( University Question)
Use Gram-Schmidt process to find an orthonormal basis from the ordered basis $B=\{b_1,b_2\}$, where $b_1=\begin{bmatrix}1\\ 1\\ 1 \end{bmatrix}$  and  $b_2=\begin{bmatrix} -1\\ 2\\ 0 \end{bmatrix}$

$u_1=b_1=\begin{bmatrix} 1\\1\\1\end{bmatrix}$
$u_2=b_2-\frac{u_1.u_1^T}{||u_1^2||}.b_2$
$=\begin{bmatrix} -1\\2\\0\end{bmatrix} - \frac{1}{3} \begin{bmatrix}1 &1 &1 \\ 1&1&1\\ 1 &1 &1\end{bmatrix}\begin{bmatrix}-1 \\ 2 \\ 0 \end{bmatrix}$
$=\begin{bmatrix} -1\\2\\0\end{bmatrix} - \frac{1}{3} \begin{bmatrix}1 \\ 1 \\ 1 \end{bmatrix}$

$=\begin{bmatrix} -4/3\\5/3\\-1/3\end{bmatrix}$

length of $u_1=\sqrt{3}$
length of $u_2=\frac{\sqrt{42}}{3}$
So the normalized vectors are
$u_1=\frac{1}{\sqrt{3}}.\begin{bmatrix} 1\\1\\1\end{bmatrix}$
$u_2=\frac{1}{\sqrt{42}}.\begin{bmatrix} -4\\5\\-1\end{bmatrix}$

Note that $u_1.u_2=0$

Example ( University Question)
Obtain  an orthonormal basis with respect to the standard inner space for the subspace of $R^3$ generated by $(1,0,1), (1,0,-1)$ and $(0,3,4)$

$u_1=b_1=\begin{bmatrix} 1\\0\\1\end{bmatrix}$
$u_2=b_2-\frac{u_1.u_1^T}{||u_1^2||}.b_2$
$=\begin{bmatrix} 1\\0\\-1\end{bmatrix} - \frac{1}{2} \begin{bmatrix}1 &0 &1 \\ 0&0&0\\ 1 &0 &1\end{bmatrix}\begin{bmatrix}1 \\ 0 \\ -1 \end{bmatrix}$
$=\begin{bmatrix} 1\\0\\-1\end{bmatrix} - \frac{1}{2} \begin{bmatrix}0 \\ 0 \\ 0 \end{bmatrix}$
$=\begin{bmatrix} 1\\0\\-1\end{bmatrix}$

$u_3=b_3-\frac{(u_1u_1^T)}{||u_1||^2}.b_3-\frac{(u_2u_2^T)}{||u_2||^2}.b_3$
$u_3=\begin{bmatrix}
0\\
3\\
0\\
\end{bmatrix}$

length of $u_1=\sqrt{2}$
length of $u_2=\sqrt{2}$
length of $u_3=3$

So the normalized vectors are
$u_1=\frac{1}{\sqrt{2}}.\begin{bmatrix} 1\\0\\1\end{bmatrix}$
$u_2=\frac{1}{\sqrt{2}}.\begin{bmatrix}1\\0\\-1\end{bmatrix}$
$u_3=\frac{1}{3}.\begin{bmatrix}0\\3\\0\end{bmatrix}$

Example ( University Question)
Using the Gram-Schmidt method, construct the orthonormal basis $(u1,u2,u3)$ to the following sequence of vectors $w_1=\begin{bmatrix}
1\\
1\\
1\\
1
\end{bmatrix}, w_2=\begin{bmatrix}
1\\
1\\
-1\\
-1

\end{bmatrix},w_3=\begin{bmatrix}
0\\
-1\\
2\\
1

\end{bmatrix}$ of $R^4$

$u_1=w_1=\begin{bmatrix}1\\
1\\
1\\
1
\end{bmatrix}$
$u_2=w_2-\frac{u_1.u_1^T}{||u_1^2||}.w_2$
$u_2=\begin{bmatrix} 1\\1\\-1\\-1 \end{bmatrix} - \frac{1}{4} \begin{bmatrix}1 &1 &1&1 \\ 1&1&1 &1\\
1 &1 &1 &1\end{bmatrix}\begin{bmatrix} -1 \\ -1 \\ 1 \\1 \end{bmatrix}$

$u_2=\begin{bmatrix} 1\\1\\-1\\-1 \end{bmatrix}$
$u_3=w_3-\frac{(u_1u_1^T)}{||u_1||^2}.w_3-\frac{(u_2u_2^T)}{||u_2||^2}.w_3$
$u_3=\begin{bmatrix}0\\
-1\\
2\\
1
\end{bmatrix}-\frac{1}{4} \begin{bmatrix}1 &1 &1&1 \\ 1&1&1 &1\\
1 &1 &1 &1\end{bmatrix}\begin{bmatrix} 0 \\ -1 \\ 2 \\1 \end{bmatrix}-\frac{1}{4} \begin{bmatrix}
1 &1 &-1&-1 \\ 1&1&-1 &-1\\
-1 &-1 &1 &1\\
-1 &-1 &1 &1\\
\end{bmatrix}\begin{bmatrix} 0 \\ -1 \\ 2 \\1 \end{bmatrix}$$u_3=\begin{bmatrix}0\\
-1\\
2\\
1
\end{bmatrix}-\begin{bmatrix}
1/2\\
1/2\\
1/2\\
1/2
\end{bmatrix}-\begin{bmatrix}
-1\\
-1\\
1\\
1
\end{bmatrix}$
$u_3=\begin{bmatrix}
1/2\\
-1/2\\
1/2\\
-1/2
\end{bmatrix}$

The length of vectors
$||u_1||=2, ||u_2||=2,||u_3||=1$

So the normalized orthogonal vectors ( orthonormal vectors) are
$u_1=\begin{bmatrix}
1/2\\
1/2\\
1/2\\
1/2
\end{bmatrix},u_2=\begin{bmatrix}
1/2\\
1/2\\
-1/2\\
-1/2

\end{bmatrix},u_3=\begin{bmatrix}
1/2\\
-1/2\\
1/2\\
-1/2
\end{bmatrix}$

Comments

Popular posts from this blog

Mathematics for Machine Learning- CST 284 - KTU Minor Notes - Dr Binu V P

  Introduction About Me Syllabus Course Outcomes and Model Question Paper Question Paper July 2021 and evaluation scheme Question Paper June 2022 and evaluation scheme Overview of Machine Learning What is Machine Learning (video) Learn the Seven Steps in Machine Learning (video) Linear Algebra in Machine Learning Module I- Linear Algebra 1.Geometry of Linear Equations (video-Gilbert Strang) 2.Elimination with Matrices (video-Gilbert Strang) 3.Solving System of equations using Gauss Elimination Method 4.Row Echelon form and Reduced Row Echelon Form -Python Code 5.Solving system of equations Python code 6. Practice problems Gauss Elimination ( contact) 7.Finding Inverse using Gauss Jordan Elimination  (video) 8.Finding Inverse using Gauss Jordan Elimination-Python code Vectors in Machine Learning- Basics 9.Vector spaces and sub spaces 10.Linear Independence 11.Linear Independence, Basis and Dimension (video) 12.Generating set basis and span 13.Rank of a Matrix 14.Linear Mapping and Matri

1.1 Solving system of equations using Gauss Elimination Method

Elementary Transformations Key to solving a system of linear equations are elementary transformations that keep the solution set the same, but that transform the equation system into a simpler form: Exchange of two equations (rows in the matrix representing the system of equations) Multiplication of an equation (row) with a constant  Addition of two equations (rows) Add a scalar multiple of one row to the other. Row Echelon Form A matrix is in row-echelon form if All rows that contain only zeros are at the bottom of the matrix; correspondingly,all rows that contain at least one nonzero element are on top of rows that contain only zeros. Looking at nonzero rows only, the first nonzero number from the left pivot (also called the pivot or the leading coefficient) is always strictly to the right of the  pivot of the row above it. The row-echelon form is where the leading (first non-zero) entry of each row has only zeroes below it. These leading entries are called pivots Example: $\begin

4.3 Sum Rule, Product Rule, and Bayes’ Theorem

 We think of probability theory as an extension to logical reasoning Probabilistic modeling  provides a principled foundation for designing machine learning methods. Once we have defined probability distributions corresponding to the uncertainties of the data and our problem, it turns out that there are only two fundamental rules, the sum rule and the product rule. Let $p(x,y)$ is the joint distribution of the two random variables $x, y$. The distributions $p(x)$ and $p(y)$ are the corresponding marginal distributions, and $p(y |x)$ is the conditional distribution of $y$ given $x$. Sum Rule The addition rule states the probability of two events is the sum of the probability that either will happen minus the probability that both will happen. The addition rule is: $P(A∪B)=P(A)+P(B)−P(A∩B)$ Suppose $A$ and $B$ are disjoint, their intersection is empty. Then the probability of their intersection is zero. In symbols:  $P(A∩B)=0$  The addition law then simplifies to: $P(A∪B)=P(A)+P(B)$  wh