{
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "markdown",
"metadata": {
"lang": "en"
},
"source": [
"## Introduction to Einstein Notation\n",
"\n",
"This notation allows for expressing an extraordinary number of operations concisely. This section is not\n",
"necessary for the following but it is an interesting intellectual exercise.\n",
"\n",
"The basic idea is to sum the terms of an equation when the same index appears twice and is not defined elsewhere. Thus:\n",
"\n",
"$$ A_{i,i} \\quad \\textrm{means} \\quad \\sum_{i=1}^N A_{i,i} \\; \\textrm{(the trace of the matrix)}$$\n",
"\n",
"If we look at the matrix multiplication $A\\, B$, for every index (i,j) of the result we have:\n",
"\n",
"$$ C_{i,j} = A_{i,k} \\, B_{k,j} \\quad \\textrm{i.e.} \\quad C_{i,j} = \\sum_{k=1}^N A_{i,k} \\, B_{k,j} $$ \n",
"\n",
"The full name of the Einstein notation being the Einstein summation convention, the Numpy function is [`einsum`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html). Here is how it works for our first 2 examples:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trace 'ii' : 12 \n",
"\n",
"Multiplication matricielle A A 'ij,jk->ik' :\n",
"[[ 15 18 21]\n",
" [ 42 54 66]\n",
" [ 69 90 111]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"A = np.arange(9).reshape(3,3)\n",
"\n",
"print(\"Trace 'ii' : \", np.einsum('ii', A), '\\n') # 0 + 4 + 8 = 12\n",
"\n",
"print(\"Multiplication matricielle A A 'ij,jk->ik' :\")\n",
"print(np.einsum('ij,jk->ik', A, A)) # notez que j'ai nommé différement les indices"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 15, 18, 21],\n",
" [ 42, 54, 66],\n",
" [ 69, 90, 111]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.dot(A) # on vérifie"
]
},
{
"cell_type": "markdown",
"metadata": {
"lang": "en"
},
"source": [
"It is noted that the arguments of `einsum` are:\n",
"\n",
"* the summation rule in a string with a comma to separate each component\n",
"* the components on which the rule applies\n",
"\n",
"We can go a little further with Numpy. Here are all the rules that `einsum` uses:\n",
"\n",
"#### Basic and additional rules\n",
"\n",
"1. a repeated index implies summation over that index unless that index is mentioned in the result
(see example of the diagonal of A below for the exception)\n",
"2. indices that repeat from one component to another imply that the referenced elements will be multiplied together
(see example of the matrix product)\n",
"3. a letter omitted in the result (after `->`) implies summation over that index
(see example of summing the elements of a vector below)\n",
"4. if you don't put the arrow, einsum will put it for you with on the right all the indices that are not doubled arranged in alphabetical order
(see example of the transpose below)"
]
},
{
"cell_type": "markdown",
"metadata": {
"lang": "en"
},
"source": [
"Here is a list of operations taken from the [blog of Dr. Goulu](https://www.drgoulu.com/2016/01/17/einsum):\n",
"\n",
"
| einsum Signature | \n", "Numpy Equivalent | \n", "Description | \n", "
|---|---|---|
('i->', v) | \n",
"sum(v) | \n",
"sum of values of vector v | \n", "
('i,i->i', u, v) | \n",
"u \\* v | \n",
"element-wise multiplication of vectors u and v | \n", "
('i,i', u, v) | \n",
"inner(u, v) | \n",
"dot product of u and v | \n", "
('i,j', u, v) | \n",
"outer(u, v) | \n",
"dyadic product of u and v | \n", "
('ij', A) | \n",
"A | \n",
"returns matrix A | \n", "
('ji', A) | \n",
"A.T | \n",
"transpose of A | \n", "
('ii->i', A) | \n",
"diag(A) | \n",
"diagonal of A | \n", "
('ii', A) | \n",
"trace(A) | \n",
"sum of the diagonal of A | \n", "
('ij->', A) | \n",
"sum(A) | \n",
"sum of values of A | \n", "
('ij->j', A) | \n",
"sum(A, axis=0) | \n",
"sum of columns of A | \n", "
('ij->i', A) | \n",
"sum(A, axis=1) | \n",
"sum of rows of A | \n", "
('ij,ij->ij', A, B) | \n",
"A \\* B | \n",
"element-wise transposed matrix multiplication of A and B | \n", "
('ij,jk', A, B) | \n",
"dot(A, B) | \n",
"dot product of A and B |
('ij,jk->ij', A, B) | \n",
"inner(A, B) | \n",
"inner product of A and B | \n", "
('ij,jk->ijk', A, B) | \n",
"A[:, None] \\* B | \n",
"each row of A multiplied by B | \n", "
('ij,kl->ijkl', A, B) | \n",
"A[:, :, None, None] \\* B | \n",
"each value of A multiplied by B | \n", "