Skip to content

Commit 2b217c4

Browse files
committed
Add more matrices concepts
1 parent 713c097 commit 2b217c4

2 files changed

Lines changed: 234 additions & 0 deletions

File tree

content/ai/math/algebra/matrices/index.en.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,120 @@ $$
532532
For an \(m \times n\) matrix: \(\text{rank}(\mathbf{A}) \leq \min(m, n)\).
533533

534534
When \(\text{rank}(\mathbf{A}) = \min(m,n)\), \(\mathbf{A}\) is **full rank**. Otherwise it is **rank-deficient** and maps a nonzero subspace of inputs to zero.
535+
536+
### The Rank-Nullity theorem
537+
538+
This theorem elegantly connects the three fundamental subspaces.
539+
540+
For any matrix \(\mathbf{A} \in \mathbb{R}^{m \times n}\):
541+
542+
$$
543+
\boxed{\text{rank}(\mathbf{A}) + \text{nullity}(\mathbf{A}) = n}
544+
$$
545+
546+
where \(\text{nullity}(\mathbf{A}) = \dim(\text{null}(\mathbf{A}))\).
547+
548+
{{< details title="Proof sketch" closed="true" >}}
549+
Let \(r = \text{rank}(\mathbf{A})\) and let \(\{\mathbf{v}_1, \ldots, \mathbf{v}_{n-r}\}\) be a basis for \(\text{null}(\mathbf{A})\). Extend this to a basis for all of \(\mathbb{R}^n\) by adding \(r\) vectors \(\{\mathbf{w}_1, \ldots, \mathbf{w}_r\}\) from the row space of \(\mathbf{A}\) (which is orthogonal to the null space). The images \(\{\mathbf{A}\mathbf{w}_1, \ldots, \mathbf{A}\mathbf{w}_r\}\) are linearly independent (can be shown) and span \(\text{col}(\mathbf{A})\). So \(\dim(\text{col}(\mathbf{A})) = r\). The total basis has \(r + (n - r) = n\) elements, matching \(\dim(\mathbb{R}^n) = n\).
550+
551+
{{< callout type="info" >}}
552+
In plain English: the \(n\) input dimensions split cleanly into two complementary parts. Some (\(r\) dimensions, the row space) get mapped to nonzero outputs. The rest (\(n - r\) dimensions, the null space) get mapped to zero. These two parts partition the input space completely and without overlap — which is why their dimensions must sum to exactly \(n\).
553+
{{< /callout >}}
554+
{{< /details >}}
555+
556+
{{< callout >}}
557+
In ML terms: if you have a weight matrix \(\mathbf{W}\) of shape \(\mathbf{W}\) with \(n > m\) (an "over-parameterized" layer), then \(\text{nullity}(\mathbf{W}) \geq n - m > 0\). There is a whole subspace of weight perturbations that produce zero change in the layer's output. This is one reason why over-parameterized models can be aggressively pruned and compressed, many weight directions literally do nothing.
558+
{{< /callout >}}
559+
560+
### The inverse
561+
562+
For a **square** matrix \(\mathbf{A} \in \mathbb{R}^{n \times n}\), the **inverse** \(\mathbf{A}^{-1}\) is the unique matrix satisfying:
563+
564+
$$
565+
\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}_n
566+
$$
567+
568+
The **invertible matrix theorem** states that the following conditions are all equivalent, *if any one holds, all hold, and the inverse exists*:
569+
570+
- \(\text{rank}(\mathbf{A}) = n\) (full rank)
571+
- \(\det(\mathbf{A}) \neq 0\)
572+
- \(\text{null}(\mathbf{A}) = \{\mathbf{0}\}\) (trivial null space)
573+
- The columns of \(\mathbf{A}\) are linearly independent
574+
- The rows of \(\mathbf{A}\) are linearly independent
575+
- The equation \(\mathbf{A}\mathbf{x} = \mathbf{b}\) has a unique solution for every \(\mathbf{b} \in \mathbb{R}^n\)
576+
577+
{{< callout type="important" >}}
578+
All six conditions are equivalent ways of saying the same thing, the transformation is fully reversible. If the matrix collapses any dimension, there is a nonzero null space, the determinant is zero, then you cannot undo the transformation. All roads lead to the same conclusion: invertibility is an all-or-nothing property.
579+
{{< /callout >}}
580+
581+
Properties of the inverse:
582+
- *Double inverse*: \((\mathbf{A}^{-1})^{-1} = \mathbf{A}\)
583+
- *Product inverse*: \((\mathbf{AB})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1}\) (order reverses, just as with the transpose)
584+
- *Transpose-inverse commutativity*: \((\mathbf{A}^\top)^{-1} = (\mathbf{A}^{-1})^\top\)
585+
- *Determinant*: \(\det(\mathbf{A}^{-1}) = \frac{1}{\det(\mathbf{A})}\)
586+
587+
#### Computing the inverse
588+
589+
For \(2 \times 2\) matrices, e.g. \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) with \(\det(\mathbf{A}) = ad - bc \neq 0\), the inverse has a closed-form formula:
590+
591+
We seek \(\mathbf{A}^{-1} = \begin{bmatrix} p & q \\ r & s \end{bmatrix}\) such that \(\mathbf{A}\mathbf{A}^{-1} = \mathbf{I}\):
592+
593+
$$
594+
\begin{aligned}
595+
\begin{bmatrix} a & b \\ c & d \end{bmatrix}\begin{bmatrix} p & q \\ r & s \end{bmatrix} &= \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \\
596+
\begin{bmatrix} ap+br & aq+bs \\ cp+dr & cq+ds \end{bmatrix} &= \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
597+
\end{aligned}
598+
$$
599+
600+
From the first column: \(ap + br = 1\) and \(cp + dr = 0\). Solving:
601+
$$
602+
\begin{aligned}
603+
p &= \frac{d}{ad-bc} \\
604+
r &= \frac{-c}{ad-bc}
605+
\end{aligned}
606+
$$
607+
608+
From the second column: \(aq + bs = 0\) and \(cq + ds = 1\). Solving:
609+
$$
610+
\begin{aligned}
611+
q &= \frac{-b}{ad-bc} \\
612+
s &= \frac{a}{ad-bc}
613+
\end{aligned}
614+
$$
615+
616+
Therefore:
617+
$$
618+
\begin{aligned}
619+
\mathbf{A}^{-1} &= \begin{bmatrix} p & q \\ r & s \end{bmatrix} \\
620+
\mathbf{A}^{-1} &= \begin{bmatrix} \frac{d}{ad-bc} & \frac{-b}{ad-bc} \\ \frac{-c}{ad-bc} & \frac{a}{ad-bc} \end{bmatrix} \\
621+
\mathbf{A}^{-1} &= \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}
622+
\end{aligned}
623+
$$
624+
625+
Knowing that \(\det(\mathbf{A}) = ad - bc\), then:
626+
627+
$$
628+
\boxed{\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}}
629+
$$
630+
631+
{{< callout type="info" >}}
632+
To invert a \(2 \times 2\) matrix, swap the diagonal entries, negate the off-diagonal entries, and divide everything by the determinant. The determinant appears in the denominator, which is exactly why a zero determinant makes the inverse undefined (division by zero).
633+
{{< /callout >}}
634+
635+
For \(n > 2\), the inverse is computed in practice via [**Gauss-Jordan elimination**](https://en.wikipedia.org/wiki/Gaussian_elimination) on the augmented matrix \([\mathbf{A}\,|\,\mathbf{I}]\).
636+
637+
## Machine Learning and AI perspective
638+
639+
Matrix operations are not merely computational primitives, they are the conceptual vocabulary of modern Machine Learning research, for example:
640+
641+
**Low-rank structure in fine-tuning.** [Hu et al. (2021)](https://arxiv.org/abs/2106.09685), which observes that weight update matrices \(\Delta\mathbf{W}\) during fine-tuning of large language models are empirically low-rank. Rather than storing the full \(\Delta\mathbf{W} \in \mathbb{R}^{d \times d}\), LoRA decomposes it as \(\Delta\mathbf{W} = \mathbf{B}\mathbf{A}\) where \(\mathbf{B} \in \mathbb{R}^{d \times r}\), \(\mathbf{A} \in \mathbb{R}^{r \times d}\), with \(r \ll d\). This is valid precisely because a rank-\(r\) matrix has a natural factorization into two thin matrices, the rank concept we just derived. LoRA reduces trainable parameters by over 10000x on GPT-3 class models, and the entire insight rests on rank intuition.
642+
643+
## Common pitfalls and debugging
644+
645+
1. **Confusing shape conventions across frameworks**. NumPy uses `(batch, features)` convention. PyTorch's `nn.Linear(in_features, out_features)` stores its weight matrix as shape `(out_features, in_features)` and computes `x @ W.T + b` internally. So \(\mathbf{W}\) is stored transposed relative to the mathematical convention. If you manually initialize weights, verify with a forward pass on dummy data before trusting gradients.
646+
647+
2. **Inverting nearly singular matrices**. `np.linalg.inv()` returns a result even for near-singular matrices, the numbers will be astronomically large and numerically meaningless. Always prefer `np.linalg.solve(A, b)` over `np.linalg.inv(A) @ b` for solving linear systems. Check `np.linalg.cond(A)` before inverting; condition numbers above \(10^{12}\) are a red flag.
648+
649+
3. **Testing `det == 0` for singularity**. In floating point, the determinant of a truly singular matrix is almost never exactly zero, it will be some very small number like `1e-17`. Do not use determinant as a singularity test in code. Use `np.linalg.matrix_rank(A) < n` or `np.linalg.cond(A) > threshold` instead.
650+
651+
4. **Forgetting that matrix multiplication is non-commutative**. The most common algebraic error when implementing attention from scratch. \(\mathbf{AB} \neq \mathbf{BA}\). Even when both products are well-defined and have the same shape (square matrices), they will produce different results. When in doubt, track shapes explicitly and reason about the semantics of each multiplication.

content/ai/math/algebra/matrices/index.es.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,120 @@ $$
541541
Para una matriz \(m \times n\): \(\text{rango}(\mathbf{A}) \leq \min(m, n)\).
542542

543543
Cuando \(\text{rango}(\mathbf{A}) = \min(m,n)\), \(\mathbf{A}\) tiene **rango completo**. De lo contrario es **deficiente en rango** y mapea un subespacio no nulo de entradas a cero.
544+
545+
### El teorema Rango-Nulidad
546+
547+
Este teorema conecta elegantemente los tres subespacios fundamentales.
548+
549+
Para cualquier matriz \(\mathbf{A} \in \mathbb{R}^{m \times n}\):
550+
551+
$$
552+
\boxed{\text{rango}(\mathbf{A}) + \text{nulidad}(\mathbf{A}) = n}
553+
$$
554+
555+
donde \(\text{nulidad}(\mathbf{A}) = \dim(\text{null}(\mathbf{A}))\).
556+
557+
{{< details title="Esquema de demostración" closed="true" >}}
558+
Sea \(r = \text{rango}(\mathbf{A})\) y sea \(\{\mathbf{v}_1, \ldots, \mathbf{v}_{n-r}\}\) una base para \(\text{null}(\mathbf{A})\). Extiende esto a una base de \(\mathbb{R}^n\) añadiendo \(r\) vectores \(\{\mathbf{w}_1, \ldots, \mathbf{w}_r\}\) del espacio fila de \(\mathbf{A}\) (que es ortogonal al espacio nulo). Las imágenes \(\{\mathbf{A}\mathbf{w}_1, \ldots, \mathbf{A}\mathbf{w}_r\}\) son linealmente independientes y generan \(\text{col}(\mathbf{A})\). Por tanto \(\dim(\text{col}(\mathbf{A})) = r\). La base total tiene \(r + (n - r) = n\) elementos, coincidiendo con \(\dim(\mathbb{R}^n) = n\).
559+
560+
{{< callout type="info" >}}
561+
En otras palabras: las \(n\) dimensiones de entrada se dividen limpiamente en dos partes complementarias. Algunas (\(r\) dimensiones, el espacio fila) se mapean a salidas no nulas. Las demás (\(n - r\) dimensiones, el espacio nulo) se mapean a cero. Estas dos partes particionan el espacio de entrada completamente y sin solapamiento, por eso sus dimensiones deben sumar exactamente \(n\).
562+
{{< /callout >}}
563+
{{< /details >}}
564+
565+
{{< callout >}}
566+
En términos de ML, si tienes una matriz de pesos \(\mathbf{W}\) de forma \(m \times n\) con \(n > m\) (una capa "sobreparametrizada"), entonces \(\text{nulidad}(\mathbf{W}) \geq n - m > 0\). Existe todo un subespacio de perturbaciones de pesos que produce cambio cero en la salida de la capa. Ésta es una razón por la que los modelos sobreparametrizados pueden podarse y comprimirse agresivamente, muchas direcciones de peso literalmente no hacen nada.
567+
{{< /callout >}}
568+
569+
### La inversa
570+
571+
Para una matriz **cuadrada** \(\mathbf{A} \in \mathbb{R}^{n \times n}\), la **inversa** \(\mathbf{A}^{-1}\) es la única matriz que satisface:
572+
573+
$$
574+
\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}_n
575+
$$
576+
577+
El **teorema de la matriz invertible** establece que las siguientes condiciones son todas equivalentes, *si cualquiera se cumple, todas se cumplen, y la inversa existe*:
578+
579+
- \(\text{rango}(\mathbf{A}) = n\) (rango completo)
580+
- \(\det(\mathbf{A}) \neq 0\)
581+
- \(\text{null}(\mathbf{A}) = \{\mathbf{0}\}\) (espacio nulo trivial)
582+
- Las columnas de \(\mathbf{A}\) son linealmente independientes
583+
- Las filas de \(\mathbf{A}\) son linealmente independientes
584+
- La ecuación \(\mathbf{A}\mathbf{x} = \mathbf{b}\) tiene solución única para todo \(\mathbf{b} \in \mathbb{R}^n\)
585+
586+
{{< callout type="important" >}}
587+
Las seis condiciones son formas equivalentes de decir lo mismo, la transformación es completamente reversible. Si la matriz colapsa alguna dimensión, hay un espacio nulo no trivial, el determinante es cero, ya no puedes deshacer la transformación. Todos los caminos llevan a la misma conclusión: la invertibilidad es una propiedad de todo o nada.
588+
{{< /callout >}}
589+
590+
Propiedades de la inversa:
591+
- *Doble inversa*: \((\mathbf{A}^{-1})^{-1} = \mathbf{A}\)
592+
- *Inversa del producto*: \((\mathbf{AB})^{-1} = \mathbf{B}^{-1}\mathbf{A}^{-1}\) (el orden se invierte, igual que con la transpuesta)
593+
- *Conmutatividad transpuesta-inversa*: \((\mathbf{A}^\top)^{-1} = (\mathbf{A}^{-1})^\top\)
594+
- *Determinante*: \(\det(\mathbf{A}^{-1}) = \frac{1}{\det(\mathbf{A})}\)
595+
596+
#### Cálculo de la inversa
597+
598+
Para matrices \(2 \times 2\), como \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) con \(\det(\mathbf{A}) = ad - bc \neq 0\), existe una fórmula explícita que se deriva de la definición de inversa y el cálculo del determinante.
599+
600+
Buscamos \(\mathbf{A}^{-1} = \begin{bmatrix} p & q \\ r & s \end{bmatrix}\) tal que \(\mathbf{A}\mathbf{A}^{-1} = \mathbf{I}\):
601+
602+
$$
603+
\begin{aligned}
604+
\begin{bmatrix} a & b \\ c & d \end{bmatrix}\begin{bmatrix} p & q \\ r & s \end{bmatrix} &= \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} \\
605+
\begin{bmatrix} ap+br & aq+bs \\ cp+dr & cq+ds \end{bmatrix} &= \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}
606+
\end{aligned}
607+
$$
608+
609+
De la primera columna tenemos que: \(ap + br = 1\) y \(cp + dr = 0\). Resolviendo, encontramos que:
610+
$$
611+
\begin{aligned}
612+
p &= \frac{d}{ad-bc} \\
613+
r &= \frac{-c}{ad-bc}
614+
\end{aligned}
615+
$$
616+
617+
De la segunda columna: \(aq + bs = 0\) y \(cq + ds = 1\). Resolviendo:
618+
$$
619+
\begin{aligned}
620+
q &= \frac{-b}{ad-bc} \\
621+
s &= \frac{a}{ad-bc}
622+
\end{aligned}
623+
$$
624+
625+
Reemplazando:
626+
$$
627+
\begin{aligned}
628+
\mathbf{A}^{-1} &= \begin{bmatrix} p & q \\ r & s \end{bmatrix} \\
629+
\mathbf{A}^{-1} &= \begin{bmatrix} \frac{d}{ad-bc} & \frac{-b}{ad-bc} \\ \frac{-c}{ad-bc} & \frac{a}{ad-bc} \end{bmatrix} \\
630+
\mathbf{A}^{-1} &= \frac{1}{ad - bc} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}
631+
\end{aligned}
632+
$$
633+
634+
Sabiendo que \(\det(\mathbf{A}) = ad - bc\), entonces:
635+
636+
$$
637+
\boxed{\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}}
638+
$$
639+
640+
{{< callout type="info" >}}
641+
Para invertir una matriz \(2 \times 2\), intercambia las entradas diagonales, niega las entradas fuera de la diagonal, y divide todo por el determinante. El determinante aparece en el denominador, exactamente por eso un determinante nulo hace indefinida la inversa (división por cero).
642+
{{< /callout >}}
643+
644+
Para \(n > 2\), la inversa se calcula en la práctica mediante [**eliminación de Gauss-Jordan**](https://es.wikipedia.org/wiki/Eliminaci%C3%B3n_de_Gauss-Jordan) sobre la matriz aumentada \([\mathbf{A}\,|\,\mathbf{I}]\).
645+
646+
## Perspectiva de Machine Learning e IA
647+
648+
Las operaciones matriciales no son meros primitivos computacionales, son el vocabulario conceptual de la investigación moderna en Machine Learning, por ejemplo:
649+
650+
**Estructura de bajo rango en el ajuste fino**. [Hu et al. (2021)](https://arxiv.org/abs/2106.09685) introdujeron *LoRA (Low-Rank Adaptation)*, que observa que las matrices de actualización de pesos \(\Delta\mathbf{W}\) durante el ajuste fino de grandes modelos de lenguaje son empíricamente de bajo rango. En lugar de almacenar la \(\Delta\mathbf{W} \in \mathbb{R}^{d \times d}\) completa, LoRA la descompone como \(\Delta\mathbf{W} = \mathbf{B}\mathbf{A}\) donde \(\mathbf{B} \in \mathbb{R}^{d \times r}\), \(\mathbf{A} \in \mathbb{R}^{r \times d}\), con \(r \ll d\). Esto es válido precisamente porque una matriz de rango \(r\) tiene una factorización natural en dos matrices delgadas, el concepto de rango que acabamos de ver. LoRA reduce los parámetros entrenables más de 10000 veces en modelos de la escala de GPT-3.
651+
652+
## Errores comunes y depuración
653+
654+
1. **Confundir las convenciones de forma entre frameworks**. NumPy usa la convención `(batch, características)`. El `nn.Linear(in_features, out_features)` de PyTorch almacena su matriz de pesos con forma `(out_features, in_features)` y calcula `x @ W.T + b` internamente, por lo que \(\mathbf{W}\) se almacena transpuesta respecto a la convención matemática. Si inicializas pesos manualmente, verifica con una pasada hacia adelante sobre datos ficticios antes de confiar en los gradientes.
655+
656+
2. **Invertir matrices casi singulares**. `np.linalg.inv()` devuelve un resultado incluso para matrices casi singulares, los números serán astronómicamente grandes y numéricamente sin sentido. Prefiere siempre `np.linalg.solve(A, b)` sobre `np.linalg.inv(A) @ b` para resolver sistemas lineales. Revisa `np.linalg.cond(A)` antes de invertir; los números de condición superiores a \(10^{12}\) son una señal de alerta.
657+
658+
3. **Usar `det == 0` para detectar singularidad**. En punto flotante, el determinante de una matriz verdaderamente singular casi nunca es exactamente cero,será algún número muy pequeño como `1e-17`. No uses el determinante como prueba de singularidad en el código. En su lugar usa `np.linalg.matrix_rank(A) < n` o `np.linalg.cond(A) > umbral`.
659+
660+
4. **Asumir que la multiplicación de matrices es conmutativa**. El error algebraico más común al implementar atención desde cero. \(\mathbf{AB} \neq \mathbf{BA}\). Incluso cuando ambos productos están bien definidos y tienen la misma forma (matrices cuadradas), producirán resultados diferentes. Ante la duda, rastrea las formas explícitamente y razona sobre la semántica de cada multiplicación.

0 commit comments

Comments
 (0)