diff options
Diffstat (limited to 'src/boost/libs/numeric/ublas/examples')
5 files changed, 499 insertions, 0 deletions
diff --git a/src/boost/libs/numeric/ublas/examples/tensor/Jamfile b/src/boost/libs/numeric/ublas/examples/tensor/Jamfile new file mode 100644 index 00000000..42f2fe63 --- /dev/null +++ b/src/boost/libs/numeric/ublas/examples/tensor/Jamfile @@ -0,0 +1,25 @@ +# Boost.uBLAS +# +# Copyright (c) 2018 Cem Bassoy +# +# Use, modification and distribution is subject to the Boost Software License, +# Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + + +# Project settings +project boost-ublas-tensor-example + : requirements + # these tests require C++17 + <cxxstd>11:<build>no + <define>BOOST_UBLAS_NO_EXCEPTIONS + <toolset>vacpp:<define>"BOOST_UBLAS_NO_ELEMENT_PROXIES" + <toolset>gcc:<cxxflags>"-Wall -pedantic -Wextra -std=c++17" + <toolset>gcc:<cxxflags>"-Wno-unknown-pragmas" + <toolset>msvc:<cxxflags>"/W4" # == all + ; + +exe construction_access : construction_access.cpp ; +exe simple_expressions : simple_expressions.cpp ; +exe prod_expressions : prod_expressions.cpp ; +exe einstein_notation : einstein_notation.cpp ;
\ No newline at end of file diff --git a/src/boost/libs/numeric/ublas/examples/tensor/construction_access.cpp b/src/boost/libs/numeric/ublas/examples/tensor/construction_access.cpp new file mode 100644 index 00000000..053690c7 --- /dev/null +++ b/src/boost/libs/numeric/ublas/examples/tensor/construction_access.cpp @@ -0,0 +1,89 @@ +// +// Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// The authors gratefully acknowledge the support of +// Fraunhofer IOSB, Ettlingen, Germany +// + +#include <boost/numeric/ublas/tensor.hpp> +#include <boost/multiprecision/cpp_bin_float.hpp> + +#include <ostream> + +int main() +{ + using namespace boost::numeric::ublas; + using namespace boost::multiprecision; + + + // creates a three-dimensional tensor with extents 3,4 and 2 + // tensor A stores single-precision floating-point number according + // to the first-order storage format + using ftype = float; + auto A = tensor<ftype>{3,4,2}; + + // initializes the tensor with increasing values along the first-index + // using a single index. + auto vf = ftype(0); + for(auto i = 0u; i < A.size(); ++i, vf += ftype(1)) + A[i] = vf; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "A=" << A << ";" << std::endl << std::endl; + + // creates a four-dimensional tensor with extents 5,4,3 and 2 + // tensor A stores complex floating-point extended double precision numbers + // according to the last-order storage format + // and initializes it with the default value. + using ctype = std::complex<cpp_bin_float_double_extended>; + auto B = tensor<ctype,last_order>(shape{5,4,3,2},ctype{}); + + // initializes the tensor with increasing values along the last-index + // using a single-index + auto vc = ctype(0,0); + for(auto i = 0u; i < B.size(); ++i, vc += ctype(1,1)) + B[i] = vc; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "B=" << B << ";" << std::endl << std::endl; + + + + auto C = tensor<ctype,last_order>(B.extents()); + // computes the complex conjugate of elements of B + // using multi-index notation. + for(auto i = 0u; i < B.size(0); ++i) + for(auto j = 0u; j < B.size(1); ++j) + for(auto k = 0u; k < B.size(2); ++k) + for(auto l = 0u; l < B.size(3); ++l) + C.at(i,j,k,l) = std::conj(B.at(i,j,k,l)); + + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "C=" << C << ";" << std::endl << std::endl; + + + // computes the complex conjugate of elements of B + // using iterators. + auto D = tensor<ctype,last_order>(B.extents()); + std::transform(B.begin(), B.end(), D.begin(), [](auto const& b){ return std::conj(b); }); + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "D=" << D << ";" << std::endl << std::endl; + + // reshaping tensors. + auto new_extents = B.extents().base(); + std::next_permutation( new_extents.begin(), new_extents.end() ); + D.reshape( shape(new_extents) ); + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "newD=" << D << ";" << std::endl << std::endl; +} diff --git a/src/boost/libs/numeric/ublas/examples/tensor/einstein_notation.cpp b/src/boost/libs/numeric/ublas/examples/tensor/einstein_notation.cpp new file mode 100644 index 00000000..1d95fc06 --- /dev/null +++ b/src/boost/libs/numeric/ublas/examples/tensor/einstein_notation.cpp @@ -0,0 +1,139 @@ +// +// Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// The authors gratefully acknowledge the support of +// Fraunhofer IOSB, Ettlingen, Germany +// + + +#include <boost/numeric/ublas/tensor.hpp> +#include <boost/numeric/ublas/matrix.hpp> +#include <boost/numeric/ublas/vector.hpp> +#include <iostream> + +int main() +{ + using namespace boost::numeric::ublas; + + using format_t = column_major; + using value_t = float; + using tensor_t = tensor<value_t,format_t>; + using matrix_t = matrix<value_t,format_t>; + using namespace boost::numeric::ublas::index; + + // Tensor-Vector-Multiplications - Including Transposition + { + + auto n = shape{3,4,2}; + auto A = tensor_t(n,1); + auto B1 = matrix_t(n[1],n[2],2); + auto v1 = tensor_t(shape{n[0],1},2); + auto v2 = tensor_t(shape{n[1],1},2); +// auto v3 = tensor_t(shape{n[2],1},2); + + // C1(j,k) = B1(j,k) + A(i,j,k)*v1(i); + // tensor_t C1 = B1 + prod(A,vector_t(n[0],1),1); +// tensor_t C1 = B1 + A(_i,_,_) * v1(_i,_); + + // C2(i,k) = A(i,j,k)*v2(j) + 4; + //tensor_t C2 = prod(A,vector_t(n[1],1),2) + 4; +// tensor_t C2 = A(_,_i,_) * v2(_i,_) + 4; + + // not yet implemented! + // C3() = A(i,j,k)*T1(i)*T2(j)*T2(k); + // tensor_t C3 = prod(prod(prod(A,v1,1),v2,1),v3,1); + // tensor_t C3 = A(_i,_j,_k) * v1(_i,_) * v2(_j,_) * v3(_k,_); + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(j,k) = B1(j,k) + A(i,j,k)*v1(i);" << std::endl << std::endl; +// std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(i,k) = A(i,j,k)*v2(j) + 4;" << std::endl << std::endl; +// std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + + } + + + // Tensor-Matrix-Multiplications - Including Transposition + { + auto n = shape{3,4,2}; + auto m = 5u; + auto A = tensor_t(n,2); + auto B = tensor_t(shape{n[1],n[2],m},2); + auto B1 = tensor_t(shape{m,n[0]},1); + auto B2 = tensor_t(shape{m,n[1]},1); + + + // C1(l,j,k) = B(j,k,l) + A(i,j,k)*B1(l,i); + // tensor_t C1 = B + prod(A,B1,1); +// tensor_t C1 = B + A(_i,_,_) * B1(_,_i); + + // C2(i,l,k) = A(i,j,k)*B2(l,j) + 4; + // tensor_t C2 = prod(A,B2) + 4; +// tensor_t C2 = A(_,_j,_) * B2(_,_j) + 4; + + // C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k); + // not yet implemented. + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(l,j,k) = B(j,k,l) + A(i,j,k)*B1(l,i);" << std::endl << std::endl; +// std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(i,l,k) = A(i,j,k)*B2(l,j) + 4;" << std::endl << std::endl; +// std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + +// // formatted output +// std::cout << "% --------------------------- " << std::endl; +// std::cout << "% --------------------------- " << std::endl << std::endl; +// std::cout << "% C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);" << std::endl << std::endl; +// std::cout << "C3=" << C3 << ";" << std::endl << std::endl; + } + + + // Tensor-Tensor-Multiplications Including Transposition + { + auto na = shape{3,4,5}; + auto nb = shape{4,6,3,2}; + auto A = tensor_t(na,2); + auto B = tensor_t(nb,3); + auto T1 = tensor_t(shape{na[2],na[2]},2); + auto T2 = tensor_t(shape{na[2],nb[1],nb[3]},2); + + + // C1(j,l) = T1(j,l) + A(i,j,k)*A(i,j,l) + 5; + // tensor_t C1 = T1 + prod(A,A,perm_t{1,2}) + 5; +// tensor_t C1 = T1 + A(_i,_j,_m)*A(_i,_j,_l) + 5; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(k,l) = T1(k,l) + A(i,j,k)*A(i,j,l) + 5;" << std::endl << std::endl; +// std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + + // C2(k,l,m) = T2(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5; + //tensor_t C2 = T2 + prod(A,B,perm_t{1,2},perm_t{3,1}) + 5; +// tensor_t C2 = T2 + A(_i,_j,_k)*B(_j,_l,_i,_m) + 5; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(k,l,m) = T2(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;" << std::endl << std::endl; +// std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + + } +} diff --git a/src/boost/libs/numeric/ublas/examples/tensor/prod_expressions.cpp b/src/boost/libs/numeric/ublas/examples/tensor/prod_expressions.cpp new file mode 100644 index 00000000..6ff72521 --- /dev/null +++ b/src/boost/libs/numeric/ublas/examples/tensor/prod_expressions.cpp @@ -0,0 +1,183 @@ +// +// Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// The authors gratefully acknowledge the support of +// Fraunhofer IOSB, Ettlingen, Germany +// + + +#include <boost/numeric/ublas/tensor.hpp> +#include <boost/numeric/ublas/matrix.hpp> +#include <boost/numeric/ublas/vector.hpp> +#include <iostream> + +int main() +{ + using namespace boost::numeric::ublas; + + using format_t = column_major; + using value_t = float; // std::complex<double>; + using tensor_t = tensor<value_t,format_t>; + using matrix_t = matrix<value_t,format_t>; + using vector_t = vector<value_t>; + + // Tensor-Vector-Multiplications - Including Transposition + { + + auto n = shape{3,4,2}; + auto A = tensor_t(n,2); + auto q = 0u; // contraction mode + + // C1(j,k) = T2(j,k) + A(i,j,k)*T1(i); + q = 1u; + tensor_t C1 = matrix_t(n[1],n[2],2) + prod(A,vector_t(n[q-1],1),q); + + // C2(i,k) = A(i,j,k)*T1(j) + 4; + q = 2u; + tensor_t C2 = prod(A,vector_t(n[q-1],1),q) + 4; + + // C3() = A(i,j,k)*T1(i)*T2(j)*T2(k); + 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); + + // C4(i,j) = A(k,i,j)*T1(k) + 4; + q = 1u; + tensor_t C4 = prod(trans(A,{2,3,1}),vector_t(n[2],1),q) + 4; + + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(j,k) = T2(j,k) + A(i,j,k)*T1(i);" << std::endl << std::endl; + std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(i,k) = A(i,j,k)*T1(j) + 4;" << std::endl << std::endl; + std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C3() = A(i,j,k)*T1(i)*T2(j)*T2(k);" << std::endl << std::endl; + std::cout << "C3()=" << C3(0) << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C4(i,j) = A(k,i,j)*T1(k) + 4;" << std::endl << std::endl; + std::cout << "C4=" << C4 << ";" << std::endl << std::endl; + + } + + + // Tensor-Matrix-Multiplications - Including Transposition + { + + auto n = shape{3,4,2}; + auto A = tensor_t(n,2); + auto m = 5u; + auto q = 0u; // contraction mode + + // C1(l,j,k) = T2(l,j,k) + A(i,j,k)*T1(l,i); + q = 1u; + tensor_t C1 = tensor_t(shape{m,n[1],n[2]},2) + prod(A,matrix_t(m,n[q-1],1),q); + + // C2(i,l,k) = A(i,j,k)*T1(l,j) + 4; + q = 2u; + tensor_t C2 = prod(A,matrix_t(m,n[q-1],1),q) + 4; + + // C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k); + q = 3u; + 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); + + // C4(i,l1,l2) = A(i,j,k)*T2(l2,k)*T1(l1,j); + 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); + + // C5(i,k,l) = A(i,k,j)*T1(l,j) + 4; + q = 3u; + tensor_t C5 = prod(trans(A,{1,3,2}),matrix_t(m,n[1],1),q) + 4; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(l,j,k) = T2(l,j,k) + A(i,j,k)*T1(l,i);" << std::endl << std::endl; + std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(i,l,k) = A(i,j,k)*T1(l,j) + 4;" << std::endl << std::endl; + std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C3(i,l1,l2) = A(i,j,k)*T1(l1,j)*T2(l2,k);" << std::endl << std::endl; + std::cout << "C3=" << C3 << ";" << std::endl << std::endl; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C4(i,l1,l2) = A(i,j,k)*T2(l2,k)*T1(l1,j);" << std::endl << std::endl; + std::cout << "C4=" << C4 << ";" << std::endl << std::endl; + std::cout << "% C3 and C4 should have the same values, true? " << std::boolalpha << (C3 == C4) << "!" << std::endl; + + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C5(i,k,l) = A(i,k,j)*T1(l,j) + 4;" << std::endl << std::endl; + std::cout << "C5=" << C5 << ";" << std::endl << std::endl; + } + + + + + + // Tensor-Tensor-Multiplications Including Transposition + { + + using perm_t = std::vector<std::size_t>; + + auto na = shape{3,4,5}; + auto nb = shape{4,6,3,2}; + auto A = tensor_t(na,2); + auto B = tensor_t(nb,3); + + + // C1(j,l) = T(j,l) + A(i,j,k)*A(i,j,l) + 5; + tensor_t C1 = tensor_t(shape{na[2],na[2]},2) + prod(A,A,perm_t{1,2}) + 5; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C1(k,l) = T(k,l) + A(i,j,k)*A(i,j,l) + 5;" << std::endl << std::endl; + std::cout << "C1=" << C1 << ";" << std::endl << std::endl; + + + // C2(k,l,m) = T(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5; + 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; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "% C2(k,l,m) = T(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;" << std::endl << std::endl; + std::cout << "C2=" << C2 << ";" << std::endl << std::endl; + + + // C3(k,l,m) = T(k,l,m) + A(i,j,k)*trans(B(j,l,i,m),{2,3,1,4})+ 5; + 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; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + 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; + std::cout << "C3=" << C3 << ";" << std::endl << std::endl; + + } +} diff --git a/src/boost/libs/numeric/ublas/examples/tensor/simple_expressions.cpp b/src/boost/libs/numeric/ublas/examples/tensor/simple_expressions.cpp new file mode 100644 index 00000000..fabb00f4 --- /dev/null +++ b/src/boost/libs/numeric/ublas/examples/tensor/simple_expressions.cpp @@ -0,0 +1,63 @@ +// +// Copyright (c) 2018-2019, Cem Bassoy, cem.bassoy@gmail.com +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// The authors gratefully acknowledge the support of +// Fraunhofer IOSB, Ettlingen, Germany +// + + +#include <boost/numeric/ublas/tensor.hpp> +#include <boost/numeric/ublas/matrix.hpp> +#include <boost/numeric/ublas/vector.hpp> +#include <ostream> + +int main() +{ + using namespace boost::numeric::ublas; + + using tensorf = tensor<float>; + using matrixf = matrix<float>; + using vectorf = vector<float>; + + auto A = tensorf{3,4,2}; + auto B = A = 2; + + // Calling overloaded operators + // and using simple tensor expression templates. + if( A != (B+1) ) + A += 2*B - 1; + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "A=" << A << ";" << std::endl << std::endl; + + auto n = shape{3,4}; + auto D = matrixf(n[0],n[1],1); + auto e = vectorf(n[1],1); + auto f = vectorf(n[0],2); + + // Calling constructor with + // vector expression templates + tensorf C = 2*f; + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "C=" << C << ";" << std::endl << std::endl; + + + // Calling overloaded operators + // and mixing simple tensor and matrix expression templates + tensorf F = 3*C + 4*prod(2*D,e); + + // formatted output + std::cout << "% --------------------------- " << std::endl; + std::cout << "% --------------------------- " << std::endl << std::endl; + std::cout << "F=" << F << ";" << std::endl << std::endl; + + +} |