| 18 | using std::vector; |
| 19 | |
| 20 | TEST(GettingStarted, SNIPPET_getting_started_gen) { |
| 21 | //! [ex_getting_started_constructors] |
| 22 | // Arrays may be created using the array constructor and dimensioned |
| 23 | // as 1D, 2D, 3D; however, the values in these arrays will be undefined |
| 24 | array undefined_1D(100); // 1D array with 100 elements |
| 25 | array undefined_2D(10, 100); // 2D array of size 10 x 100 |
| 26 | array undefined_3D(10, 10, 10); // 3D array of size 10 x 10 x 10 |
| 27 | //! [ex_getting_started_constructors] |
| 28 | |
| 29 | //! [ex_getting_started_gen] |
| 30 | // Generate an array of size three filled with zeros. |
| 31 | // If no data type is specified, ArrayFire defaults to f32. |
| 32 | // The constant function generates the data on the device. |
| 33 | array zeros = constant(0, 3); |
| 34 | |
| 35 | // Generate a 1x4 array of uniformly distributed [0,1] random numbers |
| 36 | // The randu function generates the data on the device. |
| 37 | array rand1 = randu(1, 4); |
| 38 | |
| 39 | // Generate a 2x2 array (or matrix, if you prefer) of random numbers |
| 40 | // sampled from a normal distribution. |
| 41 | // The randn function generates data on the device. |
| 42 | array rand2 = randn(2, 2); |
| 43 | |
| 44 | // Generate a 3x3 identity matrix. The data is generated on the device. |
| 45 | array iden = identity(3, 3); |
| 46 | |
| 47 | // Lastly, create a 2x1 array (column vector) of uniformly distributed |
| 48 | // 32-bit complex numbers (c32 data type): |
| 49 | array randcplx = randu(2, 1, c32); |
| 50 | //! [ex_getting_started_gen] |
| 51 | |
| 52 | { |
| 53 | vector<float> output; |
| 54 | output.resize(zeros.elements()); |
| 55 | zeros.host(&output.front()); |
| 56 | ASSERT_EQ(f32, zeros.type()); |
| 57 | for (dim_t i = 0; i < zeros.elements(); i++) |
| 58 | ASSERT_FLOAT_EQ(0, output[i]); |
| 59 | } |
| 60 | |
| 61 | if (!noDoubleTests(f64)) { |
| 62 | array ones = constant(1, 3, 2, f64); |
| 63 | vector<double> output(ones.elements()); |
| 64 | ones.host(&output.front()); |
| 65 | ASSERT_EQ(f64, ones.type()); |
| 66 | for (dim_t i = 0; i < ones.elements(); i++) |
| 67 | ASSERT_FLOAT_EQ(1, output[i]); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | vector<float> output; |
| 72 | output.resize(iden.elements()); |
| 73 | iden.host(&output.front()); |
| 74 | for (dim_t i = 0; i < iden.dims(0); i++) |
| 75 | for (dim_t j = 0; j < iden.dims(1); j++) |
| 76 | if (i == j) |
| 77 | ASSERT_FLOAT_EQ(1, output[i * iden.dims(0) + j]); |
nothing calls this directly
no test coverage detected