“定义”的等价物在数学中

发布于 2024-08-04 17:47:12 字数 471 浏览 4 评论 0原文

我需要一个函数,它将符号名称作为字符串并返回该符号是否已定义。函数 ValueQ 很接近,但它对于函数名称返回 False。此外,它需要符号而不是字符串。

示例:

defined["N"] --> True (predefined function N)
defined["x"] --> False
x = 7;
defined["x"] --> True (x is now defined)
defined["7"] --> True (7 is a number)
f[x_] := 2x
defined["f"] --> True (f has DownValues)
g[x_][y_] := x+y
defined["g"] --> True (g has SubValues)

PS:感谢 Pillsy 指出需要检查 DownValues 和 SubValues。

I need a function that takes the name of a symbol as a string and returns whether that symbol is already defined. The function ValueQ is close but it returns False for function names. Also, it takes symbols rather than strings.

Examples:

defined["N"] --> True (predefined function N)
defined["x"] --> False
x = 7;
defined["x"] --> True (x is now defined)
defined["7"] --> True (7 is a number)
f[x_] := 2x
defined["f"] --> True (f has DownValues)
g[x_][y_] := x+y
defined["g"] --> True (g has SubValues)

PS: Thanks to Pillsy for pointing out the need to check for both DownValues and SubValues.

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

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

发布评论

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

评论(4

℉服软 2024-08-11 17:47:12

我将其拼凑在一起,这似乎有效:

defined[s_] := ToExpression["ValueQ[" <> s <> "]"] || 
               Head@ToExpression[s] =!= Symbol || 
               ToExpression["Attributes[" <> s <> "]"] =!= {} ||
               ToExpression["DownValues[" <> s <> "]"] =!= {} ||
               ToExpression["SubValues[" <> s <> "]"] =!= {}

希望有一个更漂亮的解决方案。

PS:感谢 Pillsy 指出需要检查 DownValues 和 SubValues。

I cobbled this together, which seems to work:

defined[s_] := ToExpression["ValueQ[" <> s <> "]"] || 
               Head@ToExpression[s] =!= Symbol || 
               ToExpression["Attributes[" <> s <> "]"] =!= {} ||
               ToExpression["DownValues[" <> s <> "]"] =!= {} ||
               ToExpression["SubValues[" <> s <> "]"] =!= {}

Hopefully there's a prettier solution.

PS: Thanks to Pillsy for pointing out the need to check for both DownValues and SubValues.

一花一树开 2024-08-11 17:47:12

我认为名称应该可以解决问题:

Names["string"] 给出一个列表
匹配的符号名称
字符串。

如果 Names["foo"] 返回 {},则 foo 不应有定义,否则应返回 {"foo"}。
所以你的函数“定义”可能会被完成为:

defined[str_] := Names[str] != {}

至少对于符号,因为这不适用于“7”,因为 7 不是符号。您可以单独处理这种情况,例如使用 NumberQ。

此外,您还可以使用 Symbol 从字符串中创建符号(对于自动生成符号很有用),并使用 Definition 检查符号的定义。

Symbol["name"] 指的是带有
指定的名称。

定义[符号]打印为
为符号给出的定义。

编辑:比查看 Names 返回的内容更好,NameQ["name"] 会告诉您给定的名称是否存在。但仍然没有告诉您该符号是否有明确的定义,只是提到了它。

I think Names should do the trick:

Names["string"] gives a list of the
names of symbols which match the
string.

If Names["foo"] returns {}, then there should be no definitions for foo, otherwise it should return {"foo"}.
So your function 'defined' might be done as:

defined[str_] := Names[str] != {}

For symbols at least, because this doesn't work for "7", since 7 is not a symbol. You could handle this case seperately, for instance with NumberQ.

Also, you can use Symbol to make a symbol out of a string (useful for automatically generating symbols) and Definition to check the definitions of a symbol.

Symbol["name"] refers to a symbol with
the specified name.

Definition[symbol] prints as the
definitions given for a symbol.

EDIT: Better than looking at what Names returns, NameQ["name"] tells you if a given name exists. Still doesn't tell you if the symbol has an explicit definition though, just that it has been mentioned.

会发光的星星闪亮亮i 2024-08-11 17:47:12

您可以使用 DownValues 来查看是否有与某个交易品种关联的“功能”。这适用于像或这样的定义

f[x_, y_] := x + y

,但

g[3] = 72 * a;

不适用于像这样的奇异事物

h[a_][b] = "gribble";

,但大多数人无论如何都不会认为这是定义函数。如果您想检查函数定义是否存在,则需要将名称转换为表达式(并确保在执行此操作时将其包含在 Hold 中!)。这是一个相当强大的函数,可以检查 DownValuesSubValues

functionNameQ[name_String] := 
    With[{ hSymbol = ToExpression[name, InputForm, Hold] },
        MatchQ[hSymbol, Hold[_Symbol]] &&
            ((DownValues @@ hName) =!= {} || (SubValues @@ hName) =!= {})];

You can use DownValues to see if you have "functions" associated with a symbol. This will work for definitions like

f[x_, y_] := x + y

or

g[3] = 72 * a;

It won't work for exotic things like

h[a_][b] = "gribble";

but most people won't think of that as defining a function anyway. If you want to check for the existence of a function definition, you need to convert the name to an expression (and make sure it's wrapped in Hold when you do!). Here's a reasonably robust function that checks for both DownValues and SubValues:

functionNameQ[name_String] := 
    With[{ hSymbol = ToExpression[name, InputForm, Hold] },
        MatchQ[hSymbol, Hold[_Symbol]] &&
            ((DownValues @@ hName) =!= {} || (SubValues @@ hName) =!= {})];
伴随着你 2024-08-11 17:47:12
defined[str_] := Not[ToString[FullDefinition[str]] === ""]
defined[str_] := Not[ToString[FullDefinition[str]] === ""]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文