程序代写代做代考 python Homework 3¶

Homework 3¶

Knowledge: Classes and functions¶

Complete the given class. Fill in the given functions along with the class constructor.

Function 1. Inner product
Perform inner product of two arrays:
x = np.arange(24).reshape((2,3,4))
y = np.arange(4)

Function 2. Dimension of the vector space
Determine the dimension of the vector space spanned by $\mathbf{x}_1 = [1, 2, 2, 1]^T$, $\mathbf{x}_2 = [1, 1, 0, 1]^T$, $\mathbf{x}_3 = [3,5, 4, 3]^T$ using Python.

Function 3. Projection of points on a plane.
In the figure, $R$ is a point located at the intersection of the plane and its normal vector. $Q$ is the given point which is to be projected. $\hat Q$ (Q_hat in the image) is the projection of point $Q$ on the plane. $O\hat Q$ is the projection vector.
Given the plane, project all the given points onto the plane.
Complete class method “Project_points” which takes input a, b, c coefficients of the plane and returns the projection vectors of the point on the plane. Refer to comments in the function template for further instructions.

For a plane: ax + by + c*z = d; x,y,z are in Cartesian coordinate system and a, b, c are the coefficients of the plane. here, a = 10, b = 15, c = 1 and d = 7.
In [0]:
# Importing required packages. Limited to only numpy.
import numpy as np
from numpy.linalg import matrix_rank

np.random.seed(0) # Seed for randomized items, ignore this line.

# We define class named Homework3
class Homework3():

#class constructor
def __init__(self):
# x, y and z array of point coordinates for Qs3. e.g: self.x
# ___ write your code here _____

def Compute_inner_products():
“””
Function to compute inner products

Saves:
inner_product: inner-products of given vectors

“””
# ___ write your code here _____
pass

def Compute_dimention():
“””
Function to compute dimention

Saves:
dimention: dimention of given vectors

“””
# ___ write your code here _____
pass

def Project_points():
“””
Function to project the points with coordinates x, y, z onto the plane defined by a*x + b*y + c*z = 1

Args:
a: Plane coefficent
b: Plane coefficent
c: Plane coefficent

Saves:
projection_points: projection vectors of the projected point

“””
# ___ write your code here _____
pass

# main function
def main():

answer = Homework3() # Construct an object of the Homework3 class

# Question 1
x_inner = np.arange(24).reshape((2,3,4)) # matrix construction same as in homwork 2
y_inner = np.arange(4)
answer.x_inner = x_inner # Use the provided variable names (x_inner) in the class. Define the variable in class constructor to initialize.
answer.y_inner = y_inner # Initialization for the variable belonging to Homework3 object.

# Question 2
v1 = np.asarray([1,2,2,1])
v2 = np.asarray([1,1,0,1])
v3 = np.asarray([3,5,4,3])
answer.v1 = v1 # Use the given variable (v1, v2 … etc)
answer.v2 = v2
answer.v3 = v3

# Question 3
x = np.random.rand(10) # Generating random 10 point coordinates (x,y,z)
y = np.random.rand(10)
z = np.random.rand(10)
a = 0
b = 0
c = 0
d = 0
answer.x = x # x, y, z values are initailized
answer.y = y
answer.z = z

answer.Compute_inner_products() # We call the class function to compute inner products from the initialized values.
answer.Compute_dimention()
answer.Project_points(a, b, c) # a, b, c are inputs to the function.

# Saving all answers
hw3_dict = {}
hw3_dict[“0”] = x_inner
hw3_dict[“1”] = y_inner
hw3_dict[“2”] = answer.inner_product # The computed inner product is saved in variable “inner_product” of Homework3 object, save it into a dictionary
hw3_dict[“3”] = answer.dimention # The computed dimention is saved in variable “inner_product” of Homework3 object, save it into a dictionary
hw3_dict[“4”] = np.array([x,y,z])
hw3_dict[“5”] = answer.projection_points # The computed projection_points is saved in variable “inner_product” of Homework3 object, save it into a dictionary

print(“The inner product is: {}”.format(hw3_dict[“2”]))
print(“The dimention is: {}”.format(hw3_dict[“3”]))
print(“The given point coordinates are :\n {}, {}, {}”.format(x , y, z))
print(“The projected points are: \n {}”.format(hw3_dict[“4”]))

if __name__ == “__main__”:
main()

Submission:
Save your generated matrix in the form of a dictionary. Note that the keys of the dictionary are strings.
• “0” : your_constructed_matrix_x
• “1” : your_constructed_matrix_y
• “2” : Inner_product_of_matrix
• “3” : the_dimention_of_the_given_matrix (Question 2)
• “4” : the_numpy_array_of_given_point_coordinates (Question 3)
• “5” : the_numpy_array_of_projected_point_coordinates (Question 3)
Note:
• Convert the “PHW3_sol.ipynb” to “hw3_script.py”.
• Save the dictionary file as “hw3_output.npy”.
• Upload “hw3_script.py” and “hw3_output.npy” along with “hw3_theory.py”.
• Structure : The single script “main” should call the function and print the corresponding answers of each question and save them to the dictionary which is passed through the function to return. Read more about “Call by value” and “Call by reference” and “aliasing “in python. Do not include any contextual texts in the output print. The print should be just what is mentioned as required in the question.
• The Autograder will give you the score to corresponding submission.
• You can submit you code twice per day. But only the latest submission will be considered for your score.
• The script will be manually checked for the presence of the corresponding functions and score will be given only if the required structure is present.

