Re: Re: Function as an argument of the function
- To: mathgroup at smc.vnet.net
- Subject: [mg34710] Re: [mg34642] Re: [mg34638] Function as an argument of the function
- From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
- Date: Sun, 2 Jun 2002 01:14:46 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
After sending this message I received a reply from Tomek in which he explained exactly what he meant. Basically he wanted a predicate function which tests whether an expression contains just one variable and takes numerical values when a number (or a numeric) quantity is substituted for this variable. This is what he meant by a function of one variable. In particualr {x} is not a function in his sense. As a result of our discussion I come up with the following solution: FunctionQ[expr_] := With[{values = Union[Cases[expr, _Symbol?( ! NumericQ[#1] & ), {0, -1}]]}, And[Length[values] == 1 , Sequence @@ NumberQ /@ ({Denominator[expr ], Numerator[expr ]} /. values[[1]] -> Random[])]] The idea is that we first test if the expression has just one variable and then we substitute a random number between 0 and 1 and test if the denominator and numerator of the expression are numbers. This seems to work for most reasonable functions in Tomek's sense: In[19]:= FunctionQ[Sin[Sin[x]]] Out[19]= True In[20]:= FunctionQ[Sin[x]+y] Out[20]= False and even: In[21]:= FunctionQ[1/UnitStep[x - 1]] Out[21]= True Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ On Friday, May 31, 2002, at 05:26 PM, Andrzej Kozlowski wrote: > Strictly speaking your request does not make sense, since in Mathematica > almost everything is a "function" in some sense or other. For example if > f is a symbol you can always "apply" it to 2 to get f[2], and so on. > > However, I assume what yu mean is something in a very much narrower > sense, meaning either a built-in numeric function like Sin, or a user > defined function of the form f[x_]:= .... Well, something like this will > do the trick, (with various obvious limitations): > > FunctionQ[f_] := > Block[{x}, DownValues[f] != {} || MemberQ[Attributes[f], > NumericFunction]] > > Now let's define a function: > > f[x_] := x^2 > > and something that needs an argument to be a function: > > g[f_?FunctionQ, a_] := f[a] > > We get: > > In[4]:= > g[Sqrt,4] > > Out[4]= > 2 > > In[5]:= > g[Sin,x] > > Out[5]= > Sin[x] > > In[6]:= > g[f,x] > > Out[6]= > x^2 > > In[7]:= > g[p,x] > > Out[7]= > g[p, x] > > > > On Thursday, May 30, 2002, at 03:55 PM, Tomek wrote: > >> Hi. >> I have to write a short function in Mathematica. But I have one >> problem with it. One of arguments of my function have to be a >> function of one variable. >> How can I check if the argument of the function is a function of one >> variable (I wish there's FunctionQ)? >> >> >> >> > > > >