mathematica 中是否有可用于定义输入类型的函数头?

发布于 2024-11-01 22:17:37 字数 519 浏览 5 评论 0原文

我正在定义一个函数,该函数将函数作为输入,并且我想在输入类型中指定它,即 Operat[_?FunctionQ]:=... 但目前mathematica中还没有函数Q。除了根本不指定任何类型之外,我该如何解决这个问题。

有什么想法吗?

哦! 这是:测试表达式是否是函数? 可能就是我正在寻找的答案。我正在进一步阅读

那里提出的解决方案是否可靠?,即:

FunctionQ[_Function | _InterpolatingFunction | _CompiledFunction] = True;
FunctionQ[f_Symbol] := Or[
  DownValues[f] =!= {}, 
  MemberQ[ Attributes[f], NumericFunction ]]
FunctionQ[_] = False;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2024-11-08 22:17:37

所展示的定义具有很大的实用性。问题是:Mathematica 中的函数到底由什么构成?纯函数等很容易归类为函数,但是涉及模式匹配的定义又如何呢?考虑一下:

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

h 是否被视为函数?如果是这样,将很难识别,因为需要检查系统中每个品种的上值才能做出决定。 g 是一个函数吗?它有一个向上值,但 g[x] 是一个惰性表达式。

头部组合怎么样:

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

f 是一个函数吗? f[1]f[1][2] 怎么样?

然后还有各种功能,例如 JLink 和 NETLink:

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

obj@toString 是函数吗?

我讨厌在不提供解决方案的情况下提出这些问题——但我想强调,在 Mathematica 环境中什么构成函数的问题是一个棘手的问题。从理论和实践的角度来看,这都是棘手的。

我认为所展示的功能测试是否完整的答案实际上取决于您将在特定应用程序中输入的表达式类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文