📄 8_1_1.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0053)http://www.math.sjtu.edu.cn/mathematica教程/8.1.1.htm -->
<HTML><HEAD><TITLE></TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2802" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId></HEAD>
<BODY background=8_1_1.files/0532.jpg topMargin=0>
<DIV align=center>
<CENTER>
<TABLE borderColor=#c0c0c0 borderColorDark=#008080 width="103%" bgColor=#c0c0c0
borderColorLight=#c0c0c0 background=8_1_1.files/0713.jpg border=1>
<TBODY>
<TR>
<TD width="100%"><B><FONT lang=ZH-CN face=黑体 color=#ff0000
size=4>7.1模块和块中的变量
<A href="http://www.math.sjtu.edu.cn/mathematica教程/index.htm"><IMG
src="8_1_1.files/0009.gif" border=0></A>
</FONT></B></TD></TR></TBODY></TABLE></CENTER></DIV>
<P style="LINE-HEIGHT: 150%"><FONT color=#008000 size=2>
前面我们学习了有关Mathematica的各种基本运算及操作,为了使Mathematica更有效的工作,我们可对Mathematica进行模块化运算。在模块内部通过编写一系列表达式语句,使其实现一定的功能。在Mathematica内部也提供了很多程序包,我们将学习如何调用它们。</FONT>
<P style="LINE-HEIGHT: 150%"><FONT color=#008000
size=2>
一般情况下,Mathematica假设所有变量都为全局变量。也就是说无论何时你使用一个你定义的变量,Mathematica都假设你指的是同一个目标。然而在编制程序时,你则不会想把所有的变量当作全局变量,因为如果这样程序可能就不具有通用性,你也可能在调用程序时陷入混乱状态。给出定义模块或块和局部变量的常用</FONT></P>
<P><FONT color=#808000 size=2>形式:</FONT></P><FONT size=2>
<BLOCKQUOTE></FONT>
<DIV align=center>
<CENTER>
<TABLE borderColor=#ff00ff borderColorDark=#ffffff width=512
borderColorLight=#ff00ff background=8_1_1.files/0522.gif border=1>
<TBODY>
<TR>
<TD width=202>
<P><FONT color=#0000ff size=2>Module[{x,y,...},body] </FONT></P></TD>
<TD width=264>
<P><FONT color=#0000ff size=2>具有局部变量x,y…的模块 </FONT></P></TD></TR>
<TR>
<TD width=202>
<P><FONT color=#0000ff size=2>Module[{x=x0,y=y0,…},body] </FONT></P></TD>
<TD width=264>
<P><FONT color=#0000ff size=2>具有初始值的局部变量的模块 </FONT></P></TD></TR>
<TR>
<TD width=202>
<P><FONT color=#0000ff size=2>lhs:=Module[vars,rhs/:cond] </FONT></P></TD>
<TD width=264>
<P><FONT color=#0000ff size=2>rhs和cond共享局部变量</FONT></P></TD></TR>
<TR>
<TD width=202>
<P><FONT color=#0000ff size=2>Block[{x,y,... </FONT></P></TD>
<TD width=264>
<P><FONT color=#0000ff size=2>},body]运用局部值x,y, …计算body</FONT></P></TD></TR>
<TR>
<TD width=202>
<P><FONT color=#0000ff size=2>Block[{x=x0,y=y0,…},bddy] </FONT></P></TD>
<TD width=264>
<P><FONT color=#0000ff size=2>给x,y,..赋初始值
</FONT></P></TD></TR></TBODY></TABLE></CENTER></DIV><FONT color=#008000
size=2></BLOCKQUOTE></FONT><FONT color=#008000 size=3>
<P style="LINE-HEIGHT: 150%"> </FONT><FONT color=#008000
size=2>Mathematica中的模块工作很简单,每当使用模块时,就产生一个新的符号来表示它的每一个局部变量。产生的新符号具有唯一的名字,互不冲突,有效的保护了模块内外的每个变量的作用范围。首先我们来看Module函数,这个函数的第一部分参数,里说明的
变量只在Module内起作用,body执行体,包含合法的Mathematica语句,多个语句之间可用“
;”分割下面定义有初值的变量t,Mathematica默认它为全局变量: </FONT></P><FONT size=2>
<P style="LINE-HEIGHT: 150%"> </P>
<P>l<B>n[1]:=t=lO</P>
<P>Out[1]=10</P></B>
<P> </P></FONT>
<P><FONT color=#008000 size=2>模块中的t为局部变量,因此它独立于全局变量t</FONT></P><B><FONT size=2>
<P>ln[2]:=Module[{t},t=8;Print[t]]</P></FONT></B>
<P><FONT color=#008000 size=2>全局变量t的值仍为10:</FONT></P><FONT size=2>
<P><B>ln[3]=t=lO</P></B></FONT><B><FONT size=3>
<P align=justify>Out[3]=10</P></FONT></B>
<P align=justify><FONT color=#008000
size=2>下面定义函数中的中间变量t为局部变量并调用f:</FONT></P><FONT size=2>
<P align=center><IMG height=152 src="8_1_1.files/Image145.gif"
width=435></P></FONT>
<P><FONT color=#008000 size=2>全局变量t的值仍为10:</FONT></P><FONT size=2>
<P><B>ln[6]:=t=10</P>
<P>Out[6]=10</P></B></FONT>
<P><FONT color=#008000
size=2>我们可以对模块中的任意局部变量进行初始化,这些初始值总是在模块执行前就被计算出来。下面给局部变量t赋初值u:调用函数g;</FONT></P><FONT
size=2>
<P align=center><IMG height=144 src="8_1_1.files/Image146.gif"
width=474></P></FONT><FONT color=#008000 size=3>
<P
style="LINE-HEIGHT: 150%">Mathematica中的模块允许你把某变量名看作局部变量名。然而又存在有时你又希望它们为全局变量时,但变量值为局部的矛盾,这时我们可以用Block[]函数。下面是一个含有全局变量x表达式,使用x的局部值计算上面的表达式:</P></FONT><FONT
size=2>
<P align=center><IMG height=215 src="8_1_1.files/Image147.gif"
width=417></P></FONT><FONT color=#008000 size=3>
<P style="LINE-HEIGHT: 150%"> </FONT><FONT color=#008000
size=2>在Mathematica中编制程序时,必须使程序中的各个部分尽可能的独立,这样程序才便于读懂、维护和修改。确保程序各部分不相干的主要方法是设置具有一定作用域的变量。在Mathematica中有两种限制变量作用域的基本方法:模块(Module)和块(Block)。我们在书写实际程序中,模块比块更具普遍性。然而在交互式计算中需要定义作用域时,块更实用。</FONT></P><FONT
color=#0000ff size=3>
<P style="LINE-HEIGHT: 150%"> </FONT><FONT color=#808000
size=2>
Module[vars,body]所要做的是把执行模块时表达式body的形式看成Mathematica程序的“代码”。然而当“代码”中直接出现变量vats时,这些vars都将被看作局部的。Block[vats,body]并不查看表达式body的形式,而在整个计算Body的过程中,实用vars的局部值。</FONT></P>
<P><FONT color=#333399 size=2>下例中我们根据i定义m: </FONT></P><FONT size=2>
<P></FONT><B><FONT size=2>ln[12]:=m=i^2 ’</FONT></P>
<P><FONT size=2>Out[12]:=i</FONT><SUP><FONT
size=2>2</FONT></P></SUP></B><SUP><FONT color=#333399 size=2>
<P></FONT><FONT color=#333399 size=2>在计算i+m的整个过程中使用块中i的局部值: </FONT></P><B><FONT
size=2>
<P>h[13]:=BlockI(i;a),!+m]</FONT></P>
<P><FONT size=2>Out[13]=a+a2</FONT></P></B><FONT size=2>
<P></FONT><FONT color=#008000 size=2>而对于下面的例子,只有直接出现在i+m中的i,才被看作局部变量:
</FONT></P><B><FONT size=2>
<P>In[14]:=Module[{i=a},i+m]</P>
<P>Out[14]=a+i‘</P></FONT></B></SUP>
<P align=right><A
href="http://www.math.sjtu.edu.cn/mathematica教程/6.2.1.htm"><IMG
src="8_1_1.files/0171.gif" border=0></A> <A
href="http://www.math.sjtu.edu.cn/mathematica教程/8.2.1.htm"><IMG
src="8_1_1.files/0173.gif" border=0></A></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -