stan-dev / stan

Stan development repository. The master branch contains the current release. The develop branch contains the latest stable development. See the Developer Process Wiki for details.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
2.59k stars 369 forks source link

a^b syntax for pow(a,b) #648

Closed bob-carpenter closed 10 years ago

bob-carpenter commented 10 years ago

It'd be nice to support the syntax a^b as shorthand for pow(a,b).

Even nicer to vectorize. If the types are coherent, the return seems obvious:

vec^scalar : vec
scalar^vec :vec
vec^vec : vec

But what if they're mixed?

vec^row_vec : ?
aadler commented 10 years ago

Why not transpose the exponent so that it is the same structure as the base?

vec ^ row_vec --> vec ^ transpose{row_vec} = vec ^ vec row_vec ^ vec --> row_vec ^ transpose{vec} = row_vec ^ row_vec

bob-carpenter commented 10 years ago

If we want to define mixed operations, taking the base type as the result makes the most sense to me.

My main concern is that the exponent doesn't then have consistent behavior, with:

real ^ row_vec : row_vec

vec ^ row_vec : vec

I think putting the transpose{...} there explicitly highlights that it gets applied only if there's a type mismatch with the base.

On May 16, 2014, at 1:39 AM, Avraham Adler notifications@github.com wrote:

Why not transpose the exponent so that it is the same structure as the base?

vec ^ row_vec --> vec ^ transpose{row_vec} = vec ^ vec row_vec ^ vec --> row_vec ^ transpose{vec} = row_vec ^ row_vec

— Reply to this email directly or view it on GitHub.

ksvanhorn commented 10 years ago

If it's not obvious what vec ^ row_vec or row_vec ^ vec should return, should it even be allowed? It seems that no matter what is chosen, the results are going to surprise a lot of users.

-- Kevin

On May 15, 2014, at 6:59 PM, Bob Carpenter wrote:

If we want to define mixed operations, taking the base type as the result makes the most sense to me.

My main concern is that the exponent doesn't then have consistent behavior, with:

real ^ row_vec : row_vec

vec ^ row_vec : vec

I think putting the transpose{...} there explicitly highlights that it gets applied only if there's a type mismatch with the base.

  • Bob

On May 16, 2014, at 1:39 AM, Avraham Adler notifications@github.com wrote:

Why not transpose the exponent so that it is the same structure as the base?

vec ^ row_vec --> vec ^ transpose{row_vec} = vec ^ vec row_vec ^ vec --> row_vec ^ transpose{vec} = row_vec ^ row_vec

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

betanalpha commented 10 years ago

I agree - if the vectorization is ambiguous it's probably both worth the UX hassle.

On May 16, 2014, at 4:31 AM, "Kevin S. Van Horn" notifications@github.com wrote:

If it's not obvious what vec ^ row_vec or row_vec ^ vec should return, should it even be allowed? It seems that no matter what is chosen, the results are going to surprise a lot of users.

-- Kevin

On May 15, 2014, at 6:59 PM, Bob Carpenter wrote:

If we want to define mixed operations, taking the base type as the result makes the most sense to me.

My main concern is that the exponent doesn't then have consistent behavior, with:

real ^ row_vec : row_vec

vec ^ row_vec : vec

I think putting the transpose{...} there explicitly highlights that it gets applied only if there's a type mismatch with the base.

  • Bob

On May 16, 2014, at 1:39 AM, Avraham Adler notifications@github.com wrote:

Why not transpose the exponent so that it is the same structure as the base?

vec ^ row_vec --> vec ^ transpose{row_vec} = vec ^ vec row_vec ^ vec --> row_vec ^ transpose{vec} = row_vec ^ row_vec

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

bob-carpenter commented 10 years ago

Associativity and precedence to follow Fortran: