# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Problem 5.3 Writing python. 
#
# a) Write a python script, using pint, which finds the Schwarzchild radius of the Sun, in m.

#Importing libraries
from pint import UnitRegistry
import numpy as np
from halo_mass_function import HaloMassFunction
import time
from astropy import constants as const

ureg = UnitRegistry()

#creating constants using pint to set thier untis
G = const.G.value * ureg.meter**3 * ureg.kilogram**-1 * ureg.second**-2
M_sun = const.M_sun.value * ureg.kilogram
c = const.c.value * ureg.meter * ureg.second**-1
print((2*G*M_sun)/c**2)

# **b) Using the “halo_mass_function.py” script, write a python function to compute the total number of halos at z = 0 (and with the default cosmological parameters) with a mass above 10^12M⊙, according to one of the listed halo mass function formulae.**

#initializing the class with a redshift of z and the default mass function (tinker_200)
test = HaloMassFunction(0)

#creating an array of stellar masses between 10^12 and 10^20 
ugh = np.logspace(12,20,100000)
#calculating the number of halos at z=0
print(np.sum([test.dndm(ugh[ii])*(ugh[ii]) for ii in range(len(ugh))]))

# **c) Write a simple routine to multiply two matrices together in python. Write one routine using nested for loops, one using a list comprehension, and one using the built-in numpy matrix multiplication routines.**

#creating two matrices
x = np.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
y = np.array([[1.,2.,3.,4.],[5.,6.,7.,8.],[9.,1.,2.,3.]])

# +
#multiplying two matrices using nested for loops

#empty array to hold the product of the two arrays above
result = np.zeros([x.shape[0],y.shape[1]])

#looping over each row in x
for ii in range(x.shape[0]):
    #looping over each column in y
    for jj in range(y.shape[1]):
        #multiplying the column of y by the row of x
        result[ii][jj] = np.sum(x[ii]*y[:,jj])
        
print(result)
# -

#multiplying two matrices using list comprehension
result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*y)] for X_row in x]
print(result)

#multiplying two matrices using numpy.dot
result = np.dot(x,y)
print(result)

# **d) Write a simple test for the above routine that multiplies a large matrix with its inverse to obtain the identity. Use this test to evaluate how fast each of the routines in (c) are.**

#creating a large array with random elements
np.random.seed(10)
x = np.random.randint(10, size=(8, 8))
#using numpy to calculate its inverse
invx = np.linalg.inv(x)

# +
#calculating how long nested for loops take to multiply two matrices
start = time.time()
result = np.zeros([x.shape[0],invx.shape[1]])

for ii in range(x.shape[0]):
    for jj in range(invx.shape[1]):
        result[ii][jj] = np.sum(x[ii,:]*invx[:,jj])
end = time.time()
print('time elapse: '+str(end - start))

# +
#calculating how long list comprehension takes to multiply two matrices
start = time.time()

result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*invx)] for X_row in x]

end = time.time()
print('time elapse: '+str(end - start))

# +
#calculating how long numpy takes to multiply two matrices
start = time.time()

result = np.dot(x,invx)

end = time.time()
print('time elapse: '+str(end - start))
# -



StarcoderdataJupyterScriptsDedupFiltered