8

I am defining a function that takes as input a function and I want to specify it in the input type i.e. Operat[_?FunctionQ]:=... But there is no functionQ as of yet in mathematica. How do I get aroud this except not specifying any type at all.

Any ideas?

Oh! This: Test if an expression is a Function? may be the answer i am looking for. I am reading further

Is the solution proposed there robust?, i.e.:

FunctionQ[_Function | _InterpolatingFunction | _CompiledFunction] = True;
FunctionQ[f_Symbol] := Or[
  DownValues[f] =!= {}, 
  MemberQ[ Attributes[f], NumericFunction ]]
FunctionQ[_] = False;
Community
  • 1
  • 1
Phil
  • 815
  • 1
  • 8
  • 15
  • possible duplicate of [Test if an expression is a Function?](http://stackoverflow.com/questions/3736942/test-if-an-expression-is-a-function) – Mr.Wizard Apr 16 '11 at 14:10
  • @ Mr. Wizard: I just want to make sure the solution proposed there is robust. I wanted to delete this question after i found the Test if an expression is a Function and I may still do so.... in particular, does _Function | _InterpolatingFunction | _CompiledFunction cover all there is? – Phil Apr 16 '11 at 14:16
  • Phil, it was not my intention to "punish" you. I simply I think the community is better served if these two questions are combined. I advise against creating a question you intend to delete in a little while; I tried that a few days ago and it didn't go well. :-) – Mr.Wizard Apr 16 '11 at 15:26
  • @ Mr. Wizard, in making a separate question I was hoping for a discussion on robustness of what would be the test of a function and how computationally efficient such a test might be, for the test itself and how it impacts speeds of computations for functions that take functions as input. I felt this would be a separate focus to warrant a different question. @WReach's answer went in that direction. How do you combine questions without deleting answers that were provided? thanks – Phil Apr 16 '11 at 16:57
  • Phil, I do not object to that reasoning. At this point a moderator would have to do the merge I believe. – Mr.Wizard Apr 16 '11 at 17:54

1 Answers1

8

The exhibited definition has great utility. The question is: what exactly constitutes a function in Mathematica? Pure functions and the like are easily to classify as functions, but what about definitions that involve pattern-matching? Consider:

h[g[x_]] ^:= x + 1

Is h to be considered a function? If so, it will be hard to identify as it will entail examining the up-values of every symbol in the system to make that determination. Is g a function? It has an up-value, but g[x] is an inert expression.

What about head composition:

f[x_][y_][z_] := x + y + z

Is f a function? How about f[1] or f[1][2]?

And then there are the various capabilities like JLink and NETLink:

Needs["JLink`"]
obj = JavaNew["java.util.Date"]
obj@toString[]

Is obj@toString a function?

I hate to bring up these problems without offering solutions -- but I want to emphasize that the question as to what constitutes a function in the Mathematica context is a tricky one. It is tricky from both the theoretical and practical standpoints.

I think that the answer to whether the exhibited function test is complete really depends upon the types of expressions that you will be feeding it in your specific application.

WReach
  • 18,098
  • 3
  • 49
  • 93
  • @ WReach: That's a great(!) non-answer in that it explores better than i could have described the potential pitfalls here. I wonther whether leaving the type undefined is not safer, I don't know how it affects computational speed though... – Phil Apr 16 '11 at 15:04
  • 4
    points out many forms that I consider to be functions that aren't head `Function` (or even `Symbol`.) I'm going to give the same advice I invariably give others: Don't try to type-check for functions. If you're writing something that can take a function as an argument, treat any otherwise unknown input as if it is a function, and check the result for correctness, rather than trying to check the input up front. (Usually I get ignored, the first time. But they learn eventually.) – Brett Champion Apr 16 '11 at 18:53
  • Brett, that's what I was thinking, but I want something that computes efficiently and further I want to eventually compile the function. – Phil Apr 16 '11 at 19:34
  • 1
    I second @Brett - generally, trying to type-check functions seems to me like fighting with the (untyped) Mathematica language, and fighting with the language is seldom rewarding. There are other cases when typing can be beneficial in mma (e.g. implementing abstract data types), but this is clearly not it. And, as @WReach has demonstrated, this is very hard to even define well, not to mention make robust. Below is a link to one relevant MathGroup discussion: http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/6b8c6a826824ff7c/ – Leonid Shifrin Apr 18 '11 at 17:57