Skip to main content

Mathematica periodic moving map


I am pleased to have MovingMap in Mathematica 10. But, today, I encountered the problem that needs MovingMap with a periodic boundary condition.


To be specific, Consider the following example.


MovingMap[ f, Range[3], {2} ]

will gives the result {f[{1, 2}], f[{2, 3}], f[{3, 4}]}. However, what I want to get is {f[{1, 2}], f[{2, 3}], f[{3, 4}], f[{4,1}]}.


One of the easiest and straightforward way would be calculating the last one and then join the lists together. But I want to know if there is more elegant way to do it. (I checked the optional arguments of MovingMap but I couldn't find a solution.)



Answer



"Reflected" padding works as desired but "Periodic" padding is missed. There is corresponding definition for "Reflected"


RandomProcesses`TemporalDataUtilitiesDump`toCannonicalPadding[

RandomProcesses`TemporalDataUtilitiesDump`td_, "Reflected",
RandomProcesses`TemporalDataUtilitiesDump`w_,
RandomProcesses`TemporalDataUtilitiesDump`Caller_] :=
Reverse[Rest[
TemporalData`Utilities`TDResample[
RandomProcesses`TemporalDataUtilitiesDump`td,
RandomProcesses`TemporalDataUtilitiesDump`w, {},
RandomProcesses`TemporalDataUtilitiesDump`Caller]["Values"]]]

So we can add a similar definition for "Periodic"



RandomProcesses`TemporalDataUtilitiesDump`toCannonicalPadding[
RandomProcesses`TemporalDataUtilitiesDump`td_, "Periodic",
RandomProcesses`TemporalDataUtilitiesDump`w_,
RandomProcesses`TemporalDataUtilitiesDump`Caller_] :=
TemporalData`Utilities`TDResample[
RandomProcesses`TemporalDataUtilitiesDump`td,
RandomProcesses`TemporalDataUtilitiesDump`w, {},
RandomProcesses`TemporalDataUtilitiesDump`Caller]["Values"]

Verification:



MovingMap[f, Range[3], {2}, "Periodic"]
(* {f[{3, 1}], f[{1, 2}], f[{2, 3}]} *)

Comments