{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by dowloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n",
"
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n",
"
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Imports\n",
"The tutorial below imports [NumPy](http://www.numpy.org/), [Pandas](https://plotly.com/pandas/intro-to-pandas-tutorial/), and [SciPy](https://www.scipy.org/)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"from plotly.tools import FigureFactory as FF\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import scipy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Add Two Matrices"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A Matrix is a 2D array that stores real or complex numbers. A _Real Matrix_ is one such that all its elements $r$ belong to $\\mathbb{R}$. Likewise, a _Complex Matrix_ has entries $c$ in $\\mathbb{C}$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix1 = np.matrix(\n",
" [[0, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"matrix2 = np.matrix(\n",
" [[-1, 2],\n",
" [1, -2]]\n",
")\n",
"\n",
"matrix_sum = matrix1 + matrix2\n",
"\n",
"colorscale = [[0, '#EAEFC4'], [1, '#9BDF46']]\n",
"font=['#000000', '#000000']\n",
"\n",
"table = FF.create_annotated_heatmap(matrix_sum.tolist(), colorscale=colorscale, font_colors=font)\n",
"py.iplot(table, filename='matrix-sum')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Multiply Two Matrices\n",
"How to find the product of two matrices"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix1 = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"matrix2 = np.matrix(\n",
" [[-1, 2],\n",
" [1, -2]]\n",
")\n",
"\n",
"matrix_prod = matrix1 * matrix2\n",
"\n",
"colorscale = [[0, '#F1FFD9'], [1, '#8BDBF5']]\n",
"font=['#000000', '#000000']\n",
"\n",
"table = FF.create_annotated_heatmap(matrix_prod.tolist(), colorscale=colorscale, font_colors=font)\n",
"py.iplot(table, filename='matrix-prod')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Solve Matrix Equation\n",
"How to find the solution of $AX=B$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"B = np.matrix(\n",
" [[-1, 2],\n",
" [1, -2]]\n",
")\n",
"\n",
"X = np.linalg.solve(A, B)\n",
"\n",
"colorscale = [[0, '#497285'], [1, '#DFEBED']]\n",
"font=['#000000', '#000000']\n",
"\n",
"table = FF.create_annotated_heatmap(X.tolist(), colorscale=colorscale, font_colors=font)\n",
"py.iplot(table, filename='matrix-eq')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find the Determinant"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-7.9999999999999982"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"det = np.linalg.det(matrix)\n",
"det"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find the Inverse"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"inverse = np.linalg.inv(matrix)\n",
"\n",
"colorscale = [[0, '#F1FAFB'], [1, '#A0E4F1']]\n",
"font=['#000000', '#000000']\n",
"\n",
"table = FF.create_annotated_heatmap(inverse.tolist(), colorscale=colorscale, font_colors=font)\n",
"py.iplot(table, filename='inverse')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find Eigenvalues"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The eignevalues are 3.372281 and -2.372281\n"
]
}
],
"source": [
"matrix = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"eigvals = np.linalg.eigvals(matrix)\n",
"print(\"The eignevalues are %f and %f\") %(eigvals[0], eigvals[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Find SVD\n",
"How to find the Singular Value Decomposition of a matrix, i.e. break up a matrix into the product of three matrices: $U$, $\\Sigma$, $V^*$"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"matrix = np.matrix(\n",
" [[1, 4],\n",
" [2, 0]]\n",
")\n",
"\n",
"svd = np.linalg.svd(matrix)\n",
"\n",
"u = svd[0]\n",
"sigma = svd[1]\n",
"v = svd[2]\n",
"\n",
"u = u.tolist()\n",
"sigma = sigma.tolist()\n",
"v = v.tolist()\n",
"\n",
"colorscale = [[0, '#111111'],[1, '#222222']]\n",
"font=['#ffffff', '#ffffff']\n",
"\n",
"matrix_prod = [\n",
" ['$U$', '', '$\\Sigma$', '$V^*$', ''],\n",
" [u[0][0], u[0][1], sigma[0], v[0][0], v[0][1]],\n",
" [u[1][0], u[1][1], sigma[1], v[1][0], v[1][1]]\n",
"]\n",
"\n",
"table = FF.create_table(matrix_prod)\n",
"py.iplot(table, filename='svd')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting git+https://github.com/plotly/publisher.git\n",
" Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-JSnMuv-build\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.10\n",
" Uninstalling publisher-0.10:\n",
" Successfully uninstalled publisher-0.10\n",
" Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \b|\b \bdone\n",
"\u001b[?25hSuccessfully installed publisher-0.10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install git+https://github.com/plotly/publisher.git --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'python_Linear_Algebra.ipynb', 'python/linear-algebra/', 'Linear Algebra | plotly',\n",
" 'Learn how to perform several operations on matrices including inverse, eigenvalues, and determinents',\n",
" title='Linear Algebra in Python. | plotly',\n",
" name='Linear Algebra',\n",
" language='python',\n",
" page_type='example_index', has_thumbnail='false', display_as='mathematics', order=10,\n",
" ipynb= '~notebook_demo/104')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}