stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
752 stars 188 forks source link

Vectorise ternary functions #2638

Open andrjohns opened 2 years ago

andrjohns commented 2 years ago

Description

As with the unary and binary functions, it would useful to add a framework for vectorising ternary functions (like fma, inc_beta, and if_else).

Eigen has a framework CwiseTernaryOp which we can use in combination with the existing apply_scalar_binary framework to cover the various combinations

Current Version:

v4.2.1

rok-cesnovar commented 2 years ago

Great! if_else() is deprecated and will not be supported as of Stan 2.32, so there is probably no need to bother with that one?

andrjohns commented 2 years ago

Ah cool. How does stanc3 handle the ternary operator? I was thinking that the vectorised if_else would allow us to have vectorised ternary expressions as well

rok-cesnovar commented 2 years ago

With the ternary C++ operator:

real a = k > 0 ? 5 : 10;

is generated as:

a = (logical_gt(k, 0) ? 5 : 10);

Maybe we should think about using if_elsefor that case though. So the ternary operator in Stan is generated to if_else. Maybe open an issue in stanc3 so we discuss it?

andrjohns commented 2 years ago

Sounds good, will do!