Skip to content

Implement ufuncs for CSPHJY, SPHJ, SPHY, CSPHIK, SPHI, SPHIK (allow fast vectorization) #3113

Closed
@certik

Description

@certik

Currently, the only way to use vectorized spherical Bessel functions is using a variation of:

In [3]: import numpy as np

In [4]: jn_vect = np.vectorize(sph_jn)


In [9]: jn_vect(0, [0.1, 0.2, 0.3, 0.5])
Out[9]:
(array([ 0.99833417,  0.99334665,  0.98506736,  0.95885108]),
 array([-0.03330001, -0.06640038, -0.09910289, -0.16253703]))

In [10]: jn_vect([0] * 4, [0.1, 0.2, 0.3, 0.5])
Out[10]:
(array([ 0.99833417,  0.99334665,  0.98506736,  0.95885108]),
 array([-0.03330001, -0.06640038, -0.09910289, -0.16253703]))

But this is slow, per the docstring of vectorize:

     The `vectorize` function is provided primarily for convenience, not for
    performance. The implementation is essentially a for loop.

As such, the solution is to implement proper ufuncs support for these special functions on the C level. Per @pv's suggestion, nowadays, this is fairly simple to do, take a look at:
generate_ufuncs.py
specfun_wrappers.h
specfun_wrappers.c

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions