Skip to content

Missing numpy.hanning, mod, searchsorted, rollaxis, unwrap, sinc, frexp #1608

Open

Description

Hi everyone,

Firstly, thank you for this awesome math library.

Secondly, during development I have across several functions I am missing coming from NumPy. I have seen that you have list of functions to implement (https://github.com/QuantStack/xtensor/projects/1), but couldn’t find some of the functions I am missing. After a brief talk on Gitter, I was suggested to write down an issue listing these missing functions:

numpy.hanning
https://docs.scipy.org/doc/numpy/reference/generated/numpy.hanning.html

Example of usage:

numpy.hanning(12)
array([0.        , 0.07937323, 0.29229249, 0.57115742, 0.82743037,
       0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249,
       0.07937323, 0.        ])

numpy.mod
https://docs.scipy.org/doc/numpy/reference/generated/numpy.mod.html

a = numpy.array([[1.0, 2.1, 3.9, 4.0],[-5.0, -6.2, -7.8, -8.5]])
numpy.mod(a, 0.5)
array([[0. , 0.1, 0.4, 0. ],
       [0. , 0.3, 0.2, 0. ]])
numpy.mod(a, -0.5)
array([[-0. , -0.4, -0.1, -0. ],
       [-0. , -0.2, -0.3, -0. ]])

numpy.searchsorted
https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html

Example of usage:

np.searchsorted([1,2,3,4,5], 3)
2
np.searchsorted([1,2,3,4,5], 3, side='right')
3
np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3])
array([0, 5, 1, 2])

numpy.rollaxis
https://docs.scipy.org/doc/numpy/reference/generated/numpy.rollaxis.html

Example of usage:

a = np.ones((3,4,5,6))
np.rollaxis(a, 3, 1).shape
(3, 6, 4, 5)

numpy.unwrap
https://docs.scipy.org/doc/numpy/reference/generated/numpy.unwrap.html

phase = np.linspace(0, np.pi, num=5)
phase[3:] += np.pi
phase
array([ 0.        ,  0.78539816,  1.57079633,  5.49778714,  6.28318531])
np.unwrap(phase)
array([ 0.        ,  0.78539816,  1.57079633, -0.78539816,  0.        ])

numpy.sinc
https://docs.scipy.org/doc/numpy/reference/generated/numpy.sinc.html
Example of usage:

x = np.linspace(-4, 4, 41)
np.sinc(x)

array([ -3.89804309e-17, -4.92362781e-02, -8.40918587e-02,
-8.90384387e-02, -5.84680802e-02, 3.89804309e-17,
6.68206631e-02, 1.16434881e-01, 1.26137788e-01,
8.50444803e-02, -3.89804309e-17, -1.03943254e-01,
-1.89206682e-01, -2.16236208e-01, -1.55914881e-01,
3.89804309e-17, 2.33872321e-01, 5.04551152e-01,
7.56826729e-01, 9.35489284e-01, 1.00000000e+00,
9.35489284e-01, 7.56826729e-01, 5.04551152e-01,
2.33872321e-01, 3.89804309e-17, -1.55914881e-01,
-2.16236208e-01, -1.89206682e-01, -1.03943254e-01,
-3.89804309e-17, 8.50444803e-02, 1.26137788e-01,
1.16434881e-01, 6.68206631e-02, 3.89804309e-17,
-5.84680802e-02, -8.90384387e-02, -8.40918587e-02,
-4.92362781e-02, -3.89804309e-17])

numpy.frexp
https://docs.scipy.org/doc/numpy/reference/generated/numpy.frexp.html

x = np.arange(9)
y1, y2 = np.frexp(x)

y1
array([ 0. , 0.5 , 0.5 , 0.75 , 0.5 , 0.625, 0.75 , 0.875,
0.5 ])

numpy.asscalar
https://docs.scipy.org/doc/numpy/reference/generated/numpy.asscalar.html

Also, there was a discussion about adding hstack and vstack Is the decission to omit it definitive or could you reconsider the decision?

Thank you.
Best regards,
Sob3kx

Activity

  1. wolfv commented on May 21, 2019

    @wolfv
    Member

    Hi, thanks for the list.

    numpy mod is implementd with the % operator, and xt::fmod.

    I think hanning is simple to implement as a function on top of an arange/linspace.

    frexp should be straightforward, too, since it exists in C++ (look at xmath to see how to define your own xfunction, or even in the docs we have a xfunction from lambda https://en.cppreference.com/w/cpp/numeric/math/frexp

    https://xtensor.readthedocs.io/en/latest/api/xfunction.html#_CPPv4I0DpEN2xt21make_lambda_xfunctionEDaRR1FDpRR1E

    sinc will be harder, as i can only find an implementation in boost: https://www.boost.org/doc/libs/1_51_0/boost/math/special_functions/sinc.hpp

    regarding hstack & vstack, you can achieve the same functionality by passing the axis argument. it would be simple to write a wrapper if you really need it.

    numpy.unwrap should be straightforward again (implement a xfunction, e.g. with the lambda method i linked above).

    numpy.searchsorted
    numpy.rollaxis

    numpy asscalar is deprecated apparently. this is also quite simple to write as a function

    template <class E>
    auto asscalar(const xexpression<E>& expr)
    {
        XTENSOR_ASSERT(expr.derived_cast().size() == 1);
        return xt::xscalar<typename E::value_type>(expr.derived_cast()());
    }
    

    I am not sure we want this in xtensor though (I also don't really see the point).

  2. wolfv commented on May 21, 2019

    @wolfv
    Member

    Ah, I wanted to write: searchsorted and rollaxis are probably a little harder to implement.

    It would be really cool if you try your hand at this!

  3. victorgabr commented on Sep 26, 2019

    @victorgabr

    I am looking forward xt::searchsorted too!
    I was playing with std::lower_bound without success. :(

  4. doronbehar commented on Oct 26, 2026

    @doronbehar

    xt::searchsorted seems to exist now thanks to:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions