RE: Re: Re: Function as an argument of the function
- To: mathgroup at smc.vnet.net
- Subject: [mg34734] RE: [mg34710] Re: [mg34642] Re: [mg34638] Function as an argument of the function
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 4 Jun 2002 03:41:44 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Tomek, I agree with Andrzej in that the problem is not well posed (and this makes answers difficult, if not awkward). Instead I'd propose to proceed as follows (here I assumed arg2 to receive that function argument, not any or one of the arguments): (1) check if your argument is a reasonable candidate In[1]:= f[arg1_, arg2_Symbol | arg2_Function, arg3_] := {arg1, arg2[3(arg1/2 - arg3)], arg3} In[2]:= f[Pi, Sin, 0] Out[2]= {\[Pi], -1, 0} In[3]:= f[2, #^2 &, 2] Out[3]= {2, 9, 2} In[4]:= f[2, 2, 2] Out[4]= f[2, 2, 2] (2) issue an error message if the supplied argument still doesn't behave reasonably, e.g. In[5]:= f::notNumeric = "function `1` does not give a real value for argument \ `2`" In[6]:= f[arg1_, arg2_Symbol | arg2_Function, arg3_] := Module[{y}, {arg1, If[NumericQ[y = arg2[x = (arg1 - arg3)*Random[]]] && MatchQ[N[y], _Real], y, Message[f::notNumeric, arg2, x]; y], arg3}] In[7]:= f[Pi, Sin, 0] Out[7]= {Pi, 0.9271673136314859, 0} In[8]:= f[Pi, ArcSin, 0] >From In[8]:= f::"notNumeric" : "function \!\(ArcSin\) does not give a real value for \ argument \!\(1.1085808847745964`\)" Out[8]= {Pi, 1.5707963267948966 - 0.46188938085684966*I, 0} In[10]:= f[2, #1 + #2 & , 2] >From In[10]:= Function::"slotn" : "Slot number \!\(2\) in \!\(\(\(#1 + #2\)\) &\) cannot be \ filled from \!\(\(\((\(\(\(#1 + #2\)\) &\))\)\)[0]\)." >From In[10]:= f::"notNumeric" : "function \!\(\(\(#1 + #2\)\) &\) does not give a real \ value for argument \!\(0\)" Out[10]= {2, #2, 2} Of course you can install further plausibility at the definition with Condition and testing your function with special, random, or even actual arguments. But certainly it is not feasible to check whether you function passed has appropiate Domain and Range, not to speak of other properties. As quite so often, you'd spawn a more helpful discussion, if you would supply an example of you intended application. -- Hartmut > -----Original Message----- > From: Andrzej Kozlowski [mailto:andrzej at platon.c.u-tokyo.ac.jp] To: mathgroup at smc.vnet.net > Sent: Sunday, June 02, 2002 7:15 AM > Subject: [mg34734] [mg34710] Re: [mg34642] Re: [mg34638] Function as an argument > of the function > > > 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)? > >> > >> > >> > >> > > > > > > > > >