关于符号的定义和值的问题 [英] Question on Definition and values of symbols

查看:18
本文介绍了关于符号的定义和值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Definition 知道"如何定义符号的值:使用 SetSetDelayed.但是如何?据我了解,在为符号分配值后,赋值器的分配方式没有任何区别:使用 SetSetDelayed.它可以通过函数 OwnValues 来说明,它总是返回带有 Head RuleDelayed 的定义.Definiton 如何获得这些信息?

Definition "knows" the way how a value for a symbol was defined: using Set or SetDelayed. But how? As I understand, after a value for a symbol was assigned there is no any difference for the evaluator how it was assigned: by using Set or SetDelayed. It can be illustrated by the function OwnValues which always returns definitions with the Head RuleDelayed. How Definiton obtains this information?

In[1]:= a=5;b:=5;
Definition[a]
Definition[b]
OwnValues[a]

Out[2]= a=5
Out[3]= b:=5
Out[4]= {HoldPattern[a]:>5}

推荐答案

OwnValues[a] = {HoldPattern[a] ->3};OwnValues[a] 给出 {HoldPattern[a] :>3} 而不是 {HoldPattern[a] ->3}Definition[a] 显示了人们可以期待的内容.可能这个定义以 Rule 的形式存储在内部,但通过 OwnValues 转换为 RuleDelayed 以抑制对定义的 r.h.s 的评估.这个假设与我最初的理解相矛盾,即 SetSetDelayed 分配的值之间没有区别.可能这些定义以不同的形式存储:RuleRuleDelayed 相应地但从评估者的角度来看是等效的.

OwnValues[a] = {HoldPattern[a] -> 3}; OwnValues[a] gives {HoldPattern[a] :> 3} instead of {HoldPattern[a] -> 3} but Definition[a] shows what one can expect. Probably this definition is stored internally in the form of Rule but is converted to RuleDelayed by OwnValues for suppressing of evaluation of the r.h.s of the definition. This hypothesis contradicts my original understanding that there are no difference between values assigned by Set and SetDelayed. Probably such definitions are stored in different forms: Rule and RuleDelayed correspondingly but are equivalent from the evaluator's point of view.

看到 MemoryInUse[] 如何依赖于定义的类型很有趣.

It is interesting to see how MemoryInUse[] depends on the kind of definition.

在下面的实验中,我在没有前端的交互式会话中使用了 Mathematica 5.2 的内核.使用 Mathematica 6 和 7 的内核会得到不同的结果.原因之一是 在这些版本中 Set 默认重载.

In the following experiment I used the kernel of Mathematica 5.2 in interactive session without the FrontEnd. With the kernels of Mathematica 6 and 7 one will get different results. One reason for this is that in these versions Set is overloaded by default.

首先,我评估 $HistoryLength=0; 是否有 DownValues 用于 InOut 变量影响我的成绩.但似乎即使 $HistoryLength 设置为 0,当前 输入行的 In[$Line] 值仍然被存储和删除输入新的输入后.这可能是 MemoryInUse[] 的第一次评估结果总是与第二次不同的原因.

First of all I evaluate $HistoryLength=0; for having DownValues for In and Out variables not affecting my results. But it seems that even when $HistoryLength is set to 0 the value of In[$Line] for current input line is still stored and removed after entering new input. This is likely the reason why result of the first evaluation of MemoryInUse[] always differs from the second.

这是我得到的:

Mathematica 5.2 学生版:微软视窗版

Mathematica 5.2 for Students: Microsoft Windows Version

版权所有 1988-2005 Wolfram Research, Inc.

Copyright 1988-2005 Wolfram Research, Inc.

-- 终端图形初始化 --

-- Terminal graphics initialized --

In[1]:= $HistoryLength=0;

In[1]:= $HistoryLength=0;

In[2]:= MemoryInUse[]

In[2]:= MemoryInUse[]

出[2]= 1986704

Out[2]= 1986704

In[3]:= MemoryInUse[]

In[3]:= MemoryInUse[]

出[3]= 1986760

Out[3]= 1986760

In[4]:= MemoryInUse[]

In[4]:= MemoryInUse[]

出[4]= 1986760

Out[4]= 1986760

In[5]:= a=2;

In[5]:= a=2;

In[6]:= MemoryInUse[]

In[6]:= MemoryInUse[]

出[6]= 1986848

Out[6]= 1986848

In[7]:= MemoryInUse[]

In[7]:= MemoryInUse[]

出[7]= 1986824

Out[7]= 1986824

In[8]:= MemoryInUse[]

In[8]:= MemoryInUse[]

出[8]= 1986824

Out[8]= 1986824

In[9]:= a:=2;

In[9]:= a:=2;

In[10]:= MemoryInUse[]

In[10]:= MemoryInUse[]

出[10]= 1986976

Out[10]= 1986976

In[11]:= MemoryInUse[]

In[11]:= MemoryInUse[]

出[11]= 1986952

Out[11]= 1986952

In[12]:= MemoryInUse[]

In[12]:= MemoryInUse[]

出[12]= 1986952

Out[12]= 1986952

In[13]:= a=2;

In[13]:= a=2;

In[14]:= MemoryInUse[]

In[14]:= MemoryInUse[]

出[14]= 1986848

Out[14]= 1986848

In[15]:= MemoryInUse[]

In[15]:= MemoryInUse[]

出[15]= 1986824

Out[15]= 1986824

In[16]:= MemoryInUse[]

In[16]:= MemoryInUse[]

出[16]= 1986824

Out[16]= 1986824

可以看到定义 a=2;MemoryInUse[] 增加了 1986824-1986760=64 个字节.将其替换为定义 a:=2; 会将 MemoryInUse[] 增加 1986952-1986824=128 字节.用前者替换后一个定义会将 MemoryInUse[] 恢复为 1986824 字节.这意味着延迟定义需要比直接定义多 128 个字节.

One can see that defining a=2; increases MemoryInUse[] by 1986824-1986760=64 bytes. Replacing it with the definition a:=2; increases MemoryInUse[] by 1986952-1986824=128 bytes. And replacing the latter definition with the former reverts MemoryInUse[] to 1986824 bytes. It means that delayed definitions require 128 bytes more than immediate definitions.

当然这个实验并不能证明我的假设.

Of course this experiment does not prove my hypothesis.

这篇关于关于符号的定义和值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