from edumath.linear_algebra import identity_matrix, matrix_shape
A = [[1, -2, 4], [0, 3, 5]]
print(matrix_shape(A))
print(identity_matrix(3))(2, 3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
A matrix is a rectangular array of numbers. Matrices are everywhere in linear algebra because they can store data, organize equations, and describe how vectors move.
For example,
\[ A=\begin{bmatrix} 2 & -1 & 0\\ 4 & 3 & 5 \end{bmatrix} \]
has 2 rows and 3 columns, so its shape is \(2\times 3\).
By the end of this lesson, you should be able to:
A matrix entry is named by row first, column second. If
\[ A=\begin{bmatrix} 4 & 5\\ 7 & 8 \end{bmatrix}, \]
then \(a_{21}=7\) because row 2, column 1 contains 7.
The notation \(a_{ij}\) means the entry in row \(i\) and column \(j\). The row index comes first. This is like saying “go down to the correct row, then move across to the correct column.”
The shape of a matrix is always
\[ \text{number of rows} \times \text{number of columns}. \]
A \(3\times 2\) matrix has 3 rows and 2 columns:
\[ \begin{bmatrix} 1 & 2\\ 3 & 4\\ 5 & 6 \end{bmatrix}. \]
Shape is not a small detail. It decides what operations are possible. A matrix with 3 columns can multiply a vector with 3 entries, but not a vector with 2 entries.
A zero matrix has every entry equal to zero:
\[ \begin{bmatrix} 0 & 0\\ 0 & 0 \end{bmatrix}. \]
The identity matrix is the matrix version of multiplying by 1:
\[ I_2=\begin{bmatrix} 1 & 0\\ 0 & 1 \end{bmatrix}. \]
It leaves vectors unchanged: \(I\mathbf{v}=\mathbf{v}\).
A diagonal matrix has possible nonzero entries only on the main diagonal:
\[ \begin{bmatrix} 2 & 0\\ 0 & 5 \end{bmatrix}. \]
This matrix scales the first coordinate by 2 and the second coordinate by 5.
The system
\[ \begin{aligned} 2x-y &= 3,\\ 4x+5y &= 7 \end{aligned} \]
has coefficient matrix
\[ \begin{bmatrix} 2 & -1\\ 4 & 5 \end{bmatrix}. \]
The constants on the right side form a separate vector \(\begin{bmatrix}3\\7\end{bmatrix}\).
Let
\[ B=\begin{bmatrix} 1 & -2 & 4\\ 0 & 3 & 5 \end{bmatrix}. \]
Do not reverse the order of shape. A matrix with 2 rows and 3 columns is \(2\times 3\), not \(3\times 2\).
Matrix questions are usually reading questions before they are calculation questions. First find the shape, then locate rows, columns, and entries.
Guess first. Is a matrix with 4 rows and 2 columns a \(4\times2\) matrix or a \(2\times4\) matrix?
Guided exercise.
For
\[ A=\begin{bmatrix} 1&0&3\\ -2&5&4 \end{bmatrix}, \]
count rows first and columns second:
Checkpoint. Find the shape of \(\begin{bmatrix}1&2\\3&4\\5&6\\7&8\end{bmatrix}\).
Guess first. In \(a_{23}\), do you look at row 2 or column 2 first?
Guided exercise.
Let
\[ B=\begin{bmatrix} 9&8&7\\ 6&5&4 \end{bmatrix}. \]
To find \(b_{23}\):
Checkpoint. For the same matrix \(B\), find \(b_{11}\), \(b_{12}\), and \(b_{21}\).
Guess first. Which matrix should leave every vector unchanged: zero, identity, or diagonal?
Guided exercise.
The identity matrix
\[ I_2=\begin{bmatrix}1&0\\0&1\end{bmatrix} \]
leaves vectors unchanged:
\[ I_2\begin{bmatrix}a\\b\end{bmatrix}=\begin{bmatrix}a\\b\end{bmatrix}. \]
A zero matrix sends every vector to the zero vector. A diagonal matrix scales coordinates separately.
Checkpoint. Describe what \(\begin{bmatrix}4&0\\0&-1\end{bmatrix}\) does to \(\begin{bmatrix}x\\y\end{bmatrix}\).
Guess first. Where does the constant term go when you build a coefficient matrix?
Guided exercise.
For
\[ \begin{aligned} 3x-2y&=7,\\ -x+5y&=4, \end{aligned} \]
the coefficients form
\[ A=\begin{bmatrix}3&-2\\-1&5\end{bmatrix}, \qquad \mathbf{b}=\begin{bmatrix}7\\4\end{bmatrix}. \]
The constants are not part of the coefficient matrix. They belong in the right-hand-side vector.
Checkpoint. Write the coefficient matrix and right-hand-side vector for \(2x+3y=10\) and \(4x-y=-1\).
This checkpoint asks about shapes, entries, and special matrices.
Use these examples to inspect matrices with Python. SymPy is especially nice when you want exact entries such as fractions.
(2, 3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]