construction_access.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <boost/numeric/ublas/tensor.hpp>
  12. #include <boost/multiprecision/cpp_bin_float.hpp>
  13. #include <ostream>
  14. int main()
  15. {
  16. using namespace boost::numeric::ublas;
  17. using namespace boost::multiprecision;
  18. // creates a three-dimensional tensor with extents 3,4 and 2
  19. // tensor A stores single-precision floating-point number according
  20. // to the first-order storage format
  21. using ftype = float;
  22. auto A = tensor<ftype>{3,4,2};
  23. // initializes the tensor with increasing values along the first-index
  24. // using a single index.
  25. auto vf = ftype(0);
  26. for(auto i = 0u; i < A.size(); ++i, vf += ftype(1))
  27. A[i] = vf;
  28. // formatted output
  29. std::cout << "% --------------------------- " << std::endl;
  30. std::cout << "% --------------------------- " << std::endl << std::endl;
  31. std::cout << "A=" << A << ";" << std::endl << std::endl;
  32. // creates a four-dimensional tensor with extents 5,4,3 and 2
  33. // tensor A stores complex floating-point extended double precision numbers
  34. // according to the last-order storage format
  35. // and initializes it with the default value.
  36. using ctype = std::complex<cpp_bin_float_double_extended>;
  37. auto B = tensor<ctype,last_order>(shape{5,4,3,2},ctype{});
  38. // initializes the tensor with increasing values along the last-index
  39. // using a single-index
  40. auto vc = ctype(0,0);
  41. for(auto i = 0u; i < B.size(); ++i, vc += ctype(1,1))
  42. B[i] = vc;
  43. // formatted output
  44. std::cout << "% --------------------------- " << std::endl;
  45. std::cout << "% --------------------------- " << std::endl << std::endl;
  46. std::cout << "B=" << B << ";" << std::endl << std::endl;
  47. auto C = tensor<ctype,last_order>(B.extents());
  48. // computes the complex conjugate of elements of B
  49. // using multi-index notation.
  50. for(auto i = 0u; i < B.size(0); ++i)
  51. for(auto j = 0u; j < B.size(1); ++j)
  52. for(auto k = 0u; k < B.size(2); ++k)
  53. for(auto l = 0u; l < B.size(3); ++l)
  54. C.at(i,j,k,l) = std::conj(B.at(i,j,k,l));
  55. std::cout << "% --------------------------- " << std::endl;
  56. std::cout << "% --------------------------- " << std::endl << std::endl;
  57. std::cout << "C=" << C << ";" << std::endl << std::endl;
  58. // computes the complex conjugate of elements of B
  59. // using iterators.
  60. auto D = tensor<ctype,last_order>(B.extents());
  61. std::transform(B.begin(), B.end(), D.begin(), [](auto const& b){ return std::conj(b); });
  62. std::cout << "% --------------------------- " << std::endl;
  63. std::cout << "% --------------------------- " << std::endl << std::endl;
  64. std::cout << "D=" << D << ";" << std::endl << std::endl;
  65. // reshaping tensors.
  66. auto new_extents = B.extents().base();
  67. std::next_permutation( new_extents.begin(), new_extents.end() );
  68. D.reshape( shape(new_extents) );
  69. std::cout << "% --------------------------- " << std::endl;
  70. std::cout << "% --------------------------- " << std::endl << std::endl;
  71. std::cout << "newD=" << D << ";" << std::endl << std::endl;
  72. }