I need to write a Python function that when passed an array, and an integer N, returns the contents of the array divided into N sub-arrays of equal size.
If the length
For clarity, I'll refer to this:
Neach_section, extras = divmod(Ntotal, Nsections)
section_sizes = ([0] +
extras * [Neach_section+1] +
(Nsections-extras) * [Neach_section])
as
quotient, remainder = divmod(Ntotal, Nsections)
section_sizes = ([0] +
remainder * [quotient+1] +
(Nsections- remainder) * [quotient])
First lets imagine a similar case to the one shown in your question. (Modified for Quotient != remainder)
print(np.array_split(np.arange(1,15),4)
>>>[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([ 9, 10, 11]), array([12, 13, 14])]
Its easier to think it in terms of the division that this ultimately represents.
14 = 4*3 + 2
Which is also the same as
14 = (3 + 3 + 3 + 3) + 2
= (3 + 3 + 3 + 3) + (1 + 1)
And critically we can add those ones to the first two terms in the first bracket.
14 = 4 + 4 + 3 + 3
In general what we've done is we're adding one to the first (Remainder) terms of the output list leaving us with the snippet of code
...remainder * [quotient+1]...
Out of the (Quotient) terms in the output we have added the first (remainder) terms leaving us with the next (quotient-remainder) terms to fill
...(Nsections- remainder) * [quotient])
Leaving us with the final code.
Could someone even just tell me what this kind of operation is called or what branch of mathematics this deals with as it's not something I've come across before.
I believe this is loosely related to number theory and the quotient-remainder theorem is probably one of the first things you learn for it.
Anyway I hope that helped :)