Vectorizing the operation
TejasMane opened this issue · comments
Consider the following code segment:
x_indices = af.Array([0,1,0])
y_indices = af.Array([1,1,2])
data = af.Array([0, 5, 10])
I want A[0, 1] = data[0]
, A[1,1] = data[1]
and A[0, 2] = data[2]
. There are no patterns present in x_indices
and y_indices
.
Working Implementation
for i in range(x_indices.elements()) # looping over the indices
A[x_indices[i], y_indices[i]] = data[i]
How can I vectorize the above operation.
An similar operation in numpy can be done in the following manner:
A[x_indices, y_indices] = data
This would do the required operation:
A[0, 1] = data[0]
, A[1,1] = data[1]
and A[0, 2] = data[2]
But the arrayfire implementation does not seem to do the same. How can I perform this vectorized assignment using arrayfire.
As far as I know this is currently not possible. It also prevents me from implementing numpy fancy indexing with multiple arrays (what you described) in afnumpy.
Thanks, Any smarter workarounds for the same, besides converting the Arrayfire arrays to numpy arrays?
@TejasMane I assume you are a colleague of @ShyamSS-95 (I am assuming because you forked one of his repos).
He opened an issue about the same topic yesterday, please read through my comments regarding this:
#126. I am closing this issue, please focus the discussion over there.
@FilipeMaia Hmm, this is indeed tricky. The indexing in the python wrapper is a bit mixed (follows a bit of traditional python and a little bit of arrayfire-isms) at the moment. Do you think it would be ok if I added a function that performs this operation instead of using the operator ?
Sure, that would be helpful. It might even be useful to do it on the C side.
@FilipeMaia It is supported in C side. Python behaves differently from C because all missing dimensions in python are assumed to be spans.. Where as in C they are assumed to be index 0..
@pavanky The problem is not the missing dimensions, but the way the arrays are combined.
For example if one has [0,1],[0,1] as the two arrays for numpy this would mean that the coordinates retrieved would be [0,0], [1,1] while for arrayfire it would be [0,0], [0,1], [1,0], [1,1].
How to get the first behaviour using the C API?
@FilipeMaia I understand the basic problem. I was talking about the work around we use in C / C++, i.e. you can do A(y * A.dims(0) + x) = val
to do the numpy equivalent of A[x, y] = val
.
However when you do A[y * A.dims(0) + x] = val
in arrayfire python, it becomes the C++ equivalent of A(y * A.dims(0) + x, span) = val
.
@pavanky Ah ok, understood. But to reiterate, yes a python function to do this would be useful.