Create vector with dependent values

Hello,

I am new to Julia and I have to create a vector which values are dependent. Let me put an example:

If I have MyStruct(22, [0, 0, 0], 0) , the next values can be MyStruct(21, [0, 0, 1], 0) , MyStruct(21, [0, 1, 0], 0) , MyStruct(21, [1, 0, 0], 0) , MyStruct(20, [0, 1, 1], 0) , MyStruct(20, [1, 0, 1], 0) … That is b::SVector{3, Int64} increases as a::Int decreases.

I tried to solve it by doing:

struct MyStruct
    a::Int 
    b::SVector{3, Int64} 
    c::Int 
end 


V1 = [MyStruct(a,(b),0) for a in 0:15 for b in [Iterators.product(ntuple(_ -> 0:10, 3)...)...]] #Terminal states remaining time 0

But with this I get impossible things as MyStruct(22, [10, 10, 10], 0) .

Can you help me? Thanks :slight_smile:

Hello @rafael.guerra,

Consider that from a::Int you moove to b::SVector{3, Int64} . That’s what I mean with:

In your example they are increasing.

If I understand your goal correctly, you almost had it. Something like this?

julia> mystructgenerator(amin, amax) = 
  ( MyStruct(a, b, 0) for a in amax:-1:amin
      for b in Iterators.product(0:amax-a,0:amax-a,0:amax-a)
        if a+sum(b)==amax
  )
mystructgenerator (generic function with 1 method)

julia> gen = mystructgenerator(18, 20)
Base.Iterators.Flatten{Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}}(Base.Generator{StepRange{Int64, Int64}, var"#39#40"{Int64}}(var"#39#40"{Int64}(20), 20:-1:18))

julia> collect(gen)
10-element Vector{MyStruct}:
 MyStruct(20, [0, 0, 0], 0)
 MyStruct(19, [1, 0, 0], 0)
 MyStruct(19, [0, 1, 0], 0)
 MyStruct(19, [0, 0, 1], 0)
 MyStruct(18, [2, 0, 0], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 2, 0], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [0, 0, 2], 0)

I tend to agree with this comment Sum elements of a struct - #8 by stevengj, that you may not be using the right data structure for your problem. It’s hard to tell, because you haven’t explained what it is you are ultimately trying to achieve. You have been asking a few questions around MyStruct and how to extend various methods for it. Yet, I for one fail to see where you are going with this. The operations you are defining for this type seem quite artificial to me.

Anyway, hope this helps and please feel free to add any details.

2 Likes

Hello and thank you so much for your help, it works perfectly but I have realized that I was wrong. In your example:

 MyStruct(20, [0, 0, 0], 0)
 MyStruct(19, [1, 0, 0], 0)
 MyStruct(19, [0, 1, 0], 0)
 MyStruct(19, [0, 0, 1], 0)
 MyStruct(18, [2, 0, 0], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 2, 0], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [0, 0, 2], 0)
.........

The sum of a and the 3 elements in b can`t be greater than the first value of a. I mean:

 MyStruct(18, [0, 0, 0], 0)
 MyStruct(18, [0, 0, 1], 0)
 MyStruct(18, [0, 1, 0], 0)
 MyStruct(18, [1, 0, 0], 0)
 MyStruct(18, [0, 1, 1], 0)
 MyStruct(18, [1, 0, 1], 0)
 MyStruct(18, [1, 1, 0], 0)
 MyStruct(18, [0, 0, 2], 0)
.........

You only need to alter the if statement in the generator expression slightly to make that happen.

1 Like