# Efficient way to vectorize the power function

Hi,

I am wondering whether there is any efficient way of vectorizing the power function in Stan. I have read some documentation of pow(vector, vector), pow(real, vector) and pow(vector, real) but not sure how they are applied in the practical setting. Is there any detailed numerical examples about them?

Thx!

More information about the vectorised binary functions and how to use them is available in this chapter: Stan Functions Reference

the same topic has been discussed earlier Vectorizing power function - #4 by Bob_Carpenter - as I understood there is not so much gain in terms of computational time if the vectorization is used

That’s actually discussing an older framework that never made it to release, the binary vectorisation currently in Stan is a bit different to that. But yeah, there’s no speed-up in using the vectorised versions of the binary functions it’s just more concise.

1 Like

Even though there is currently no speed gain from vectorised elementwise operations like `pow`, as @andrjohns notes, there are changes in the development pipeline that will speed up vectorised code by a non-negligible factor so we would advise you write your models in a vectorized way wherever possible.

4 Likes

Hi,

Sorry to interrupt but a quick follow up question is that accoridng to this page 3.1 Vectorization of real-valued functions | Stan Functions Reference we could use both pow(real, real) and pow (vector, vector) if the inputs are of same type and size. But when I actually ran my code the error messgae is like (I am using rstan Version 2.21.2):

``````SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

pow(vector, vector)

Available argument signatures for pow:

pow(real, real)
``````

May I ask that why the above error exists and does stan allow expressions like pow(real, vector) and pow(vector, real) since I also tried the following:

``````SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

pow(real, vector)

Available argument signatures for pow:

pow(real, real)
``````

and

``````SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:

pow(vector, real)

Available argument signatures for pow:

pow(real, real)
``````

Thx!

Hi,

the reference manual you linked is for Stan 2.26 (see the URL) but you are using rstan, which is at version 2.21. An upgrade is due in the next weeks.

So after rstan has been upgraded, do you mean that I could use either pow(real, vector), pow(vector, real) and pow(vector, vector)?

Also I found that this reference 5.4 Elementwise functions | Stan Functions Reference might suggest that ‘vector .^ real’ is equivalent to pow(vector, real), but this elementwise operation ‘.^’ also did not work for me? Is this still a rstan version issue or they are actually not equivalent?

Thx!

Correct. The vectorized pow() is available since 2.25. The next rstan release will be 2.26 so it will have all these features.

.^ is just the operator form of `pow()` and will be available once rstan is at 2.26.

Thanks so much !

Hopefully I could get the updated version of rstan soon. Will using cmdstanr instead would catch up with the most recent version of stan?

Also may I ask that what’s the meaning of T1, T2 and R in the Stan Functions Reference for pow() function?

``````R pow(T1 x, T2 y)
Vectorized implementation of the pow function
``````

Thx!

You can use these in cmdstanr yes. Rstan update is getting closer, but not imminent.

The R just notes that the types will get promoted to real. Meaning that
pow(int, int) return a real.

As doe
pow(int, real)
pow(int, vector)
and so on.

You can find all available signatures here:
https://rok-cesnovar.github.io/stanc3js-demo/signatures.html