Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 79

Basic vector operations

In this module we will learn how to define vectors, and do basic arithmetic operations with them, including:

  • vector addition
  • scalar multipication
  • dot products
  • cross products
  • triple products

 

How to define vectors
# Example 1: Let a = (0,4), b = (3,-1), c = (2,-1,-4) be vectors. # Here is one way to define them in Sage: a = vector( [0,4] ) b = vector( [3,-1] ) c = vector( [2,-1,-4] ) # To check the result, try: print a, b, c
(0, 4) (3, -1) (2, -1, -4)
The implementation of vector addition and scalar multipication is very natural and intuiutive in sage. See the following example.
# Example 2: Let us define some vectors and add them. # Let a = (0,4), b = (3,-1), c = (2,-1,-4), d = (-3,2,1). # Find a+b, and c+d. Try to find a+c and/or a+d. # Find 3a-2b, and 0c+3d. a = vector( [0,4] ) b = vector( [3,-1] ) c = vector( [2,-1,-4] ) d = vector( [-3,2,1] ) print a+b print c+d #print a+c print 3*a - 2*b print 0*c + 3*d
(3, 3) (-1, 1, -3) (-6, 14) (-9, 6, 3)
Here is how to see vector addition graphically:
# Example 2b: Use the same vectors from Example 2: # Let a = (0,4), b = (3,-1), c = (2,-1,-4), d = (-3,2,1). # Graph a+b, and c+d. a = vector( [0,4] ) b = vector( [3,-1] ) c = vector( [2,-1,-4] ) d = vector( [-3,2,1] ) # This graphs a+b: plot(a) + plot(b, start=a) + plot(a+b) # This graphs c+d: plot(c) + plot(d, start=c) + plot(c+d)
3D rendering not yet implemented
Exercise 1: Do these problems from Sec.9.2 of our textbook:

16, 18, 27.

For 16 and 18, also include a graph showing a{\bf a}, b{\bf b} and a+b{\bf a+b}.

For 27, show a graph of the relevant vector addition, together with the algebraic form of the answer.

NOTE: Although you can write your own code to find the absolute value of your vectors, the abs() function in sage does the right thing with vectors as well.
The implementation of dot products uses the function dot_product as shown in the following example.
# Example 2: Let's define the same vectors we had in Example 2 and dot them. # Let a = <0,4>, b = <3,-1>, c = <2,-1,-4>, d = <-3,2,1>. a = vector( [0,4] ) b = vector( [3,-1] ) c = vector( [2,-1,-4] ) d = vector( [-3,2,1] ) print 'a.b=', a.dot_product(b) print 'c.d=', c.dot_product(d) # print 'a.c=', a.dot_product(c) # uncomment to see the error message you get
a.b= -4 c.d= -12
# Example 3: Let's define the same vectors we had in Example 2 and dot them. # Let a = <0,4>, b = <3,-1>, c = <2,-1,-4>, d = <-3,2,1>. a = vector( [0,4] ) b = vector( [3,-1] ) c = vector( [2,-1,-4] ) d = vector( [-3,2,1] ) print 'a.b=', a.dot_product(b) print 'c.d=', c.dot_product(d) # print 'a.c=', a.dot_product(c) # uncomment to see the error message you get
a.b= -4 c.d= -12
You can also do vector operations when you have parameters or variables as your vector components.
# Example 4: Let a=<-t,3t,2t>, b=<e^t,-e^t,2e^t>. # Find a+b, a.b, 3a-2b. t = var('t') a = vector( [-t,3*t,2*t] ) b = vector( [e^t,-e^t,2*e^t] ) print 'a+b =', a+b print 'a.b =', a.dot_product(b) print '3a-2b =', 3*a-2*b
a+b = (-t + e^t, 3*t - e^t, 2*t + 2*e^t) a.b = 0 3a-2b = (-3*t - 2*e^t, 9*t + 2*e^t, 6*t - 4*e^t)
Exercise 2: Do these problems from Sec.9.3 of our textbook:

18, 23, 45.

For 23, also include a graph showing the triangle in 3D.
The implementation of cross products uses the function cross_product

Note that you must define 2D vectors with 3 components for cross_product to work right.
# Example 5: Use the same vectors we had in Example 2 and cross them. # Let a = <0,4>, b = <3,-1>, c = <2,-1,-4>, d = <-3,2,1>. # Find a x b, c x d. Also, show the result graphically. a = vector( [0,4,0] ) b = vector( [3,-1,0] ) c = vector( [2,-1,-4] ) d = vector( [-3,2,1] ) print 'a x b=', a.cross_product(b) print 'c x d=', c.cross_product(d) plot(a) + plot(b) + plot( a.cross_product(b) )
a x b= (0, 0, -12) c x d= (7, 10, 1)
3D rendering not yet implemented
# Example 6: Let's try some triple products, both scalar and vector. # Suppose a=<1,-4,-1>, b=<0,3,0>, c=<1,2,3>, d=<-1,-2,0> # Find a.(b x c) and a x (b x c) a = vector( [1,-4,-1] ) b = vector( [0,3,0] ) c = vector( [1,2,3] ) res1 = a.dot_product( b.cross_product(c) ) res2 = a.cross_product( b.cross_product(c) ) print 'a.(b x c) =', res1 print 'a x (b x c) =', res2
a.(b x c) = 12 a x (b x c) = (12, -6, 36)
# Example 6b: Verify that for the vectors in Example 6, the # following scalar triple products are equal: # a.(b x c) = b.(c x a) = c.(a x b) a = vector( [1,-4,-1] ) b = vector( [0,3,0] ) c = vector( [1,2,3] ) res1 = a.dot_product( b.cross_product(c) ) res2 = b.dot_product( c.cross_product(a) ) res3 = c.dot_product( a.cross_product(b) ) print 'a.(b x c) =', res1 print 'b.(c x a) =', res2 print 'c.(a x b) =', res3
a.(b x c) = 12 b.(c x a) = 12 c.(a x b) = 12
Exercise 3: Do these problems from Sec.9.4 of our textbook:

28, 32.

For 32, also include a graph showing the vectors you used to check for coplanarity.