Skip to content

Commit

Permalink
Prettify the output of the astropy table
Browse files Browse the repository at this point in the history
  • Loading branch information
zachetienne committed Sep 10, 2018
1 parent 0d6f177 commit 0865de6
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions Tutorial-Finite_Difference_Derivatives-FDtable_soln.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating the Finite-Difference Table for Centered First and Second Derivatives, from Second through Eighth-Order Accuracy\n",
"# Creating the Finite-Difference Table for Centered First and Second Derivatives, from 2nd through 12th-Order Accuracy\n",
"\n",
"## Courtesy Brandon Clark"
"## *Courtesy Brandon Clark*"
]
},
{
Expand All @@ -18,16 +18,20 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Derivative Accuracy -4 -3 -2 -1 0 1 2 3 4 \n",
"---------- -------- ------ ------ ----- ---- ------- --- ----- ----- ------\n",
" First 2 0 0 0 -1/2 0 1/2 0 0 0\n",
" First 4 0 0 1/12 -2/3 0 2/3 -1/12 0 0\n",
" First 6 0 -1/60 3/20 -3/4 0 3/4 -3/20 1/60 0\n",
" First 8 1/280 -4/105 1/5 -4/5 0 4/5 -1/5 4/105 -1/280\n",
" Second 2 0 0 0 1 -2 1 0 0 0\n",
" Second 4 0 0 -1/12 4/3 -5/2 4/3 -1/12 0 0\n",
" Second 6 0 1/90 -3/20 3/2 -49/18 3/2 -3/20 1/90 0\n",
" Second 8 -1/560 8/315 -1/5 8/5 -205/72 8/5 -1/5 8/315 -1/560\n"
"Derivative Accuracy -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 \n",
"---------- -------- -------- ------- ------- ------ ------ ---- ---------- ---- ------ ------ ------- ------ --------\n",
" 1st 2 -1/2 1/2 \n",
" 1st 4 1/12 -2/3 2/3 -1/12 \n",
" 1st 6 -1/60 3/20 -3/4 3/4 -3/20 1/60 \n",
" 1st 8 1/280 -4/105 1/5 -4/5 4/5 -1/5 4/105 -1/280 \n",
" 1st 10 -1/1260 5/504 -5/84 5/21 -5/6 5/6 -5/21 5/84 -5/504 1/1260 \n",
" 1st 12 1/5544 -1/385 1/56 -5/63 15/56 -6/7 6/7 -15/56 5/63 -1/56 1/385 -1/5544\n",
" 2nd 2 1 -2 1 \n",
" 2nd 4 -1/12 4/3 -5/2 4/3 -1/12 \n",
" 2nd 6 1/90 -3/20 3/2 -49/18 3/2 -3/20 1/90 \n",
" 2nd 8 -1/560 8/315 -1/5 8/5 -205/72 8/5 -1/5 8/315 -1/560 \n",
" 2nd 10 1/3150 -5/1008 5/126 -5/21 5/3 -5269/1800 5/3 -5/21 5/126 -5/1008 1/3150 \n",
" 2nd 12 -1/16632 2/1925 -1/112 10/189 -15/56 12/7 -5369/1800 12/7 -15/56 10/189 -1/112 2/1925 -1/16632\n"
]
}
],
Expand All @@ -38,22 +42,26 @@
"from astropy.table import Table\n",
"\n",
"# Step 1: Set the maximum finite-difference accuracy order\n",
"max_fdorder = 8\n",
"max_fdorder =12\n",
"\n",
"# Step 2: Set up table parameters\n",
"# One column for deriv order, one for deriv accuracy, and max_fdorder+1\n",
"numcols = 2 + max_fdorder + 1\n",
"# 8 rows: 4 accuracy orders per derivative order.\n",
"numrows = 8\n",
"# 8 rows: max_fdorder accuracy orders per derivative order, times 2 derivative orders (first & second derivative)\n",
"numrows = int(max_fdorder/2 * 2)\n",
"# Center column index of table will be at 2 + max_fdorder/2 (zero-offset indexing)\n",
"column_corresponding_to_zero_fd_point = 2 + int(max_fdorder/2)\n",
"# The table is initialized as a matrix of zeroes in numpy:\n",
"# The table is initialized as a matrix of zeroes in numpy...\n",
"numpy_matrix = np.zeros((numrows, numcols), dtype=object)\n",
"# Then we replace all elements with the empty string to match the Wikipedia article.\n",
"for row in range(numrows):\n",
" for col in range(numcols):\n",
" numpy_matrix[row,col] = \"\"\n",
"\n",
"# Step 3: Construct the first-order derivative finite difference coefficients\n",
"rowcount = 0\n",
"for fdorder in range(2, max_fdorder+1, 2): # loop runs from 2 to max_fdorder inclusive, skipping odd orders.\n",
" numpy_matrix[rowcount, 0] = \"First\"\n",
" numpy_matrix[rowcount, 0] = \"1st\"\n",
" numpy_matrix[rowcount, 1] = fdorder \n",
" fdcoeffs, fdstencl = fin.compute_fdcoeffs_fdstencl(\"D0\", fdorder)\n",
" for i in range(fdorder):\n",
Expand All @@ -62,16 +70,19 @@
"\n",
"# Step 4: Construct the first-order derivative finite difference coefficients\n",
"for fdorder in range(2, max_fdorder+1, 2): # loop runs from 2 to max_fdorder inclusive, skipping odd orders.\n",
" numpy_matrix[rowcount, 0] = \"Second\"\n",
" numpy_matrix[rowcount, 0] = \"2nd\"\n",
" numpy_matrix[rowcount, 1] = fdorder\n",
" fdcoeffs, fdstencl = fin.compute_fdcoeffs_fdstencl(\"DD00\", fdorder)\n",
" for i in range(fdorder+1):\n",
" numpy_matrix[rowcount, column_corresponding_to_zero_fd_point + fdstencl[i][0]] = fdcoeffs[i]\n",
" rowcount += 1 \n",
"\n",
"# Step 5: Construct an astropy table from the numpy matrix with the following header info, and then print it:\n",
"table = Table(numpy_matrix, names=('Derivative', 'Accuracy', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4'))\n",
"print(table)"
"colnames = ['Derivative','Accuracy']\n",
"for i in range(-int(max_fdorder/2),int(max_fdorder/2)+1):\n",
" colnames.append(str(i))\n",
"table = Table(numpy_matrix, names=colnames)\n",
"table.pprint(max_width=-1)"
]
}
],
Expand Down

0 comments on commit 0865de6

Please sign in to comment.