Re: Determine if a parameter is a function
- To: mathgroup at smc.vnet.net
- Subject: [mg101863] Re: Determine if a parameter is a function
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Mon, 20 Jul 2009 06:00:23 -0400 (EDT)
- References: <h3kddu$g35$1@smc.vnet.net> <h3uv4g$k8j$1@smc.vnet.net>
Yours is a fine solution, but why do you need AtomQ? At the moment, after heavy trying, I use to test if f is not to be considered a function: KeineFunktion::nofun="Parameter `1` sollte eine Funktion sein."; KeineFunktion[f_]:= If[Not[ (Head[f]===Function) || (Head[f] === Symbol && ((DownValues[f] =!= {}) || (Intersection[Attributes[f],{NumericFunction}] =!= {}))) ], Message[KeineFunktion::nofun, f]; True, False]; This passes nearly all usual cases, except "Set", which will be considered a function, whereas e.g. SetDelayed, Rule or constants where not. This is, because Set has DownValues. One -- in my opinion not very complicated case -- is with functions which are defined by SubValues. So u[x_][y_]:=x^2+y KeineFunktion[u[x]] --> True KeineFunktion[u] --> True but KeineFunktion[u[x][#]&] --> False (not astonishing) The length of "systemFunctions" you calculated is bigger than the length calculated with my function, because I have them restricted to NumericFunktion. ADL wrote: > Actually, after some work, I found a solution which appears more > reliable (but who knows...): > > ClearAll[FunctionQ]; > FunctionQ[f_] := (functionQtest2[f] || functionQtest3[f]) && > functionQtest1[f]; > > ClearAll[functionQtest1,functionQtest2,functionQtest3]; > functionQtest1[f_Symbol] := (Length[StringPosition[ToString > [f::usage,OutputForm],"["]]>0); > functionQtest1[___] := True; > functionQtest2[f_] := (Head[f]===Function); > functionQtest3[f_] := (AtomQ[f] && (Head[f]===Symbol) && (!NumericQ > [f]) && ( > (DownValues[f]=!={})||(Attributes[f]=!={}) > )); > > The functionQtest1 test comes from a little heuristics about usage > messages. > Then, the following comes out: > > In:= FunctionQ[Red] > Out= False > > In:= FunctionQ[\[ScriptCapitalA]] > Out= False > > In:= FunctionQ[Log] > Out= True > > In:= FunctionQ[Algebraics] > Out= False > > In:= FunctionQ[Log[Sin[#]] &] > Out= True > > In:= systemFunctions = Select[ToExpression /@ Names["System`*"], > FunctionQ]; > In:= systemFunctions // Length > Out= 1661 > > ADL > > > On Jul 15, 1:09 pm, Peter Breitfeld <ph... at t-online.de> wrote: >> Suppose I have a function eg >> >> myfunc[f_,x_]:= <some definitions> >> >> f should be a pure function like (#^2&) or Function[{x},x^2] or a named >> function either self defined, like >> >> f[x_]:=x^2 or g[x_]=x^2 >> >> or built-in like Sin, Log, ... >> >> How can I test if f is any of these, to be able to yield a message on >> wrong input? >> >> I found that the pure-functions have Head Function, but all the others >> have Head Symbol, so asking for the head is not sufficient. >> >> -- >> _________________________________________________________________ >> Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de > > -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de