%matplotlib
import numpy as np
# backend TkAgg is the default apparently
# this opens another window where the plotting is done!
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg') # if you want to hard set the backend
# the plot buttons are very flacky, apparently a common problem on macOS
# It's easier to get off the window and back on than to get a click to work
# double clicks sometimes work and single ones don't
plt.figure()
x = np.arange(0,10,0.1)
y= x**2
plt.plot(x,y,'ro')
# restart the kernel and then try this
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
plt.figure()
x = np.arange(0,10,0.1)
y= x**2
plt.plot(x,y,'ro')
# the buttons work better in this backend
# lets add to the plot
plt.plot(x,np.sin(x),'b-')
# restart the kernel and then this
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
the file is called subs.py and contains the following:
import numpy as np
def simple_function(x):
return np.sin(3*x)
The file is in the same directory as my notebook
import subs
x = np.arange(0,10,0.1)
z = subs.simple_function(x)
plt.plot(x,z)
If you edit subs.py the function that you have loaded into python will not necessarily be updated. You can update it with importlib.reload
I notice a directory pycache and in it is a binary file called subs.cpython-38.pyc which is a compiled version of my subroutine. The subroutine is recompiled when I import the function again with reload().
As of 2021 autoreload seems not to work anymore.
from importlib import reload
reload(subs)
x = np.arange(0,2,0.01)
z = subs.simple_function(x)
plt.plot(x,z)
# the plot has changed!
np.log(-1)
z = np.log(-1)
?z
np.isnan(z)
You add a regular number to a nan and you get a nan.
If there are interactions between particles, this can make an entire particle based simulation crash.
z = np.log(-1)
w = z+1
print(w) # you add a regular number to a nan and you get a nan
print(type(w))
import numpy as np
w1 = np.sqrt(-1)
w2 = np.sqrt(-2)
if (w1 > w2):
print("nan is bigger than nan")
if (w1 <= w2):
print("nan is smaller or equal to nan")
if (w1 == w2):
print("nan is equal to nan")
nothing was printed so comparisons give False when comparing two nans
print(w1>w2)
print(w1<=w2)
print(w1==w2)
np.exp(1000)
#10**(-400)
type(np.exp(1000))
z = np.exp(1000)
np.isinf(z)
for i in range(10,1000,10):
print(10.0**i)
This is dependent on the number of digits available in the exponent for double precision floats
for i in range(1,370,10):
print (10**(-i))
ww = 1.0/0.0
print(ww)
Divide by zero crashes code
import pdb # the debugger!
def add_to_life_universe_everything(x):
answer = 42
pdb.set_trace()
answer += x
return answer
add_to_life_universe_everything(12)
# to exit this type continue, you can type in variable names and
# find out what they contain
z=1+2j
z
z?
c1 = complex(1,2)
print(c1)
np.sqrt(c1)
np.sqrt(-1 + 0j)
c1.imag
c1.real