-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Delegate ternary operator to Math if_else()
#1086
Comments
Thanks for popping this up, @andrjohns. The ternary operator in Stan pretty much has to compile to the ternary operator. That's the only way to get the appropriate short-circuiting behavior without some sort of elaborate lazy evaluation wrapper around the arguments. We can extend the ternary operator to vectors, but have to be careful to compile it to a different function. I would recommend actually writing an I find the implementation choice in R to be perplexing in this instance.
The doc is here:
I suppose that's what you get when you conflate scalars and singleton arrays. I would prefer the return type to match the positive and negative arguments, which should be checked for match in size and shape. For 1D arrays, that would look like this
where
This could potentially be extended to higher-dimensional arrays, vectors, and matrices in the obvious way. For example,
The above definition is consistent with broadcasting a single
which applies to
There's a question of whether |
I'm working through mail backward and saw the note in the math lib about broadcasting. We could broadcast the arguments, but that can start to get confusing with the ternary operator. These two are natural consequences of having an unvectorized and fully vectorize form,
The natural broadcasting is this:
It feels dangerous given the ternary op and fully vectorized forms, but I don't see any cases where this'd run into ambiguity. It gets trickier with matrices and vectors---we've always shied away from broadcasting vectors up to matrices by copying columsn or rwo vectors to matrices by copying rows. |
Thanks Bob, that makes sense to me! In that case it could work to have stanc3 conditionally transpile to As for the broadcasting, the rules would be identical to the binary functions - all containers have to be the same dimension (i.e., no mixing vectors and matrices) and any scalars are recycled/broadcast - so the signatures that you posted above would be correct. |
That's the only way I can see to maintain backward compatibility for short-circuiting.
Perfect, thanks! |
We end up doing something pretty similar for all the other operators - e.g. we use the building C++ operator if everything is a scalar, otherwise we call stanc3/src/stan_math_backend/Expression_gen.ml Lines 218 to 226 in 9218def
|
At the moment the ternary operator in stan directly transpiles to the C++ ternary operator. However, there have been requests in the past for a vectorised ternary operator:
I'm currently working on a ternary vectorisation framework which could be used to vectorise the
if_else()
function in the Math library: stan-dev/math#2638If stanc3 were to transpile the ternary operator to
if_else
, then Stan would automatically support vectorised ternary statements once theif_else
function is vectorised.The text was updated successfully, but these errors were encountered: