# How to sum elements raised to a power

**URL:** <https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013>\
**Category:** General Usage\
**Created:** [February 24, 2022, 9:25am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013 "2022-02-24T09:25:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ovt](https://avatars.discourse-cdn.com/v4/letter/o/87869e/32.png) [@ovt](https://discourse.julialang.org/u/ovt)\
**Post date:** [February 24, 2022, 9:25am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/1 "2022-02-24T09:25:20Z")

</div>

Let’s assume that `X` is a matrix. I want to efficiently compute `sum(X.^2)` but without computing `X.^2` which will allocate some memory for nothing. So I’m trying to do something like `sum(^(2),X)` which throws a method error. I thought this could work because `sum(>(0.2),X)` works for instance.  
I know that I could easily create an auxillary function `f(x)=x^2` and then compute `sum(f,X)`. But it’s not the best in terms of readability. So my question is, is there a way to properly use `^(x, y)` to achieve my purpose?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 24, 2022, 9:46am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/2 "2022-02-24T09:46:32Z")

</div>

Use an anonymous function.

```julia
sum(x -> x^2, X)

```

---

<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:** [February 24, 2022, 12:39pm UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/3 "2022-02-24T12:39:48Z")

</div>

You could also do

```julia
sum(abs2, X)

```

which will handle both real- and complex-valued matrices, and should have the same performance as `x->x^2`.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 24, 2022, 1:19pm UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/4 "2022-02-24T13:19:12Z")

</div>

I use abs2 simply for ease in this type of situation

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 24, 2022, 1:31pm UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/5 "2022-02-24T13:31:32Z")

</div>

You can have a faster sum (not as safe as the default sum, which takes some care to not overflow things), using a parallel sum with `@tturbo`:

```julia
julia> function mysum(f,x)
           s = zero(eltype(f(x[begin])))
           @tturbo for i in eachindex(x)
               s += f(x[i])
           end
           return s
       end
mysum (generic function with 2 methods)

julia> x = rand(1000,1000);

julia> @btime mysum(abs2,$x) # 4 threads (julia -t4)
  83.500 μs (0 allocations: 0 bytes)
333115.0216837497

julia> #vs
       @btime sum(abs2,$x)
  327.982 μs (0 allocations: 0 bytes)
333115.0216837495

```

If that is within a peformance critical part of the code and the numbers are well behaved, it is a good alternative.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [May 4, 2022, 1:43am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/6 "2022-05-04T01:43:07Z")

</div>

While the `abs2` solution is good enough for this case, I think generalizing the `^(n)` would be a nice addition to Julia, the same as we do `sqrt`.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 4, 2022, 6:15am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/7 "2022-05-04T06:15:04Z")

</div>

Note that `abs2` is actually different from `x -> x^2` for complex numbers.

```julia
julia> X = rand(ComplexF64, 2, 2)
2×2 Matrix{ComplexF64}:
 0.681033+0.323086im 0.756258+0.454746im
 0.704345+0.603132im 0.0489073+0.595638im

julia> sum(X.^2)
0.5044940375620476 + 2.035762251252853im

julia> sum(abs2, X)
2.563956079161798

julia> sum(x -> x^2, X)
0.5044940375620476 + 2.035762251252853im

```

---

<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:** [May 4, 2022, 6:39am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/8 "2022-05-04T06:39:48Z")

</div>

Yeah, that’s sort of what I meant by saying that it also handles complex values. But if the OP actually _wants_ to sum squared complex values, my suggestion won’t fly.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 4, 2022, 11:45am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/9 "2022-05-04T11:45:18Z")

</div>

what about `LinearAlgerbra.norm`?

---

<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:** [May 4, 2022, 11:57am UTC](https://discourse.julialang.org/t/how-to-sum-elements-raised-to-a-power/77013/10 "2022-05-04T11:57:30Z")

</div>

> [@lmiq](#):
>
> `s = zero(eltype(f(x[begin])))`

I don’t think you need `eltype` here, btw.
