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 University Question Papers and Evaluation Scheme -Mathematics for Machine learning CST 284 KTU 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...

Vectors in Machine Learning

As data scientists we work with data in various formats such as text images and numerical values We often use vectors to represent data in a structured and efficient manner especially in machine learning applications In this blog post we will explore what vectors are in terms of machine learning their significance and how they are used What is a Vector? In mathematics, a vector is a mathematical object that has both magnitude and direction. In machine learning, a vector is a mathematical representation of a set of numerical values. Vectors are usually represented as arrays or lists of numbers, and each number in the list represents a specific feature or attribute of the data. For example, suppose we have a dataset of houses, and we want to predict their prices based on their features such as the number of bedrooms, the size of the house, and the location. We can represent each house as a vector, where each element of the vector represents a specific feature of the house, such as the nu...

2.14 Singular Value Decomposition

The Singular Value Decomposition ( SVD) of a matrix is a central matrix decomposition method in linear algebra.It can be applied to all matrices,not only to square matrices and it always exists.It has been referred to as the 'fundamental theorem of linear algebra'( strang 1993). SVD Theorem: Let $A^{m \times n}$ be a rectangular matrix of rank $r \in [0,min(m,n)]$. The SVD of A is a decomposition of the form. $A= U \Sigma V^T $ with an orthogonal matrix $U \in \mathbb{R}^{m \times m}$ with column vectors $u_i, i=1,\ldots,m$ and an orthogonal matrix $V \in \mathbb{R}^{n \times n}$ with column vectors $v_j, j=1,\ldots,n$.Moreover, $\Sigma$ is an $m \times n$ matrix with $\sum_{ii} = \sigma \ge 0$ and $\sigma_{ij}=0, i \ne j$. The diagonal entries $\Sigma_i=1,\ldots,r$ of $\sigma$ are called singular values . $u_i$ are called left singular vectors , and $v_j$ are called right singular vectors .By convention singular values are ordered ie; $\sigma_1 \ge \sigma_2 \ldots \sigma_r \...