hgrecco / numbakit-ode

Leveraging numba to speed up ODE integration
Other
68 stars 3 forks source link

First code sample from docs has several errors #15

Closed astrojuanlu closed 3 years ago

astrojuanlu commented 3 years ago

First code sample from https://numbakit-ode.readthedocs.io/en/latest/#numbakit-ode-leveraging-numba-to-speed-up-ode-integration:

>>> import nbkode
>>> def func(t, y):
...     return -0.1. * y
>>> t0 = 0.
>>> y0 = 1.
>>> solver = nbkode.Euler(func, t0, y0)
>>> ts, ys = solver.run([0., 5., 10.])

Has a trailing . in -0.1. * y:

In [1]: >>> import nbkode
   ...: >>> def func(t, y):
   ...: ...     return -0.1. * y
   ...: >>> t0 = 0.
   ...: >>> y0 = 1.
   ...: >>> solver = nbkode.Euler(func, t0, y0)
   ...: >>> ts, ys = solver.run([0., 5., 10.])
  File "<ipython-input-1-45132754029e>", line 3
    return -0.1. * y
                 ^
SyntaxError: invalid syntax

But even after removing it, it fails:

In [2]: >>> import nbkode                                                                                                                                                                                                           [34/36107]
   ...: >>> def func(t, y):
   ...: ...     return -0.1 * y                   
   ...: >>> t0 = 0.                       
   ...: >>> y0 = 1.
   ...: >>> solver = nbkode.Euler(func, t0, y0)                                                                        
   ...: >>> ts, ys = solver.run([0., 5., 10.])
<ipython-input-2-6b34ad789574>:6: UserWarning: The name 'Euler' is an alias and its use is discouraged. Use 'ForwardEuler' instead.
  solver = nbkode.Euler(func, t0, y0)                                                                                  
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-6b34ad789574> in <module>
      5 y0 = 1.                                                                                                        
      6 solver = nbkode.Euler(func, t0, y0)
----> 7 ts, ys = solver.run([0., 5., 10.])   

~/.pyenv/versions/poliastro38/lib/python3.8/site-packages/nbkode/core.py in run(self, t)
    347         if is_to_interpolate:
    348             t_old = t[to_interpolate]
--> 349             y_old = np.asarray([self.interpolate(_t) for _t in t_old])                  
    350             t_to_run = t[np.logical_not(to_interpolate)]
    351         else:

~/.pyenv/versions/poliastro38/lib/python3.8/site-packages/nbkode/core.py in <listcomp>(.0)
    347         if is_to_interpolate:
    348             t_old = t[to_interpolate]
--> 349             y_old = np.asarray([self.interpolate(_t) for _t in t_old])                        
    350             t_to_run = t[np.logical_not(to_interpolate)]
    351         else:            

~/.pyenv/versions/poliastro38/lib/python3.8/site-packages/nbkode/core.py in interpolate(self, t)
    394             )
    395 
--> 396         return self._interpolate(t, *self._step_args)
    397 
    398     @staticmethod

~/.pyenv/versions/poliastro38/lib/python3.8/site-packages/nbkode/multistep/core.py in _step_args(self)
     82     @property
     83     def _step_args(self):
---> 84         return self.rhs, self.cache, self.h
     85 
     86 

AttributeError: 'ForwardEuler' object has no attribute 'h'
hgrecco commented 3 years ago

Thanks for the report. I have just pushed a commit that fixes some aspects of the docs (actually updates them to the latest version) but also adds doctest in travis. numbakit-ode API is still quite in flow and this will help us to properly document this.