# Linear Algebra in Python/v3

Learn how to perform several operations on matrices including inverse, eigenvalues, and determinents

See our Version 4 Migration Guide for information about how to upgrade.

#### New to Plotly?¶

Plotly's Python library is free and open source! Get started by dowloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

#### Imports¶

The tutorial below imports NumPy, Pandas, and SciPy.

In :
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF

import numpy as np
import pandas as pd
import scipy


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}$.

In :
matrix1 = np.matrix(
[[0, 4],
[2, 0]]
)

matrix2 = np.matrix(
[[-1, 2],
[1, -2]]
)

matrix_sum = matrix1 + matrix2

colorscale = [[0, '#EAEFC4'], [1, '#9BDF46']]
font=['#000000', '#000000']

table = FF.create_annotated_heatmap(matrix_sum.tolist(), colorscale=colorscale, font_colors=font)
py.iplot(table, filename='matrix-sum')

Out:

#### Multiply Two Matrices¶

How to find the product of two matrices

In :
matrix1 = np.matrix(
[[1, 4],
[2, 0]]
)

matrix2 = np.matrix(
[[-1, 2],
[1, -2]]
)

matrix_prod = matrix1 * matrix2

colorscale = [[0, '#F1FFD9'], [1, '#8BDBF5']]
font=['#000000', '#000000']

table = FF.create_annotated_heatmap(matrix_prod.tolist(), colorscale=colorscale, font_colors=font)
py.iplot(table, filename='matrix-prod')

Out:

#### Solve Matrix Equation¶

How to find the solution of $AX=B$

In :
A = np.matrix(
[[1, 4],
[2, 0]]
)

B = np.matrix(
[[-1, 2],
[1, -2]]
)

X = np.linalg.solve(A, B)

colorscale = [[0, '#497285'], [1, '#DFEBED']]
font=['#000000', '#000000']

table = FF.create_annotated_heatmap(X.tolist(), colorscale=colorscale, font_colors=font)
py.iplot(table, filename='matrix-eq')

Out:

#### Find the Determinant¶

In :
matrix = np.matrix(
[[1, 4],
[2, 0]]
)

det = np.linalg.det(matrix)
det

Out:
-7.9999999999999982

#### Find the Inverse¶

In :
matrix = np.matrix(
[[1, 4],
[2, 0]]
)

inverse = np.linalg.inv(matrix)

colorscale = [[0, '#F1FAFB'], [1, '#A0E4F1']]
font=['#000000', '#000000']

table = FF.create_annotated_heatmap(inverse.tolist(), colorscale=colorscale, font_colors=font)
py.iplot(table, filename='inverse')

Out:

#### Find Eigenvalues¶

In :
matrix = np.matrix(
[[1, 4],
[2, 0]]
)

eigvals = np.linalg.eigvals(matrix)
print("The eignevalues are %f and %f") %(eigvals, eigvals)

The eignevalues are 3.372281 and -2.372281


#### Find SVD¶

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^*$

In :
matrix = np.matrix(
[[1, 4],
[2, 0]]
)

svd = np.linalg.svd(matrix)

u = svd
sigma = svd
v = svd

u = u.tolist()
sigma = sigma.tolist()
v = v.tolist()

colorscale = [[0, '#111111'],[1, '#222222']]
font=['#ffffff', '#ffffff']

matrix_prod = [
['$U$', '', '$\Sigma$', '$V^*$', ''],
[u, u, sigma, v, v],
[u, u, sigma, v, v]
]

table = FF.create_table(matrix_prod)
py.iplot(table, filename='svd')

Out: 