Skip to content

Instantly share code, notes, and snippets.

@DBremen
Created February 26, 2020 12:21
Show Gist options
  • Save DBremen/476e8a254dda320f41f1aa42d953f357 to your computer and use it in GitHub Desktop.
Save DBremen/476e8a254dda320f41f1aa42d953f357 to your computer and use it in GitHub Desktop.
Matrices
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import sympy as sym\nimport numpy as np\nfrom IPython.display import display,Math",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def disp(expr):\n display(Math(sym.latex(sym.sympify(expr))))",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "A = np.array([[1,2], [3,4]])\ndisp(A)",
"execution_count": 8,
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}1 & 2\\\\3 & 4\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mat = np.zeros([4,6])\ndisp(mat)\nmat[0,1] = 2\ndisp(mat)",
"execution_count": 11,
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}0.0 & 2.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "numrange = range(0,4)\nmat = np.zeros([4,6])\nfor row in numrange:\n for col in numrange:\n #alternate between -1 and 1\n mat[row,col] = (-1)**(row+col)\ndisp(mat)",
"execution_count": 18,
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}1.0 & -1.0 & 1.0 & -1.0 & 1.0 & -1.0\\\\-1.0 & 1.0 & -1.0 & 1.0 & -1.0 & 1.0\\\\1.0 & -1.0 & 1.0 & -1.0 & 1.0 & -1.0\\\\-1.0 & 1.0 & -1.0 & 1.0 & -1.0 & 1.0\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x,y = sym.symbols('x y')\nexp = (4+x)*(2-y)\nnumrange = range(0,3)\nmat = np.zeros([3,3])\nfor i in numrange:\n for j in numrange:\n mat[i,j] = exp.subs({x:i,y:j})\n\ndisp(mat)",
"execution_count": 24,
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}8.0 & 4.0 & 0.0\\\\10.0 & 5.0 & 0.0\\\\12.0 & 6.0 & 0.0\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def multiplicationMatrix(numRange = range(1,11)):\n end = len(numRange)\n mat = np.zeros([end,end],dtype=int)\n for row in numRange:\n for col in numRange:\n mat[row-1,col-1] = row * col\n disp(mat)\nmultiplicationMatrix()",
"execution_count": 44,
"outputs": [
{
"data": {
"text/latex": "$\\displaystyle \\left[\\begin{matrix}1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10\\\\2 & 4 & 6 & 8 & 10 & 12 & 14 & 16 & 18 & 20\\\\3 & 6 & 9 & 12 & 15 & 18 & 21 & 24 & 27 & 30\\\\4 & 8 & 12 & 16 & 20 & 24 & 28 & 32 & 36 & 40\\\\5 & 10 & 15 & 20 & 25 & 30 & 35 & 40 & 45 & 50\\\\6 & 12 & 18 & 24 & 30 & 36 & 42 & 48 & 54 & 60\\\\7 & 14 & 21 & 28 & 35 & 42 & 49 & 56 & 63 & 70\\\\8 & 16 & 24 & 32 & 40 & 48 & 56 & 64 & 72 & 80\\\\9 & 18 & 27 & 36 & 45 & 54 & 63 & 72 & 81 & 90\\\\10 & 20 & 30 & 40 & 50 & 60 & 70 & 80 & 90 & 100\\end{matrix}\\right]$",
"text/plain": "<IPython.core.display.Math object>"
},
"metadata": {},
"output_type": "display_data"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.3",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"gist": {
"id": "",
"data": {
"description": "Matrices",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment