import sympy def gram_schmidt_sp(V): # YOUR CODE HERE from nose.tools import assert_equal import sympy # Test that i

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

import sympy def gram_schmidt_sp(V): # YOUR CODE HERE from nose.tools import assert_equal import sympy # Test that i

Post by answerhappygod »

Import Sympy Def Gram Schmidt Sp V Your Code Here From Nose Tools Import Assert Equal Import Sympy Test That I 1
Import Sympy Def Gram Schmidt Sp V Your Code Here From Nose Tools Import Assert Equal Import Sympy Test That I 1 (83.99 KiB) Viewed 48 times
Import Sympy Def Gram Schmidt Sp V Your Code Here From Nose Tools Import Assert Equal Import Sympy Test That I 2
Import Sympy Def Gram Schmidt Sp V Your Code Here From Nose Tools Import Assert Equal Import Sympy Test That I 2 (75.06 KiB) Viewed 48 times
import sympy
def gram_schmidt_sp(V):
# YOUR CODE HERE
from nose.tools import assert_equal
import sympy
# Test that it works for a specific matrix V
V1 = sympy.Matrix([[1,2],[3,4]])
W1 = sympy.Matrix([[sympy.sqrt(10)/10, 3*sympy.sqrt(10)/10],
[3*sympy.sqrt(10)/10, -sympy.sqrt(10)/10]])
assert_equal(W1-gram_schmidt_sp(V1),sympy.zeros(2,2))
from nose.tools import assert_equal
import sympy
# Check that it works for a case when vectors that are linearly
dependent
V2 = sympy.Matrix([[1,2],[2,4]])
W2 = sympy.Matrix([[sympy.sqrt(5)/5, 2*sympy.sqrt(5)/5], [0,
0]])
assert_equal(W2-gram_schmidt_sp(V2),sympy.zeros(2,2))
from nose.tools import assert_equal
import sympy
from sympy import sqrt
# Hidden test that it works for another matrix input.
V3 = sympy.Matrix([[1,2,3],[4,5,6],[7,8,10]])
W3=sympy.Matrix([[sqrt(66)/66, 2*sqrt(66)/33,
7*sqrt(66)/66],

[3*sqrt(11)/11, sqrt(11)/11, -sqrt(11)/11],

[sqrt(6)/6, -sqrt(6)/3, sqrt(6)/6]])
assert_equal(W3-gram_schmidt_sp(V3),sympy.zeros(3,3))
from nose.tools import assert_equal
import sympy
from sympy import sqrt
# Hidden test that it works for another matrix input.
from nose.tools import assert_raises
import sympy
# Check that errors are raised for wrong input.
import sympy
assert_raises(ValueError,gram_schmidt_sp,1)
from nose.tools import assert_raises
import sympy
# Check that errors are raised for wrong input.
import sympy
assert_raises(ValueError,gram_schmidt_sp,sympy.Matrix([[1,2],[3,4],[5,6]]))
The Gram-Schmidt process is an algorithm which can be used to construct a set of vectors {w;} which are orthonormal, in other words they are pairwise orthogonal to each other and have lengths equal to 1. This condition can be expressed in terms of the standard Euclidean scalar (or "dot") product as W;W W; = 1 0 if i = j, if i + j. If the input to the Gram-Schmidt process is a set of n linearly independent vectors {v; | i = 1, ..., ,n} in R”, then the output will be a set of n orthonormal vectors {w; | i = 1, ... ,n} in R”. In general, if the input vectors are not all linearly independent, then the output will be a set of n vectors containing an orthonormal basis for the linear span of the vectors {Vi} together with a number of zero-vectors. The method is summarised by the following instructions: 1. Construct an orthogonal set of vectors {w;} using the formula: W1 = V1 and 1-1 Vi. W W; = V; - Σ i>1. j=1 l/w; ||2 2. If W; from the previous step is not the zero vector 0, normalise it to have length 1 by setting W = Wi ||w; ||
(b) (45 marks] Write a function gram_schmidt_sp which takes as argument an n x n square matrix V. The columns of V represent the set of n-dimensional vectors {V;} and the function should use the Gram-Schmidt process to construct an orthonormal set of vectors {W;}, which are then returned in the form of rows of a matrix W. • The input matrix V can be assumed to be of type sympy. Matrix • Your function should check that the input V is a square matrix of type sympy.Matrix, and raise a ValueError if not. The output matrix W should also be of type sympy.Matrix. Note: while the algorithm is the same as above you will need to figure out for yourself how to extract columns from a Sympy matrix (the columns will themselves be nx 1 matrices), how to take the dot product and norm of a SymPy matrix and how to join rows to make a new Sympy matrix. (Hint: It is often easier to join columns and then take the transpose. The function sympy.Matrix.hstack can be useful.)
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply