prod_expressions.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <iostream>
  15. int main()
  16. {
  17. using namespace boost::numeric::ublas;
  18. using format_t = column_major;
  19. using value_t = float; // std::complex<double>;
  20. using tensor_t = tensor<value_t,format_t>;
  21. using matrix_t = matrix<value_t,format_t>;
  22. using vector_t = vector<value_t>;
  23. // Tensor-Vector-Multiplications - Including Transposition
  24. {
  25. auto n = shape{3,4,2};
  26. auto A = tensor_t(n,2);
  27. auto q = 0u; // contraction mode
  28. // C1(j,k) = T2(j,k) + A(i,j,k)*T1(i);
  29. q = 1u;
  30. tensor_t C1 = matrix_t(n[1],n[2],2) + prod(A,vector_t(n[q-1],1),q);
  31. // C2(i,k) = A(i,j,k)*T1(j) + 4;
  32. q = 2u;
  33. tensor_t C2 = prod(A,vector_t(n[q-1],1),q) + 4;
  34. // C3() = A(i,j,k)*T1(i)*T2(j)*T2(k);
  35. tensor_t C3 = prod(prod(prod(A,vector_t(n[0],1),1),vector_t(n[1],1),1),vector_t(n[2],1),1);
  36. // C4(i,j) = A(k,i,j)*T1(k) + 4;
  37. q = 1u;
  38. tensor_t C4 = prod(trans(A,{2,3,1}),vector_t(n[2],1),q) + 4;
  39. // formatted output
  40. std::cout << "% --------------------------- " << std::endl;
  41. std::cout << "% --------------------------- " << std::endl << std::endl;
  42. std::cout << "% C1(j,k) = T2(j,k) + A(i,j,k)*T1(i);" << std::endl << std::endl;
  43. std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  44. // formatted output
  45. std::cout << "% --------------------------- " << std::endl;
  46. std::cout << "% --------------------------- " << std::endl << std::endl;
  47. std::cout << "% C2(i,k) = A(i,j,k)*T1(j) + 4;" << std::endl << std::endl;
  48. std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  49. // formatted output
  50. std::cout << "% --------------------------- " << std::endl;
  51. std::cout << "% --------------------------- " << std::endl << std::endl;
  52. std::cout << "% C3() = A(i,j,k)*T1(i)*T2(j)*T2(k);" << std::endl << std::endl;
  53. std::cout << "C3()=" << C3(0) << ";" << std::endl << std::endl;
  54. // formatted output
  55. std::cout << "% --------------------------- " << std::endl;
  56. std::cout << "% --------------------------- " << std::endl << std::endl;
  57. std::cout << "% C4(i,j) = A(k,i,j)*T1(k) + 4;" << std::endl << std::endl;
  58. std::cout << "C4=" << C4 << ";" << std::endl << std::endl;
  59. }
  60. // Tensor-Matrix-Multiplications - Including Transposition
  61. {
  62. auto n = shape{3,4,2};
  63. auto A = tensor_t(n,2);
  64. auto m = 5u;
  65. auto q = 0u; // contraction mode
  66. // C1(l,j,k) = T2(l,j,k) + A(i,j,k)*T1(l,i);
  67. q = 1u;
  68. tensor_t C1 = tensor_t(shape{m,n[1],n[2]},2) + prod(A,matrix_t(m,n[q-1],1),q);
  69. // C2(i,l,k) = A(i,j,k)*T1(l,j) + 4;
  70. q = 2u;
  71. tensor_t C2 = prod(A,matrix_t(m,n[q-1],1),q) + 4;
  72. // C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);
  73. q = 3u;
  74. tensor_t C3 = prod(prod(A,matrix_t(m+1,n[q-2],1),q-1),matrix_t(m+2,n[q-1],1),q);
  75. // C4(i,l1,l2) = A(i,j,k)*T2(l2,k)*T1(l1,j);
  76. tensor_t C4 = prod(prod(A,matrix_t(m+2,n[q-1],1),q),matrix_t(m+1,n[q-2],1),q-1);
  77. // C5(i,k,l) = A(i,k,j)*T1(l,j) + 4;
  78. q = 3u;
  79. tensor_t C5 = prod(trans(A,{1,3,2}),matrix_t(m,n[1],1),q) + 4;
  80. // formatted output
  81. std::cout << "% --------------------------- " << std::endl;
  82. std::cout << "% --------------------------- " << std::endl << std::endl;
  83. std::cout << "% C1(l,j,k) = T2(l,j,k) + A(i,j,k)*T1(l,i);" << std::endl << std::endl;
  84. std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  85. // formatted output
  86. std::cout << "% --------------------------- " << std::endl;
  87. std::cout << "% --------------------------- " << std::endl << std::endl;
  88. std::cout << "% C2(i,l,k) = A(i,j,k)*T1(l,j) + 4;" << std::endl << std::endl;
  89. std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  90. // formatted output
  91. std::cout << "% --------------------------- " << std::endl;
  92. std::cout << "% --------------------------- " << std::endl << std::endl;
  93. std::cout << "% C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);" << std::endl << std::endl;
  94. std::cout << "C3=" << C3 << ";" << std::endl << std::endl;
  95. // formatted output
  96. std::cout << "% --------------------------- " << std::endl;
  97. std::cout << "% --------------------------- " << std::endl << std::endl;
  98. std::cout << "% C4(i,l1,l2) = A(i,j,k)*T2(l2,k)*T1(l1,j);" << std::endl << std::endl;
  99. std::cout << "C4=" << C4 << ";" << std::endl << std::endl;
  100. std::cout << "% C3 and C4 should have the same values, true? " << std::boolalpha << (C3 == C4) << "!" << std::endl;
  101. // formatted output
  102. std::cout << "% --------------------------- " << std::endl;
  103. std::cout << "% --------------------------- " << std::endl << std::endl;
  104. std::cout << "% C5(i,k,l) = A(i,k,j)*T1(l,j) + 4;" << std::endl << std::endl;
  105. std::cout << "C5=" << C5 << ";" << std::endl << std::endl;
  106. }
  107. // Tensor-Tensor-Multiplications Including Transposition
  108. {
  109. using perm_t = std::vector<std::size_t>;
  110. auto na = shape{3,4,5};
  111. auto nb = shape{4,6,3,2};
  112. auto A = tensor_t(na,2);
  113. auto B = tensor_t(nb,3);
  114. // C1(j,l) = T(j,l) + A(i,j,k)*A(i,j,l) + 5;
  115. tensor_t C1 = tensor_t(shape{na[2],na[2]},2) + prod(A,A,perm_t{1,2}) + 5;
  116. // formatted output
  117. std::cout << "% --------------------------- " << std::endl;
  118. std::cout << "% --------------------------- " << std::endl << std::endl;
  119. std::cout << "% C1(k,l) = T(k,l) + A(i,j,k)*A(i,j,l) + 5;" << std::endl << std::endl;
  120. std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  121. // C2(k,l,m) = T(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;
  122. tensor_t C2 = tensor_t(shape{na[2],nb[1],nb[3]},2) + prod(A,B,perm_t{1,2},perm_t{3,1}) + 5;
  123. // formatted output
  124. std::cout << "% --------------------------- " << std::endl;
  125. std::cout << "% --------------------------- " << std::endl << std::endl;
  126. std::cout << "% C2(k,l,m) = T(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;" << std::endl << std::endl;
  127. std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  128. // C3(k,l,m) = T(k,l,m) + A(i,j,k)*trans(B(j,l,i,m),{2,3,1,4})+ 5;
  129. tensor_t C3 = tensor_t(shape{na[2],nb[1],nb[3]},2) + prod(A,trans(B,{2,3,1,4}),perm_t{1,2}) + 5;
  130. // formatted output
  131. std::cout << "% --------------------------- " << std::endl;
  132. std::cout << "% --------------------------- " << std::endl << std::endl;
  133. std::cout << "% C3(k,l,m) = T(k,l,m) + A(i,j,k)*trans(B(j,l,i,m),{2,3,1,4})+ 5;" << std::endl << std::endl;
  134. std::cout << "C3=" << C3 << ";" << std::endl << std::endl;
  135. }
  136. }