Questions from Scientific Analysis Session in SPWP 2021
import numpy as np
from scipy import optimize
Question: Could you introduce the lambda function a bit more?
The Lambda functionality allows to have shorthand defintiions for function according to the schema
lambda arguments : expression
for or while loop)f = lambda x: x**2
print(type(f))
f(3)
def g(x):
return x**2
print(type(g))
g(3)
h = lambda x,y,z: (x**2+y**2+z**2)**0.5 # Cartesian length for 3D-vector
h(1,2,2)
They are helpful if you want to define function on the fly, e.g. as arguments of another method/function
a = ["spam","cheese","parrot"]
sorted(a) # Equivalent sorted(a,key=lambda x:x)
Let's define via the key variable a sorting that uses the reversed strings.
sorted(a,key=lambda x:x[-1])
Question: A general question: what is the differences using array and matrix?
Most important: np.matrix is depreciated
np.matrix is a subclass of np.ndarray and it has some further property like an Inverse and a Hermitian
m = np.matrix([[1,0],[1,2]])
print("Is matrix an instance of ndarray?",isinstance(m,np.ndarray))
print("Inverse:\n",m.I)
print("Hermitian:\n",m.H)
But it can only be one- (vector) or two-dimensional (matrix)
n = np.matrix(np.ones((3,3,3)))
v = np.matrix(np.ones(3))
m = np.matrix(np.ones((3,3)))
print(v)
print(m)
print(v.shape)
print(m.shape)
Question: In the example where quad fails to be accurate, it also gave an estimated error of 0. Can I ever trust the error estimate then? Can we also get error bounds?
The uncertainty that the integration method returns is purely numerical and does not include any information about the convergence behaviour or any potential occurance of systematic issue!
Question: Why do algorithms like optimize.brentq provide a separate mean of providing the arguments via args instead of telling you to use a lambda function?
Because it gives more flexibility and is less cumbersome and comprehensable than solving this via the a lambda function
def f(x,a):
return x-a
optimize.brentq(f,-1000,1000,args=(5,))
optimize.brentq(lambda x:f(x,5),-1000,1000)
Question to "Data structures" lecture: Slide 18. Why `(3,4,1)(8)is possible, but(3,2,5)(6)` is not?
Numpy is in the implicit broadcasting matching the arrays starting with the last dimension. Matching a dimension means that the number of entries need to be the same or the number of entries in at least one array needs to be 1. Hence the first case works since the first array has 1 entry along the last dimension and hence matchable to 8. In the second case we try to match arrays that have 5 and 6 entries in the last dimension repecitvely and thus fail.
To match dimensions we can inject new axes as shown below. These can be injected also in the middle of the array dimensions using : to indicate a column or ... (ellipsis) to indicate all remaining column. Thus the ellipsis can only be used once in such an expression
a=np.ones((3,2,5,4))
b=np.ones((3,5,4))
c = a*b[:,np.newaxis,:,:]
print(c.shape)