In this example, we show how to initialize arrays and assign to them in different ways.
All the following code snippets are part of the same main function:
#include <complex>
#include <iostream>
int main(int argc, char *argv[]) {
}
Includes all relevant headers for the core nda library.
Assigning a scalar to an array
The simplest way to initialize an array is to assign a scalar to it:
A = 0.1 + 0.2i;
std::cout << "A = " << A << std::endl;
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Output:
A =
[[(0.1,0.2),(0.1,0.2)]
[(0.1,0.2),(0.1,0.2)]
[(0.1,0.2),(0.1,0.2)]]
Note that the behavior of the assignment depends on the algebra of the array.
While the scalar is assigned to all elements of an array, for matrices it is only assigned to the elements on the shorter diagonal:
M = 0.1 + 0.2i;
std::cout << "M = " << M << std::endl;
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
Output:
M =
[[(0.1,0.2),(0,0)]
[(0,0),(0.1,0.2)]
[(0,0),(0,0)]]
The scalar type can also be more complex, e.g. another nda::array:
std::cout << "A_arr = " << A_arr << std::endl;
Output:
A_arr = [[1,2,3],[1,2,3],[1,2,3],[1,2,3]]
Copy/Move assignment
The copy and move assignment operations behave as expected:
- copy assignment simply copies the memory handle and layout of an array and
- move assignment moves them:
M_copy = M;
std::cout << "M_copy = " << M_copy << std::endl;
M_move = std::move(M_copy);
std::cout << "M_move = " << M_move << std::endl;
std::cout <<
"M_copy.empty() = " << M_copy.
empty() << std::endl;
bool empty() const
Is the view/array empty?
Output:
M_copy =
[[(0.1,0.2),(0,0)]
[(0,0),(0.1,0.2)]
[(0,0),(0,0)]]
M_move =
[[(0.1,0.2),(0,0)]
[(0,0),(0.1,0.2)]
[(0,0),(0,0)]]
M_copy.empty() = 1
Note: Be careful when reusing an object after it has been moved (see the note at Copy/Move constructors)!
Assigning an nda::Array to an array
To assign an object that satisfies the nda::Array concept to an array is similar to Constructing an array from an nda::Array.
M_f = M + M;
std::cout << "M_f = " << M_f << std::endl;
A2 = M_f;
std::cout << "A2 = " << A2 << std::endl;
Contiguous layout policy with Fortran-order (column-major order).
Output:
M_f =
[[(0.2,0.4),(0,0)]
[(0,0),(0.2,0.4)]
[(0,0),(0,0)]]
A2 =
[[(0.2,0.4),(0,0)]
[(0,0),(0.2,0.4)]
[(0,0),(0,0)]]
Many assignment operations resize the left hand side array if necessary.
In the above example, the first assignment does not need to do a resize because the left hand side array already has the correct shape.
This is not true for the second assignment. A2 has been default constructed and therefore has a size of 0.
Assigning a contiguous range
It is possible to assign an object that satisfies the std::ranges::contiguous_range concept to an 1-dimensional array:
std::vector<long> vec{1, 2, 3, 4, 5};
A_vec = vec;
std::cout << "A_vec = " << A_vec << std::endl;
Output:
As expected, the elements of the range are simply copied into the array.
Initializing an array manually
We can also initialize an array by assigning to each element manually. This can be done in different ways.
For example,
While the traditional for-loops are perhaps the most flexible option, it becomes tedious quite fast with increasing dimensionality.
Initializing an array using automatic assignment
This has already been explained in Initializing with CLEF's automatic assignment.
Let us give another example:
using namespace nda::clef::literals;
std::cout << "C = " << C << std::endl;
constexpr auto j_
Generic placeholder #2.
constexpr auto i_
Generic placeholder #1.
constexpr auto k_
Generic placeholder #3.
constexpr auto l_
Generic placeholder #4.
Output:
C = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
The advantage of CLEF's automatic assignment becomes obvious when we rewrite line 3 from above using traditional for-loops:
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < 2; ++l) {
C(i, j, k, l) = i * 8 + j * 4 + k * 2 + l;
}
}
}
}