# \`logsumexp\` using Accessors?

**URL:** <https://discourse.julialang.org/t/logsumexp-using-accessors/134835>\
**Category:** General Usage\
**Created:** [January 2, 2026, 1:13am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835 "2026-01-02T01:13:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 2, 2026, 1:13am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/1 "2026-01-02T01:13:38Z")

</div>

Is it possible to do `logsumexp` in a cool way using Accessors?

```julia-auto
using Accessors
julia> @modify(log, 1:1.0:9 |> sum(_[∗]))
ERROR: This should be unreachable. You probably need to overload
`Accessors.set(obj::Float64, ::typeof(sum), val::Float64)`

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [January 3, 2026, 11:52am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/2 "2026-01-03T11:52:39Z")

</div>

As far as I know, the point of Accessors.jl is to update parts of objects. I.e. the output of a `modify` should be something similar to the input (in this case some `AbstractArray`). A reduction (like `sum`) does not fit into this picture.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 3, 2026, 2:20pm UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/3 "2026-01-03T14:20:54Z")

</div>

I concur with @eldee, I don’t understand what you are trying to do.

Also, note that `LogExpFunctions.logsumexp` is not simply `log(sum(exp.(input)))`, under the hood it ensures numerical stability and handles various corner cases and magnitude discrepancies.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 4, 2026, 3:59am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/4 "2026-01-04T03:59:59Z")

</div>

As for why I want this in general—there are a lot of common patterns that do work under a certain transformation, and I would like a way to reify that intent (`x |> g⁻¹ ∘ f ∘ g`) concisely, such as with a binary operator expression like `f⨟g`.

It won’t always be optimal – here it requires an awkward `@o exp.(_)` and produces overflow – but I expect often enough it will be a nice and effective reification of intent. I’d at least like to experiment with it.

This is what I was looking for:

```julia-auto
julia> using InverseFunctions, Accessors

julia> f ⨟ g = inverse(g) ∘ f ∘ g;

julia> exps = @o exp.(_);

julia> (1:9) |> sum⨟exps
9.458551727967379

julia> log(sum(exp.(1:9)))
9.458551727967379

```

> [@eldee](#):
>
> the point of [Accessors.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Accessors) is to update parts of objects.

Indeed—In this case the “parts of an object” are elements of the `UnitRange` on the input side and the sole element of a broadcast over a `Float64` on the output side.

> [@Tamas\_Papp](#):
>
> Also, note that `LogExpFunctions.logsumexp` is not simply `log(sum(exp.(input)))`, under the hood it ensures numerical stability and handles various corner cases and magnitude discrepancies.

That’s a good point:

```julia-auto
julia> using LogExpFunctions;
julia> x = 1:1000
       ((sum⨟exps)(x), log(sum(exp.(x))), logsumexp(x))
(Inf, Inf, 1000.4586751453871)

```

Thanks.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [January 4, 2026, 9:02am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/5 "2026-01-04T09:02:28Z")

</div>

I like the `⨟` operator idea, but still fail to see the connection with Accessors.jl. Since you use all elements of the iterable, you could use `Base` methods like `map` or `broadcast`. Now these do seem to fail out of the box with InverseFunctions.jl, but `Broadcast.BroadcastFunction`, which is what `exps` evaluates to, has no issues. Of course, I admit that `@o exp.(_)` is more convenient to type than `Broadcast.BroadcastFunction(exp)` 🙂 . (Though you could always alias the latter to something shorter.)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 4, 2026, 9:41am UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/6 "2026-01-04T09:41:11Z")

</div>

> [@eldee](#):
>
> the connection with [Accessors.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Accessors)

It’s just that I’m viewing logsumexp as a lensed operation. I think of Accessors.jl as the Julia lens library, generalizing its purpose from picking out parts of an object to transforming into an alternate space, working on it, and transforming back.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [January 4, 2026, 1:46pm UTC](https://discourse.julialang.org/t/logsumexp-using-accessors/134835/7 "2026-01-04T13:46:42Z")

</div>

> [@jar1](#):
>
> I think of [Accessors.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Accessors) as the Julia lens library

It is indeed 🙂 Do you have examples of how this operation looks in other languages with lens libraries?

> [@jar1](#):
>
> `f ⨟ g`

FYI, the same symbol is defined in CompositionsBase with a different meaning.
