1 //
2 // Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // The authors gratefully acknowledge the support of
9 // Fraunhofer IOSB, Ettlingen, Germany
10 //
11
12 #include <boost/numeric/ublas/tensor.hpp>
13 #include <boost/multiprecision/cpp_bin_float.hpp>
14
15 #include <ostream>
16
main()17 int main()
18 {
19 using namespace boost::numeric::ublas;
20 using namespace boost::multiprecision;
21
22
23 // creates a three-dimensional tensor with extents 3,4 and 2
24 // tensor A stores single-precision floating-point number according
25 // to the first-order storage format
26 using ftype = float;
27 auto A = tensor<ftype>{3,4,2};
28
29 // initializes the tensor with increasing values along the first-index
30 // using a single index.
31 auto vf = ftype(0);
32 for(auto i = 0u; i < A.size(); ++i, vf += ftype(1))
33 A[i] = vf;
34
35 // formatted output
36 std::cout << "% --------------------------- " << std::endl;
37 std::cout << "% --------------------------- " << std::endl << std::endl;
38 std::cout << "A=" << A << ";" << std::endl << std::endl;
39
40 // creates a four-dimensional tensor with extents 5,4,3 and 2
41 // tensor A stores complex floating-point extended double precision numbers
42 // according to the last-order storage format
43 // and initializes it with the default value.
44 using ctype = std::complex<cpp_bin_float_double_extended>;
45 auto B = tensor<ctype,last_order>(shape{5,4,3,2},ctype{});
46
47 // initializes the tensor with increasing values along the last-index
48 // using a single-index
49 auto vc = ctype(0,0);
50 for(auto i = 0u; i < B.size(); ++i, vc += ctype(1,1))
51 B[i] = vc;
52
53 // formatted output
54 std::cout << "% --------------------------- " << std::endl;
55 std::cout << "% --------------------------- " << std::endl << std::endl;
56 std::cout << "B=" << B << ";" << std::endl << std::endl;
57
58
59
60 auto C = tensor<ctype,last_order>(B.extents());
61 // computes the complex conjugate of elements of B
62 // using multi-index notation.
63 for(auto i = 0u; i < B.size(0); ++i)
64 for(auto j = 0u; j < B.size(1); ++j)
65 for(auto k = 0u; k < B.size(2); ++k)
66 for(auto l = 0u; l < B.size(3); ++l)
67 C.at(i,j,k,l) = std::conj(B.at(i,j,k,l));
68
69 std::cout << "% --------------------------- " << std::endl;
70 std::cout << "% --------------------------- " << std::endl << std::endl;
71 std::cout << "C=" << C << ";" << std::endl << std::endl;
72
73
74 // computes the complex conjugate of elements of B
75 // using iterators.
76 auto D = tensor<ctype,last_order>(B.extents());
77 std::transform(B.begin(), B.end(), D.begin(), [](auto const& b){ return std::conj(b); });
78 std::cout << "% --------------------------- " << std::endl;
79 std::cout << "% --------------------------- " << std::endl << std::endl;
80 std::cout << "D=" << D << ";" << std::endl << std::endl;
81
82 // reshaping tensors.
83 auto new_extents = B.extents().base();
84 std::next_permutation( new_extents.begin(), new_extents.end() );
85 D.reshape( shape(new_extents) );
86 std::cout << "% --------------------------- " << std::endl;
87 std::cout << "% --------------------------- " << std::endl << std::endl;
88 std::cout << "newD=" << D << ";" << std::endl << std::endl;
89 }
90