# At dot of function of five arguments against five vectors to five dimensional array

**URL:** https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146
**Category:** New to Julia
**Tags:** broadcast, arrays
**Created:** [May 11, 2024, 5:32pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146 "2024-05-11T17:32:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![PeterSP](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PeterSP](https://discourse.julialang.org/u/PeterSP)
#### Post date: [May 11, 2024, 5:32pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/1 "2024-05-11T17:32:52Z")

</div>

I’ve seen many examples that generate a two dimensional array (matrix) from a function of two arguments against two vectors:

```julia
x = 1:100
y = 1:100
f(x,y) = x*y
z = @. f(x', y)

```

I want to do something analagous, but five-dimensional:

```julia
v = 1:100
w = 1:100
x = 1:100
y = 1:100
z = 1:100
g(v,w,x,y,z) = v*w*x*y*z
u = @. g(reshape(v,(1,1,1,1,100)),reshape(w,(1,1,1,100,1)),reshape(x,(1,1,100,1,1)),reshape(y,(1,100,1,1,1,1)),reshape(z,(100,1,1,1,1)))

```

But when I try to do the latter I get

```julia
ERROR: DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 100 and 5

```

(I also tried nonsense like `v''''` but that just results in `v` again because it limits itself to 2 dimensions)

Is there a good way to do what I’m trying to do?

---

<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: [May 11, 2024, 5:38pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/2 "2024-05-11T17:38:05Z")

</div>

`@.` is too powerful for what you want to do, it fails to broadcast the reshape calls. Try

```julia
u = g.(reshape(v,(1,1,1,1,100)),reshape(w,(1,1,1,100,1)),reshape(x,(1,1,100,1,1)),reshape(y,(1,100,1,1,1,1)),reshape(z,(100,1,1,1,1)))

```

instead.

---

<div class="post-metadata">

### Author: ![PeterSP](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@PeterSP](https://discourse.julialang.org/u/PeterSP)
#### Post date: [May 11, 2024, 5:39pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/3 "2024-05-11T17:39:48Z")

</div>

> [@GunnarFarneback](#):
>
> `u = g.(reshape(v,(1,1,1,1,100)),reshape(w,(1,1,1,100,1)),reshape(x,(1,1,100,1,1)),reshape(y,(1,100,1,1,1,1)),reshape(z,(100,1,1,1,1)))`

That gives me an out of memory error, which is promising! Thanks! (Same as I’d get for `collect(1:100^5)` which makes sense)

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [May 11, 2024, 7:55pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/4 "2024-05-11T19:55:13Z")

</div>

Perhaps adding a utility function can make the expression simpler. Here is an example:

```julia
alongdim(v,d,D) = reshape(v,ntuple(t->t==d ? D : 1))
alongdim(v,d) = alongdim(v,d,d)

```

Using `alongdim` in this context:

```julia
julia> u = (sin∘prod∘vcat).(alongdim.(Ref(1:2),1:4)...)
2×2×2×2 Array{Float64, 4}:
[:, :, 1, 1] =
 0.841471 0.909297
 0.909297 -0.756802

[:, :, 2, 1] =
  0.909297 -0.756802
 -0.756802 0.989358

[:, :, 1, 2] =
  0.909297 -0.756802
 -0.756802 0.989358

[:, :, 2, 2] =
 -0.756802 0.989358
  0.989358 -0.287903

```

`alongdim(v,d,D)` would return an array of dimension `D` with the vector `v` laid out a long dimension `d`.

```julia
u = g.(reshape(v,(1,1,1,1,100)),reshape(w,(1,1,1,100,1)),reshape(x,(1,1,100,1,1)),reshape(y,(1,100,1,1,1,1)),reshape(z,(100,1,1,1,1)))

```

can be rewritten as:

```julia
u = g.(
  alongdim(v,5,5),
  alongdim(w,4,5),
  alongdim(x,3,5), 
  alongdim(y,2,5), 
  alongdim(z,1,5)
)

```

or even shorter:

```julia
u = g.(alongdim.((v,w,x,y,z),5:-1:1,5)...)

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 11, 2024, 8:15pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/5 "2024-05-11T20:15:00Z")

</div>

```julia
Base.splat(g).(Iterators.product(v,w,x,y,z))

```

```julia
g.([v;],[w...;;],[x...;;;],[y...;;;;],[z...;;;;;])

```

```julia

a=[v,w,x,y,z]

args=ntuple(i->cat(a[i]...,dims=i),5)

g.(args...)

```

```julia
nn=ntuple(i->ntuple(j->j==i ? (:) : 1,5),5)
g.(reshape.(a,nn)...)

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [May 11, 2024, 10:00pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/6 "2024-05-11T22:00:49Z")

</div>

```julia
u = [g(V, W, X, Y, Z) for Z in z, Y in y, X in x, W in w, V in v]

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 12, 2024, 6:13pm UTC](https://discourse.julialang.org/t/at-dot-of-function-of-five-arguments-against-five-vectors-to-five-dimensional-array/114146/7 "2024-05-12T18:13:25Z")

</div>

Writing this using TensorCast.jl is compact and has the merit of clarity in tracking indices:

```julia
using TensorCast
@cast u[i,j,k,l,m] := g(v[i], w[j], x[k], y[l], z[m])

```
