0
  [   /ˆl–Ãa¾”êh¥2 ¸&®6â˜´o�þZeÇnRÉÜcüÿÐ„¢m¯Ï_‹I|¥�+ýÿ¹ä³a–ßi~” T¾R( 	Å ýÀã S�ÿÎ ?÷     ///
//  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
//


#ifndef BOOST_UBLAS_TENSOR_FUNCTIONS_HPP
#define BOOST_UBLAS_TENSOR_FUNCTIONS_HPP


#include <stdexcept>
#include <vector>
#include <algorithm>
#include <numeric>


#include "multiplication.hpp"
#include "algorithms.hpp"
#include "expression.hpp"
#include "expression_evaluation.hpp"
#include "storage_traits.hpp"

namespace boost {
namespace numeric {
namespace ublas {

template<class Value, class Format, class Allocator>
class tensor;

template<class Value, class Format, class Allocator>
class matrix;

template<class Value, class Allocator>
class vector;




/** @brief Computes the m-mode tensor-times-vector product
 *
 * Implements C[i1,...,im-1,im+1,...,ip] = A[i1,i2,...,ip] * b[im]
 *
 * @note calls ublas::ttv
 *
 * @param[in] m contraction dimension with 1 <= m <= p
 * @param[in] a tensor object A with order p
 * @param[in] b vector object B
 *
 * @returns tensor object C with order p-1, the same storage format and allocator type as A
*/
template<class V, class F, class A1, class A2>
auto prod(tensor<V,F,A1> const& a, vector<V,A2> const& b, const std::size_t m)
{

	using tensor_type  = tensor<V,F,A1>;
	using extents_type = typename tensor_type::extents_type;
	using ebase_type   = typename extents_type::base_type;
	using value_type   = typename tensor_type::value_type;
	using size_type = typename extents_type::value_type;

	auto const p = std::size_t(a.rank());
	
	if( m == 0)
		throw std::length_error("error in boost::numeric::ublas::prod(ttv): contraction mode must be greater than zero.");

	if( p < m )
		throw std::length_error("error in boost::numeric::ublas::prod(ttv): rank of tensor must be greater than or equal to the modus.");

	if( p == 0)
		throw std::length_error("error in boost::numeric::ublas::prod(ttv): rank of tensor must be greater than zero.");

	if( a.empty() )
		throw std::length_error("error in boost::numeric::ublas::prod(ttv): first argument tensor should not be empty.");

	if( b.size() == 0)
		throw std::length_error("error in boost::numeric::ublas::prod(ttv): second argument vector should not be empty.");


	auto nc = ebase_type(std::max(p-1, size_type(2)) , size_type(1));
	auto nb = ebase_type{b.size(),1};


	for(auto i = 0u, j = 0u; i < p; ++i)
		if(i != m-1)
			nc[j++] = a.extents().at(i);

	auto c = tensor_type(extents_type(nc),value_type{});

	auto bb = &(b(0));

	ttv(m, p,
	    c.data(), c.extents().data(), c.strides().data(),
	    a.data(), a.extents().data(), a.strides().data(),
	    bb, nb.data(), nb.data());


	return c;
}



/** @brief Computes the m-mode tensor-times-matrix product
 *
 * Implements C[i1,...,im-1,j,im+1,...,ip] = A[i1,i2,...,ip] * B[j,im]
 *
 * @note calls ublas::ttm
 *
 * @param[in] a tensor object A with order p
 * @param[in] b vector object B
 * @param[in] m contraction dimension with 1 <= m <= p
 *
 * @returns tensor object C with order p, the same storage format and allocator type as A
*/
template<class V, class F, class A1, class A2>
auto prod(tensor<V,F,A1> const& a, matrix<V,F,A2> const& b, const std::size_t m)
{

	using tensor_type  = tensor<V,F,A1>;
	using extents_type = typename tensor_type::extents_type;
	using strides_type = typename tensor_type::strides_type;
	using value_type   = typename tensor_type::value_type;


	auto const p = a.rank();

	if( m == 0)
		throw std::length_error("error in boost::numeric::ublas::prod(ttm): contraction mode must be greater than zero.");

	if( p < m || m > a.extents().size())
		throw std::length_error("error in boost::numeric::ublas::prod(ttm): rank of the tensor must be greater equal the modus.");

	if( p == 0)
		throw std::length_error("error in boost::numeric::ublas::prod(ttm): rank of th