<jupyter_start><jupyter_text>DeterminantAuthor: [<NAME>](https://patel-zeel.github.io/), [<NAME>](https://nipunbatra.github.io/) In this notebook, we will look at determinants. https://youtu.be/Ip3X9LOh2dk<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc

rc('font', size=16)
rc('text', usetex=True)<jupyter_output><empty_output><jupyter_text>Transformation of N-D space Consider a N-D vector $\mathbf{v}_1$. We can get a transformed N-D vector $\mathbf{v}_1'$ by applying an appropriate linear transformation $A$ on $\mathbf{v}_1$.    Note that $A$ will be a $N \times N$ matrix here. This transformarion is given as,$$\mathbf{v}_1' = A\mathbf{v}_1$$  1D case If $\mathbf{v}_1$ is a scaler, we get transformed scaler $\mathbf{v}_2$ by applying transformation $A$. $A$ would also be a scaler here.  If $\mathbf{v}_1=3$, $A=5$ then $\mathbf{v}_1' = 5 \cdot 3 = 15$.Ultimately, $\mathbf{v}_1'$ is $\mathbf{v}_1$ scaled by factor of $A$.  2D case consider two vectors $\mathbf{v}_1$ and $\mathbf{v}_2$. We will focus on the area of the parallelogram spaned by these vectors.$$\mathbf{v}_1 =\begin{bmatrix}0\\1\end{bmatrix}, \mathbf{v}_2 =\begin{bmatrix}1\\0\end{bmatrix}$$<jupyter_code>def get_area(v1, v2):
    return np.abs(np.sum(np.cross(v1.ravel(), v2.ravel())))
def plot_area(v1, v2, suffix='', annotate=False, c='b'):
    vectors = [v1, v2]
    for vi, v in enumerate(vectors, 1):
        plt.arrow(x=0, y=0, dx=v[0, 0], dy=v[1, 0], shape='full', 
                  head_width=0.2, head_length=0.2, length_includes_head=True, color=c)
        if annotate:
            plt.text(v[0, 0]-0.5, v[1, 0]+0.1, f'v{vi}{suffix}=({v[0, 0]}, {v[1, 0]})')

    ## Filling the area
    plt.fill([0, v1[0,0], v1[0,0]+v2[0,0], v2[0,0]], 
             [0, v1[1,0], v1[1,0]+v2[1,0], v2[1,0]], alpha=0.4, label=f'area = {get_area(v1, v2)}')
    
    plt.text(0-0.5, 0+0.1, f'({0}, {0})')
    plt.grid()
    plt.ylim(np.min([0, v1[1,0], v2[1,0]])-1, v1[1,0]+v2[1,0]+1)
    plt.xlim(np.min([0, v1[0,0], v2[0,0]])-1, v1[0,0]+v2[0,0]+1)
    plt.xlabel('x');plt.ylabel('y');
    if annotate:
        plt.legend(bbox_to_anchor=(1,1))
vec1 = np.array([2, 0]).reshape(-1,1)
vec2 = np.array([2, 1]).reshape(-1,1)

plot_area(vec1, vec2, annotate=True)<jupyter_output><empty_output><jupyter_text>Let us apply the following linear tranformation on $\mathbf{v_1}$ and $\mathbf{v_2}$. $$A = \begin{bmatrix}2 & 1\\2 & 3\end{bmatrix}$$<jupyter_code>def plot_transform(v1, v2, A, annotate=False):
    plot_area(v1, v2, annotate=annotate)
    xlim, ylim = plt.xlim(), plt.ylim()
    
    v1_, v2_ = A@v1, A@v2
    
    plot_area(v1_, v2_, suffix='\'', annotate=annotate, c='r')
    
    plt.xlim(min(plt.xlim()[0], xlim[0]), max(plt.xlim()[1], xlim[1]))
    plt.ylim(min(plt.ylim()[0], ylim[0]), max(plt.ylim()[1], ylim[1]))
    plt.title(f'Area should be scaled by $|A|={np.linalg.det(A)}$')
A = np.array([[2, 1], [2, 3]])

plot_transform(vec1, vec2, A, annotate=True)<jupyter_output><empty_output><jupyter_text>Note that the area spanned by $\mathbf{v}_1'$ and $\mathbf{v}_2'$ is scaled by factor factor of $|A|$ (determinant of $A$). More concretely,$$\text{Area}(\mathbf{v}_1', \mathbf{v}_2') = |A| \cdot \text{Area}(\mathbf{v}_1, \mathbf{v}_2)$$Let us choose one more example of $A$,$$A = \begin{bmatrix}2 & 4\\1 & 2\end{bmatrix}$$<jupyter_code>A = np.array([[2, 4], [1, 2]])

plot_transform(vec1, vec2, A, annotate=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.215007215007215, 'fraction_numerical': 0.05512265512265512, 'mean_word_length': 4.51033386327504, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '511013', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1458, 'n_words': 315, 'path': 'explain-ml-book/notebooks/2021-03-15-determinant.ipynb', 'repo_name': 'explain-ml/explain-ml.github.io', 'size': '3476'}