Search code examples
c++arraysarrayfire

Create 2d array from a division of two 1d-arrays in arrayfire


I have two 1d-arrays in arrayfire, x and y. I would like to divide them through each other, and create a 2d-array from the result, i.e. as shown in the following code:

#include <arrayfire>

int main(void){

    const size_t x_len = 1024, y_len = 2048;
    af::array x(x_len, f64), y(y_len, f64);
    //Fill x, y with y \neq 0
    //Now either
    af::array xy(x_len, y_len, f64); //Gives a 2d-array
    for(size_t i = 0; i < x.dims(0); ++i)
        for(size_t j = 0; j < y.dims(0); ++j)
            xy(i, j) = x(i) / y(j);
    //or
    af::array xy = x / y; //Gives a 1d-array

    return 0;
}

The former approach gives me the targeted 2d-array, the latter approach does not (and will result in a crash if x_len != y_len. I could use the approach written above, but I assume that it will be significantly slower than specialized commands.
Therefore, are there such commands available in arrayfire, or do I have to use loops?


Solution

  • af::array xy = matmulNT(x, 1/y);