Solution:¶
In [0]:
# Importing required packages. Limited to only numpy.
import numpy as np
from numpy.linalg import matrix_rank

np.random.seed(0) # Seed for randomized items, ignore this line.

# We define class named Homework3
class Homework3():

#class constructor
def __init__(self):
# x, y and z array of point coordinates for Qs3. e.g: self.x
self.x_inner = 0
self.y_inner = 0
self.v1 = 0
self.v2 = 0
self.v3 = 0
self.x = 0
self.y = 0
self.z = 0

def Compute_inner_products(self):
“””
Saves:
inner_product: inner-products of given vectors (self.inner_product)

“””
self.inner_product = np.inner(self.x_inner, self.y_inner)

def Compute_dimention(self):
“””
Saves:
dimention: dimention of given vectors

“””
X = np.matrix([self.v1, self.v2, self.v3])
rank = matrix_rank(X)
self.dimention = rank

def Project_points(self, a,b,c,d):
“””
Projects the points with coordinates x, y, z onto the plane defined by a*x + b*y + c*z = 7

Args:
a: Plane coefficent
b: Plane coefficent
c: Plane coefficent

Saves:
projection_points: projection vectors of the projected point

“””

# Calculate the vector norm
vector_norm = a*a + b*b + c*c

# Compute normal vector – unit vector
normal_vector = np.array([a, b, c]) / np.sqrt(vector_norm)

# Point in plane, Point R. Make vector OR
OR = np.array([a, b, c]) / np.abs(vector_norm)

# Given point Q. Make vector OQ
OQ = np.column_stack((self.x, self.y, self.z))

# Vector to Q from R, OQ – OR = RQ
RQ = OQ – OR

# Projection of Vector RQ onto Plane Normal OR, i.e RQ’
RQ_dash = np.dot(RQ, normal_vector)

# Get projection parallel to plane QQ’
Q_dashQ = (RQ – RQ_dash[:, None] * normal_vector)

# Get projection on the plane
RQ_dash = OR + Q_dashQ

pts = RQ_dash
pts.T[0] = pts.T[0] + a*(d-1)/(a*a + b*b + c*c)
pts.T[1] = pts.T[1] + b*(d-1)/(a*a + b*b + c*c)
pts.T[2] = pts.T[2] + c*(d-1)/(a*a + b*b + c*c)

self.projection_points = pts

def main():

answer = Homework3()

# Question 1
x_inner = np.arange(24).reshape((2,3,4))
y_inner = np.arange(4)
answer.x_inner = x_inner
answer.y_inner = y_inner

# Question 2
v1 = np.asarray([1,2,2,1])
v2 = np.asarray([1,1,0,1])
v3 = np.asarray([3,5,4,3])
answer.v1 = v1
answer.v2 = v2
answer.v3 = v3

# Question 3
x = np.random.rand(10)
y = np.random.rand(10)
z = np.random.rand(10)
a = 10
b = 15
c = 1
d = 7
answer.x = x
answer.y = y
answer.z = z

answer.Compute_inner_products()
answer.Compute_dimention()
answer.Project_points(a, b, c, d)

# Saving all answers
hw3_dict = {}
hw3_dict[“0”] = x_inner
hw3_dict[“1”] = y_inner
hw3_dict[“2”] = answer.inner_product
hw3_dict[“3”] = answer.dimention
hw3_dict[“4”] = np.array([x,y,z])
hw3_dict[“5”] = answer.projection_points

print(“The inner product is: {}”.format(hw3_dict[“2”]))
print(“The dimention is: {}”.format(hw3_dict[“3”]))
print(“The given point coordinates are :\n {}, {}, {}”.format(x , y, z))
print(np.array([x,y,z]).T.shape)
print(“The projected points are: \n {}”.format(hw3_dict[“5”]))
np.save(“hw3_output”, hw3_dict)
# TO do sanity check of points: uncomment next line
# print(a*answer.projection_points.T[0] + b*answer.projection_points.T[1] + c*answer.projection_points.T[2])

if __name__ == “__main__”:
main()

The inner product is: [[ 14 38 62]
[ 86 110 134]]
The dimention is: 2
The given point coordinates are :
[0.5488135 0.71518937 0.60276338 0.54488318 0.4236548 0.64589411
0.43758721 0.891773 0.96366276 0.38344152], [0.79172504 0.52889492 0.56804456 0.92559664 0.07103606 0.0871293
0.0202184 0.83261985 0.77815675 0.87001215], [0.97861834 0.79915856 0.46147936 0.78052918 0.11827443 0.63992102
0.14335329 0.94466892 0.52184832 0.41466194]
(10, 3)
The projected points are:
[[0.2008801 0.26982493 0.943825 ]
[0.44265943 0.12010001 0.77190557]
[0.35706456 0.19949633 0.43690948]
[0.14263439 0.32222345 0.7403043 ]
[0.47210991 0.14371873 0.12311994]
[0.60277136 0.02244517 0.63560875]
[0.50438165 0.12041005 0.15003273]
[0.42086206 0.12625344 0.89757782]
[0.50872941 0.09575673 0.47635499]
[0.06751332 0.39611985 0.38306912]]