einstein_notation.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  20. using tensor_t = tensor<value_t,format_t>;
  21. using matrix_t = matrix<value_t,format_t>;
  22. using namespace boost::numeric::ublas::index;
  23. // Tensor-Vector-Multiplications - Including Transposition
  24. {
  25. auto n = shape{3,4,2};
  26. auto A = tensor_t(n,1);
  27. auto B1 = matrix_t(n[1],n[2],2);
  28. auto v1 = tensor_t(shape{n[0],1},2);
  29. auto v2 = tensor_t(shape{n[1],1},2);
  30. // auto v3 = tensor_t(shape{n[2],1},2);
  31. // C1(j,k) = B1(j,k) + A(i,j,k)*v1(i);
  32. // tensor_t C1 = B1 + prod(A,vector_t(n[0],1),1);
  33. // tensor_t C1 = B1 + A(_i,_,_) * v1(_i,_);
  34. // C2(i,k) = A(i,j,k)*v2(j) + 4;
  35. //tensor_t C2 = prod(A,vector_t(n[1],1),2) + 4;
  36. // tensor_t C2 = A(_,_i,_) * v2(_i,_) + 4;
  37. // not yet implemented!
  38. // C3() = A(i,j,k)*T1(i)*T2(j)*T2(k);
  39. // tensor_t C3 = prod(prod(prod(A,v1,1),v2,1),v3,1);
  40. // tensor_t C3 = A(_i,_j,_k) * v1(_i,_) * v2(_j,_) * v3(_k,_);
  41. // formatted output
  42. std::cout << "% --------------------------- " << std::endl;
  43. std::cout << "% --------------------------- " << std::endl << std::endl;
  44. std::cout << "% C1(j,k) = B1(j,k) + A(i,j,k)*v1(i);" << std::endl << std::endl;
  45. // std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  46. // formatted output
  47. std::cout << "% --------------------------- " << std::endl;
  48. std::cout << "% --------------------------- " << std::endl << std::endl;
  49. std::cout << "% C2(i,k) = A(i,j,k)*v2(j) + 4;" << std::endl << std::endl;
  50. // std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  51. }
  52. // Tensor-Matrix-Multiplications - Including Transposition
  53. {
  54. auto n = shape{3,4,2};
  55. auto m = 5u;
  56. auto A = tensor_t(n,2);
  57. auto B = tensor_t(shape{n[1],n[2],m},2);
  58. auto B1 = tensor_t(shape{m,n[0]},1);
  59. auto B2 = tensor_t(shape{m,n[1]},1);
  60. // C1(l,j,k) = B(j,k,l) + A(i,j,k)*B1(l,i);
  61. // tensor_t C1 = B + prod(A,B1,1);
  62. // tensor_t C1 = B + A(_i,_,_) * B1(_,_i);
  63. // C2(i,l,k) = A(i,j,k)*B2(l,j) + 4;
  64. // tensor_t C2 = prod(A,B2) + 4;
  65. // tensor_t C2 = A(_,_j,_) * B2(_,_j) + 4;
  66. // C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);
  67. // not yet implemented.
  68. // formatted output
  69. std::cout << "% --------------------------- " << std::endl;
  70. std::cout << "% --------------------------- " << std::endl << std::endl;
  71. std::cout << "% C1(l,j,k) = B(j,k,l) + A(i,j,k)*B1(l,i);" << std::endl << std::endl;
  72. // std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  73. // formatted output
  74. std::cout << "% --------------------------- " << std::endl;
  75. std::cout << "% --------------------------- " << std::endl << std::endl;
  76. std::cout << "% C2(i,l,k) = A(i,j,k)*B2(l,j) + 4;" << std::endl << std::endl;
  77. // std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  78. // // formatted output
  79. // std::cout << "% --------------------------- " << std::endl;
  80. // std::cout << "% --------------------------- " << std::endl << std::endl;
  81. // std::cout << "% C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);" << std::endl << std::endl;
  82. // std::cout << "C3=" << C3 << ";" << std::endl << std::endl;
  83. }
  84. // Tensor-Tensor-Multiplications Including Transposition
  85. {
  86. auto na = shape{3,4,5};
  87. auto nb = shape{4,6,3,2};
  88. auto A = tensor_t(na,2);
  89. auto B = tensor_t(nb,3);
  90. auto T1 = tensor_t(shape{na[2],na[2]},2);
  91. auto T2 = tensor_t(shape{na[2],nb[1],nb[3]},2);
  92. // C1(j,l) = T1(j,l) + A(i,j,k)*A(i,j,l) + 5;
  93. // tensor_t C1 = T1 + prod(A,A,perm_t{1,2}) + 5;
  94. // tensor_t C1 = T1 + A(_i,_j,_m)*A(_i,_j,_l) + 5;
  95. // formatted output
  96. std::cout << "% --------------------------- " << std::endl;
  97. std::cout << "% --------------------------- " << std::endl << std::endl;
  98. std::cout << "% C1(k,l) = T1(k,l) + A(i,j,k)*A(i,j,l) + 5;" << std::endl << std::endl;
  99. // std::cout << "C1=" << C1 << ";" << std::endl << std::endl;
  100. // C2(k,l,m) = T2(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;
  101. //tensor_t C2 = T2 + prod(A,B,perm_t{1,2},perm_t{3,1}) + 5;
  102. // tensor_t C2 = T2 + A(_i,_j,_k)*B(_j,_l,_i,_m) + 5;
  103. // formatted output
  104. std::cout << "% --------------------------- " << std::endl;
  105. std::cout << "% --------------------------- " << std::endl << std::endl;
  106. std::cout << "% C2(k,l,m) = T2(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;" << std::endl << std::endl;
  107. // std::cout << "C2=" << C2 << ";" << std::endl << std::endl;
  108. }
  109. }