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