#ifndef STAN_MATH_PRIM_FUN_PROMOTE_ELEMENTS_HPP #define STAN_MATH_PRIM_FUN_PROMOTE_ELEMENTS_HPP #include #include #include #include namespace stan { namespace math { /** * Struct with static function for elementwise type promotion. * *

This base implementation promotes one scalar value to another. * * @tparam T type of promoted element * @tparam S type of input element, must be assignable to T */ template struct promote_elements { /** * Return input element. * * @param u input of type S, assignable to type T * @returns input as type T */ inline static T promote(const S& u) { return u; } }; /** * Struct with static function for elementwise type promotion. * *

This specialization promotes scalar values of the same type. * * @tparam T type of elements */ template struct promote_elements { /** * Return input element. * * @param u input of type T * @returns input as type T */ inline static const T& promote(const T& u) { return u; } }; /** * Struct with static function for elementwise type promotion. * *

This specialization promotes vector elements of different types * which must be compatible with promotion. * * @tparam T type of promoted elements * @tparam S type of input elements, must be assignable to T */ template struct promote_elements, std::vector > { /** * Return input vector of type S as vector of type T. * * @param u vector of type S, assignable to type T * @returns vector of type T */ inline static std::vector promote(const std::vector& u) { std::vector t; t.reserve(u.size()); for (size_t i = 0; i < u.size(); ++i) { t.push_back(promote_elements::promote(u[i])); } return t; } }; /** * Struct with static function for elementwise type promotion. * *

This specialization promotes vector elements of the same type. * * @tparam T type of elements */ template struct promote_elements, std::vector > { /** * Return input vector. * * @param u vector of type T * @returns vector of type T */ inline static const std::vector& promote(const std::vector& u) { return u; } }; /** * Struct with static function for elementwise type promotion. * *

This specialization promotes matrix elements of different types * which must be compatible with promotion. * * @tparam T type of promoted elements * @tparam S type of input elements, must be assignable to T * @tparam R number of rows, can be Eigen::Dynamic * @tparam C number of columns, can be Eigen::Dynamic */ template struct promote_elements, Eigen::Matrix > { /** * Return input matrix of type S as matrix of type T. * * @param u matrix of type S, assignable to type T * @returns matrix of type T */ inline static Eigen::Matrix promote( const Eigen::Matrix& u) { Eigen::Matrix t(u.rows(), u.cols()); for (int i = 0; i < u.size(); ++i) { t(i) = promote_elements::promote(u(i)); } return t; } }; /** * Struct with static function for elementwise type promotion. * *

This specialization promotes matrix elements of the same type. * * @tparam T type of elements in the matrices * @tparam R number of rows, can be Eigen::Dynamic * @tparam C number of columns, can be Eigen::Dynamic */ template struct promote_elements, Eigen::Matrix > { /** * Return input matrix. * * @param u matrix of type T * @returns matrix of type T */ inline static const Eigen::Matrix& promote( const Eigen::Matrix& u) { return u; } }; } // namespace math } // namespace stan #endif