# Save memory of a translational symmetric matrix

**URL:** https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433
**Category:** Performance
**Tags:** question
**Created:** [July 17, 2018, 12:04pm UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433 "2018-07-17T12:04:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [July 17, 2018, 12:04pm UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433/1 "2018-07-17T12:04:14Z")

</div>

I have a mult-dimension matrix (or perhaps tensor more appropriately), for example:  
`a = rand(5, 5, 5, 5)`. It satisfies translational symmetry, i.e. `a[i, j, k, l] = a[i - k, j, l]`.  
In such case, how could I save the memory for constructing this? I would use it for some tensor contraction. Thank you!

---

<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: [July 17, 2018, 1:14pm UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433/2 "2018-07-17T13:14:09Z")

</div>

You could create a custom `<: AbstractArray` type that calculates the required elements on demand, just storing a single one from each equivalence class. Just implement the [Array interface](https://docs.julialang.org/en/latest/manual/interfaces/#man-interface-array-1) (link is to latest, if you are using v0.6 change accordingly).

---

<div class="post-metadata">

### Author: ![shipengcheng1230](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shipengcheng1230/32/4089_2.png) [@shipengcheng1230](https://discourse.julialang.org/u/shipengcheng1230)
#### Post date: [July 19, 2018, 6:20pm UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433/3 "2018-07-19T18:20:13Z")

</div>

Following your suggestion, I am trying something like this:

```julia
struct TransSymmetric{T, N} <: AbstractArray{T, N}
    data::AbstractArray{T, N-1}
    dims::NTuple{N, Int}
end

```

Since the underlying data is one dim less than what it appears, I intend to try `N-1` in the type parameters, which is wrong, however. Then I tried:

```julia
struct TT1{T, N} <: AbstractArray{T, N}
    data::AbstractArray{T, typeof(TT1).parameters[2]-1}
    dims::NTuple{N, Int}
end

```

This shows that `BoundsError: attempt to access svec()`. I assume the struct hasn’t been initiated before accessing its type parameters? My question is how can I restrict the dim of `data` inside?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 19, 2018, 8:47pm UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433/4 "2018-07-19T20:47:37Z")

</div>

This isn’t something you can do, unfortunately. For a workaround, see [templates - Expressions depending on integer type parameters in type definitions in Julia are not allowed - Stack Overflow](https://stackoverflow.com/a/34075704) and [Arithmetic on integer type parameters](https://discourse.julialang.org/t/arithmetic-on-integer-type-parameters/12401)

---

<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: [July 20, 2018, 4:57am UTC](https://discourse.julialang.org/t/save-memory-of-a-translational-symmetric-matrix/12433/5 "2018-07-20T04:57:36Z")

</div>

> [@shipengcheng1230](#):
>
> how can I restrict the dim of `data` inside?

Use two independent type parameters, and have an [inner constructor](https://docs.julialang.org/en/latest/manual/constructors/#Inner-Constructor-Methods-1) check them.
