How can I use Cython well to solve a differential equation faster?

后端 未结 3 1376
梦如初夏
梦如初夏 2020-12-17 03:41

I would like to lower the time Scipy\'s odeint takes for solving a differential equation.

To practice, I used the example covered in Python in scientific computat

3条回答
  •  情深已故
    2020-12-17 04:04

    The easiest change to make (which will probably gain you a lot) is to use the C math library sin and cos for operations on single numbers instead of number. The call to numpy and the time spent working out that it isn't an array is fairly costly.

    from libc.math cimport sin, cos
    
        # later
        -omega/Q + sin(theta) + d*cos(Omega*t)
    

    I'd be tempted to assign a type to the input d (none of the other inputs are easily typed without changing the interface):

    def f(y, double t, params):
    

    I think I'd also just return a list like you do in your Python version. I don't think you gain a lot by using a C array.

提交回复
热议问题