pyerrors/examples/05_matrix_operations.ipynb

405 lines
8 KiB
Text
Raw Permalink Normal View History

2020-10-13 16:53:00 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pyerrors as pe\n",
"import numpy as np"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an example we look at a symmetric 2x2 matrix which positive semidefinte and has an error on all entries"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4.10(20) -1.00(10)]\n",
" [-1.00(10) 1.000(10)]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
"obs11 = pe.pseudo_Obs(4.1, 0.2, 'e1')\n",
"obs22 = pe.pseudo_Obs(1, 0.01, 'e1')\n",
"obs12 = pe.pseudo_Obs(-1, 0.1, 'e1')\n",
"matrix = np.asarray([[obs11, obs12], [obs12, obs22]])\n",
"print(matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We require to use `np.asarray` here as it makes sure that we end up with a numpy array of `Obs`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The standard matrix product can be performed with `@`"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[17.81 -5.1]\n",
" [-5.1 2.0]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
"print(matrix @ matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplication with unit matrix leaves the matrix unchanged"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4.1 -1.0]\n",
" [-1.0 1.0]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
"print(matrix @ np.identity(2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-01-06 12:15:26 +01:00
"For large matrices overloading the standard operator `@` can become inefficient as pyerrors has to perform a large number of elementary opeations. For these situations pyerrors provides the function `linalg.matmul` which optimizes the required automatic differentiation. The function can take an arbitray number of operands."
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 5,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[78.12099999999998 -22.909999999999997]\n",
" [-22.909999999999997 7.1]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"print(pe.linalg.matmul(matrix, matrix, matrix)) # Equivalent to matrix @ matrix @ matrix but faster for large matrices"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-01-06 12:15:26 +01:00
"Mathematical functions work elementwise"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 6,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[30.16185746098009 -1.1752011936438014]\n",
" [-1.1752011936438014 1.1752011936438014]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"print(np.sinh(matrix))"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a vector of `Obs`, we again use `np.asarray` to end up with the correct object"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 7,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2.00(40) 1.00(10)]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"vec1 = pe.pseudo_Obs(2, 0.4, 'e1')\n",
"vec2 = pe.pseudo_Obs(1, 0.1, 'e1')\n",
"vector = np.asarray([vec1, vec2])\n",
"pe.gm(vector)\n",
2022-01-06 12:15:26 +01:00
"print(vector)"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-01-06 12:15:26 +01:00
"The matrix times vector product can then be computed via"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 8,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-07-20 12:20:06 +01:00
"[7.2(1.7) -1.00(46)]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"product = matrix @ vector\n",
"pe.gm(product)\n",
2022-01-06 12:15:26 +01:00
"print(product)"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-01-06 12:15:26 +01:00
"`pyerrors` provides the user with wrappers to the `numpy.linalg` functions which work on `Obs` valued matrices. We can for example calculate the determinant of the matrix via"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 9,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2022-01-06 12:15:26 +01:00
"3.10(28)\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"det = pe.linalg.det(matrix)\n",
"det.gamma_method()\n",
"print(det)"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The cholesky decomposition can be obtained as follows"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": 10,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.025(49) 0.0]\n",
2023-07-20 12:20:06 +01:00
" [-0.494(51) 0.870(29)]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"cholesky = pe.linalg.cholesky(matrix)\n",
"pe.gm(cholesky)\n",
2020-10-13 16:53:00 +02:00
"print(cholesky)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now check if the decomposition was succesfull"
]
},
{
"cell_type": "code",
"execution_count": 11,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-8.881784197001252e-16 0.0]\n",
" [0.0 0.0]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
"check = cholesky @ cholesky.T\n",
"print(check - matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now further compute the inverse of the cholesky decomposed matrix and check that the product with its inverse gives the unit matrix with zero error."
]
},
{
"cell_type": "code",
"execution_count": 12,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.494(12) 0.0]\n",
2023-07-20 12:20:06 +01:00
" [0.280(40) 1.150(39)]]\n",
2020-10-13 16:53:00 +02:00
"Check:\n",
"[[1.0 0.0]\n",
" [0.0 1.0]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2022-01-06 12:15:26 +01:00
"inv = pe.linalg.inv(cholesky)\n",
"pe.gm(inv)\n",
2020-10-13 16:53:00 +02:00
"print(inv)\n",
"print('Check:')\n",
"check_inv = cholesky @ inv\n",
"print(check_inv)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Eigenvalues and eigenvectors\n",
"We can also compute eigenvalues and eigenvectors of symmetric matrices with a special wrapper `eigh`"
]
},
{
"cell_type": "code",
"execution_count": 13,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Eigenvalues:\n",
2023-07-20 12:20:06 +01:00
"[0.705(57) 4.39(19)]\n",
2020-10-13 16:53:00 +02:00
"Eigenvectors:\n",
2023-07-20 12:20:06 +01:00
"[[-0.283(26) -0.9592(76)]\n",
" [-0.9592(76) 0.283(26)]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
"e, v = pe.linalg.eigh(matrix)\n",
"pe.gm(e)\n",
2020-10-13 16:53:00 +02:00
"print('Eigenvalues:')\n",
"print(e)\n",
"pe.gm(v)\n",
2020-10-13 16:53:00 +02:00
"print('Eigenvectors:')\n",
"print(v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check that we got the correct result"
]
},
{
"cell_type": "code",
"execution_count": 14,
2020-10-13 16:53:00 +02:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-07-20 12:20:06 +01:00
"[[ True True]\n",
" [ True True]]\n"
2020-10-13 16:53:00 +02:00
]
}
],
"source": [
2023-07-20 12:20:06 +01:00
"print(matrix @ v == e * v)"
2020-10-13 16:53:00 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
2020-10-13 16:53:00 +02:00
"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.11.0"
2020-10-13 16:53:00 +02:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}