Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Math 224 Intro to IPython.ipynb

52 views
Kernel: Python 3

In this tutorial we will cover the basics of IPython and Jupyter.

IPython Notebook is a web-based interactive computational environment for creating IPython notebooks. An IPython notebook is a document containing an ordered list of input and output cells which can contain code, text, mathematics, plots and any comnination of these. It is a useful tool to share information with other students or partners. This cell is currently in markdown. Notice in the toolbar you have the ability to change from code, markdown and Raw NBConvert. Markdown allows you to type text in the specific cell. Markdown can recognize latex format. For example if you wanted to type and equation, simply use the $ signs like in Latex. For example 0 = x^2 + 4x + 4 will become

0=x2+4x+40 = x^2 + 4x + 4

Now when you need to write code, obviously you will select code in the toolbar. The first thing that should be done before any code is written is to import any libraries that you know you will be neccessary. You are able to import them later but it is easier to do it at the beggining to avoid errors. These imported libraries give you access to pre-made functions and modules that can save you a lot of time when writing code.

Let's say we wanted to import the math library. This library gives you access to many mathematical functions. In the next cell we will import the math library and nickname it "mth."

import math as mth #import math to access mathematical Python Functions

Now we can access the math library anywhere in our program by using the command mth. Let's demonstrate this by entering mth.sqrt(), which is the square root function.

mth.sqrt(4) #input
2.0

The main other library we will need for Math 224 is sympy, which we import as "sp."

import sympy as sp

For the first example we will code and complete the following computations. (2.32)(4.52)\sqrt{(2.3^2)(4.5^2)} LaTeX\LaTeX 11.34+5.1611.3^4 + 5.1^6 LaTeX\LaTeX 21.63\sqrt[3]{21.6}

Python's mathematical syntax can be different from other languages or maple. For example, if we wanted to raise to a power, lets say 424^2, instead of 424^2 or 4exp(2) python requires the form 424**2

23=82**3 = 89.5=39**.5 = 3

The following operations are then completed below.

#To express output in a neat way use the print() command as follows. print((2.3**3*4.5)**.5) # To raise to a power use **. For example 3^2 = 3**2. print(mth.sqrt(2.3**3 * 4.5)) #The mth.sqrt( ) command or rasisng to **1/2 power are both valid. print(11.3**4 + 5.1**6) print(21.6**(1/3))
7.399425653386889 7.399425653386889 33901.02390099999 2.7849533001676674

We will primarily be interested in matrix algebra. So it will be useful to know how to define matrices and how to treat them as objects that can be used in computations.

Let's say you want to define the following 3x3 matrix: A=(.25.25.5.3.3.4.1.6.3).A=\left( \begin{array}{ccc} .25 & .25 & .5 \\ .3 & .3 & .4 \\ .1 & .6 & .3 \end{array} \right).

A = sp.Matrix([[.25,.25,.5],[.3,.3,.4],[.1,.6,.3]]) #defines the matrix A #prints the matrix
Matrix([ [0.25, 0.25, 0.5], [ 0.3, 0.3, 0.4], [ 0.1, 0.6, 0.3]])

You might want to compute AA to some power. Let's compute A3,A10,A58A^3, A^{10}, A^{58} and print each out.

A**3
Matrix([ [0.215625, 0.403125, 0.38125], [ 0.21175, 0.40675, 0.3815], [ 0.21225, 0.40225, 0.3855]])
A**10
Matrix([ [0.212765962272949, 0.404255307438965, 0.382978730288086], [0.212765962789258, 0.404255313754102, 0.382978723456641], [0.212765949126367, 0.404255331349023, 0.382978719524609]])
A**58
Matrix([ [0.212765957446808, 0.404255319148936, 0.382978723404255], [0.212765957446808, 0.404255319148936, 0.382978723404255], [0.212765957446808, 0.404255319148936, 0.382978723404255]])

You might also want to define a row vector. (This is a fancy way of saying a 1xn matrix.) Let's say u=(.8,.05,.15)u=(.8,.05,.15).

u = sp.Matrix([[.8, .05,.15]]) print(u)
Matrix([[0.800000000000000, 0.0500000000000000, 0.150000000000000]])

As long as matrices have the correct dimension, you can multiply them.

Here is uAu\cdot A.

u*A
Matrix([[0.23, 0.305, 0.465]])

If BB is also a 3x3 matrix, you can compute A+BA+B, ABA-B, ABA\cdot B, BAB\cdot A, etc.

Let's define BB to be the 3x3 matrix with 1,2,31,2,3 on its diagonal and zeros everywhere else. You can do this by hand or by using this slick command.

B = sp.diag(1,2,3) B
Matrix([ [1, 0, 0], [0, 2, 0], [0, 0, 3]])
A+B
Matrix([ [1.25, 0.25, 0.5], [ 0.3, 2.3, 0.4], [ 0.1, 0.6, 3.3]])
A-B
Matrix([ [-0.75, 0.25, 0.5], [ 0.3, -1.7, 0.4], [ 0.1, 0.6, -2.7]])
A*B
Matrix([ [0.25, 0.5, 1.5], [ 0.3, 0.6, 1.2], [ 0.1, 1.2, 0.9]])
B*A
Matrix([ [0.25, 0.25, 0.5], [ 0.6, 0.6, 0.8], [ 0.3, 1.8, 0.9]])

If your matrix is invertible, you can calculate the inverse.

A_inv = A**-1 A_inv
Matrix([ [-6.0, 9.0, -2.0], [-2.0, 1.0, 2.0], [ 6.0, -5.0, 0]])

There are loads of built-in Linear Algebra commands in the sympy library. The following tutorial is useful: http://docs.sympy.org/dev/tutorial/matrices.html#