# Element-wise exponentiation

**URL:** <https://discourse.julialang.org/t/element-wise-exponentiation/43480>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 22, 2020, 9:48am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480 "2020-07-22T09:48:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [July 22, 2020, 9:48am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/1 "2020-07-22T09:48:50Z")

</div>

Hi,

I have this code:

```julia
N = 100;
n = 1:N;
2.^(-n)

```

and the final line returns the error:

```julia
ERROR: syntax: invalid syntax "2.^"; add space(s) to clarify

```

I’ve tried adding a space before ^ and after it, and neither of those efforts to fix this error worked. Naturally, what I want is a vector of [1, 1/2, 1/2^2, 1/2^3, 1/2^4, …, 1/2^N]. I know one round-about way of achieving what I want (namely `exp.(-log(2)*n)`), but I just wanted to know if there are any other ways (besides using a loop and defining my desired vector element-by-element, of course) of doing this?

Thanks for your time

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2020, 9:55am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/2 "2020-07-22T09:55:11Z")

</div>

this works for me (I guess `.^` is the operator here and should be ‘separated by spaces’)

```julia
julia> 2 .^ (1:10)
10-element Array{Int64,1}:
    2
    4
    8
   16
   32
   64
  128
  256
  512
 1024

```

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [July 22, 2020, 9:56am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/3 "2020-07-22T09:56:20Z")

</div>

Odd, doing that with -n doesn’t work, I just tried it.

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 22, 2020, 9:58am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/4 "2020-07-22T09:58:06Z")

</div>

The error message tells you what to do:

```julia
julia> 2 .^ (-n)
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Make x or -1 a float by adding a zero decimal (e.g., 2.0^-1 or 2^-1.0 instead of 2^-1), or write 1/x^1, float(x)^-1, x^float(-1) or (x//1)^-1

```

Here’s the result:

```julia
julia> 2.0 .^ (-n)
100-element Array{Float64,1}:
 0.5
 0.25
 0.125
 0.0625
 0.03125
 0.015625
 0.0078125
 0.00390625
 0.001953125
 0.0009765625
 0.00048828125
 0.000244140625
 0.0001220703125
 ⋮
 1.6155871338926322e-27
 8.077935669463161e-28
 4.0389678347315804e-28
 2.0194839173657902e-28
 1.0097419586828951e-28
 5.048709793414476e-29
 2.524354896707238e-29
 1.262177448353619e-29
 6.310887241768095e-30
 3.1554436208840472e-30
 1.5777218104420236e-30
 7.888609052210118e-31

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2020, 9:59am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/5 "2020-07-22T09:59:36Z")

</div>

Yeah. I am sure there is a reason for that (someone can probably elaborate).  
Interestingly `2 ^ -1` works just fine.  
So I guess Julia CAN **raise an integer x to a negative power -1.**

EDIT: I realized this does not work with `z=-1; 2^z `  
So I guess its a matter of how/when the minus 1 / inversion is applied

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [July 22, 2020, 10:00am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/6 "2020-07-22T10:00:18Z")

</div>

Oops, I just skimmed passed the error message as I was getting used to ones that I couldn’t understand.

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 22, 2020, 10:04am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/7 "2020-07-22T10:04:20Z")

</div>

“You should get in the habit of reading through all the error messages that you encounter.”  
Bla bla bla, and so on and so forth.  
We all know the deal: Often times we just want things to work and don’t take enough time to read and understand.  
Julia error messages aren’t always helpful but in 50% of the cases they immediately tell me what I need to do, esp. when it comes to little syntax or method problems.

On a sidenote: One of the best error messages I’ve ever encountered are implemented in Elm. They are helpful, explanatory and respectful at the same time. It’s astonishing how error messages can shape programming culture. 🙂

Quick edit to suggest sth to read: [news/compiler-errors-for-humans](https://elm-lang.org/news/compiler-errors-for-humans)

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2020, 10:05am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/8 "2020-07-22T10:05:52Z")

</div>

Still trying to understand `2^-1`  
Why does the first expression work, but when I try to evaluate what ‘code\_lowered’ returns, I get an error…?

```julia
julia> 2 ^ -1
0.5

julia> @code_lowered 2 ^ -1
CodeInfo(
1 ─ %1 = Base.power_by_squaring(x, p)
└── return %1
)

julia> Base.power_by_squaring(2,-1)
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Make x or -1 a float by adding a zero decimal (e.g., 2.0^-1 or 2^-1.0 instead of 2^-1), or write 1/x^1, float(x)^-1, x^float(-1) or (x//1)^-1
Stacktrace:
 [1] throw_domerr_powbysq(::Int64, ::Int64) at .\intfuncs.jl:193
 [2] power_by_squaring(::Int64, ::Int64) at .\intfuncs.jl:214
 [3] top-level scope at REPL[42]:1

julia> Base.power_by_squaring(2,8)
256

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [July 22, 2020, 10:08am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/9 "2020-07-22T10:08:35Z")

</div>

This error message is also a bit strange:

```julia
julia> Base.power_by_squaring(2.0,-1)
ERROR: DomainError with -1:
Cannot raise an integer x to a negative power -1.
Convert input to float.
Stacktrace:
 [1] throw_domerr_powbysq(::Float64, ::Int64) at .\intfuncs.jl:190
 [2] power_by_squaring(::Float64, ::Int64) at .\intfuncs.jl:214
 [3] top-level scope at REPL[50]:1

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [July 22, 2020, 11:09am UTC](https://discourse.julialang.org/t/element-wise-exponentiation/43480/10 "2020-07-22T11:09:31Z")

</div>

This was already discussed [here](https://discourse.julialang.org/t/repl-domain-error-for-exponentiation/31408), for example. The TLDR is, that the Julia parser special-cases literal integer exponents to call `Base.literal_pow`, which makes it possible to return a floating point value for negative powers without any type instabilities. If this were the case for non-literal integer powers as well, you’d either always have to return a floating point result, which is awkward, if you just want to work with integers, or introduce type instabilities, which would make `^` for integers significantly slower as a whole.
