From 1634d5f2a8b422527b6aeab651d4142df3b8f371 Mon Sep 17 00:00:00 2001 From: deveshvaidyaofficial Date: Sun, 18 Oct 2026 11:18:40 +0530 Subject: [PATCH 1/5] Added is_even and is_odd functions --- stan/math/prim/fun/is_even.hpp | 48 ++++++++++++ stan/math/prim/fun/is_odd.hpp | 48 ++++++++++++ test/unit/math/prim/fun/is_even_odd_test.cpp | 81 ++++++++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 stan/math/prim/fun/is_even.hpp create mode 100644 stan/math/prim/fun/is_odd.hpp create mode 100644 test/unit/math/prim/fun/is_even_odd_test.cpp diff --git a/stan/math/prim/fun/is_even.hpp b/stan/math/prim/fun/is_even.hpp new file mode 100644 index 00000000000..f89e42c41ea --- /dev/null +++ b/stan/math/prim/fun/is_even.hpp @@ -0,0 +1,48 @@ +#ifndef STAN_MATH_PRIM_FUN_IS_EVEN_HPP +#define STAN_MATH_PRIM_FUN_IS_EVEN_HPP + +#include +#include + +namespace stan { +namespace math { + +/** + * Return 1 if the integer is even and 0 otherwise. + * + * @tparam T Integer type + * @param n Integer to check + * @return 1 if n is even, 0 otherwise + */ +template * = nullptr> +inline int is_even(T n) { + return n % 2 == 0 ? 1 : 0; +} + +/** + * Vectorization wrapper for is_even. + */ +struct is_even_fun { + template + static inline int fun(const T& x) { + return is_even(static_cast(x)); + } +}; + +/** + * Apply is_even elementwise to a container. + * + * @tparam T Container type + * @param x Container of integers + * @return Elementwise parity results + */ +template * = nullptr> +inline auto is_even(T&& x) { + return apply_scalar_unary::apply( + std::forward(x)); +} + +} // namespace math +} // namespace stan + +#endif \ No newline at end of file diff --git a/stan/math/prim/fun/is_odd.hpp b/stan/math/prim/fun/is_odd.hpp new file mode 100644 index 00000000000..70f8bacf1ad --- /dev/null +++ b/stan/math/prim/fun/is_odd.hpp @@ -0,0 +1,48 @@ +#ifndef STAN_MATH_PRIM_FUN_IS_ODD_HPP +#define STAN_MATH_PRIM_FUN_IS_ODD_HPP + +#include +#include + +namespace stan { +namespace math { + +/** + * Return 1 if the integer is odd and 0 otherwise. + * + * @tparam T Integer type + * @param n Integer to check + * @return 1 if n is odd, 0 otherwise + */ +template * = nullptr> +inline int is_odd(T n) { + return n % 2 != 0 ? 1 : 0; +} + +/** + * Vectorization wrapper for is_odd. + */ +struct is_odd_fun { + template + static inline int fun(const T& x) { + return is_odd(static_cast(x)); + } +}; + +/** + * Apply is_odd elementwise to a container. + * + * @tparam T Container type + * @param x Container of integers + * @return Elementwise parity results + */ +template * = nullptr> +inline auto is_odd(T&& x) { + return apply_scalar_unary::apply( + std::forward(x)); +} + +} // namespace math +} // namespace stan + +#endif \ No newline at end of file diff --git a/test/unit/math/prim/fun/is_even_odd_test.cpp b/test/unit/math/prim/fun/is_even_odd_test.cpp new file mode 100644 index 00000000000..5f9f0d4c4a9 --- /dev/null +++ b/test/unit/math/prim/fun/is_even_odd_test.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +TEST(MathFunctions, is_even_scalar) { + EXPECT_EQ(1, stan::math::is_even(0)); + EXPECT_EQ(0, stan::math::is_even(1)); + EXPECT_EQ(1, stan::math::is_even(2)); + EXPECT_EQ(0, stan::math::is_even(3)); + + EXPECT_EQ(0, stan::math::is_even(-1)); + EXPECT_EQ(1, stan::math::is_even(-2)); + EXPECT_EQ(0, stan::math::is_even(-3)); +} + +TEST(MathFunctions, is_odd_scalar) { + EXPECT_EQ(0, stan::math::is_odd(0)); + EXPECT_EQ(1, stan::math::is_odd(1)); + EXPECT_EQ(0, stan::math::is_odd(2)); + EXPECT_EQ(1, stan::math::is_odd(3)); + + EXPECT_EQ(1, stan::math::is_odd(-1)); + EXPECT_EQ(0, stan::math::is_odd(-2)); + EXPECT_EQ(1, stan::math::is_odd(-3)); +} + +TEST(MathFunctions, is_even_vector) { + std::vector input{0, 1, 2, 3, -3, -4}; + std::vector expected{1, 0, 1, 0, 0, 1}; + + EXPECT_EQ(expected, stan::math::is_even(input)); +} + +TEST(MathFunctions, is_odd_vector) { + std::vector input{0, 1, 2, 3, -3, -4}; + std::vector expected{0, 1, 0, 1, 1, 0}; + + EXPECT_EQ(expected, stan::math::is_odd(input)); +} + +TEST(MathFunctions, is_even_nested_vector) { + std::vector> input{ + {1, 2}, + {3, 4} + }; + + std::vector> expected{ + {0, 1}, + {0, 1} + }; + + EXPECT_EQ(expected, stan::math::is_even(input)); +} + +TEST(MathFunctions, is_odd_nested_vector) { + std::vector> input{ + {1, 2}, + {3, 4} + }; + + std::vector> expected{ + {1, 0}, + {1, 0} + }; + + EXPECT_EQ(expected, stan::math::is_odd(input)); +} + +TEST(MathFunctions, is_even_empty_vector) { + std::vector input; + std::vector expected; + + EXPECT_EQ(expected, stan::math::is_even(input)); +} + +TEST(MathFunctions, is_odd_empty_vector) { + std::vector input; + std::vector expected; + + EXPECT_EQ(expected, stan::math::is_odd(input)); +} \ No newline at end of file From 01a3150be32d3229b7e542a0d30348e714c5276b Mon Sep 17 00:00:00 2001 From: deveshvaidyaofficial Date: Sun, 18 Oct 2026 11:28:36 +0530 Subject: [PATCH 2/5] "Added is_even and is_odd functions" --- stan/math/prim.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stan/math/prim.hpp b/stan/math/prim.hpp index 208818ae116..1cbd9e8045a 100644 --- a/stan/math/prim.hpp +++ b/stan/math/prim.hpp @@ -14,6 +14,8 @@ #include #include +#include +#include #include #include From f9496ab1e7e7a76a3cfc7c3192f06851c4c92e65 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 18 Oct 2026 02:08:56 -0400 Subject: [PATCH 3/5] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- stan/math/prim/fun/is_even.hpp | 3 +-- stan/math/prim/fun/is_odd.hpp | 3 +-- test/unit/math/prim/fun/is_even_odd_test.cpp | 22 +++++--------------- 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/stan/math/prim/fun/is_even.hpp b/stan/math/prim/fun/is_even.hpp index f89e42c41ea..37ca0f49178 100644 --- a/stan/math/prim/fun/is_even.hpp +++ b/stan/math/prim/fun/is_even.hpp @@ -38,8 +38,7 @@ struct is_even_fun { */ template * = nullptr> inline auto is_even(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/is_odd.hpp b/stan/math/prim/fun/is_odd.hpp index 70f8bacf1ad..6da14493335 100644 --- a/stan/math/prim/fun/is_odd.hpp +++ b/stan/math/prim/fun/is_odd.hpp @@ -38,8 +38,7 @@ struct is_odd_fun { */ template * = nullptr> inline auto is_odd(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/test/unit/math/prim/fun/is_even_odd_test.cpp b/test/unit/math/prim/fun/is_even_odd_test.cpp index 5f9f0d4c4a9..e57935f890d 100644 --- a/test/unit/math/prim/fun/is_even_odd_test.cpp +++ b/test/unit/math/prim/fun/is_even_odd_test.cpp @@ -39,29 +39,17 @@ TEST(MathFunctions, is_odd_vector) { } TEST(MathFunctions, is_even_nested_vector) { - std::vector> input{ - {1, 2}, - {3, 4} - }; + std::vector> input{{1, 2}, {3, 4}}; - std::vector> expected{ - {0, 1}, - {0, 1} - }; + std::vector> expected{{0, 1}, {0, 1}}; EXPECT_EQ(expected, stan::math::is_even(input)); } TEST(MathFunctions, is_odd_nested_vector) { - std::vector> input{ - {1, 2}, - {3, 4} - }; - - std::vector> expected{ - {1, 0}, - {1, 0} - }; + std::vector> input{{1, 2}, {3, 4}}; + + std::vector> expected{{1, 0}, {1, 0}}; EXPECT_EQ(expected, stan::math::is_odd(input)); } From 0d9e322fb178f56bfb22f4f0ba6bbc659867c872 Mon Sep 17 00:00:00 2001 From: deveshvaidyaofficial Date: Sun, 18 Oct 2026 12:04:24 +0530 Subject: [PATCH 4/5] Fix missing final newlines in parity functions --- stan/math/prim/fun/is_even.hpp | 2 +- stan/math/prim/fun/is_odd.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/prim/fun/is_even.hpp b/stan/math/prim/fun/is_even.hpp index 37ca0f49178..2a50bfff887 100644 --- a/stan/math/prim/fun/is_even.hpp +++ b/stan/math/prim/fun/is_even.hpp @@ -44,4 +44,4 @@ inline auto is_even(T&& x) { } // namespace math } // namespace stan -#endif \ No newline at end of file +#endif diff --git a/stan/math/prim/fun/is_odd.hpp b/stan/math/prim/fun/is_odd.hpp index 6da14493335..191f3f3cd59 100644 --- a/stan/math/prim/fun/is_odd.hpp +++ b/stan/math/prim/fun/is_odd.hpp @@ -44,4 +44,4 @@ inline auto is_odd(T&& x) { } // namespace math } // namespace stan -#endif \ No newline at end of file +#endif From f4721ffbc04da1bb39a8dfe33fb13d43e2d416b2 Mon Sep 17 00:00:00 2001 From: deveshvaidyaofficial Date: Sun, 18 Oct 2026 12:16:58 +0530 Subject: [PATCH 5/5] Fixed newline error in the test --- test/unit/math/prim/fun/is_even_odd_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/prim/fun/is_even_odd_test.cpp b/test/unit/math/prim/fun/is_even_odd_test.cpp index e57935f890d..f9c3f0657cb 100644 --- a/test/unit/math/prim/fun/is_even_odd_test.cpp +++ b/test/unit/math/prim/fun/is_even_odd_test.cpp @@ -66,4 +66,4 @@ TEST(MathFunctions, is_odd_empty_vector) { std::vector expected; EXPECT_EQ(expected, stan::math::is_odd(input)); -} \ No newline at end of file +}