Skip to content

Commit

Permalink
FIX: fix the issue #111
Browse files Browse the repository at this point in the history
This bug occured because the type extent_of_rank_one_array_v was
bool instead of std::size_t, which in turn casting the std::size_t
value into bool. If the size of the array was N, it would cast it
into 1.
  • Loading branch information
amitsingh19975 committed Apr 29, 2021
1 parent 9cf2e9b commit eb08923
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
35 changes: 18 additions & 17 deletions examples/tensor/multiply_tensors_product_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ void multiply_tensors_with_static_order()
using value_t = float; // std::complex<double>;
using matrix_t = matrix<value_t,format_t>;
using vector_t = vector<value_t>;
using tensor_t = dynamic_tensor<value_t>;
using tensor2_t = fixed_rank_tensor<value_t,2U>;
using tensor3_t = fixed_rank_tensor<value_t,3U>;
// using tensor4_t = fixed_rank_tensor<value_t,4>;
// using shape_t = typename tensor_t::extents_type;
// using shape2_t = typename tensor2_t::extents_type;
using tensor4_t = fixed_rank_tensor<value_t,4>;
using shape_t = typename tensor_t::extents_type;
using shape2_t = typename tensor2_t::extents_type;
using shape3_t = typename tensor3_t::extents_type;
// using shape4_t = typename tensor4_t::extents_type;
using shape4_t = typename tensor4_t::extents_type;

// Tensor-Vector-Multiplications - Including Transposition
// dynamic_extents with static rank
Expand Down Expand Up @@ -296,41 +297,41 @@ void multiply_tensors_with_static_order()
// dynamic_extents with static rank
{

// using perm_t = std::array<std::size_t,2>;
using perm_t = std::array<std::size_t,2>;

// auto na = shape3_t{3,4,5};
// auto nb = shape4_t{4,6,3,2};
// auto nc = shape2_t{5,5};
// auto A = tensor3_t(na,2.0F);
// auto B = tensor4_t(nb,3.0F);
// auto C = tensor2_t(nc,2.0F);
auto na = shape3_t{3,4,5};
auto nb = shape4_t{4,6,3,2};
auto nc = shape2_t{5,5};
auto A = tensor3_t(na,2.0F);
auto B = tensor4_t(nb,3.0F);
auto C = tensor2_t(nc,2.0F);

// C1(j,l) = T(j,l) + A(i,j,k)*A(i,j,l) + 5;
// Right now there exist no tensor other than dynamic_extents with
// dynamic rank so every tensor times tensor operator automatically
// to dynamic tensor
// auto C1 = C + prod(A,A,perm_t{1,2}) + 5.0F;
auto C1 = C + prod(A,A,perm_t{1,2}) + 5.0F;
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=" << tensor_t(C1) << ";" << std::endl << std::endl;
std::cout << "C1=" << tensor_t(C1) << ";" << std::endl << std::endl;


// C2(k,l,m) = T(k,l,m) + A(i,j,k)*B(j,l,i,m) + 5;
// Similar Problem as above
// tensor_t C2 = tensor_t(shape_t{na[2],nb[1],nb[3]},2.0F) + prod(A,B,perm_t{1,2},perm_t{3,1}) + 5.0F;
tensor_t C2 = tensor3_t(shape3_t{na[2],nb[1],nb[3]},2.0F) + prod(A,B,perm_t{1,2},perm_t{3,1}) + 5.0F;
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;
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;
// Similar Problem as above
// tensor_t C3 = tensor_t(shape_t{na[2],nb[1],nb[3]},2.0F) + prod(A,trans(B,{2,3,1,4}),perm_t{1,2}) + 5.0F;
tensor_t C3 = tensor3_t(shape3_t{na[2],nb[1],nb[3]},2.0F) + prod(A,trans(B,{2,3,1,4}),perm_t{1,2}) + 5.0F;
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;
std::cout << "C3=" << C3 << ";" << std::endl << std::endl;

}
}
Expand Down
6 changes: 4 additions & 2 deletions include/boost/numeric/ublas/tensor/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace boost::numeric::ublas
{};

template<typename T>
inline static constexpr bool extent_of_rank_one_array_v = extent_of_rank_one_array<T>::value;
inline static constexpr std::size_t extent_of_rank_one_array_v = extent_of_rank_one_array<T>::value;

} // namespace detail

Expand Down Expand Up @@ -360,6 +360,9 @@ namespace boost::numeric::ublas
using lextents_type = std::decay_t< decltype(e1) >;
using rextents_type = std::decay_t< decltype(e2) >;
using array_type = std::decay_t< decltype(a1) >;

[[maybe_unused]] extents_size_type const size = ( e1.size() + e2.size() ) - ( a1.size() + a2.size() );

if constexpr(
detail::is_bounded_array_v<array_type> &&
is_static_rank_v<lextents_type> &&
Expand All @@ -371,7 +374,6 @@ namespace boost::numeric::ublas
res.fill(1u);
return res;
}else{
extents_size_type const size = ( e1.size() + e2.size() ) - ( a1.size() + a2.size() );
using extents_base_type = typename extents<>::base_type;
auto arr = extents_base_type( std::max(size, extents_size_type(2)), 1u );
return extents<>(std::move(arr));
Expand Down

0 comments on commit eb08923

Please sign in to comment.