Suppose deg is an angle in degrees

Q1. Suppose deg is an angle in degrees. Write down the NumPy code that computes the counterclockwise rotation matrix. Don’t forget to convert the angles into radians when necessary.

ÂÂ

def rotate(deg):

ÂÂ

Q2. Let x be a vector. How do you use rotate(deg) to find a vector y that is normal to x? Namely x,y> = 0.

ÂÂ

y = …

ÂÂ

Q3. Let x be a vector. How do you find a vector v in the same direction as x but with |v| = 1.

ÂÂ

v = …

ÂÂ

Q4. A hyperplane in 2D is the same as a line. A hyperplane is defined by a normal vector w and a bias scalar b. Write down the expression that defines points on the line that is the hyperplane in 2D defined by (w, b).

ÂÂ

{ x : … }

ÂÂ

Q5. Given a vector x, and a hyperplane P defined by (w, b). Describe how to compute the point v that is the projection of x on to the plane P.

The Correct Answer and Explanation is :

Q1. Code for counterclockwise rotation matrix

To compute the counterclockwise rotation matrix, we use the following NumPy code. Remember that the angle must be converted into radians since NumPy trigonometric functions use radians.

import numpy as np

def rotate(deg):
    rad = np.deg2rad(deg)  # Convert degree to radians
    rotation_matrix = np.array([[np.cos(rad), -np.sin(rad)],
                                [np.sin(rad), np.cos(rad)]])
    return rotation_matrix

This function takes an angle in degrees and returns the corresponding 2D counterclockwise rotation matrix.

Q2. Finding a vector ( y ) normal to vector ( x )

To find a vector ( y ) that is normal to ( x ), we can rotate ( x ) by 90 degrees counterclockwise. In 2D, rotating a vector by 90 degrees counterclockwise swaps the components and changes the sign of the new x-component.

If ( x = [x_1, x_2] ), then ( y ) will be:

x = np.array([x1, x2])
y = rotate(90).dot(x)  # Apply a 90 degree rotation to x

Q3. Finding a unit vector in the same direction as ( x )

To find a unit vector ( v ) in the same direction as ( x ), you need to normalize ( x ) by dividing it by its magnitude (norm):

x = np.array([x1, x2])
v = x / np.linalg.norm(x)  # Normalize x to get a unit vector

This code will return a vector ( v ) of magnitude 1 that points in the same direction as ( x ).

Q4. Expression for points on the hyperplane in 2D

In 2D, a hyperplane is simply a line, and its equation can be represented by the normal vector ( w ) and the bias ( b ). The points on this line are given by:

[
{ x : w \cdot x + b = 0 }
]

Here, ( w \cdot x ) is the dot product of ( w ) and ( x ), and the equation represents all points ( x ) on the line (hyperplane).

Q5. Projection of vector ( x ) onto hyperplane ( P )

The projection of a vector ( x ) onto a hyperplane ( P ) defined by the normal vector ( w ) and the bias ( b ) can be found using the following steps:

  1. First, compute the dot product of ( x ) and ( w ).
  2. Compute the dot product of ( w ) with itself.
  3. Use these results to calculate the scalar projection of ( x ) onto ( w ).
  4. Subtract this projection from ( x ) to get the point ( v ) on the plane.

The formula for the projection ( v ) is:

[
v = x – \frac{x \cdot w + b}{w \cdot w} w
]

This formula computes the projection of ( x ) onto the hyperplane defined by ( w ) and ( b ).

Explanation

To explain the process of computing the projection in more detail, we consider that the projection of ( x ) onto the hyperplane defined by the normal vector ( w ) is the closest point on the hyperplane to ( x ). The term ( \frac{x \cdot w + b}{w \cdot w} w ) gives the vector that points from ( x ) to the hyperplane, and subtracting this from ( x ) yields the point ( v ) on the hyperplane.

Illustration

I’ll generate a diagram showing the vector ( x ), the hyperplane ( P ), and the projection ( v ). Let me create that image for you.

You’ve hit your daily maximum number of images. To ensure the best experience for everyone, we have rate limits in place. Please wait for the next day before generating more images. Your daily maximum will reset in 10 hours and 29 minutes.

Let me know if you’d like further explanations or assistance with any of the questions!

Scroll to Top