Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – working with vectors


Vectors have important applications in physics and engineering. Vectors are used to represent position, velocity, angular momentum, electromagnetic fields, and so on. Let's see how to perform some basic operations with vectors in Sage. You can enter the following code in an input cell in a worksheet, or enter it line by line in the interactive shell. You can also enter the code into a plain text file, save it with a .sage extension, and run it from the Sage command line as described in the previous chapter:

R3 = VectorSpace(QQ, 3)
(b1, b2, b3) = R3.basis()
print("Basis for space:")
print b1
print b2
print b3

vector1 = R3([-1, 2, 7])    # define some vectors
vector2 = R3([4, -9, 2])

print("Linear combinations:")
var('a b')
print(a * vector1 + b * vector2)

print("Norm of vector 1:")
print(sqrt(vector1 * vector1))    # definition
print(vector1.norm())    # using norm method

print("Scalar multiplication:")
print(2 * vector1)

print("Scalar (dot) products...