For information, this is the function I use for periodic (circularly) padding (it could be use to write and debug new code):
array paddingArrayCircularly(const array& arrayToPad,
bool isPaddingDim0, bool isPaddingDim1, bool isPaddingDim2, bool isPaddingDim3)
{
int dim0 = arrayToPad.dims(0);
int dim1 = arrayToPad.dims(1);
int dim2 = arrayToPad.dims(2);
int dim3 = arrayToPad.dims(3);
array id0 = isPaddingDim0 ? join(0, seq(dim0 - 1, dim0 - 1), seq(0, dim0 - 1), seq(0, 0)) : seq(0, dim0 - 1);
array id1 = isPaddingDim1 ? join(0, seq(dim1 - 1, dim1 - 1), seq(0, dim1 - 1), seq(0, 0)) : seq(0, dim1 - 1);
array id2 = isPaddingDim2 ? join(0, seq(dim2 - 1, dim2 - 1), seq(0, dim2 - 1), seq(0, 0)) : seq(0, dim2 - 1);
array id3 = isPaddingDim3 ? join(0, seq(dim3 - 1, dim3 - 1), seq(0, dim3 - 1), seq(0, 0)) : seq(0, dim3 - 1);
return arrayToPad(id0, id1, id2, id3);
}
I'm pretty sure that whatever you come up with will be faster and will consume less memory. As it stand right now, This function use approximately twice the memory inside the array "arrayToPad" to work.