xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.33k stars 398 forks source link

`repeat` function unavailable? #1383

Open ktnyt opened 5 years ago

ktnyt commented 5 years ago

Hello, the NumPy coverage chart suggests that a numpy.repeat equivalent is available but searching through the docs/code I couldn't quite find the implementation. I guess stack is pretty close but not quite identical. Am I missing somewhere to look?

JohanMabille commented 5 years ago

By NumPy coverage chart you mean this project ? It might be incomplete, the cheat sheet is more reliable.

I've added the numpy.repeat function to the list of features to implement, do you want to implement it?

ktnyt commented 5 years ago

I saw the Google Spreadsheet data from another issue but I might have been looking at something different. I might have a go when I get the time on hands to do it!

wolfv commented 5 years ago

Now that we have the keep slice we could implement repeat with this mechanism and it would only be a few lines of code!

wolfv commented 5 years ago

e.g. xt::xview(a, keep(0,0,0,0)) would repeat the 4 times.

MaratZakirov commented 5 years ago

e.g. xt::xview(a, keep(0,0,0,0)) would repeat the 4 times.

How to do the same N(not statically known) times?

wolfv commented 5 years ago

i think you can do keep(std::vector...).

jorbs commented 5 years ago

@wolfv is the third parameter of xt::xview similar to the axis parameter on numpy? https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html#numpy.repeat

jorbs commented 5 years ago

By NumPy coverage chart you mean this project ? It might be incomplete, the cheat sheet is more reliable.

I've added the numpy.repeat function to the list of features to implement, do you want to implement it?

@JohanMabille how much complex is it? could you give me details?

JohanMabille commented 5 years ago

The problem with implementing it with xview and keep slices is that you don't have a feature as dynamic as NumPy.

xt::xview(a, keep({0,0,0,0})) would be for repeating the first element across the first axis. But if you want something more generic (for instance repeating the element across axis=0 for a 2D container) you would have to do something like xt::view(a, xt::all(), kepp({0, 0, 0, 0})). Generalizing it is not possiblewith xview since the return type would depend on dynamic information. Maybe it's possible with dynamic_view but that would be horribly slow.

@jobs the parameters of xview are the expression first, and then the slices (the position of the slice in the argument list corresponds to the axis parameter).

If we want the repeat function to be lazy, I think we have no choice but implementing it in a dedicated expression class. This is not that hard and could be a good project to dive into the internals of xtensor. I can provide some guidance if you decide to tackle it.

jorbs commented 5 years ago

@JohanMabille how I can achieve this: np.repeat(arr, repeats=c, axis=-1) with dynamic_view, even it becoming slow?

Could you give an introduction about the repeat implementation so that I could evaluate if I can tackle it?

tort-dla-psa commented 4 years ago

+1, having "repeat" functionality would be very nice

kolibri91 commented 4 years ago

I took a look into the code to check how a repeat function similar to np.repeat could be implemented. I'm not an expert in the xtensor code and this is my personal opinion how it could be implemented. I didn't find a better place to start the discussion about the feature, so please let me know if there is already an other place for it.

Similar to @JohanMabille, I think a new expression has to be implemented. The additional implementation basically replaces the iterator of the enclosed expression by an iterator which repeats the elements for given number in the given axis.

This sounds not that hard, but you have to be aware to the complexity of introducing a new expression. I think a solution like this will have a lot of test effort to be sure every functionality which can be applied to a expression works as expected on the additional implementation. I don't know if there is might already a set of test which tests the interface of a expression-like object?

On the other side, when you create the testing infrastructure for adding more expression-like objects, you would be able to expand the xtensor-framework with more powerful expression.

@JohanMabille Would be very nice if can give me some guidance. I'd like to take the challenge to dive into the internals of xtensor.

JohanMabille commented 4 years ago

There are some common test cases for container API and container-based operations, but we don't have real generic tests for all expressions (in part because some expressions have specificities that must be tested in dedicated test cases).

Regarding the internals of xtensor, there are two "entry points":

Do not hesitate if you have any question, I would be happy to help!

kolibri91 commented 4 years ago

This could be closed. In addition, numpy coverage in projects should be updated.