<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LU decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  1., -1.,  1.],\n",
       "       [ 1.,  2., -4., -2.],\n",
       "       [ 2.,  1.,  1.,  5.],\n",
       "       [-1.,  0., -2., -4.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [1, 1, 0, 0],\n",
    "              [2,-1, 1, 0],\n",
    "              [-1,1, 0, 1]])\n",
    "\n",
    "U = np.array([[1, 1, -1, 1], \n",
    "              [0, 1, -3, -3],\n",
    "              [0, 0, 0, 0],\n",
    "              [0, 0, 0, 0]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "P @ L @ U @ np.linalg.inv(P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2. ,  3. ,  1. ,  2. ],\n",
       "       [-2. ,  4. , -1. ,  5. ],\n",
       "       [ 3. ,  7. ,  1.5,  1. ],\n",
       "       [ 6. ,  9. ,  3. ,  7. ]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.array([[  1,    0,   0,  0], \n",
    "              [ -1,    1,   0,  0],\n",
    "              [3/2, 5/14,   1,  0],\n",
    "              [  3,    0,   0,  1]])\n",
    "\n",
    "U = np.array([[ 2,     3,   1,   2], \n",
    "              [ 0,     7,   0,   7],\n",
    "              [ 0,     0,   0,-9/2],\n",
    "              [ 0,     0,   0,   1]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "P @ L @ U @ np.linalg.inv(P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.,  2.,  6.],\n",
       "       [ 3.,  0.,  7.],\n",
       "       [-2., -1., -3.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.array([[  1,   0,   0], \n",
    "              [3/4,   1,   0],\n",
    "              [-1/2,  0,   1]])\n",
    "\n",
    "U = np.array([[ 4,    2,   6], \n",
    "              [ 0, -3/2, 5/2],\n",
    "              [ 0,     0, 0]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "P @ L @ U @ np.linalg.inv(P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.,  0.,  1.,  2.],\n",
       "       [ 1.,  1.,  0.,  2.],\n",
       "       [ 2., -1.,  3.,  1.],\n",
       "       [ 3., -1.,  4.,  3.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.array([[  1,    0,   0,  0], \n",
    "              [1/2,   1,   0,  0],\n",
    "              [  1,   -1,   1,  0],\n",
    "              [3/2,   -1, 4/3,  1]])\n",
    "\n",
    "U = np.array([[ 2,  0,   1,   2], \n",
    "              [ 0,  1,-1/2,   1],\n",
    "              [ 0,  0, 3/2,   0],\n",
    "              [ 0,  0,   0,   1]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "P @ L @ U @ np.linalg.inv(P)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LDU decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4. -2.  1.]\n",
      " [20. -7. 12.]\n",
      " [-8. 13. 17.]]\n",
      "[[ 4. -2.  1.]\n",
      " [20. -7. 12.]\n",
      " [-8. 13. 17.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[  1,   0,   0], \n",
    "              [  5,   1,   0],\n",
    "              [ -2,   3,   1]])\n",
    "\n",
    "U1 = np.array([[  4,  -2,   1], \n",
    "               [ 0,   3,   7],\n",
    "               [ 0,   0,  -2]])\n",
    "\n",
    "D = np.array([[ 4, 0, 0], \n",
    "              [0, 3, 0],\n",
    "              [0, 0,-2]])\n",
    "\n",
    "U2 = np.array([[ 1,-1/2,1/4], \n",
    "               [ 0,   1,7/3],\n",
    "               [ 0,   0,  1]])\n",
    "\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U1 @ np.linalg.inv(P))\n",
    "print(P @ L @ D @ U2 @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   2.  -2.   1.]\n",
      " [  4.   5.  -7.   6.]\n",
      " [  6. -12.  -6.  22.]\n",
      " [  5.  25. -15.   3.]]\n",
      "[[  1.   2.  -2.   1.]\n",
      " [  4.   5.  -7.   6.]\n",
      " [  6. -12.  -6.  22.]\n",
      " [  5.  25. -15.   3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [6, 8, 1, 0],\n",
    "              [5, -5, 0, 1]])\n",
    "\n",
    "U1 = np.array([[1, 2, -2, 1],\n",
    "              [0, -3, 1, 2],\n",
    "              [0, 0, -2, 0],\n",
    "              [0, 0, 0, 8]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0],\n",
    "              [0, -3, 0, 0],\n",
    "              [0, 0, -2, 0],\n",
    "              [0, 0, 0, 8]])\n",
    "\n",
    "U2 = np.array([[1, 2, -2, 1],\n",
    "               [0, 1, -1/3, -2/3],\n",
    "               [0, 0, 1, 0],\n",
    "               [0, 0, 0, 1]])\n",
    "\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U1 @ np.linalg.inv(P))\n",
    "print(P @ L @ D @ U2 @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2. -2.  1.  1.]\n",
      " [ 0. -3.  1.  2.  2.]\n",
      " [ 4.  2. -8.  8.  9.]\n",
      " [ 5.  1. -5. 13. 11.]\n",
      " [ 0.  0.  0.  0.  2.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0, 0], \n",
    "              [0, 1, 0, 0, 0],\n",
    "              [4, 2, 1, 0, 0],\n",
    "              [5, 3, -1, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "U = np.array([[1, 2, -2, 1, 1],\n",
    "              [0, 1, -1/3, -2/3, -2/3],\n",
    "              [0, 0, 1, 0, -1/2],\n",
    "              [0, 0, 0, 1, 1/2],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, -3, 0, 0, 0],\n",
    "              [0, 0, -2, 0, 0],\n",
    "              [0, 0, 0, 2, 0],\n",
    "              [0, 0, 0, 0, 2]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, 1, 0, 0, 0],\n",
    "              [0, 0, 1, 0, 0],\n",
    "              [0, 0, 0, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ U @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LDt(L) decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4. -2. -8.]\n",
      " [-2. -7. 13.]\n",
      " [-8. 13. 17.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0], \n",
    "              [-1/2, 1, 0],\n",
    "              [-2, -9/8, 1]])\n",
    "\n",
    "D = np.array([[4, 0, 0], \n",
    "              [0, -8, 0],\n",
    "              [0, 0, 89/8]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L) @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   4.  -2.   1.]\n",
      " [  4.   5. -12.  25.]\n",
      " [ -2. -12.  -6. -15.]\n",
      " [  1.  25. -15.  -3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [-2, 4/11, 1, 0],\n",
    "              [1, -21/11, 227/94, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0], \n",
    "              [0, -11, 0, 0],\n",
    "              [0, 0, -94/11, 0],\n",
    "              [0, 0, 0, 8077/94]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L) @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   4.  -2.   1.]\n",
      " [  4.   5. -12.  25.]\n",
      " [ -2. -12.  -6. -15.]\n",
      " [  1.  25. -15.  -3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [-2, 4/11, 1, 0],\n",
    "              [1, -21/11, 227/94, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0], \n",
    "              [0, -11, 0, 0],\n",
    "              [0, 0, -94/11, 0],\n",
    "              [0, 0, 0, 8077/94]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L) @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2.  4.  5.  1.]\n",
      " [ 2. -3.  2.  2.  2.]\n",
      " [ 4.  2. -8. -5.  9.]\n",
      " [ 5.  2. -5.  4.  1.]\n",
      " [ 1.  2.  9.  1.  2.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0, 0], \n",
    "              [2, 1, 0, 0, 0],\n",
    "              [4, 6/7, 1, 0, 0],\n",
    "              [5, 8/7, 127/132, 1, 0],\n",
    "              [1, 0, -35/132, -1163/739, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0, 0], \n",
    "              [0, -7, 0, 0, 0],\n",
    "              [0, 0, -132/7, 0, 0],\n",
    "              [0, 0, 0, 739/132, 0],\n",
    "              [0, 0, 0, 0,  -8528/739]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, 1, 0, 0, 0],\n",
    "              [0, 0, 1, 0, 0],\n",
    "              [0, 0, 0, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L) @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = Rt(R) decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4.  2. -4.]\n",
      " [ 2. 10.  4.]\n",
      " [-4.  4.  9.]]\n"
     ]
    }
   ],
   "source": [
    "R = np.array([[2, 0, 0],\n",
    "              [1, 3, 0],\n",
    "              [-2, 2, 1]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ R @ np.transpose(R) @ np.linalg.inv(P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4. 2. 0.]\n",
      " [2. 9. 4.]\n",
      " [0. 4. 5.]]\n"
     ]
    }
   ],
   "source": [
    "R = np.array([[2, 0, 0],\n",
    "              [1, np.sqrt(8), 0],\n",
    "              [0, np.sqrt(8) / 2, np.sqrt(3)]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ R @ np.transpose(R) @ np.linalg.inv(P))"
   ]
  }
 ],
 "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",<commit_msg>Fixed bug related with permutation matrix<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LU decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  1 -1  1]\n",
      " [ 1  2 -4 -2]\n",
      " [ 2  1  1  5]\n",
      " [-1  0 -2 -4]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [1, 1, 0, 0],\n",
    "              [2,-1, 1, 0],\n",
    "              [-1,1, 0, 1]])\n",
    "\n",
    "U = np.array([[1, 1, -1, 1], \n",
    "              [0, 1, -3, -3],\n",
    "              [0, 0, 0, 0],\n",
    "              [0, 0, 0, 0]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.   3.   1.   2. ]\n",
      " [-2.   4.  -1.   5. ]\n",
      " [ 3.   7.   1.5  1. ]\n",
      " [ 6.   9.   3.   7. ]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[  1,    0,   0,  0], \n",
    "              [ -1,    1,   0,  0],\n",
    "              [3/2, 5/14,   1,  0],\n",
    "              [  3,    0,   0,  1]])\n",
    "\n",
    "U = np.array([[ 2,     3,   1,   2], \n",
    "              [ 0,     7,   0,   7],\n",
    "              [ 0,     0,   0,-9/2],\n",
    "              [ 0,     0,   0,   1]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4.,  2.,  6.],\n",
       "       [ 3.,  0.,  7.],\n",
       "       [-2., -1., -3.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = np.array([[  1,   0,   0], \n",
    "              [3/4,   1,   0],\n",
    "              [-1/2,  0,   1]])\n",
    "\n",
    "U = np.array([[ 4,    2,   6], \n",
    "              [ 0, -3/2, 5/2],\n",
    "              [ 0,     0, 0]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "P @ L @ U @ np.linalg.inv(P)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.  0.  1.  2.]\n",
      " [ 1.  1.  0.  2.]\n",
      " [ 2. -1.  3.  1.]\n",
      " [ 3. -1.  4.  3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[  1,    0,   0,  0], \n",
    "              [1/2,   1,   0,  0],\n",
    "              [  1,   -1,   1,  0],\n",
    "              [3/2,   -1, 4/3,  1]])\n",
    "\n",
    "U = np.array([[ 2,  0,   1,   2], \n",
    "              [ 0,  1,-1/2,   1],\n",
    "              [ 0,  0, 3/2,   0],\n",
    "              [ 0,  0,   0,   1]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LDU decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4 -2  1]\n",
      " [20 -7 12]\n",
      " [-8 13 17]]\n",
      "[[ 4. -2.  1.]\n",
      " [20. -7. 12.]\n",
      " [-8. 13. 17.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[  1,   0,   0], \n",
    "              [  5,   1,   0],\n",
    "              [ -2,   3,   1]])\n",
    "\n",
    "U1 = np.array([[  4,  -2,   1], \n",
    "               [ 0,   3,   7],\n",
    "               [ 0,   0,  -2]])\n",
    "\n",
    "D = np.array([[ 4, 0, 0], \n",
    "              [0, 3, 0],\n",
    "              [0, 0,-2]])\n",
    "\n",
    "U2 = np.array([[ 1,-1/2,1/4], \n",
    "               [ 0,   1,7/3],\n",
    "               [ 0,   0,  1]])\n",
    "\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U1)\n",
    "print(P @ L @ D @ U2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1   2  -2   1]\n",
      " [  4   5  -7   6]\n",
      " [  6 -12  -6  22]\n",
      " [  5  25 -15   3]]\n",
      "[[  1.   2.  -2.   1.]\n",
      " [  4.   5.  -7.   6.]\n",
      " [  6. -12.  -6.  22.]\n",
      " [  5.  25. -15.   3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [6, 8, 1, 0],\n",
    "              [5, -5, 0, 1]])\n",
    "\n",
    "U1 = np.array([[1, 2, -2, 1],\n",
    "              [0, -3, 1, 2],\n",
    "              [0, 0, -2, 0],\n",
    "              [0, 0, 0, 8]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0],\n",
    "              [0, -3, 0, 0],\n",
    "              [0, 0, -2, 0],\n",
    "              [0, 0, 0, 8]])\n",
    "\n",
    "U2 = np.array([[1, 2, -2, 1],\n",
    "               [0, 1, -1/3, -2/3],\n",
    "               [0, 0, 1, 0],\n",
    "               [0, 0, 0, 1]])\n",
    "\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ U1)\n",
    "print(P @ L @ D @ U2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2. -2.  1.  1.]\n",
      " [ 0. -3.  1.  2.  2.]\n",
      " [ 4.  2. -8.  8.  9.]\n",
      " [ 5.  1. -5. 13. 11.]\n",
      " [ 0.  0.  0.  0.  2.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0, 0], \n",
    "              [0, 1, 0, 0, 0],\n",
    "              [4, 2, 1, 0, 0],\n",
    "              [5, 3, -1, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "U = np.array([[1, 2, -2, 1, 1],\n",
    "              [0, 1, -1/3, -2/3, -2/3],\n",
    "              [0, 0, 1, 0, -1/2],\n",
    "              [0, 0, 0, 1, 1/2],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, -3, 0, 0, 0],\n",
    "              [0, 0, -2, 0, 0],\n",
    "              [0, 0, 0, 2, 0],\n",
    "              [0, 0, 0, 0, 2]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, 1, 0, 0, 0],\n",
    "              [0, 0, 1, 0, 0],\n",
    "              [0, 0, 0, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ U)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = LDt(L) decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4. -2. -8.]\n",
      " [-2. -7. 13.]\n",
      " [-8. 13. 17.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0], \n",
    "              [-1/2, 1, 0],\n",
    "              [-2, -9/8, 1]])\n",
    "\n",
    "D = np.array([[4, 0, 0], \n",
    "              [0, -8, 0],\n",
    "              [0, 0, 89/8]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   4.  -2.   1.]\n",
      " [  4.   5. -12.  25.]\n",
      " [ -2. -12.  -6. -15.]\n",
      " [  1.  25. -15.  -3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [-2, 4/11, 1, 0],\n",
    "              [1, -21/11, 227/94, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0], \n",
    "              [0, -11, 0, 0],\n",
    "              [0, 0, -94/11, 0],\n",
    "              [0, 0, 0, 8077/94]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   4.  -2.   1.]\n",
      " [  4.   5. -12.  25.]\n",
      " [ -2. -12.  -6. -15.]\n",
      " [  1.  25. -15.  -3.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0], \n",
    "              [4, 1, 0, 0],\n",
    "              [-2, 4/11, 1, 0],\n",
    "              [1, -21/11, 227/94, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0], \n",
    "              [0, -11, 0, 0],\n",
    "              [0, 0, -94/11, 0],\n",
    "              [0, 0, 0, 8077/94]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0],\n",
    "              [0, 1, 0, 0],\n",
    "              [0, 0, 1, 0],\n",
    "              [0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2.  4.  5.  1.]\n",
      " [ 2. -3.  2.  2.  2.]\n",
      " [ 4.  2. -8. -5.  9.]\n",
      " [ 5.  2. -5.  4.  1.]\n",
      " [ 1.  2.  9.  1.  2.]]\n"
     ]
    }
   ],
   "source": [
    "L = np.array([[1, 0, 0, 0, 0], \n",
    "              [2, 1, 0, 0, 0],\n",
    "              [4, 6/7, 1, 0, 0],\n",
    "              [5, 8/7, 127/132, 1, 0],\n",
    "              [1, 0, -35/132, -1163/739, 1]])\n",
    "\n",
    "D = np.array([[1, 0, 0, 0, 0], \n",
    "              [0, -7, 0, 0, 0],\n",
    "              [0, 0, -132/7, 0, 0],\n",
    "              [0, 0, 0, 739/132, 0],\n",
    "              [0, 0, 0, 0,  -8528/739]])\n",
    "\n",
    "P = np.array([[1, 0, 0, 0, 0],\n",
    "              [0, 1, 0, 0, 0],\n",
    "              [0, 0, 1, 0, 0],\n",
    "              [0, 0, 0, 1, 0],\n",
    "              [0, 0, 0, 0, 1]])\n",
    "\n",
    "print(P @ L @ D @ np.transpose(L))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A = Rt(R) decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 4  2 -4]\n",
      " [ 2 10  4]\n",
      " [-4  4  9]]\n"
     ]
    }
   ],
   "source": [
    "R = np.array([[2, 0, 0],\n",
    "              [1, 3, 0],\n",
    "              [-2, 2, 1]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ R @ np.transpose(R))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4. 2. 0.]\n",
      " [2. 9. 4.]\n",
      " [0. 4. 5.]]\n"
     ]
    }
   ],
   "source": [
    "R = np.array([[2, 0, 0],\n",
    "              [1, np.sqrt(8), 0],\n",
    "              [0, np.sqrt(8) / 2, np.sqrt(3)]])\n",
    "\n",
    "P = np.array([[1, 0, 0],\n",
    "              [0, 1, 0],\n",
    "              [0, 0, 1]])\n",
    "\n",
    "print(P @ R @ np.transpose(R))"
   ]
  }
 ],
 "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",
{'fraction_non_alphanumeric': 0.28969790859798605, 'fraction_numerical': 0.07811773818745159, 'mean_word_length': 1.02168806764798, 'pattern_counts': {'":': 276, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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}}
{'dir': 'git-commits-cleaned', 'id': '4106816', 'n_tokens_mistral': 13042, 'n_tokens_neox': 11741, 'n_words': 2492}