simple_expressions.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/numeric/ublas/matrix.hpp>
  13. #include <boost/numeric/ublas/vector.hpp>
  14. #include <ostream>
  15. int main()
  16. {
  17. using namespace boost::numeric::ublas;
  18. using tensorf = tensor<float>;
  19. using matrixf = matrix<float>;
  20. using vectorf = vector<float>;
  21. auto A = tensorf{3,4,2};
  22. auto B = A = 2;
  23. // Calling overloaded operators
  24. // and using simple tensor expression templates.
  25. if( A != (B+1) )
  26. A += 2*B - 1;
  27. // formatted output
  28. std::cout << "% --------------------------- " << std::endl;
  29. std::cout << "% --------------------------- " << std::endl << std::endl;
  30. std::cout << "A=" << A << ";" << std::endl << std::endl;
  31. auto n = shape{3,4};
  32. auto D = matrixf(n[0],n[1],1);
  33. auto e = vectorf(n[1],1);
  34. auto f = vectorf(n[0],2);
  35. // Calling constructor with
  36. // vector expression templates
  37. tensorf C = 2*f;
  38. // formatted output
  39. std::cout << "% --------------------------- " << std::endl;
  40. std::cout << "% --------------------------- " << std::endl << std::endl;
  41. std::cout << "C=" << C << ";" << std::endl << std::endl;
  42. // Calling overloaded operators
  43. // and mixing simple tensor and matrix expression templates
  44. tensorf F = 3*C + 4*prod(2*D,e);
  45. // formatted output
  46. std::cout << "% --------------------------- " << std::endl;
  47. std::cout << "% --------------------------- " << std::endl << std::endl;
  48. std::cout << "F=" << F << ";" << std::endl << std::endl;
  49. }