Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions Homework_1_Hatam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "52bbea1d-5c01-4cba-a0ad-fd7f61f5786c",
"metadata": {},
"source": [
"a)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "811d25bd-911d-47d5-a09d-81937dbbf7aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The Schwarzschild radius of the Sun is 2953.85 meter\n"
]
}
],
"source": [
"import pint\n",
"\n",
"units = pint.UnitRegistry()\n",
"\n",
"G = 6.674e-11 * units.meter**3 / (units.kilogram * units.second**2)\n",
"c = 2.998e8 * units.meter / units.second\n",
"M_sun = 1.989e30 * units.kilogram\n",
"\n",
"def Schwarzschild_Radius(M):\n",
" R_s = (2 * G * M) / c**2\n",
" return R_s\n",
"\n",
"print(f\"The Schwarzschild radius of the Sun is {Schwarzschild_Radius(M_sun).to(units.meter):.2f}\")\n"
]
},
{
"cell_type": "markdown",
"id": "c774bdbc-0c90-47ab-8bf7-90abd035c262",
"metadata": {},
"source": [
"b)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "44ad1650-f8dd-48c0-be28-aa43d763274d",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[9, 63, 54], [8, 40, 72], [21, 74, 84]] \n",
" [[9, 63, 54], [8, 40, 72], [21, 74, 84]] \n",
" [[ 9 63 54]\n",
" [ 8 40 72]\n",
" [21 74 84]]\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"#Nested loop\n",
"def nested(A,B):\n",
" rows_A,cols_A=np.shape(A)\n",
" rows_B,cols_B=np.shape(B)\n",
" \n",
" result=[]\n",
" for i in range(rows_A):\n",
" result.append([0]*cols_B)\n",
" \n",
" for i in range(rows_A):\n",
" for j in range(cols_B):\n",
" for k in range(cols_A):\n",
" result[i][j]+=A[i][k]*B[k][j]\n",
" return result\n",
"\n",
"\n",
"#list comprehension\n",
"def list_comp(A, B):\n",
" transB=list(zip(*B))\n",
" return [[sum(x*y for x,y in zip(i,j)) for j in transB] for i in A]\n",
"\n",
"\n",
"#numpy\n",
"def npy(A, B):\n",
" A_np = np.array(A)\n",
" B_np = np.array(B)\n",
" return np.dot(A_np, B_np)\n",
"\n",
"matrix1 = np.random.randint(0, 10, size=(3, 3))\n",
"matrix2 = np.random.randint(0, 10, size=(3, 3))\n",
"\n",
"print(nested(matrix1,matrix2),'\\n',list_comp(matrix1,matrix2),'\\n',npy(matrix1,matrix2))"
]
},
{
"cell_type": "markdown",
"id": "eb2cec3f-0bec-4fc8-b2a5-829f8f0b6c2f",
"metadata": {},
"source": [
"c)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c6e7431a-a995-4437-9f86-afbf027c0076",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nested loop - Mean time: 0.844403 s, Standard Deviation: 0.045546\n",
"List comprehension - Mean time: 0.423700 s, Standard Deviation: 0.026283\n",
"NumPy - Mean time: 0.000099 s, Standard Deviation: 0.000296\n"
]
}
],
"source": [
"import time\n",
"\n",
"def benchmark(multiply_func, runs=10, size=100):\n",
" times = []\n",
"\n",
" for _ in range(runs):\n",
" matrix1 = np.random.randint(0, 10, size=(size, size))\n",
" matrix2 = np.linalg.inv(matrix1)\n",
"\n",
" start = time.time()\n",
" multiply_func(matrix1, matrix2)\n",
" end = time.time()\n",
" times.append(end - start)\n",
"\n",
" mean_time = np.mean(times)\n",
" std_time = np.std(times)\n",
"\n",
" return mean_time, std_time\n",
"\n",
"mean_nested, std_nested = benchmark(nested, runs=10, size=100)\n",
"mean_list_comp, std_list_comp = benchmark(list_comp, runs=10, size=100)\n",
"mean_npy, std_npy = benchmark(npy, runs=10, size=100)\n",
"\n",
"print(f\"Nested loop - Mean time: {mean_nested:.6f} s, Standard Deviation: {std_nested:.6f}\")\n",
"print(f\"List comprehension - Mean time: {mean_list_comp:.6f} s, Standard Deviation: {std_list_comp:.6f}\")\n",
"print(f\"NumPy - Mean time: {mean_npy:.6f} s, Standard Deviation: {std_npy:.6f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5eaccdc-f517-4e47-92fd-faae4de90586",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
4 changes: 2 additions & 2 deletions pbhmergers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def rhocrit(self):
hubz2 = (self.overden.omega_matter0/aa**3 + self.overden.omega_lambda0) * hubble**2
#Critical density at redshift in units of kg m^-3
rhocrit = 3 * hubz2 / (8*math.pi* self.ureg.newtonian_constant_of_gravitation)
print "rhocrit = ", rhocrit
print("rhocrit = ", rhocrit)
return rhocrit.to_base_units()

def R200(self, mass):
Expand Down Expand Up @@ -224,7 +224,7 @@ def mergerhalflife(self,mass,threefac=True, bhmass=None):
threefac = self.threebodyratio(mass)
threefac = np.max([threefac, np.ones_like(threefac)],axis=0)
rate *= threefac
return 0.5*(mass/bhmass)/rat
return 0.5*(mass/bhmass)/rate

def bias(self,mass):
"""The formula for halo bias in EPS theory (Mo & White 1996), eq. 13"""
Expand Down
2 changes: 2 additions & 0 deletions pbhmergers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.ruff]
select = ["E", "F", "W"]
Empty file added power_spec.toml
Empty file.
Loading