#ifndef PYTHONIC_INCLUDE_NUMPY_DOT_HPP #define PYTHONIC_INCLUDE_NUMPY_DOT_HPP #include "pythonic/include/types/ndarray.hpp" #include "pythonic/include/numpy/sum.hpp" #include "pythonic/include/types/numpy_expr.hpp" #include "pythonic/include/types/traits.hpp" template struct is_blas_type : pythonic::types::is_complex { }; template <> struct is_blas_type : std::true_type { }; template <> struct is_blas_type : std::true_type { }; template struct is_strided { template static decltype(T::is_strided, std::true_type{}) get(T *); static std::false_type get(...); static constexpr bool value = decltype(get((E *)nullptr))::value; }; template struct is_blas_array { // FIXME: also support gexpr with stride? static constexpr bool value = pythonic::types::is_array::value && is_blas_type>::value && !is_strided::value; }; PYTHONIC_NS_BEGIN namespace numpy { template typename std::enable_if::value && types::is_dtype::value, decltype(std::declval() * std::declval())>::type dot(E const &e, F const &f); /// Vector / Vector multiplication template typename std::enable_if< types::is_numexpr_arg::value && types::is_numexpr_arg::value && E::value == 1 && F::value == 1 && (!is_blas_array::value || !is_blas_array::value || !std::is_same::value), typename __combined::type>::type dot(E const &e, F const &f); template typename std::enable_if::value && std::is_same::value && is_blas_array::value && is_blas_array::value, float>::type dot(E const &e, F const &f); template typename std::enable_if::value && std::is_same::value && is_blas_array::value && is_blas_array::value, double>::type dot(E const &e, F const &f); template typename std::enable_if< E::value == 1 && F::value == 1 && std::is_same>::value && std::is_same>::value && is_blas_array::value && is_blas_array::value, std::complex>::type dot(E const &e, F const &f); template typename std::enable_if< E::value == 1 && F::value == 1 && std::is_same>::value && std::is_same>::value && is_blas_array::value && is_blas_array::value, std::complex>::type dot(E const &e, F const &f); /// Matrix / Vector multiplication // We transpose the matrix to reflect our C order template typename std::enable_if::value && std::tuple_size::value == 2 && std::tuple_size::value == 1, types::ndarray>>::type dot(types::ndarray const &f, types::ndarray const &e); // The trick is to not transpose the matrix so that MV become VM template typename std::enable_if::value && std::tuple_size::value == 1 && std::tuple_size::value == 2, types::ndarray>>::type dot(types::ndarray const &e, types::ndarray const &f); // If arguments could be use with blas, we evaluate them as we need pointer // on array for blas template typename std::enable_if< types::is_numexpr_arg::value && types::is_numexpr_arg::value // It is an array_like && (!(types::is_ndarray::value && types::is_ndarray::value) || !std::is_same::value) && is_blas_type::value && is_blas_type::value // With dtype compatible with // blas && E::value == 2 && F::value == 1, // And it is matrix / vect types::ndarray< typename __combined::type, types::pshape>>::type dot(E const &e, F const &f); // If arguments could be use with blas, we evaluate them as we need pointer // on array for blas template typename std::enable_if< types::is_numexpr_arg::value && types::is_numexpr_arg::value // It is an array_like && (!(types::is_ndarray::value && types::is_ndarray::value) || !std::is_same::value) && is_blas_type::value && is_blas_type::value // With dtype compatible with // blas && E::value == 1 && F::value == 2, // And it is vect / matrix types::ndarray< typename __combined::type, types::pshape>>::type dot(E const &e, F const &f); // If one of the arg doesn't have a "blas compatible type", we use a slow // matrix vector multiplication. template typename std::enable_if< (!is_blas_type::value || !is_blas_type::value) && E::value == 1 && F::value == 2, // And it is vect / matrix types::ndarray< typename __combined::type, types::pshape>>::type dot(E const &e, F const &f); // If one of the arg doesn't have a "blas compatible type", we use a slow // matrix vector multiplication. template typename std::enable_if< (!is_blas_type::value || !is_blas_type::value) && E::value == 2 && F::value == 1, // And it is vect / matrix types::ndarray< typename __combined::type, types::pshape>>::type dot(E const &e, F const &f); /// Matrix / Matrix multiplication // The trick is to use the transpose arguments to reflect C order. // We want to perform A * B in C order but blas order is F order. // So we compute B'A' == (AB)'. As this equality is perform with F order // We doesn't have to return a texpr because we want a C order matrice!! template typename std::enable_if::value && std::tuple_size::value == 2 && std::tuple_size::value == 2, types::ndarray>>::type dot(types::ndarray const &a, types::ndarray const &b); template typename std::enable_if< is_blas_type::value && std::tuple_size::value == 2 && std::tuple_size::value == 2 && std::tuple_size::value == 2, types::ndarray>::type & dot(types::ndarray const &a, types::ndarray const &b, types::ndarray &c); // texpr variants: MT, TM, TT template typename std::enable_if::value && std::tuple_size::value == 2 && std::tuple_size::value == 2, types::ndarray>>::type dot(types::numpy_texpr> const &a, types::ndarray const &b); template typename std::enable_if::value && std::tuple_size::value == 2 && std::tuple_size::value == 2, types::ndarray>>::type dot(types::ndarray const &a, types::numpy_texpr> const &b); template typename std::enable_if::value && std::tuple_size::value == 2 && std::tuple_size::value == 2, types::ndarray>>::type dot(types::numpy_texpr> const &a, types::numpy_texpr> const &b); // If arguments could be use with blas, we evaluate them as we need pointer // on array for blas template typename std::enable_if< types::is_numexpr_arg::value && types::is_numexpr_arg::value // It is an array_like && (!(types::is_ndarray::value && types::is_ndarray::value) || !std::is_same::value) && is_blas_type::value && is_blas_type::value // With dtype compatible with // blas && E::value == 2 && F::value == 2, // And both are matrix types::ndarray< typename __combined::type, types::array>>::type dot(E const &e, F const &f); // If one of the arg doesn't have a "blas compatible type", we use a slow // matrix multiplication. template typename std::enable_if< (!is_blas_type::value || !is_blas_type::value) && E::value == 2 && F::value == 2, // And it is matrix / matrix types::ndarray< typename __combined::type, types::array>>::type dot(E const &e, F const &f); // N x M where N >= 3 and M == 1 template typename std::enable_if< (E::value >= 3 && F::value == 1), types::ndarray< typename __combined::type, types::array>>::type dot(E const &e, F const &f); // N x M where N >= 3 and M >= 2 template typename std::enable_if< (E::value >= 3 && F::value >= 2), types::ndarray< typename __combined::type, types::array>>::type dot(E const &e, F const &f); DEFINE_FUNCTOR(pythonic::numpy, dot); } PYTHONIC_NS_END #endif