Skip to main content
Corrected Spelling
Source Link
gwr
  • 13.6k
  • 2
  • 48
  • 82

Maybe asAs an alternative to Szabolcs' answer I would sugest a different approach, whichthat quite literally follows the mathematical defintiondefinition of $G(f)$. Note that we may avoid the premature evaluation of any argument that is not numeric by using Divisible instead of EvenQ or OddQ:

g[ f_Function ] := Function[ x, 
    Piecewise[
        {
          {     f[ FractionalPart @ x ], EvenQ @     Divisible[ Floor[x], 2 ] },
          { 1 / f[ FractionalPart @ x ], OddQNot @ Divisible[ Floor[x], 2 ] }
        },
        Indeterminate (* in all other cases *)
    ]
]

Then weWe may dothen use this for numeric arguments:

f = Function[ x, 2 + Sin[x] ];
h = g[f]; (* or directly g[f] @ x *)
h[ 5/2 ]

$2 + \text{Sin}[\frac{1}{2}]$

In the given form we can now also work symbolically:

h[x] // Head

Piecewise

Maybe as an alternative, which quite literally follows the mathematical defintion of $G(f)$:

g[ f_Function ] := Function[ x, 
    Piecewise[
        {
          { f[ FractionalPart @ x ], EvenQ @ Floor[x] },
          { 1 / f[ FractionalPart @ x ], OddQ @ Floor[x] }
        }
    ]
]

Then we may do:

f = Function[ x, 2 + Sin[x] ];
h = g[f]; (* or directly g[f] @ x *)
h[ 5/2 ]

$2 + \text{Sin}[\frac{1}{2}]$

As an alternative to Szabolcs' answer I would sugest a different approach, that quite literally follows the mathematical definition of $G(f)$. Note that we may avoid the premature evaluation of any argument that is not numeric by using Divisible instead of EvenQ or OddQ:

g[ f_Function ] := Function[ x, 
    Piecewise[
        {
          {     f[ FractionalPart @ x ],       Divisible[ Floor[x], 2 ] },
          { 1 / f[ FractionalPart @ x ], Not @ Divisible[ Floor[x], 2 ] }
        },
        Indeterminate (* in all other cases *)
    ]
]

We may then use this for numeric arguments:

f = Function[ x, 2 + Sin[x] ];
h = g[f]; (* or directly g[f] @ x *)
h[ 5/2 ]

$2 + \text{Sin}[\frac{1}{2}]$

In the given form we can now also work symbolically:

h[x] // Head

Piecewise

Source Link
gwr
  • 13.6k
  • 2
  • 48
  • 82

Maybe as an alternative, which quite literally follows the mathematical defintion of $G(f)$:

g[ f_Function ] := Function[ x, 
    Piecewise[
        {
          { f[ FractionalPart @ x ], EvenQ @ Floor[x] },
          { 1 / f[ FractionalPart @ x ], OddQ @ Floor[x] }
        }
    ]
]

Then we may do:

f = Function[ x, 2 + Sin[x] ];
h = g[f]; (* or directly g[f] @ x *)
h[ 5/2 ]

$2 + \text{Sin}[\frac{1}{2}]$