10
$\begingroup$

I came across the difficulty of understanding the logic of interaction of the fourth and fifth arguments of Partition.

Here is an example:

l = Range[7];
p1 = Partition[l, 2, 1, {-1, 1}, {a}]
p2 = Partition[l, 2, 1, {-1, 1}, {a, b}]
p3 = Partition[l, 2, 1, {-1, 1}, {a, b, c}]
p4 = Partition[l, 2, 1, {-1, 1}, {a, b, c, d}]
{{a, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, a}}

{{b, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, b}}

{{c, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, b}}

{{d, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {7, d}}

What is the logic behind padding the list from the right? Why both in p2 and p3 the last element is b? Why at the same time in p4 the last element is d?

Similarly, please explain the following:

p5 = Partition[l, 3, 1, {-1, 1}, {a, b}]
p6 = Partition[l, 3, 1, {-1, 1}, {a, b, c}]
p7 = Partition[l, 3, 1, {-1, 1}, {a, b, c, d}]
p8 = Partition[l, 3, 1, {-1, 1}, {a, b, c, d, e}]
{{a, b, 1}, {b, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, b}, {7, b, a}}

{{b, c, 1}, {c, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, b}, {7, b, c}}

{{c, d, 1}, {d, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, d}, {7, d, a}}

{{d, e, 1}, {e, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, c}, {7, c, d}}

And also this:

p9  = Partition[l, 3, 1, {2, 2}, {a, b}]
p10 = Partition[l, 3, 1, {2, 2}, {a, b, c}]
p11 = Partition[l, 3, 1, {2, 2}, {a, b, c, d}]
p12 = Partition[l, 3, 1, {2, 2}, {a, b, c, d, e}]
{{b, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, b}}

{{c, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, b}}

{{d, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, d}}

{{e, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, c}}

Is it possible to make Partition[l, 3, 1, {2, 2}, ?????] returning {{a, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, b}}?

$\endgroup$
3
  • 3
    $\begingroup$ With regards to the last part of your question, this works too: Partition[l, 3, 1, {2, 2}, {"", b, a}] $\endgroup$
    – M.R.
    Sep 2, 2021 at 2:49
  • $\begingroup$ @M.R. It doesn't work if l = Range[8];. $\endgroup$ Sep 2, 2021 at 5:00
  • $\begingroup$ Sure, Partition[Range@n,3,1,2,Join[Table[1,Floor[(n-1)/2]-1],{b,b,a}]]. This is about the same as @MichaelE2 answer. $\endgroup$
    – M.R.
    Sep 2, 2021 at 17:28

1 Answer 1

9
$\begingroup$

I think these explain it, if one thinks about the elements that extend beyond limits of Range[7]:

{Range[-1, 7 + 1],
  Flatten@Table[{a, b}, 5]} // Grid

{Range[-2, 7 + 2],
  Flatten@Table[{a, b, c}, 4]} // Grid

{Range[-3, 7 + 3],
  Flatten@Table[{a, b, c, d}, 3]} // Grid

The following gives the desired outcome:

Partition[l, 3, 1, {2, 2}, 
 Append[ConstantArray[b, 1 + Length@l], a]]

A drawing that tries to highlight what I intended people to see in the second example. The 3,1 partition of overlapping sublists is a bit complicated to visualize, at least in my mind.

enter image description here

$\endgroup$
5
  • $\begingroup$ So, what is the algorithm? When the padlist is of length n, we prepend it before, and then add as many copies, as needed? How does it conform to the Documentation statement: "Partition[list,{n[1],n[2],...,n[r]},klist,padlist] effectively makes a depth-r array of copies of padlist, then superimposes list on them and partitions the result."? $\endgroup$ Sep 2, 2021 at 8:44
  • $\begingroup$ @AlexeyPopkov The 3,1-partitioning of array l in row 1 is padded according to the 3,1-partitioning of the a,b,c... array in row 2, which is a depth-1 array of copies of {a,b,c,...} (perhaps Join[##, r] & @@ Table[{a, b, c}, 4] with r = 1 makes that clearer than Flatten@Table[...]). $\endgroup$
    – Michael E2
    Sep 2, 2021 at 11:38
  • $\begingroup$ I mean, the algorithm is that the array of copies of padlist is anchored from the left at the first element of the original list, and then padded from the left with padlist. The main point here is that the array of copies of padlist is anchored at (aligned by) the first element of the original list. It makes things clear, but I must say that the name padlist used in the documentation is wrong because this behavior has nothing common with usual padding performed by such functions as ArrayPad etc. $\endgroup$ Sep 2, 2021 at 12:40
  • $\begingroup$ @AlexeyPopkov I added a drawing to help explain. I think you already understood what I meant, though. I suppose there's some application in which the way padlist is used is convenient, but I found it confusing the first time I tried to use it. $\endgroup$
    – Michael E2
    Sep 2, 2021 at 14:23
  • $\begingroup$ Thanks for the illustration. It is sad that the Documentation doesn't provide an example illustrating this logic... I still can't imagine a real-world application of such behavior. $\endgroup$ Sep 2, 2021 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.