123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- #ifndef _BOOST_UBLAS_TENSOR_ALGORITHMS_HPP
- #define _BOOST_UBLAS_TENSOR_ALGORITHMS_HPP
- #include <stdexcept>
- #include <complex>
- #include <functional>
- namespace boost {
- namespace numeric {
- namespace ublas {
- template <class PointerOut, class PointerIn, class SizeType>
- void copy(const SizeType p, SizeType const*const n,
- PointerOut c, SizeType const*const wc,
- PointerIn a, SizeType const*const wa)
- {
- static_assert( std::is_pointer<PointerOut>::value & std::is_pointer<PointerIn>::value,
- "Static error in boost::numeric::ublas::copy: Argument types for pointers are not pointer types.");
- if( p == 0 )
- return;
- if(c == nullptr || a == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::copy: Pointers shall not be null pointers.");
- if(wc == nullptr || wa == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::copy: Pointers shall not be null pointers.");
- if(n == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::copy: Pointers shall not be null pointers.");
- std::function<void(SizeType r, PointerOut c, PointerIn a)> lambda;
- lambda = [&lambda, n, wc, wa](SizeType r, PointerOut c, PointerIn a)
- {
- if(r > 0)
- for(auto d = 0u; d < n[r]; c += wc[r], a += wa[r], ++d)
- lambda(r-1, c, a );
- else
- for(auto d = 0u; d < n[0]; c += wc[0], a += wa[0], ++d)
- *c = *a;
- };
- lambda( p-1, c, a );
- }
- template <class PointerOut, class PointerIn, class SizeType, class UnaryOp>
- void transform(const SizeType p,
- SizeType const*const n,
- PointerOut c, SizeType const*const wc,
- PointerIn a, SizeType const*const wa,
- UnaryOp op)
- {
- static_assert( std::is_pointer<PointerOut>::value & std::is_pointer<PointerIn>::value,
- "Static error in boost::numeric::ublas::transform: Argument types for pointers are not pointer types.");
- if( p == 0 )
- return;
- if(c == nullptr || a == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(wc == nullptr || wa == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(n == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- std::function<void(SizeType r, PointerOut c, PointerIn a)> lambda;
- lambda = [&lambda, n, wc, wa, op](SizeType r, PointerOut c, PointerIn a)
- {
- if(r > 0)
- for(auto d = 0u; d < n[r]; c += wc[r], a += wa[r], ++d)
- lambda(r-1, c, a);
- else
- for(auto d = 0u; d < n[0]; c += wc[0], a += wa[0], ++d)
- *c = op(*a);
- };
- lambda( p-1, c, a );
- }
- template <class PointerIn, class ValueType, class SizeType>
- ValueType accumulate(SizeType const p, SizeType const*const n,
- PointerIn a, SizeType const*const w,
- ValueType k)
- {
- static_assert(std::is_pointer<PointerIn>::value,
- "Static error in boost::numeric::ublas::transform: Argument types for pointers are not pointer types.");
- if( p == 0 )
- return k;
- if(a == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(w == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(n == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- std::function<ValueType(SizeType r, PointerIn a, ValueType k)> lambda;
- lambda = [&lambda, n, w](SizeType r, PointerIn a, ValueType k)
- {
- if(r > 0u)
- for(auto d = 0u; d < n[r]; a += w[r], ++d)
- k = lambda(r-1, a, k);
- else
- for(auto d = 0u; d < n[0]; a += w[0], ++d)
- k += *a;
- return k;
- };
- return lambda( p-1, a, k );
- }
- template <class PointerIn, class ValueType, class SizeType, class BinaryOp>
- ValueType accumulate(SizeType const p, SizeType const*const n,
- PointerIn a, SizeType const*const w,
- ValueType k, BinaryOp op)
- {
- static_assert(std::is_pointer<PointerIn>::value,
- "Static error in boost::numeric::ublas::transform: Argument types for pointers are not pointer types.");
- if( p == 0 )
- return k;
- if(a == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(w == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- if(n == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::transform: Pointers shall not be null pointers.");
- std::function<ValueType(SizeType r, PointerIn a, ValueType k)> lambda;
- lambda = [&lambda, n, w, op](SizeType r, PointerIn a, ValueType k)
- {
- if(r > 0u)
- for(auto d = 0u; d < n[r]; a += w[r], ++d)
- k = lambda(r-1, a, k);
- else
- for(auto d = 0u; d < n[0]; a += w[0], ++d)
- k = op ( k, *a );
- return k;
- };
- return lambda( p-1, a, k );
- }
- template <class PointerOut, class PointerIn, class SizeType>
- void trans( SizeType const p, SizeType const*const na, SizeType const*const pi,
- PointerOut c, SizeType const*const wc,
- PointerIn a, SizeType const*const wa)
- {
- static_assert( std::is_pointer<PointerOut>::value & std::is_pointer<PointerIn>::value,
- "Static error in boost::numeric::ublas::trans: Argument types for pointers are not pointer types.");
- if( p < 2)
- return;
- if(c == nullptr || a == nullptr)
- throw std::runtime_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(na == nullptr)
- throw std::runtime_error("Error in boost::numeric::ublas::trans: Pointers shall not be null.");
- if(wc == nullptr || wa == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(na == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(pi == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- std::function<void(SizeType r, PointerOut c, PointerIn a)> lambda;
- lambda = [&lambda, na, wc, wa, pi](SizeType r, PointerOut c, PointerIn a)
- {
- if(r > 0)
- for(auto d = 0u; d < na[r]; c += wc[pi[r]-1], a += wa[r], ++d)
- lambda(r-1, c, a);
- else
- for(auto d = 0u; d < na[0]; c += wc[pi[0]-1], a += wa[0], ++d)
- *c = *a;
- };
- lambda( p-1, c, a );
- }
- template <class ValueType, class SizeType>
- void trans( SizeType const p,
- SizeType const*const na,
- SizeType const*const pi,
- std::complex<ValueType>* c, SizeType const*const wc,
- std::complex<ValueType>* a, SizeType const*const wa)
- {
- if( p < 2)
- return;
- if(c == nullptr || a == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(wc == nullptr || wa == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(na == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- if(pi == nullptr)
- throw std::length_error("Error in boost::numeric::ublas::trans: Pointers shall not be null pointers.");
- std::function<void(SizeType r, std::complex<ValueType>* c, std::complex<ValueType>* a)> lambda;
- lambda = [&lambda, na, wc, wa, pi](SizeType r, std::complex<ValueType>* c, std::complex<ValueType>* a)
- {
- if(r > 0)
- for(auto d = 0u; d < na[r]; c += wc[pi[r]-1], a += wa[r], ++d)
- lambda(r-1, c, a);
- else
- for(auto d = 0u; d < na[0]; c += wc[pi[0]-1], a += wa[0], ++d)
- *c = std::conj(*a);
- };
- lambda( p-1, c, a );
- }
- }
- }
- }
- #endif
|