Skip to main content

Linear Algebra with SageMath: Learn math with open-source software

Section 2.1 Vectors

In this section, we will see how to define vectors, and perform basic operations on them.
Sage provides built-in support for vectors. In Sage, vectors are represented as \(n\)-tuples, \((v_1, v_2, ..., v_n) \in R^n\) where \(n\) is the number of components in the vector. Vectors can be defined using vector command, and passing the values of the vectors components.
The number of components \(n\) of a vector \(v = (v_1, \dots, v_n)\) is obtained in Sage by using the command degree.
To retrieve the components of a vector as a list, the method list can be used
Note that the return type of that command is the Python built-in List type; an ordered list of numbers where the order matters. As such, any and all native list methods can be used on the returned value.
Recall that lists in Sage are 0-indexed, meaning that the first element of the list is at index 0. To access a specific component of a vector, we can use vector indexing method.
The magnitude of a vector, \(||v|| = \sqrt{v_1^2 + v_2^2 + ... + v_n^2}\) is obtained in Sage by using the vector method norm.
A vector in \(R^{n+1}\) can be constructed from a vector in \(R^n\) by appending the values for the additional components.
Vectors in Sage can be created in different fields based on the datatype of the components of the vector. While working in a specific field, we need to explicitly pass the field when instantiating a vector using the vector command.
Note that \(ZZ\) is Sage notation for the Integers field. Similarly, \(QQ\) is for Rational numbers, \(RR\) for Reals, and \(CC\) for Complex numbers. The method base_ring returns the base ring of the vector.
If the field is omitted, Sage will infer the field from the datatype of the vector components.
For the complex field \(CC\text{,}\) Sage use \(i\) to represent the imaginary unit number.
The geometrical representation of a vector can be visualized as an arrow starting from the origin to the point \(P=(v_1, v_2, ..., v_n)\text{.}\) In lower dimensions (\(n \leq 3\)), the geometric representation is intuitive, but not so much in higher dimensions, where we should focus on abstract concepts rather than geometric representation.
To display a vector in Sage, we can use the internal method plot of vector class.
We can also obtain the same visual representation using Sage default plot method.