0% found this document useful (0 votes)
27 views11 pages

Module 1-Sage Vectors and Norm

This document provides an introduction to using SageMath for linear algebra, focusing on vector operations and norms. It includes examples of defining vectors, performing operations such as dot and cross products, calculating norms, and visualizing vectors in 2D. Additionally, it covers scalar multiplication, vector addition and subtraction, and orthogonal projections with corresponding SageMath code snippets.

Uploaded by

Gnana Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Module 1-Sage Vectors and Norm

This document provides an introduction to using SageMath for linear algebra, focusing on vector operations and norms. It includes examples of defining vectors, performing operations such as dot and cross products, calculating norms, and visualizing vectors in 2D. Additionally, it covers scalar multiplication, vector addition and subtraction, and orthogonal projections with corresponding SageMath code snippets.

Uploaded by

Gnana Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Getting Started with SageMath for Linear Algebra

22.01.2025
Module 1
Vector Operations and Norm
Defining a Vector

You can create a vector by passing a list of components to the vector()


function

Example:

# Define a vector in 2D # To print a vector

v1 = vector([1, 2]) print(v1) or show(v1)

# Define a vector in 3D

v2 = vector([3, 4, 5])

Vector Plotting in 2D

# Define the 2D vectors


v1 = vector([1, 2])
v2 = vector([-5, 7])
# Plot the vectors as arrows
plot_v1 = arrow((0, 0), (v1[0], v1[1]), color='green', thickness=2)
plot_v2 = arrow((0, 0), (v2[0], v2[1]), color='blue', thickness=2)
# Combine the plots and display
show(plot_v1 + plot_v2)
Explanation

• 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

Performing Operations on Vectors

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)

Code for norm of a vector


v = vector([1, 2, 2])
norm_v = v.norm()
print("Norm of v:", norm_v)

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

Plot the orthogonal projection vector


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)
p = plot(vector([0, 0, 0]), color="black", legend_label="Origin")
p += arrow((0, 0, 0), u, color="blue", legend_label="u")
p += arrow((0, 0, 0), v, color="green", legend_label="v")
p += arrow((0, 0, 0), projection, color="red", legend_label="Projection of u onto
v")
p.show(aspect_ratio=1)

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy