# Is there any way to optimize array additions and multiplications with transposes?

**URL:** <https://discourse.julialang.org/t/is-there-any-way-to-optimize-array-additions-and-multiplications-with-transposes/80268>\
**Category:** Numerics\
**Tags:** tullio, tensors\
**Created:** [April 29, 2022, 6:24pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-optimize-array-additions-and-multiplications-with-transposes/80268 "2022-04-29T18:24:36Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![Geositta](https://avatars.discourse-cdn.com/v4/letter/g/e5b9ba/32.png) [@Geositta](https://discourse.julialang.org/u/Geositta)\
**Post date:** [May 1, 2022, 7:58pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-optimize-array-additions-and-multiplications-with-transposes/80268/16 "2022-05-01T19:58:27Z")

</div>

Sorry there is one more question towards systematic test for all permutations.

Suppose I would like to compute  
` 0.1 A[i,j,k,l] + 0.2 * A[i,j,l,k] + 0.3 * A[i,k,i,j] + 0.4 * A[i,k,j,i] + 0.5 * A[i,l,j,k]...`  
Is there any simple way to permutations, than specifying `[i,k,j,i]` etc (I can try to generate strings as the indices, but it may be a bit complicted)?  
I used [Get all Permutations of Array?](https://discourse.julialang.org/t/get-all-permutations-of-array/48578)

```julia
using BenchmarkTools, Combinatorics

function perms(a) 
    B = reverse(collect(permutations(a)))
    B
end

n=30
A=rand(Float64,(n,n,n,n))
@btime p = perms(A)

```

Got `ERROR: LoadError: OverflowError: 531439031701620000 * 809997 overflowed for type Int64`

And, if there is a prefactor equals zero, e.g., 0.0\* A[i,l,k,j], can I skip the permutation to `i,l,k,j`? The `permutations` will generate all permutations as I understand it (In Python, I can use  
`np.transpose(A,(0,3,2,1))` to specify the given transpose.)

Edited: found `PermutedDimsArray(A, (2,1,3,4))` in `test3` is similar to `Python transpose`

---

_[View the full topic](https://discourse.julialang.org/t/is-there-any-way-to-optimize-array-additions-and-multiplications-with-transposes/80268)._
