python question
import numpy as np
def gram_schmidt_np(V):
# YOUR CODE HERE
from nose.tools import assert_almost_equal
import numpy as np
# Check that it works for a specific matrix V of linearly
independent vectors.
V1 = [np.array([1,3]), np.array([2,4])]
W1 = [np.array([ 0.31622777, 0.9486833 ]), np.array([
0.9486833 , -0.31622777])]
assert_almost_equal(np.linalg.norm(np.array(W1)-gram_schmidt_np(V1)),0,delta=1e-8)
from nose.tools import assert_almost_equal
import numpy as np
# Check that it works for a case when vectors are linearly
dependent
V2 = [np.array([1,2]),np.array([2,4])]
W2 = [np.array([0.4472136 , 0.89442719]), np.array([0., 0.])]
assert_almost_equal(np.linalg.norm(np.array(W2)-gram_schmidt_np(V2)),0,delta=1e-8)
from nose.tools import assert_almost_equal
import numpy as np
V3 = [np.array([1,4,7]),np.array([2,5,8]),np.array([3,6,10])]
W3 = [np.array([0.12309149, 0.49236596, 0.86164044]), np.array([
0.90453403, 0.30151134, -0.30151134]),
np.array([ 0.40824829, -0.81649658,
0.40824829])]
assert_almost_equal(np.linalg.norm(np.array(W3)-gram_schmidt_np(V3)),0,delta=1e-8)
from nose.tools import assert_almost_equal
import numpy as np
# Check that errors are raised for wrong input (vectors not of
same length).
from nose.tools import assert_raises
assert_raises(ValueError,gram_schmidt_np,[np.array([1,2]),np.array([1,5,1]),np.array([7,8])])
# Check that errors are raised for wrong input (not a
list)
from nose.tools import assert_raises
assert_raises(ValueError,gram_schmidt_np,np.array([1,2]))
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; ||
(a) [45 marks] Write a function gram_schmidt_np which takes as input a list of length n containing n NumPy arrays, {Vi}, each of length n and then uses the Gram-Schmidt process to construct an orthonormal set of vectors {w;}, which is then returned (in order). • Your function should check that the input V is list of arrays of the same length and raise a ValueError if not. • In the normalisation step you can check if w; has norm less than 1€ = 1.19 · 10-7 instead of checking if it is exactly 0 and in this case just replace it by the zero vector instead of normalising it. Note: The number e is sometimes called "machine-epsilon" and can be obtained in NumPy for single precision floating point numbers as np. finfo('float32').eps )
python question import numpy as np def gram_schmidt_np(V): # YOUR CODE HERE from nose.tools import assert_almost_equ
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
python question import numpy as np def gram_schmidt_np(V): # YOUR CODE HERE from nose.tools import assert_almost_equ
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!