# Tools¶

• Purpose: To introduce and provide resources for the tools used to build and work with SimPEG

In this tutorial, we cover some of the basic tools you will need for scientific computing with Python. This follows the Tools section of the SimPEG Tutorials.

This development environment is the Jupyter Notebook. - To run a cell, is Shift + Enter - To clear your kernel Esc + 00 - other keyboard shortcuts are available through the help

In this notebook, we cover some basics of: - Python - NumPy - SciPy - Matplotlib

## Jupyter Notebook¶

A notebook containing the following examples is available for you to download and follow along. In the directory where you downloaded the notebook, open up a Jupyter Notebook from a terminal

jupyter notebook


and open tools.ipynb. A few things to note

• To execute a cell is Shift + Enter
• To restart the kernel (clean your slate) is Esc + 00

Throughout this tutorial, we will show a few tips for working with the notebook.

## Python¶

Python is a high-level interpreted computing language. Here we outline a few of the basics and common trip-ups. For more information and tutorials, check out the “Python Documentation.

### Types¶

In [1]:

# python has /types
print(type(1) == int)
print(type(1.) == float)
print(type(1j) == complex)
type(None)

True
True
True

Out[1]:

NoneType

In [2]:

# What happens if you add values of different types?
print(1 + 1.)

2.0


### Lists¶

In [3]:

mylist = [6, 5, 4, 3]
type(mylist)

Out[3]:

list

In [4]:

# length of a list
len(mylist)

Out[4]:

4

In [5]:

# python uses zero based indexing
print(mylist[0])

6

In [6]:

print(mylist[:2]) # counting up
print(mylist[2:]) # starting from
print(mylist[-1]) # going back

[6, 5]
[4, 3]
3


### Loops and List Comprehension¶

In [7]:

n = 10 # try making this larger --> see which is faster

In [8]:

%%time
a = []
for i in range(n): # for loop assignment
a.append(i)
print(a)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
CPU times: user 433 µs, sys: 228 µs, total: 661 µs
Wall time: 519 µs

In [9]:

%%time
b = [i for i in range(n)]  # list comprehension
print(b)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
CPU times: user 177 µs, sys: 130 µs, total: 307 µs
Wall time: 225 µs

In [10]:

# Enumerateing
mylist = ['Monty', 'Python', 'Flying', 'Circus']
for i, val in enumerate(mylist):
print(i, val)

(0, 'Monty')
(1, 'Python')
(2, 'Flying')
(3, 'Circus')


### If, elif, else¶

In [11]:

# Pick a random number between 0 and 100
import numpy as np  # n-dimensional array package
number = (100.*np.random.rand(1)).round() # make it an integer
if number > 42:
print('{} is too high'.format(number))
elif number < 42:
print('{} is too low'.format(number))
else:
print('you found the secret to life. {}'.format(number))

[ 80.] is too high


### Functions¶

In [12]:

def pickAnumber(number):
if number > 42:
print('{} is too high'.format(number))
return False
elif number < 42:
print('{} is too low'.format(number))
return False
else:
print('you found the secret to life. {}'.format(number))
return True

In [13]:

print(pickAnumber(10))

10 is too low
False


## NumPy¶

In [14]:

import numpy as np

In [15]:

a = np.array(1) # scalar
print(a.shape)

b = np.array([1]) # vector
print(b.shape)

c = np.array([[1]]) # array
print(c.shape)

()
(1,)
(1, 1)

In [16]:

# vectors
v = np.random.rand(10)
a = v.T * v
print(a.shape)

(10,)

In [17]:

b = v.dot(v)
b.shape

Out[17]:

()

In [18]:

# arrays
w = np.random.rand(10,1)
w.shape

Out[18]:

(10, 1)

In [19]:

M = np.random.rand(10,10)

In [20]:

M*w

Out[20]:

array([[  3.25188743e-02,   4.92519569e-01,   3.17769482e-01,
1.62990360e-01,   8.00466183e-02,   3.11101540e-01,
6.65949536e-01,   5.67346931e-01,   2.80542007e-01,
2.50803390e-01],
[  7.22183982e-02,   3.86448026e-01,   4.67098459e-01,
7.97864836e-01,   3.41720915e-01,   2.89982502e-02,
5.42760970e-02,   5.80674906e-01,   5.79362050e-01,
8.99394176e-02],
[  4.53133096e-01,   3.65661125e-01,   1.28520684e-01,
4.63030183e-01,   2.65291293e-01,   1.26083976e-01,
1.99241030e-01,   4.70528071e-01,   2.15402983e-01,
1.79831562e-01],
[  2.05142855e-02,   1.27534730e-01,   7.01471887e-02,
9.91664917e-02,   2.48915497e-01,   1.41795026e-01,
1.04706300e-01,   6.55260506e-02,   5.24234012e-03,
1.82409773e-01],
[  3.63753592e-01,   1.66280243e-02,   1.76370399e-01,
3.35909879e-01,   2.21186333e-01,   1.88711763e-01,
1.19357847e-01,   2.32056250e-01,   3.09422091e-01,
6.91429881e-02],
[  4.12529652e-03,   1.15257282e-02,   1.08327291e-02,
2.80340180e-03,   8.90018530e-03,   8.07691125e-03,
2.95786163e-04,   5.39511251e-03,   3.89623815e-03,
6.73232414e-03],
[  1.21740911e-02,   1.74437381e-02,   6.78170009e-03,
9.76612815e-03,   1.78418409e-03,   2.06502604e-02,
2.16636016e-02,   1.39762425e-02,   9.06194337e-03,
6.81148530e-03],
[  5.53735274e-01,   6.23230033e-02,   7.01777420e-01,
1.11887285e-01,   1.73087575e-01,   1.59849059e-02,
5.62557010e-01,   7.45454500e-01,   7.08426405e-01,
6.66128071e-01],
[  3.52409083e-01,   4.75285279e-01,   4.01001096e-01,
3.88714112e-01,   1.78702239e-01,   2.62767827e-01,
3.11659310e-01,   6.77770359e-01,   1.91372082e-01,
4.49230938e-01],
[  7.03959432e-01,   1.55762937e-01,   2.16987667e-01,
8.94950712e-01,   4.53569699e-01,   5.44972753e-01,
2.48014003e-01,   5.70798534e-02,   3.50781232e-01,
3.06749502e-01]])


## Resources¶

• Software Carpentry
• Python Tutorial
In [ ]: