Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: defining a function whose parameter must be a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131027] Re: defining a function whose parameter must be a function
  • From: Roman <rschmied at gmail.com>
  • Date: Tue, 4 Jun 2013 02:01:44 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <20130601102829.0E29D6A2D@smc.vnet.net> <koehga$2t2$1@smc.vnet.net>

Thanks a lot to Bob and David for answers!

Bob's first part of test-calling FreeQ[f[Unique[], Unique[]], f] looks like a good idea, but I believe his second part can be improved since it would fail on a function like Function[{x, y}, x] which happens to not depend on one of its arguments. Something like this:

FunctionWithTwoParametersQ[f_] :=
  Quiet[
    Check[FreeQ[f[Unique[], Unique[]], f],
      False,
      {Function::slotn, Function::fpct}],
    {Function::slotn, Function::fpct}]

David's comment on performance is spot-on: test-calling f[Unique[], Unique[]] may incur an unbearable cost if f is slow to execute when called with nonnumeric arguments but fast with numeric arguments. This makes the idea of test-calling the function f[Unique[], Unique[]] a bit unclean since in principle I would only like to find out whether or not I *can* call the function, and not to actually call it and execute it all the way through. I would hope that the Mathematica internals would allow me to do this somehow, i.e.  to test-call f[Unique[], Unique[]] but stop after the pattern-matching step, and not go into the actual function execution. I don't know what I'd have to Hold[] in order to achieve this, though.

In practice I will probably be using something closer to this, which combines Bob's idea and David's warning:

NumericFunctionWithTwoNumericArgumentsQ[f_] :=
  Quiet[NumericQ[f[Random[], Random[]]], {Function::slotn, Function::fpct}]

In a concrete case one might even replace the Random[] calls by zero values if this is guaranteed not to cause problems.

Any thoughts on this would be appreciated.
Cheers!
Roman



Am Sonntag, 2. Juni 2013 06:25:46 UTC+2 schrieb Bob Hanlon:
> f1[x_] = x;
>
> f1[x_, y_] = x^2 - y^2;
>
> f1[x_, y_, z_] = x^2 - y^2 - 3 z;
>
>
>
>
>
> f2 = #1^2 - #2^2 &;
>
> f3 = Function[{x, y}, x^2 - y^2];
>
> f4 = #1 &;
>
> f5 = Function[{x}, x];
>
> f6[a_, b_, c_] = a^2 - b^2 - 3 c;
>
>
>
>
>
> F[f_] := f[2, 3] /;
>
>   FreeQ[f[Unique[], Unique[]], f] &&
>
>    Length[Union[
>
>       Cases[f[Unique[], Unique[]],
>
>        _Symbol?(! NumericQ[#] &),
>
>        Infinity]]] == 2
>
>
>
>
>
> F /@ {f1, f2, f3, f4, f5, f6}
>
>
>
>
>
> {-5, -5, -5, F[#1 &], F[Function[{x}, x]], F[f6]}
>
>
>
>
>
>
>
> Bob Hanlon
>
>
>
>
>
>
>
>
>
> > Dear all,
>
> > I am trying to define a function F which will only execute if its
>
> > parameter is a function with two parameters. Let's say I define it thus,
>
> > without any checks on the parameter pattern f:
>
> >
>
> > In[1] := F[f_] := f[2,3]
>
> >
>
> > There are several ways of calling F:
>
> > 1) pass it a function of two parameters:
>
> > In[2] := F[Function[{a,b},a^2-b^2]]
>
> > Out[2] = -5
>
> >
>
> > 2) pass it an anonymous function of two parameters:
>
> > In[3] := F[#1^2-#2^2 &]
>
> > Out[3] = -5
>
> >
>
> > 3) pass it a pre-defined function:
>
> > In[4] := g[a_,b_] = a^2-b^2;
>
> > In[5] := F[g]
>
> > Out[5] = -5
>
> >
>
> > My question is: how can I define a pattern in the definition of F[f_] such
>
> > that this function F will execute these three cases while not executing if
>
> > called with any other kind of parameter? The following calls should fail,
>
> > for example:
>
> >
>
> > In[6] := F[Function[{a,b,c},a^2-b^2-3c]]
>
> > Out[6] = F[Function[{a,b,c},a^2-b^2-3c]]
>
> >
>
> > In[7] := F[#1^2-#2^2-3#3 &]
>
> > Out[7] = F[#1^2-#2^2-3#3 &]
>
> >
>
> > In[8] := h[a_,b_,c_] = a^2-b^2-3c;
>
> > In[9] := F[h]
>
> > Out[9] = F[h]
>
> >
>
> > Further, for bonus points, if there are multiple definitions of a
>
> > function, I'd like to pick the one with two parameters:
>
> > In[10] := k[a_,b_] = a^2-b^2;
>
> > In[11] := k[a_,b_,c_] = a^2-b^2-3c;
>
> > In[12] := F[k]
>
> > Out[12] = -5
>
> >
>
> > Thanks for any help!
>
> > Roman
>
> >
>
> >



  • Prev by Date: Re: Warsaw Univ. course, was Re: Work on Basic Mathematica
  • Next by Date: Re: Work on Basic Mathematica Stephen!
  • Previous by thread: Re: defining a function whose parameter must be a function
  • Next by thread: Interval vs default, was Re: Mathematica numerics and... Re: Applying