Matrices#
Introduction#
Definition: Matrix
In linear algebra, a matrix is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. Matrices are fundamental in representing and solving linear equations, performing transformations, and various other applications in mathematics and engineering.
Matrices are used to compactly represent and manipulate linear systems. They allow us to solve complex problems efficiently and provide a framework for understanding higher-dimensional spaces.
Solving Linear Equations#
Let’s start by considering this System of Linear equations:
Let’s put this into Matrix format:
# the first parameter '3' means use three rows
A = matrix(3, [1,2,3,0,1,2,0,0,1])
show(A)
b = vector([4,3,2])
show(b)
Documentation:
A.solve_right(b)
(0, -1, 2)
The output tells us the following values solve the equation:
\(x = 0\), \(y = -1\), and \(z = 2\).
A linear system may have no solution, one solution or many solutions. We will explore this aspect in a subsequent notebook.
# get the 'solution' vector from solve_right
s = A.solve_right(b)
The solution vector contains the values for x, y, z. We can plug those back into our system:
\(As = b\)
A*s
(4, 3, 2)
As expected, the result from \(As\) is equal to the value of \(b\) vector:
A*s == b
True
Documentation: sage.matrix.matrix2.Matrix.solve_right
Solve right versus Solve left#
In SageMath, solve_right and solve_left are methods used to solve matrix equations. The difference lies in which side of the equation the matrix is placed and which side the solution is returned.
solve_right: This method is used to solve matrix equations of the form ( AX = B ), where ( A ) is the matrix and ( B ) is the right-hand side. In other words, you provide the matrix ( A ) and the result ( B ), and the method will return the solution ( X ).solve_left: This method is used to solve matrix equations of the form ( XA = B ), where ( A ) is the matrix and ( B ) is the right-hand side. Here, you provide the matrix ( A ) and the result ( B ), and the method returns the solution ( X ).
What are QQ, ZZ, RR, CC, etc?#
You will frequently see matrix() and vector() having the first parameter like the following:
A = matrix(QQ, 3, [1,2,3,0,1,2,0,0,1])
b = vector(QQ, [4,3,2])
Here QQ means Rational Numbers. Other number types are described below.
Definition: Ring
In mathematics, a ring is an algebraic structure consisting of a set equipped with two binary operations: addition and multiplication. These operations must satisfy certain properties, such as associativity and distributivity. Rings generalize the concepts of arithmetic operations found in familiar number systems.
The term “ring” is used because the operations of addition and multiplication form a structure that “rings” around itself, meaning the results of these operations always remain within the set, forming a closed loop or “ring.” This closed-loop nature is a key characteristic of rings, differentiating them from other algebraic structures like groups and fields.
Many common sets of numbers naturally form rings:
\(ZZ\) - Integer Numbers
\(RR\) - Real Number
\(QQ\) - Rational Numbers
\(CC\) - Complex Numbers
\(GF(x)\) - Finite Field of size x
Closely related to Rings are Fields:
Definition: Field
A field is a special type of ring where not only addition and multiplication are defined and follow the standard arithmetic properties, but also every non-zero element has a multiplicative inverse. This means for every non-zero element \(a\) in a field, there exists an element \(b\) such that \(a \cdot b = 1\). Fields are fundamental in algebra because they allow for division by non-zero elements, making them very versatile and powerful in solving equations and various mathematical problems.
The term “field” reflects its broad applicability and utility in various areas of mathematics, much like how fields of study encompass wide areas of knowledge.
Fields ensure that operations like addition, subtraction, multiplication, and division (except by zero) can be performed within the set, providing a robust framework for various algebraic and geometric concepts.
You can change the type using change_ring()
v = vector(QQ['x,y'], [1..5])
v = v.change_ring(GF(3))
# display the current ring
v.parent()
Vector space of dimension 5 over Finite Field of size 3
Here the output reports the Field type, Finite Field of size 3, i.e. GF(3).
We cover “Vector Space” and “Dimensions” in later lessons.
A = Matrix(QQ, 2, 2, [1/2, 1/3, 1/3, 1/4])
A = A.change_ring(GF(5))
# display the current ring
A.parent()
Full MatrixSpace of 2 by 2 dense matrices over Ring of integers modulo 5
Note that both v (GF(3)) and A (GF(5)) are finite fields. The distinction between “finite field” and “ring of integers modulo 5” in the outputs may be a matter of how SageMath represents and labels the structures.
Documentation: