# Splatting a vector into hypot

**URL:** <https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049>\
**Category:** General Usage\
**Tags:** numbers, numerics\
**Created:** [November 6, 2021, 2:05pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049 "2021-11-06T14:05:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tictaccat](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tictaccat](https://discourse.julialang.org/u/tictaccat)\
**Post date:** [November 6, 2021, 2:05pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049/1 "2021-11-06T14:05:12Z")

</div>

The following code takes an extremely long time to compile on my computer:

```julia
x = randn(10000);
hypot(x...) # 220.104289 seconds (66.32 M allocations: 5.422 GiB, 0.88% gc time, 100.00% compilation time)
hypot(x...) # 0.001436 seconds (60.03 k allocations: 2.290 MiB)

```

I’d imagine that splatting such a large vector causes issues. I’d like to clarify: is this intended behaviour? Is the underlying algorithm for `hypot` only a good idea for small vectors regardless, or does the fact that we have to splat prevent the use of `hypot` on larger vectors even though it would be a good idea to do so otherwise?

If any case, perhaps some clarification should be added to the [Julia docs](https://docs.julialang.org/en/v1/base/math/#Base.Math.hypot), because as a new Julia user looking for a way to find the L\_2 norm of a vector, I simply saw `hypot(x...)` and ran with it, which led me to code with a very long compilation time, that crashed for very large vectors.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 6, 2021, 3:54pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049/2 "2021-11-06T15:54:20Z")

</div>

This is not only a problem of `hypot` but a more general one. (Don’t) try this one:

```julia
x = randn(10000)
y = (x...,)

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 6, 2021, 3:59pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049/3 "2021-11-06T15:59:33Z")

</div>

Seems like type inference for `NTuple{10000, Float64}` is a tough call.

Edit: it is not only type inference, but compilation itself. It seems we are generating specializations for `n in 1:10000`. Issue: [https://github.com/JuliaLang/julia/issues/35619](https://github.com/JuliaLang/julia/issues/35619)

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 6, 2021, 4:02pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049/4 "2021-11-06T16:02:54Z")

</div>

From base/math.jl:

```julia
hypot(x::Number, y::Number, xs::Number...) = _hypot(float.(promote(x, y, xs...))...)

```

The problematic part here seems to be `float.(...)`  
But you can work around with:

```julia
julia> Base.Math._hypot(x...)
100.39290077763592

```

when you know that x is `Array{Float}`.  
But it’s probably not recommended.

I think it’s worth to open an issue.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [November 6, 2021, 5:00pm UTC](https://discourse.julialang.org/t/splatting-a-vector-into-hypot/71049/5 "2021-11-06T17:00:30Z")

</div>

You might be looking for `LinearAlgebra.norm`. Which arguably should be linked from the help.

> Is the underlying algorithm for `hypot` only a good idea for small vectors

For more than two arguments, it isn’t doing anything very exotic: [https://github.com/JuliaLang/julia/blob/1389c2fc4af952f5c8b9759cf6fe633995b523f9/base/math.jl#L708-L717](https://github.com/JuliaLang/julia/blob/1389c2fc4af952f5c8b9759cf6fe633995b523f9/base/math.jl#L708-L717)
