Module 1-Sage Vectors and Norm
Module 1-Sage Vectors and Norm
22.01.2025
Module 1
Vector Operations and Norm
Defining a Vector
Example:
# Define a vector in 3D
v2 = vector([3, 4, 5])
Vector Plotting in 2D
• arrow((0, 0), (v1[0], v1[1])): Draws an arrow (or vector) starting at the
origin (0,0) and pointing to the endpoint of v1 (1,2)
• color='green': Sets the color of the vector to green
• thickness=2: Increases the thickness of the arrow for better visibility
• show(plot_v1 + plot_v2)
• plot_v1 + plot_v2: Combines the two vector plots into a single plot
• show(): Displays the combined plot
Dot Product
u = vector([3, 4, 0])
v = vector([1, 2, 2])
dot_product = u.dot_product(v)
print("Dot product:", dot_product)
Output: Dot product: 11
Manual Verification
a. Cross Product
u = vector([3, 4, 0])
v = vector([1, 2, 2])
# Compute the cross product
cross_product = u.cross_product(v)
# Print the result
print("Cross product of u and v:", cross_product)
Output
Cross_product of u and v: (8, -6, 2)
Manual Verification
b. Norm(Magnitude of a vector)
Output: Norm of v: 3
Manual Calculation
Another example
x=vector([2, -1, 3, 2])
y=vector([3, 2, 1, -4])
print(x.norm()) # calculate the norm of x
print(y.norm()) # calculate the norm of y
print((x-y).norm()) # calculate distance
Output
3*sqrt(2)
sqrt(30)
5*sqrt(2)
Manual verification
c. Scalar Multiplication
v = vector([1, 2, 2])
scalar = 3
scaled_vector = scalar * v
print("Scaled vector:", scaled_vector)
Output: [3,6,6]
Manual Verification
d. Vector Addition
u = vector([1, 2, 3])
v = vector([4, 5, 6])
sum_vector = u + v
print("Vector sum:", sum_vector)
Ans: [5,7,9]
Vector Subtraction
Similarly we can define vector difference by using
Vector Difference=u-v
Orthogonal Projection
Sage Code
u = vector([3, 4, 0])
v = vector([1, 2, 2])
dot_product_uv = u.dot_product(v)
dot_product_vv = v.dot_product(v)
projection = (dot_product_uv / dot_product_vv) * v
print("Orthogonal projection of u onto v:", projection)
Output
Manual Verification
Explanation:
1. Plot Initialization:
o p = plot(vector([0, 0, 0]), color="black", legend_label="Origin")
creates an initial empty plot with a reference point at the origin.
2. Vector Visualization:
o arrow((0, 0, 0), u, color="blue", legend_label="u"): Plots the vector u
in blue.
o arrow((0, 0, 0), v, color="green", legend_label="v"): Plots the vector
v in green.
o arrow((0, 0, 0), projection, color="red", legend_label="Projection of
u onto v"): Plots the orthogonal projection of u onto v in red.
3. Aspect Ratio:
o p.show(aspect_ratio=1) ensures the scaling of all axes is equal,
providing an accurate geometric representation of the vectors.
4. # note(p+=y means p=p+y)