Search code examples
listnested-listsmaple

In maple, how to create a sequence of alternating variables?


I'm quite new to Maple and I would like to create the following list:

U__N := u__1[0], u__2[0], u__1[1], u__2[1], u__1[2], u__2[2], u__1[3], u__2[3], u__1[4], u__2[4], u__1[5], u__2[5]

I came up with the following two options. For both I lack the knowledge for the last step

Option 1

U__N := seq([u__1[k], u__2[k]], k = 0 .. 5)

which gives me a nested list: U__N := [u__1[0], u__2[0]], [u__1[1], u__2[1]], [u__1[2], u__2[2]], [u__1[3], u__2[3]], [u__1[4], u__2[4]], [u__1[5], u__2[5]]. However, now I do not know how to "un-nest" the nested list.

Option 2:

Create two separate lists

U__N2 := seq(u__2[k], k = 0 .. 5 - 1)

which returns U__N1 := u__1[0], u__1[1], u__1[2], u__1[3], u__1[4]

U__N2 := seq(u__2[k], k = 0 .. 5 - 1)

which returns U__N2 := u__2[0], u__2[1], u__2[2], u__2[3], u__2[4]. Now I would like to concatenate/combine these two lists alternatively.

Do you have any suggestions for one of these two options or an alternative solution?


Solution

  • I would prefer to create the pair-wise portions and then utilize those directly, than to form the whole list-of-lists and Flatten it.

    The special-evaluation rules of the seq command allows for its first argument to not be evaluated until after k attains concrete numeric values.

    This allow you to adjust your first method and extract the operands of the pair-wise inner lists, using the op command.

    seq(op([u__1[k], u__2[k]]), k = 0 .. 5);
    
       u__1[0], u__2[0], u__1[1], u__2[1],
       u__1[2], u__2[2], u__1[3], u__2[3],
       u__1[4], u__2[4], u__1[5], u__2[5]
    
    seq([u__1[k], u__2[k]][], k = 0 .. 5);
    
       u__1[0], u__2[0], u__1[1], u__2[1],
       u__1[2], u__2[2], u__1[3], u__2[3],
       u__1[4], u__2[4], u__1[5], u__2[5]
    

    The trailing [] in [...][] acts like op([...]).