# Power as vector rather than a value

**URL:** <https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207>\
**Category:** New to Julia\
**Created:** [July 5, 2023, 11:50am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207 "2023-07-05T11:50:03Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 5, 2023, 11:50am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/1 "2023-07-05T11:50:03Z")

</div>

Can we solve something like this ?

```julia
10^[1, 2, 3, 4]

```

Is there any way ?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [July 5, 2023, 11:52am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/2 "2023-07-05T11:52:19Z")

</div>

```julia
julia> 10 .^[1, 2, 3, 4]
4-element Vector{Int64}:
    10
   100
  1000
 10000

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 5, 2023, 11:53am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/3 "2023-07-05T11:53:48Z")

</div>

I tried it but without space and got error 😅

like this

```julia
10.^[1, 2, 3]

```

Thank you

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [July 5, 2023, 11:55am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/4 "2023-07-05T11:55:03Z")

</div>

Me too (😄), but got help

```julia
julia> 10.^[1, 2, 3, 4]
ERROR: ParseError:
# Error @ REPL[78]:1:1
10.^[1, 2, 3, 4]
└─┘ ── ambiguous `.` syntax; add whitespace to clarify (eg `1.+2` might be `1.0+2` or `1 .+ 2`)

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 5, 2023, 11:57am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/5 "2023-07-05T11:57:22Z")

</div>

how you got this ?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [July 5, 2023, 11:58am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/6 "2023-07-05T11:58:24Z")

</div>

Probably the Julia version. I’m using 1.9.0 here.

EDIT: sorry, correction, Julia 1.10dev

---

<div class="post-metadata">

**Author:** ![ederag](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ederag/32/4106_2.png) [@ederag](https://discourse.julialang.org/u/ederag)\
**Post date:** [July 5, 2023, 12:10pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/7 "2023-07-05T12:10:10Z")

</div>

In `julia-1.9.1` it was good enough,

```julia
julia> 10.^[1, 2, 3, 4]
ERROR: syntax: invalid syntax "10.^"; add space(s) to clarify

```

but I like the new message even better 👍.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 5, 2023, 12:27pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/8 "2023-07-05T12:27:23Z")

</div>

I wish this was solved by just disallowing the `10.` notation for floats. Too late, of course, but this is really annoying for `.^`, since `10.^(1:3)` is so natural, while `10 .^(1:3)` is awkward.

(I also really dislike the trailing or leading dot-notation for floats in general.)

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [July 6, 2023, 10:33pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/9 "2023-07-06T22:33:40Z")

</div>

> [@DNF](#):
>
> `10.^(1:3)` is so natural, while `10 .^(1:3)` is awkward.

The most readable version in my opinion is `10 .^ (1:3)`. Whitespaces should show visually the logical grouping of the operations.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 6, 2023, 10:45pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/10 "2023-07-06T22:45:17Z")

</div>

For `+`, `*`, etc (with and without dots) I agree, but not for power, that should be without space. I belive most style guides say the same thing.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [July 7, 2023, 6:39am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/11 "2023-07-07T06:39:37Z")

</div>

I would skip the spaces in a more complicated expression, to highlight operator precedence, but I would keep them If the `.^` is the main operation on a line of code (possibly with an assignment). I believe that’s what most style guides suggest, but that’s a matter of interpretation.

The Julia manual [here](https://docs.julialang.org/en/v1/manual/mathematical-operations/) uses:

> `x ^ y` | power | raises `x` to the `y`th power

(note that there are spaces!) and notes that:

> (By convention, we tend to space operators more tightly if they get applied before other nearby operators. For instance, we would generally write `-x + 2` to reflect that first `x` gets negated, and then `2` is added to that result.)

As for style guides, the most detailed one seems [BlueStyle](https://github.com/invenia/BlueStyle). It says:

> Surround most binary operators with a single space on either side: assignment (`=`), [updating operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Updating-operators-1) (`+=`, `-=`, etc.), [numeric comparisons operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Numeric-Comparisons-1) (`==`, `<`, `>`, `!=`, etc.), [lambda operator](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1) (`->`). Binary operators that may be excluded from this guideline include: the [range operator](https://docs.julialang.org/en/v1/base/math/#Base.::) (`:`), [rational operator](https://docs.julialang.org/en/v1/base/math/#Base.://) (`//`), [exponentiation operator](https://docs.julialang.org/en/v1/base/math/#Base.:%5E-Tuple%7BNumber,Number%7D) (`^`), [optional arguments/keywords](https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments-1) (e.g. `f(x=1; y=2)`).

but note the “may”. And in my opinion this falls under another specification:

> - Use whitespace to make the code more readable.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 7, 2023, 7:08am UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/12 "2023-07-07T07:08:15Z")

</div>

> [@fph](#):
>
> > `x ^ y` | power | raises `x` to the `y`th power

This is just to highlight the operation itself.

> [@fph](#):
>
> (By convention, we tend to space operators more tightly if they get applied before other nearby operators. For instance, we would generally write `-x + 2` to reflect that first `x` gets negated, and then `2` is added to that result.)

The above supports my view. I find `-x + 2` to be the analogue of `2 + x^2`, while `2 + x ^ 2` would be similar to `- x + 2`.

> [@fph](#):
>
> Use whitespace to make the code more readable.

This is why I _don’t_ use spaces around `^`, it helps readability.

Ultimately, it’s a matter of taste, which is why it’s unfortunate that `.^` parsing prevents that, and also causes inconsistent formatting. But I would be in favor of removing `1.` and `.5` on its own terms.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [July 7, 2023, 2:34pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/13 "2023-07-07T14:34:13Z")

</div>

> [@DNF](#):
>
> The above supports my view. I find `-x + 2` to be the analogue of `2 + x^2`, while `2 + x ^ 2` would be similar to `- x + 2`.

OK, I agree. Why do you disagree with `10 .^ (1:3)` then? Isn’t this another instance of the same principle? More space around the operator that is applied later.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 7, 2023, 3:36pm UTC](https://discourse.julialang.org/t/power-as-vector-rather-than-a-value/101207/14 "2023-07-07T15:36:33Z")

</div>

Here you use parentheses to denote order, I’m not following your reasoning. And `^` anyway has higher presedence than `:`

This is about convention, and personal preference. I never use space around `^` (and this is also the common convention), having to use it around `.^` is jarring.

In fact, personally, I don’t even think of `^` as an operator, `^2` is an operator to me. In mathematics you would write x^2, and `^2` is ascii art for superscript, which gives you an idea why exponentiation has different anesthetics than + or \*.

In the end it’s a personal choice, and I have made mine and want to stick with it.
