# Applying exponentiation to vectorized form avoiding for loop

**URL:** <https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787>\
**Category:** General\
**Created:** [March 21, 2020, 5:44pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787 "2020-03-21T17:44:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 21, 2020, 5:44pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/1 "2020-03-21T17:44:04Z")

</div>

Hi Everyone,

is there a way to apply exponentiation to vectors without using for loop?

for example,

I have,

model{  
vector[100] p;  
vector[100] q;  
…  
q = p^3.02;  
…  
}

How can I do this without for loop like this,

for (n in 1:100) {  
q[n] = p[n] ^ 3.02;  
}

Looking forward for an efficient solution to avoid using for loop.

Thanks,

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [March 21, 2020, 8:31pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/2 "2020-03-21T20:31:36Z")

</div>

There is no elementwise power operator in the Stan language, but if there were it would evaluate to the same loop as

```stan
for (n in 1:100) q[n] = p[n] ^ 3.02;

```

---

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 21, 2020, 8:34pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/3 "2020-03-21T20:34:55Z")

</div>

so there is no way for me to avoid the for loop when I have to use the power operator?

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [March 21, 2020, 8:47pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/4 "2020-03-21T20:47:33Z")

</div>

Given that Stan for loops are translated to C++ there is no drop in performance. And I don’t think there is any linear algebra stuff for making AD any faster with elementwise operations (I could be wrong).

Bottom line: for loops are not slow in Stan. In some cases vectorized stuff has better AD properties and are then recommended.

---

<div class="post-metadata">

**Author:** ![Murali\_037](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Murali\_037](https://discourse.mc-stan.org/u/Murali_037)\
**Post date:** [March 21, 2020, 8:50pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/5 "2020-03-21T20:50:17Z")

</div>

I have to use a lot of separate for loops in my model because of the power operator at different parts of my model equation.

Hence, trying to find an alternative to make it vectorized

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [March 21, 2020, 9:52pm UTC](https://discourse.mc-stan.org/t/applying-exponentiation-to-vectorized-form-avoiding-for-loop/13787/6 "2020-03-21T21:52:50Z")

</div>

Maybe create a user function for it?
