Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds is_vt_complex and is_not_vt_complex type traits #2694

Merged
merged 2 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions stan/math/prim/meta/is_complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ struct scalar_type<T, std::enable_if_t<is_complex<T>::value>> {
STAN_ADD_REQUIRE_UNARY(complex, is_complex, require_stan_scalar_complex);
STAN_ADD_REQUIRE_UNARY_INNER(complex, is_complex, require_stan_scalar_complex);

/**
* If the `value_type` of the type `T` is of type
* `std::complex` or a cv-qualified version thereof, provides the
* member constant `value` equal `true`; for any other type the value is
* `false`.
*
* @tparam T type to check
* @ingroup type_trait
*/
template <typename T>
struct is_vt_complex : is_complex<value_type_t<std::decay_t<T>>> {};

/**
* If the `value_type` of the type `T` is not of type
* `std::complex` or a cv-qualified version thereof, provides the
* member constant `value` equal `true`; for any other type the value is
* `false`.
*
* @tparam T type to check
* @ingroup type_trait
*/
template <typename T>
struct is_vt_not_complex
: bool_constant<!is_complex<value_type_t<std::decay_t<T>>>::value> {};

} // namespace stan

#endif
82 changes: 82 additions & 0 deletions test/unit/math/mix/meta/is_complex_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,85 @@ TEST(stanMathMix, isComplex) {
expect_is_complex<false, double>();
expect_is_complex<false, std::string>();
}

template <bool expected, typename T>
void expect_is_vt_complex() {
EXPECT_EQ(expected, stan::is_vt_complex<T>::value);
}

template <typename T>
void test_is_vt_complex() {
using complex_std_vec = std::vector<std::complex<T>>;
using complex_eigen_vec = Eigen::Matrix<std::complex<T>, -1, 1>;
expect_is_vt_complex<true, complex_std_vec>();
expect_is_vt_complex<true, const complex_std_vec>();
expect_is_vt_complex<true, complex_std_vec&>();
expect_is_vt_complex<true, const complex_std_vec&>();
expect_is_vt_complex<false, complex_std_vec*>();
expect_is_vt_complex<false, const complex_std_vec*>();

expect_is_vt_complex<true, complex_eigen_vec>();
expect_is_vt_complex<true, const complex_eigen_vec>();
expect_is_vt_complex<true, complex_eigen_vec&>();
expect_is_vt_complex<true, const complex_eigen_vec&>();
expect_is_vt_complex<false, complex_eigen_vec*>();
expect_is_vt_complex<false, const complex_eigen_vec*>();
}

TEST(stanMathMix, is_vt_Complex) {
using stan::math::fvar;
using stan::math::var;
test_is_vt_complex<double>();
test_is_vt_complex<var>();
test_is_vt_complex<fvar<double>>();
test_is_vt_complex<fvar<fvar<double>>>();
test_is_vt_complex<fvar<var>>();
test_is_vt_complex<fvar<fvar<var>>>();

expect_is_vt_complex<false, std::vector<bool>>();
expect_is_vt_complex<false, std::vector<int>>();
expect_is_vt_complex<false, std::vector<size_t>>();
expect_is_vt_complex<false, std::vector<double>>();
expect_is_vt_complex<false, std::vector<std::string>>();
}

template <bool expected, typename T>
void expect_is_vt_not_complex() {
EXPECT_EQ(expected, stan::is_vt_not_complex<T>::value);
}

template <typename T>
void test_is_vt_not_complex() {
using complex_std_vec = std::vector<std::complex<T>>;
using complex_eigen_vec = Eigen::Matrix<std::complex<T>, -1, 1>;
expect_is_vt_not_complex<!true, complex_std_vec>();
expect_is_vt_not_complex<!true, const complex_std_vec>();
expect_is_vt_not_complex<!true, complex_std_vec&>();
expect_is_vt_not_complex<!true, const complex_std_vec&>();
expect_is_vt_not_complex<!false, complex_std_vec*>();
expect_is_vt_not_complex<!false, const complex_std_vec*>();

expect_is_vt_not_complex<!true, complex_eigen_vec>();
expect_is_vt_not_complex<!true, const complex_eigen_vec>();
expect_is_vt_not_complex<!true, complex_eigen_vec&>();
expect_is_vt_not_complex<!true, const complex_eigen_vec&>();
expect_is_vt_not_complex<!false, complex_eigen_vec*>();
expect_is_vt_not_complex<!false, const complex_eigen_vec*>();
}

TEST(stanMathMix, is_vt_not_Complex) {
using stan::math::fvar;
using stan::math::var;
test_is_vt_not_complex<double>();
test_is_vt_not_complex<var>();
test_is_vt_not_complex<fvar<double>>();
test_is_vt_not_complex<fvar<fvar<double>>>();
test_is_vt_not_complex<fvar<var>>();
test_is_vt_not_complex<fvar<fvar<var>>>();

expect_is_vt_not_complex<!false, std::vector<bool>>();
expect_is_vt_not_complex<!false, std::vector<int>>();
expect_is_vt_not_complex<!false, std::vector<size_t>>();
expect_is_vt_not_complex<!false, std::vector<double>>();
expect_is_vt_not_complex<!false, std::vector<std::string>>();
}