---
jupytext:
  formats: ipynb,md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.13.6
kernelspec:
  display_name: Python 3 (phys-581)
  language: python
  name: phys-581
---

Project Ideas
=============

Here are some ideas about how to use some of the things taught in this course to achieve
some interesting results.

(proj:improve-solve_ivp)=
## Improve `solve_ivp`

*Difficulty: Medium*


There are issues with `solve_ivp`, especially with the adaptive refinement (see the
issues below).  One could try to improve this in several ways.  Low-hanging fruit would
be to develop some tests to help understand this issue.

* [RK45 produces wrong result for specific setting
  #9899](https://github.com/scipy/scipy/issues/9899): Demonstration of issues with the
  adaptive step refinement with the `RK45` (and `DoPri5`) methods.
* [Proposal: Update Runge-Kutta step size algorithms for predictable performance
  #9822](https://github.com/scipy/scipy/issues/9822): Proposal to improve the algorithm.
* [All `solve_ivp` issues](https://github.com/scipy/scipy/search?q=solve_ivp&type=issues).

```{code-cell}
:tags: [margin]
import math
import numpy as np
%timeit math.sin(1.2)
%timeit np.sin(1.2)
```
```{code-cell}
:tags: [margin]
xs = np.linspace(0, 1, 1000)
%timeit [math.sin(x) for x in xs]
%timeit np.sin(xs)
```
:::{margin}
Simple demonstration that {py:func}`math.sin` is faster that {py:func}`numpy.sin` for
floats, but that the latter is faster when applied directly to arrays.
:::
## Improved `timeit`:

*Difficulty: Medium*

The {py:mod}`timeit` module provides code for measuring the performance of a chunk of
code.  To improve the reliability of the results, it runs your code a specified `number`
of times.  The {py:mod}`IPython` [%timeit][] magic improves this by provide some simple
machinery to estimate how big `number` should be, balancing accuracy vs. runtime.  It
also provides the average and standard deviation of `repeat` measurements to give an
idea of the variability.

The project is to write an improved version that determines how many `repeats` are
needed to attain a certain uncertainty with a desired confidence level.  Additionally,
consider if the mean is the appropriate measure.  In many contexts one would prefer the
minimum.

To make this problem definite: suppose that the measured runtime is a random variable $T$
drawn from some distribution $\rho(T)$.
1. If this supposition is true, then how will the minimum of $N$ samples (repeated
   measurements) be distributed?
2. As you collect data, you can construct the [empirical distribution function
   (EDF)][EDF].  How can you use this to determine when you have obtained enough samples
   to provide a reliable estimate of the minimum within a specified confidence interval?
3. Use MCMC to validate your analytic results, then test your procedure.  Is the
   assumption that $T$ can be described by a fixed distribution valid?
4. If not, can you make an improved model?

## Support Multiple Kernels in MyST-NB or MyST-MD

*Difficulty: Hard*

I would like to be able to compare code run in Python, C++, Julia, etc. in a single
MyST Markdown document.  For example, I would like to verify that the three sets of
example code on the course homepage work by actually running them.  This requires having
the ability to run Python, Octave/Matlab, and C++ in the same document.

Under the hood, I believe that the documents are executed as Jupyter Notebooks, which
seems to imply that we might need a way to support multiple kernels in a single
notebook.  I do not think this functionality exists generally but see [Script of Scripts
(SoS)][SoS].

Parts of this project:

1. Figure out how technically this might be done.
2. Converge on a good syntax for this: please engage various members of the community
   including [CoCalc][], [SoS][], and [Executable Books][] to see if a unified syntax
   can be used.  See [executablebooks discussion #1137][] for contacts to reach out to.
3. Implement something that works for us.

### References 

* Code cell format, CoCalc, Multiple Kernels etc. -- [executablebooks discussion
  #1137][].  Start here: this links to many other sources and identifies the people to
  speak with.
* [CoCalc Markdown][]: The Markdown editor on [CoCalc][] has a syntax that allows you to
  run multiple kernels in a single file. See also:
  * [cocalc issue #8257][]:
  * [cocalc issue #7193][]:
* [SoS PolyGlot][]: Apparently has support for multiple kernels in a single notebook.
* https://github.com/executablebooks/MyST-NB/issues/32


(proj:TestingNotebookCells)=
## Testing Notebook Cells

*Difficulty: Medium*

I would like to be able to write doctests or similar in my notebooks/markdown files and
have them execute.

### References

* <https://github.com/orgs/executablebooks/discussions/776>: Discussion.
* <https://github.com/executablebooks/MyST-NB/issues/290>: Main issue Start here.
* <https://github.com/executablebooks/MyST-Parser/discussions/601>: Potential solution?
* <https://github.com/mwouts/jupytext/issues/220>
* <https://testbook.readthedocs.io/en/latest/index.html>
* <https://sphinx-book-theme.readthedocs.io/en/stable/reference/kitchen-sink/blocks.html#doctest-blocks>
* [`ipython_doctester`](https://github.com/catherinedevlin/ipython_doctester): Unmaintained.
* <https://github.com/python/cpython/issues/113329>

[cocalc issue #8257]: <https://github.com/sagemathinc/cocalc/issues/8257>
[cocalc issue #7193]: <https://github.com/sagemathinc/cocalc/issues/7193>
[executablebooks discussion #1137]: <https://github.com/orgs/executablebooks/discussions/1137>

[SoS PolyGlot]: <https://github.com/vatlab/jupyterlab-sos>
[SoS]: <https://vatlab.github.io/sos-docs/>
[CoCalc Markdown]: <https://doc.cocalc.com/markdown.html>

[%timeit]: <https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit>
[EDF]: <https://en.wikipedia.org/wiki/Empirical_distribution_function>
[MCMC]: <https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo>

[Executable Books]: <https://executablebooks.org/en/latest/>
[CoCalc]: <https://cocalc.com>
