Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Potential duplicate, but doesn't seem to solve my problem: Using patterns in pure functions

I have a function that returns an InterpolatingFunction, and it works exactly the way I needed for passing to NIntegrate etc.: if I pass on a symbolic argument, the function returns itself; if the argument is numeric, it evaluates and gives a number.

func := Module[{...}, ... (y /. First@NDSolve[{y[1] == 1, y'[x] == x}, y, {x, -1, 1}])]
func[x] (* gives InterpolatingFunction[(-1. 1.),<>](x) *)
func[0.5] (* gives 0.625 *) 

I need a slightly modified behavior: the returned function should return 0 if outside the range instead of attempting to interpolate. Seems easy enough:

func := Module[{interpolation,...}, ...
        interpolation = (y /. First@NDSolve[{y[1] == 1, y'[x] == x}, y, {x, -1, 1}]);
        Function[x,If[Not@IntervalMemberQ[Interval[interpolation[[1, 1]]], x], 0, 
func[0.5] (* gives 0.625 *) 
func[2] (* gives 0 since it's outside the interpolation interval *)
func[x] (* gives 0!! *)

How can one mimic the behavior of InterpolatingFunction such that func[x] for non-numeric x gives something like Function[<>](x) instead?

share|improve this question
From the documentation of Function: "Function][params, body, {attr_1, attr_2, ...}] represents a pure function that is to be treated as having attributes attr_i for the purpose of evaluation". – m_goldberg May 20 '14 at 20:36

This is not neat at all but it gives the bahaviour you need:

func := Module[{interpolation},
  interpolation = (y /. First@NDSolve[{y[1] == 1, y'[x] == x}, y, {x, -1, 1}]);
      ! NumericQ[x], interpolation[x], 
      TrueQ[LessEqual[#, x, #2] & @@ interpolation[[1, 1]]], interpolation[x],
      True, 0]]]

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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