Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sage/calculus/ode.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cdef class ode_system:
cdef int c_j(self, double , double *, double *, double *) noexcept
cdef int c_j(self, double , const double *, double *, double *) noexcept

cdef int c_f(self, double t, double* , double*) noexcept
cdef int c_f(self, double t, const double* , double*) noexcept
55 changes: 26 additions & 29 deletions src/sage/calculus/ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
self.y_n = x

cdef class ode_system:
cdef int c_j(self, double t, double *y, double *dfdy, double *dfdt) noexcept:
cdef int c_j(self, double t, const double *y, double *dfdy, double *dfdt) noexcept:
return 0

cdef int c_f(self, double t, double* y, double* dydt) noexcept:
cdef int c_f(self, double t, const double* y, double* dydt) noexcept:
return 0

cdef int c_jac_compiled(double t, const double *y, double *dfdy, double *dfdt, void *params) noexcept:
Expand Down Expand Up @@ -303,35 +303,32 @@
is slow on systems that require many function evaluations. It
is possible to pass a compiled function by deriving from the
class :class:`ode_system` and overloading ``c_f`` and ``c_j`` with C
functions that specify the system. The following will work in the
notebook:

.. code-block:: cython

%cython
cimport sage.calculus.ode
import sage.calculus.ode
from sage.libs.gsl.all cimport *

cdef class van_der_pol(sage.calculus.ode.ode_system):
cdef int c_f(self, double t, double *y, double *dydt):
dydt[0]=y[1]
dydt[1]=-y[0]-1000*y[1]*(y[0]*y[0]-1)
return GSL_SUCCESS
cdef int c_j(self, double t, double *y, double *dfdy, double *dfdt):
dfdy[0]=0
dfdy[1]=1.0
dfdy[2]=-2.0*1000*y[0]*y[1]-1.0
dfdy[3]=-1000*(y[0]*y[0]-1.0)
dfdt[0]=0
dfdt[1]=0
return GSL_SUCCESS
functions that specify the system. (You can also use the ``%%cython``
cell magic, see :meth:`~sage.repl.ipython_extension.SageMagics.cython`.) ::

sage: cython('''

Check warning on line 309 in src/sage/calculus/ode.pyx

View workflow job for this annotation

GitHub Actions / Conda (ubuntu, Python 3.12, all, editable)

Warning: slow doctest:

slow doctest:: Test ran for 5.28s cpu, 5.33s wall Check ran for 0.00s cpu, 0.00s wall

Check warning on line 309 in src/sage/calculus/ode.pyx

View workflow job for this annotation

GitHub Actions / Conda (macos, Python 3.13, all)

Warning: slow doctest:

slow doctest:: Test ran for 6.71s cpu, 10.96s wall Check ran for 0.01s cpu, 0.00s wall
....: cimport sage.calculus.ode
....: import sage.calculus.ode
....: from sage.libs.gsl.all cimport *
....:
....: cdef class van_der_pol(sage.calculus.ode.ode_system):
....: cdef int c_f(self, double t, const double *y, double *dydt) noexcept:
....: dydt[0]=y[1]
....: dydt[1]=-y[0]-1000*y[1]*(y[0]*y[0]-1)
....: return GSL_SUCCESS
....: cdef int c_j(self, double t, const double *y, double *dfdy, double *dfdt) noexcept:
....: dfdy[0]=0
....: dfdy[1]=1.0
....: dfdy[2]=-2.0*1000*y[0]*y[1]-1.0
....: dfdy[3]=-1000*(y[0]*y[0]-1.0)
....: dfdt[0]=0
....: dfdt[1]=0
....: return GSL_SUCCESS
....: ''')

After executing the above block of code you can do the
following (WARNING: the following is *not* automatically
doctested)::
following::

sage: # not tested
sage: T = ode_solver()
sage: T.algorithm = "bsimp"
sage: vander = van_der_pol()
Expand Down Expand Up @@ -455,7 +452,7 @@
raise MemoryError("error allocating memory")
result = []
v = [0] * dim
cdef gsl_odeiv_step_type * T
cdef const gsl_odeiv_step_type * T

for i in range(dim): # copy initial conditions into C array
y[i] = self.y_0[i]
Expand Down
4 changes: 2 additions & 2 deletions src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ def compile_and_load(code, **kwds):
r"""
INPUT:

- ``code`` -- string containing code that could be in a .pyx file
that is attached or put in a %cython block in the notebook
- ``code`` -- string containing code that could be in a ``.pyx`` file
that is attached or put in a ``%%cython`` block

See the function :func:`sage.misc.cython.cython` for documentation
for the other inputs.
Expand Down
Loading