Matrix: Difference between revisions
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
A '''matrix''' (plural: '''matrices''') is a 2-dimensional ordered list of numbers: | |||
$$ | |||
\mathbf{M}_{m \times n} = | |||
\begin{bmatrix} | |||
a_{11} & a_{12} & \cdots & a_{1n} \\ | |||
a_{21} & a_{22} & \cdots & a_{2n} \\ | |||
\vdots & \vdots & \ddots & \vdots \\ | |||
a_{m1} & a_{m2} & \cdots & a_{mn} | |||
\end{bmatrix}, | |||
$$ | |||
where element $a_{ij}$ is in row $i$, column $j$. | |||
It can be therefore written: | |||
$$ | |||
\mathbf{M}_{m \times n} = (a_{ij}),i \in \{1,\dots,m\}, j \in \{1,\dots,n\} | |||
$$ | |||
== Formal definition == | |||
Any function satisfying | |||
$$ | |||
\mathbf{M} : \{1,\dots,m\} \times \{1,\dots,n\} \to \mathbb{F} | |||
$$ | |||
is a matrix of '''dimension''' $m \times n$ over [[field (mathematical concept)|field]] $\mathbb{F}$. | |||
== Dimension == | |||
If a matrix $\mathbb{M}_{m \times n}$ has $m$ rows and $n$ columns, its '''dimension''' is $m \times n$. | |||
Special cases: | |||
{| class="wikitable" | |||
|- | |||
! | |||
! Known as | |||
|- | |||
| $m=n$ | |||
| '''Square matrix''' | |||
|- | |||
| $m=1$ | |||
| '''Row vector''' | |||
|- | |||
| $n=1$ | |||
| '''Column vector''' | |||
|} | |||
== [[Binary operation]]s == | |||
=== Single matrix operations === | |||
For matrice $\mathbf{A}=(a_{ij}) \in \mathbb{F}$: | |||
{| class="wikitable" | |||
|- | |||
! Name | |||
! Operation | |||
|- | |||
| '''scalar multiplication''' | |||
| $c\mathbf{A} = (c\,a_{ij})$ | |||
|- | |||
| '''transposition''' | |||
| $\mathbf{A}^T = (a_{ji})$ | |||
|} | |||
=== Two matrices operations === | |||
==== Matrix addition/subtraction ==== | |||
It is defined for two matrices of '''identical sizes''' $\mathbf{A}_{m \times n}=(a_{ij}), \mathbf{B}_{m \times n}=(b_{ij})\in \mathbb{F}$: | |||
$$ | |||
(\mathbf{A} \pm \mathbf{B})= (a_{ij} \pm b_{ij}) | |||
$$ | |||
==== Matrix multiplication ==== | |||
It is defined for two matrices of '''compatible sizes''' $\mathbf{A}_{m \times n}=(a_{ij}), \mathbf{B}_{n \times p}=(b_{ij})\in \mathbb{F}$: | |||
$$ | |||
\forall i \in \{1,\dots,m\}, j \in \{1,\dots,p\} \\ | |||
(\mathbf{A}\mathbf{B})_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj}. | |||
$$ | |||
<blockquote> | |||
Notice that $\mathbf{AB}$ is a $m \times p$ matrix over $\mathbb{F}$ | |||
</blockquote> | |||
== Identity matrix == | |||
If '''square matrix''' $\mathbf{I}=(a_{ij})$ of dimension $m \times m$ satisfies | |||
$$ | |||
\begin{cases} | |||
i=j: a_{ij}=1 \\ | |||
i \neq j: a_{ij}=0 | |||
\end{cases} | |||
$$ | |||
then it is an '''identity matrix''' of dimension $m$, denoted $\mathbf{I}_m$. | |||
From the definition of [[#Matrix multiplication]], it can be inferred that | |||
$$ | |||
\mathbf{A}_{m \times n}\mathbf{I}_n = \mathbf{A}_{m \times n} = \mathbf{I}_m\mathbf{A}_{m \times n} | |||
$$ | |||
== Determinant == | |||
== Matrix operations in python == | == Matrix operations in python == | ||
Latest revision as of 12:07, 17 October 2026
A matrix (plural: matrices) is a 2-dimensional ordered list of numbers:
$$ \mathbf{M}_{m \times n} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}, $$ where element $a_{ij}$ is in row $i$, column $j$.
It can be therefore written:
$$ \mathbf{M}_{m \times n} = (a_{ij}),i \in \{1,\dots,m\}, j \in \{1,\dots,n\} $$
Formal definition
Any function satisfying
$$ \mathbf{M} : \{1,\dots,m\} \times \{1,\dots,n\} \to \mathbb{F} $$
is a matrix of dimension $m \times n$ over field $\mathbb{F}$.
Dimension
If a matrix $\mathbb{M}_{m \times n}$ has $m$ rows and $n$ columns, its dimension is $m \times n$.
Special cases:
| Known as | |
|---|---|
| $m=n$ | Square matrix |
| $m=1$ | Row vector |
| $n=1$ | Column vector |
Single matrix operations
For matrice $\mathbf{A}=(a_{ij}) \in \mathbb{F}$:
| Name | Operation |
|---|---|
| scalar multiplication | $c\mathbf{A} = (c\,a_{ij})$ |
| transposition | $\mathbf{A}^T = (a_{ji})$ |
Two matrices operations
Matrix addition/subtraction
It is defined for two matrices of identical sizes $\mathbf{A}_{m \times n}=(a_{ij}), \mathbf{B}_{m \times n}=(b_{ij})\in \mathbb{F}$:
$$ (\mathbf{A} \pm \mathbf{B})= (a_{ij} \pm b_{ij}) $$
Matrix multiplication
It is defined for two matrices of compatible sizes $\mathbf{A}_{m \times n}=(a_{ij}), \mathbf{B}_{n \times p}=(b_{ij})\in \mathbb{F}$:
$$\begin{gathered} \forall i \in \{1,\dots,m\}, j \in \{1,\dots,p\} \\ (\mathbf{A}\mathbf{B})_{ij} = \sum_{k=1}^{n} a_{ik} b_{kj}. \end{gathered}$$
Notice that $\mathbf{AB}$ is a $m \times p$ matrix over $\mathbb{F}$
Identity matrix
If square matrix $\mathbf{I}=(a_{ij})$ of dimension $m \times m$ satisfies
$$ \begin{cases} i=j: a_{ij}=1 \\ i \neq j: a_{ij}=0 \end{cases} $$
then it is an identity matrix of dimension $m$, denoted $\mathbf{I}_m$.
From the definition of #Matrix multiplication, it can be inferred that
$$ \mathbf{A}_{m \times n}\mathbf{I}_n = \mathbf{A}_{m \times n} = \mathbf{I}_m\mathbf{A}_{m \times n} $$
Determinant
Matrix operations in python
Initialisation
import numpy as np # import dependencies: always the 1st step!
The matrices are to be inputted as 2D numpy arrays, so:
$$ \forall a_{ij} \in \mathbf{A}, \text{a[i][j]}=a_{ij} $$
For example, to initialise the identity matrice $\mathbf{I}_3$:
$$ I=\begin{pmatrix}1&0&0\\0&1&0\\0&0&1\end{pmatrix} $$
I=np.array([[1,0,0],[0,1,0],[0,0,1]])
For two matrices $\mathbf{A},\mathbf{B}$ initialised as 2D arrays:
Single matrix operations
Transposition, determinant, comatrix, inverse
A_T=A.T # Transpose A: -> 2D nparray: since A is an nparray, nupmy understands what you mean!
det_A=np.linalg.det(A): -> float
inverse_A=np.linalg.inv(A): -> 2D nparray
Element-wise,column-wise, and row-wise sum
sum_A=np.sum(A) # -> float: element-wise sum of A
column_wise_sum=np.sum(A, axis=0): -> 1D nparray
row_wise_sum=np.sum(A, axis=1): -> 1D nparray
Two matrices operations
Element-wise operations
sum_A_B=np.add(A,B) # A+B
difference_A_B=np.subtract(A,B) # A-B
element_wise_product_A_B=np.multiply(A,B) # A \circ B
quotient_A_B=np.divide(A,B) # A/B
Be extremely careful with
np.multiply, which gives the element-wise product, NOT the matrix product.
Matrix operations
product_A_B=np.dot(A,B) # A.B