Skip to content

Request change in ArrayFire API for empty array. #2683

@sebastienleclaire

Description

@sebastienleclaire

Hi,

I would like to request a change in the API behavior of ArrayFire for empty array.

At the moment, when an array is empty by the result of a declaration or an operation (such as with the “where” function), the resulting array has its dimension fixed until a new assignation to this array variable is made (well this is my interpretation of the results I have observed, it might not be completely right all the time as I did not check everything).

Example A:

Let say “indexE” is an empty array of dimension (0,1,1,1) which would be the result from the function “where”. (Note that indexE might not always be empty in my code and only the empty case is causing me problem). Let then say I use this indexing array to extract some data from some arrayA [of dim, (1, 10,1,1)] and arrayB [of dim of (1,10,2,1)].

arrayA(span, indexE, span, span) -> resulting array dim of (1,0,1,1)
arrayB(span, indexE, span, span) -> resulting array of dim of (1,0,2,1)

Up to now, this make sense to me as indexE is empty such that the resulting array would also be empty in the corresponding dimension where indexE is applied.

Later I would like to apply, as an example, the function “tile” to arrayA such that:
arrayTile = tile(arrayA(span, indexE, span, span), 1, 1, 2, 1) -> resulting array dim of (1,0,1,1)

However, I would expect the arrayTile to be of dimension (1,0,2,1), but “tile” ignore the change of dimension, most probably because the input array is empty.

This is causing me problem because I cannot do operation with other array, such as arrayB. Because of that, I have to do branching to treat different scenarios, otherwise ArrayFire crash.

The current behavior of ArrayFire is not what one would expect from array-based type of operation. I have test this behavior on MATLAB r2018b and the “repmat” function (“tile” equivalent function) correctly adjust the size of the output array, even if the input array is empty. I also tried similar code on Octave 5.1 as well as with Python 3.7 (with numpy), and they always correctly adjust the size of the output array, even if the input array is empty.

I have a second example where the behavior of empty array is not consistent with what one would expect.

Exemple B:

The application of the “sum” function on empty array. Right now, in ArrayFire:

sum(arrayB(span, indexE, span, span), 0) -> empty array of dim (1,0,2,1)
sum(arrayB(span, indexE, span, span), 1) -> empty array of dim (1,0,2,1)
sum(arrayB(span, indexE, span, span), 2) -> empty array of dim (1,0,2,1)

The function sum completely ignore its normal operation, most probably because the array is empty. I believe the function sum should do what it does usually, i.e. collapse the dimension, and then evaluate if the array is still empty or if the sum should be zero. The behavior I would expect:

sum(arrayB(span, indexE, span, span), 0) should give an empty array of dim (1,0,2,1).
sum(arrayB(span, indexE, span, span), 1) should give a non-empty array of dim (1,1,2,1) with value of 0 everywhere.
sum(arrayB(span, indexE, span, span), 2) should give an empty array of dim (1,0,1,1)

Notice that the resulting dimension is different for the three scenarios above and only the first case give the same result as in ArrayFire. This is causing me problem because I cannot do a normal flow of operations and because of that, I have to do branching to treat different scenarios, otherwise ArrayFire crash.

I know MATLAB r2018b is correctly doing it. I did not test on Octave 5.1 or Python 3.7.

Maybe there are other functions in ArrayFire that would need a change in the API in order to be more consistent in term of what one would expect from array-based operations.

I hope the description of the change request is clear enough and let me know if clarification is needed.

Thank you for considering this change in the API.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions