Skip to content

Commit

Permalink
Merge pull request #92 from n-takumasa/fix
Browse files Browse the repository at this point in the history
Fix example and docs
  • Loading branch information
maroba authored Dec 30, 2024
2 parents b0258bc + ad3d468 commit e42d226
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest pytest-cov
python -m pip install pytest pytest-cov ipykernel nbclient matplotlib
- name: Install package
run: pip install -e .
- name: Show installed packages
Expand Down
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py
configuration: docs/source/conf.py

build:
os: ubuntu-22.04
Expand Down
3 changes: 3 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ scipy
sphinxcontrib.bibtex>=2.0
sphinx_rtd_theme
ipython
ipykernel
nbsphinx
nbsphinx-link
3 changes: 3 additions & 0 deletions docs/conf.py → docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"IPython.sphinxext.ipython_console_highlighting",
"IPython.sphinxext.ipython_directive",
"sphinx.ext.mathjax",
"nbsphinx",
"nbsphinx_link",
]


nbsphinx_execute = "always"
nbsphinx_kernel_name = "python3"

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down
1 change: 0 additions & 1 deletion docs/source/examples-periodic.ipynb

This file was deleted.

3 changes: 3 additions & 0 deletions docs/source/examples-periodic.nblink
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"path": "../../examples/examples-periodic.ipynb"
}
2 changes: 1 addition & 1 deletion docs/source/getstarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ This returns
('H', 'C'): {(-3, 0): -1.0, (-2, 0): 4.0, (-1, 0): -5.0, (0, -1): 1.0, (0, 0): 0.0, (0, 1): 1.0},
('H', 'H'): {(-3, 0): -1.0, (-2, 0): 4.0, (-1, 0): -5.0, (0, -3): -1.0, (0, -2): 4.0, (0, -1): -5.0, (0, 0): 4.0}}
In the interior of the grid (the :code:`('C', 'C') case), the stencil looks
In the interior of the grid (the :code:`('C', 'C')` case), the stencil looks
like this:

.. image:: images/laplace2d.png
Expand Down
12 changes: 6 additions & 6 deletions docs/index.rst → docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A Python package for finite difference numerical derivatives
and partial differential equations in any number of dimensions.


.. image:: source/images/findiff_logo.png
.. image:: images/findiff_logo.png
:width: 300
:align: center

Expand All @@ -33,11 +33,11 @@ Content
.. toctree::
:maxdepth: 1

source/getstarted
source/examples
source/theory
source/citation
source/modules
getstarted
examples
theory
citation
modules


Indices and tables
Expand Down
146 changes: 79 additions & 67 deletions examples/examples-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"jupyter": {
"is_executing": true
"is_executing": true,
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"from findiff import Diff"
],
"outputs": [],
"execution_count": null
]
},
{
"cell_type": "markdown",
Expand All @@ -42,24 +44,22 @@
"Suppose we want to differentiate two 1D-arrays `f` and `g`, which are filled with values from a function\n",
"\n",
"$$\n",
"f(x) = \\sin(x) \\quad \\mbox{and}\\quad g(x) = \\cos(x)\n",
"f(x) = \\sin(x) \\quad \\text{and}\\quad g(x) = \\cos(x)\n",
"$$\n",
"\n",
"and we want to take the 2nd derivative. This is easy done analytically:\n",
"\n",
"$$\n",
"\\frac{d^2f}{dx^2} = -\\sin(x) \\quad \\mbox{and}\\quad \\frac{d^2g}{dx^2} = -\\cos(x)\n",
"\\frac{d^2f}{dx^2} = -\\sin(x) \\quad \\text{and}\\quad \\frac{d^2g}{dx^2} = -\\cos(x)\n",
"$$\n",
"\n",
"Let's do this numerically with _findiff_. First we set up the grid and the arrays:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 100)\n",
Expand All @@ -77,12 +77,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": "d2_dx2 = Diff(0, dx=dx, acc=2)"
"source": [
"d2_dx2 = Diff(0, dx, acc=2)"
]
},
{
"cell_type": "markdown",
Expand All @@ -95,10 +95,8 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"result_f = d2_dx2(f)\n",
Expand All @@ -123,28 +121,36 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 18,
"metadata": {
"collapsed": false
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'backward': {'coefficients': array([-1., 4., -5., 2.]),\n",
" 'offsets': array([-3, -2, -1, 0])},\n",
" 'center': {'coefficients': array([ 1., -2., 1.]),\n",
" 'offsets': array([-1, 0, 1])},\n",
"{'center': {'coefficients': array([ 1., -2., 1.]),\n",
" 'offsets': array([-1, 0, 1]),\n",
" 'accuracy': 2},\n",
" 'forward': {'coefficients': array([ 2., -5., 4., -1.]),\n",
" 'offsets': array([0, 1, 2, 3])}}"
" 'offsets': array([0, 1, 2, 3]),\n",
" 'accuracy': 2},\n",
" 'backward': {'coefficients': array([-1., 4., -5., 2.]),\n",
" 'offsets': array([-3, -2, -1, 0]),\n",
" 'accuracy': 2}}"
]
},
"execution_count": 5,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from findiff import coefficients\n",
"\n",
"coefficients(deriv=2, acc=2)"
]
},
Expand All @@ -157,29 +163,35 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 19,
"metadata": {
"collapsed": false
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'backward': {'coefficients': array([ -0.53253968, 6.42373016, -35.55158728, 119.41369042,\n",
" -271.26190464, 439.39444427, -521.11333314, 457.02976176,\n",
" -295.51984119, 138.59325394, -44.43730158, 7.56162698]),\n",
" 'offsets': array([-11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0])},\n",
" 'center': {'coefficients': array([ 3.17460317e-04, -4.96031746e-03, 3.96825397e-02, -2.38095238e-01,\n",
"{'center': {'coefficients': array([ 3.17460317e-04, -4.96031746e-03, 3.96825397e-02, -2.38095238e-01,\n",
" 1.66666667e+00, -2.92722222e+00, 1.66666667e+00, -2.38095238e-01,\n",
" 3.96825397e-02, -4.96031746e-03, 3.17460317e-04]),\n",
" 'offsets': array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])},\n",
" 'forward': {'coefficients': array([ 7.56162876, -44.43731776, 138.59331976, -295.52000468,\n",
" 457.03003946, -521.1136706 , 439.39474213, -271.26209495,\n",
" 119.41377646, -35.55161345, 6.42373497, -0.53254009]),\n",
" 'offsets': array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])}}"
" 'offsets': array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]),\n",
" 'accuracy': 10},\n",
" 'forward': {'coefficients': array([ 7.56163106, -44.43733488, 138.59338192, -295.52014839,\n",
" 457.03027109, -521.11394092, 439.39497337, -271.26223908,\n",
" 119.41384033, -35.55163256, 6.42373843, -0.53254037]),\n",
" 'offsets': array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),\n",
" 'accuracy': 1},\n",
" 'backward': {'coefficients': array([ -0.53253968, 6.42373016, -35.55158728, 119.41369042,\n",
" -271.26190464, 439.39444426, -521.11333313, 457.02976175,\n",
" -295.51984119, 138.59325394, -44.43730158, 7.56162698]),\n",
" 'offsets': array([-11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]),\n",
" 'accuracy': 7}}"
]
},
"execution_count": 6,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -199,12 +211,17 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 20,
"metadata": {
"collapsed": false
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"from findiff import FinDiff\n",
"\n",
"d2_dx2 = FinDiff(0, dx, 2, acc=10)\n",
"result = d2_dx2(f)"
]
Expand All @@ -229,10 +246,8 @@
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"x, y, z = [np.linspace(0, 10, 100)]*3\n",
Expand All @@ -250,10 +265,8 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"d_dx = FinDiff(0, dx)\n",
Expand All @@ -269,10 +282,8 @@
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"d3_dx2dy = FinDiff((0, dx, 2), (1, dy))\n",
Expand Down Expand Up @@ -304,9 +315,12 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 24,
"metadata": {
"collapsed": false
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
Expand All @@ -330,12 +344,12 @@
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"from findiff import Coefficient\n",
"\n",
"linear_op = Coefficient(X) * FinDiff(0, dx) + Coefficient(Y**2) * FinDiff(1, dy)"
]
},
Expand All @@ -348,10 +362,8 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"result = linear_op(f)"
Expand All @@ -360,7 +372,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
Expand All @@ -374,9 +386,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading

0 comments on commit e42d226

Please sign in to comment.