# What is the correct way to iterate over "Kroneker Product" like terms?

**URL:** <https://discourse.julialang.org/t/what-is-the-correct-way-to-iterate-over-kroneker-product-like-terms/34363>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 9, 2020, 2:02am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-iterate-over-kroneker-product-like-terms/34363 "2020-02-09T02:02:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [February 9, 2020, 2:02am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-iterate-over-kroneker-product-like-terms/34363/1 "2020-02-09T02:02:17Z")

</div>

I would like to write functions that iterate over objects that have a “Kroneker Product” like structure.

Consider for example:

```julia
using StaticArrays

struct MyKronekerType{D,N,T}
    data::SVector{T,N}
    function MyKronekerType(D::Int, data::SVector{T,N}) where {T,N}
        @assert D > 0
        new{D,N,T}(data)
    end
end

function Base.getindex(m::MyKronekerType{1}, i)
    return m.data[i]
end

function Base.getindex(m::MyKronekerType{2}, i, j)
    return m.data[i], m.data[j]
end

data = @SVector [1,2,3]
m1 = MyKronekerType(1, data)
m2 = MyKronekerType(2, data)

println("m1[1] = ", m1[1]) # 1
println("m2[1,2] = ", m2[1,2]) # (1,2)

```

So `MyKronekerType{D}` represents a “D-dimensional” version of its `data` field. I wish to now iterate over all the entries of `m::MyKronekerType{D,N,T}`. For example, I wish to write code like:

```julia
m2 = MyKronekerType(2, data)
for d in m2
    # d is m2[1,1], m2[2,1], ... m2[3,3]
end

```

What is the best way to do this? I figured I can explicitly convert a linear index into a “D-dimensional” index, but I was hoping there is a more elegant way to do this. I thought maybe `CartesianIndices` might be what I require here but I don’t quite know how to use it for this purpose.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [February 9, 2020, 5:29pm UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-iterate-over-kroneker-product-like-terms/34363/2 "2020-02-09T17:29:49Z")

</div>

You can achieve this by defining `Base.iterate` for your type.

```
Base.iterate(d::MyKronekerType{D}) where {D} = iterate(Iterators.product((d.data for i=1:D)...))
Base.iterate(d::MyKronekerType{D},state) where {D} = iterate(Iterators.product((d.data for i=1:D)...),state)

```

With these defined you obtain

```
julia> for d in m2
   println(d)
   end
(1, 1)
(2, 1)
(3, 1)
(1, 2)
(2, 2)
(3, 2)
(1, 3)
(2, 3)
(3, 3)

```

---

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [February 9, 2020, 5:48pm UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-iterate-over-kroneker-product-like-terms/34363/3 "2020-02-09T17:48:29Z")

</div>

This is perfect! Thanks!
