Skip to content

Instantly share code, notes, and snippets.

@jstac
Created September 26, 2021 23:05
Show Gist options
  • Save jstac/3ce1a2305a74248d33f140b023490272 to your computer and use it in GitHub Desktop.
Save jstac/3ce1a2305a74248d33f140b023490272 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8177b07c",
"metadata": {},
"outputs": [],
"source": [
"from sympy.matrices import Matrix, eye, ones"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "efe4d83a",
"metadata": {},
"outputs": [],
"source": [
"M = ((1, 2, 0),\n",
" (0, 0, 1))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "51f60f23",
"metadata": {},
"outputs": [],
"source": [
"M = Matrix(M)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1520f448",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}1 & 2 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[1, 2, 0],\n",
"[0, 0, 1]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7019a07f",
"metadata": {},
"outputs": [],
"source": [
"U, Σ, Vt = M.singular_value_decomposition()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "db659ab3",
"metadata": {},
"outputs": [],
"source": [
"V = Vt.transpose()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1939e959",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}0 & 1\\\\1 & 0\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[0, 1],\n",
"[1, 0]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"U"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ada4525c",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}1 & 0\\\\0 & \\sqrt{5}\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[1, 0],\n",
"[0, sqrt(5)]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Σ"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "557d065a",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left[\\begin{matrix}0 & 0 & 1\\\\\\frac{\\sqrt{5}}{5} & \\frac{2 \\sqrt{5}}{5} & 0\\end{matrix}\\right]$"
],
"text/plain": [
"Matrix([\n",
"[ 0, 0, 1],\n",
"[sqrt(5)/5, 2*sqrt(5)/5, 0]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"V"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5387019",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment