Skip to content
Open

HW1 #20

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
1 change: 1 addition & 0 deletions .~lock.ssh-keygen -t ed25519 -C "ssham033@ucr.edu".pub#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,samaneh,samaneh-N552VW,27.09.2024 14:32,file:///home/samaneh/.config/libreoffice/4;
173 changes: 173 additions & 0 deletions Computational Astrophysics.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3767ec62",
"metadata": {},
"source": [
"###Problem 1.3\n",
"##Part a)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4412eec9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Schwarzschild radius of the Sun: 2954.126555055405 meter\n"
]
}
],
"source": [
"\n",
"UR = UnitRegistry()\n",
"\n",
"G = 6.67430e-11 * UR.meter**3 / (UR.kilogram * UR.second**2)\n",
"M_sun = 1.989e30 * UR.kilogram\n",
"c = 299792458 * UR.meter / UR.second\n",
"\n",
"R_s = 2 * G * M_sun / c**2\n",
"print(R_s.to(UR.meter))\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "ea10a00a",
"metadata": {},
"source": [
"##Part b) Matrix Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ba49d93",
"metadata": {},
"outputs": [],
"source": [
"from pint import UnitRegistry\n",
"import numpy as np\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "657814a6",
"metadata": {},
"outputs": [],
"source": [
"def matrix_multiply_for_loops(X, Y):\n",
" rows_X, cols_X = len(X), len(X[0])\n",
" rows_Y, cols_Y = len(Y), len(Y[0])\n",
" \n",
" result = [[0 for _ in range(cols_Y)] for _ in range(rows_X)]\n",
" \n",
" for i in range(rows_X):\n",
" for j in range(cols_Y):\n",
" for k in range(cols_X):\n",
" result[i][j] += X[i][k] * Y[k][j]\n",
" \n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fc57baa",
"metadata": {},
"outputs": [],
"source": [
"def matrix_multiply_list_comprehension(X, Y):\n",
" return [[sum(X[i][k] * Y[k][j] for k in range(len(X[0]))) for j in range(len(Y[0]))] for i in range(len(X))]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b434e35b",
"metadata": {},
"outputs": [],
"source": [
"def matrix_multiply_numpy(X, Y):\n",
" return np.dot(X, Y)\n"
]
},
{
"cell_type": "markdown",
"id": "2997814c",
"metadata": {},
"source": [
"##Part C"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec4d0e2a",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"def generate_random_matrix(size):\n",
" return np.random.rand(size, size)\n",
"\n",
"def test_multiplication_routines(matrix_size):\n",
" A = generate_random_matrix(matrix_size)\n",
" A_inv = np.linalg.inv(A)\n",
" A_list = A.tolist()\n",
" A_inv_list = A_inv.tolist()\n",
"\n",
" start_time = time.time()\n",
" result_for_loops = matrix_multiply_for_loops(A_list, A_inv_list)\n",
" time_for_loops = time.time() - start_time\n",
"\n",
" start_time = time.time()\n",
" result_list_comprehension = matrix_multiply_list_comprehension(A_list, A_inv_list)\n",
" time_list_comprehension = time.time() - start_time\n",
"\n",
" start_time = time.time()\n",
" result_numpy = matrix_multiply_numpy(A, A_inv)\n",
" time_numpy = time.time() - start_time\n",
"\n",
" identity_matrix = np.identity(matrix_size)\n",
" result_for_loops = np.array(result_for_loops)\n",
" result_list_comprehension = np.array(result_list_comprehension)\n",
"\n",
" print(f\"Time for loops: {time_for_loops:.6f} seconds, Correct: {np.allclose(result_for_loops, identity_matrix)}\")\n",
" print(f\"Time list comprehension: {time_list_comprehension:.6f} seconds, Correct: {np.allclose(result_list_comprehension, identity_matrix)}\")\n",
" print(f\"Time numpy: {time_numpy:.6f} seconds, Correct: {np.allclose(result_numpy, identity_matrix)}\")\n",
"\n",
"test_multiplication_routines(100)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading