|
| 1 | +# Documenting Code |
| 2 | + |
| 3 | +When developing a new class or function, or improving current classes in `pySDC`, adding Python docstring to document the code is important, in particular : |
| 4 | + |
| 5 | +- to help developer understanding how classes and functions work when reading the code |
| 6 | +- to help user understanding how classes and functions work when reading the [documentation](https://parallel-in-time.org/pySDC/#api-documentation) |
| 7 | + |
| 8 | +`pySDC` follows the [NumPy Style Python Docstring](https://numpydoc.readthedocs.io/en/latest/format.html), below is simplified example |
| 9 | +for a class and a function : |
| 10 | + |
| 11 | +> :bell: Don't document the `init` function, but rather the class itself. Also describe parameters (given to the `__init__`) and attributes (stored into the class) separately. |
| 12 | +
|
| 13 | +```python |
| 14 | +class LagrangeApproximation(object): |
| 15 | + r""" |
| 16 | + Class approximating any function on a given set of points using barycentric |
| 17 | + Lagrange interpolation. |
| 18 | +
|
| 19 | + Let note :math:`(t_j)_{0\leq j<n}` the set of points, then any scalar |
| 20 | + function :math:`f` can be approximated by the barycentric formula : |
| 21 | +
|
| 22 | + .. math:: |
| 23 | + p(x) = |
| 24 | + \frac{\displaystyle \sum_{j=0}^{n-1}\frac{w_j}{x-x_j}f_j} |
| 25 | + {\displaystyle \sum_{j=0}^{n-1}\frac{w_j}{x-x_j}}, |
| 26 | +
|
| 27 | + where :math:`f_j=f(t_j)` and |
| 28 | +
|
| 29 | + .. math:: |
| 30 | + w_j = \frac{1}{\prod_{k\neq j}(x_j-x_k)} |
| 31 | +
|
| 32 | + are the barycentric weights. |
| 33 | + The theory and implementation is inspired from `this paper <http://dx.doi.org/10.1137/S0036144502417715>`_. |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + points : list, tuple or np.1darray |
| 38 | + The given interpolation points, no specific scaling, but must be |
| 39 | + ordered in increasing order. |
| 40 | +
|
| 41 | + Attributes |
| 42 | + ---------- |
| 43 | + points : np.1darray |
| 44 | + The interpolating points |
| 45 | + weights : np.1darray |
| 46 | + The associated barycentric weights |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, points): |
| 50 | + pass # Implementation ... |
| 51 | + |
| 52 | + @property |
| 53 | + def n(self): |
| 54 | + """Number of points""" |
| 55 | + pass # Implementation ... |
| 56 | + |
| 57 | + def getInterpolationMatrix(self, times): |
| 58 | + r""" |
| 59 | + Compute the interpolation matrix for a given set of discrete "time" |
| 60 | + points. |
| 61 | +
|
| 62 | + For instance, if we note :math:`\vec{f}` the vector containing the |
| 63 | + :math:`f_j=f(t_j)` values, and :math:`(\tau_m)_{0\leq m<M}` |
| 64 | + the "time" points where to interpolate. |
| 65 | + Then :math:`I[\vec{f}]`, the vector containing the interpolated |
| 66 | + :math:`f(\tau_m)` values, can be obtained using : |
| 67 | +
|
| 68 | + .. math:: |
| 69 | + I[\vec{f}] = P_{Inter} \vec{f}, |
| 70 | +
|
| 71 | + where :math:`P_{Inter}` is the interpolation matrix returned by this |
| 72 | + method. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + times : list-like or np.1darray |
| 77 | + The discrete "time" points where to interpolate the function. |
| 78 | +
|
| 79 | + Returns |
| 80 | + ------- |
| 81 | + PInter : np.2darray(M, n) |
| 82 | + The interpolation matrix, with :math:`M` rows (size of the **times** |
| 83 | + parameter) and :math:`n` columns. |
| 84 | + """ |
| 85 | + pass # Implementation ... |
| 86 | + |
| 87 | +``` |
| 88 | + |
| 89 | +A more detailed example is given [here ...](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html) |
| 90 | + |
| 91 | +:arrow_left: [Back to custom implementations](./04_custom_implementations.md) --- |
| 92 | +:arrow_up: [Contributing Summary](./../../CONTRIBUTING.md) --- |
| 93 | +:arrow_right: [Next to a cute picture of cat](https://www.vecteezy.com/photo/2098203-silver-tabby-cat-sitting-on-green-background) |
0 commit comments