Some remarks on Assignment 1

Relative difference

$$\Delta = \frac{|\mathrm{LHS}-\mathrm{RHS}|}{\max(|\mathrm{LHS}|,|\mathrm{RHS}|)}$$

This quantity is defined for each mass shell individually. If $\max$ was taken over the whole star, we would be comparing quantities in different regions, which limits the usefulness if the relative difference.

max vs fmax

In [2]:
A = array([1,5,1e10])
B = array([-17,24,16])
In [3]:
max(A) # overall maximum in a single array
Out[3]:
10000000000.0
In [4]:
fmax(A,B) # element-wise maximum, result is array of the same shape
Out[4]:
array([  1.00000000e+00,   2.40000000e+01,   1.00000000e+10])
In [5]:
m = mesa.profile('m-17.data')
rhs = - mesa.G * m.mass_grams / (4 * pi * m.radius_cm**4)
dmbar = (0.5 * (m.dm[:-1] + m.dm[1:]))
lhs = (m.pressure[:-1] - m.pressure[1:]) / dmbar
plot(m.mass[1:], abs(lhs - rhs[1:]) / np.fmax(np.abs(lhs),np.abs(rhs[1:])))
xlabel('$M/M_\odot$')
ylabel('$\Delta$')
yscale('log')

Some remarks on Python

Arrays

Python has lists of arbitrary objects marked by square brackets [...]. For computations these have to converted to an array (uniform datatype, supports mathematical operations on the whole array).

In [6]:
A = [1, 2, -1e-30] # list
B = array(A)

Selecting parts of an array

Indices in Python start at 0.

In [7]:
A = array([1, 2, 7.5, -1e-30])
In [8]:
A[1] # second element only
Out[8]:
2.0
In [9]:
A[-1] # last element
Out[9]:
-1.0000000000000001e-30
In [10]:
A[1:3] # elements 1 and 2 (last index not included)
Out[10]:
array([ 2. ,  7.5])
In [11]:
A[2:] # start at third (index 2) element to the end
Out[11]:
array([  7.50000000e+00,  -1.00000000e-30])
In [12]:
A.shape # shape of the whole array
Out[12]:
(4,)