第9讲变换规则和定义1.应用变换规则2.对规则的操作3.定义4.赋值的一些特殊形式5.索引的定义6.函数的定义7.定义的次序8.即时和延时的定义9.记住已知值的函数10.定义联系不同的符号11.定义数值值12.修改内置函数13.使用赋值列表TOP2November18,2004StartLearningStepbyStep1.应用变换规则expr/.rules在表达式上应用一次规则expr//.rules在表达式上重复应用规则直道结果不再改变应用变换规则expr/.lhs-rhs在表达式上应用某个变换规则expr/.{lhs1-rhs1,lhs2-rhs,...}在表达式上依次应用一系列规则应用一组变换规则expr/.{rules1,rules2,...}应用一组规则得到一个数组一次或重复应用规则TOP3November18,2004StartLearningStepbyStep1.应用变换规则(续)Applyingrulestowholeexpressionsexpr/.rulesapplyrulestoallsubpartsofexprReplace[expr,rules]applyrulestothewholeofexpronlyReplace[expr,rules,levspec]applyrulestopartsofexpronlevelsspecifiedbylevspecApplyingrulesinonewayorallpossiblewaysReplace[expr,rules]applyrulesinonewayonlyReplaceList[expr,rules]applyrulesinallpossiblewaysTOP4November18,2004StartLearningStepbyStep2.对规则的操作CreatingandusingdispatchtablesDispatch[rules]createarepresentationofalistofrulesthatincludesdispatchtablesexpr/.drulesapplyrulesthatincludedispatchtablesTOP5November18,2004StartLearningStepbyStep3.定义Manualandautomaticapplicationoftransformationrulesexpr/.lhs-rhsapplyatransformationruletoaspecificexpressionlhs=rhsassignavaluewhichdefinesatransformationruletobeusedwheneverpossibleRemovingassignmentsx=.removethevalueassignedtotheobjectxClear[x,y,...]clearallthevaluesofx,y,...TOP6November18,2004StartLearningStepbyStep4.赋值的一些特殊形式Modifyingvaluesofvariablesi++incrementthevalueofiby1i--decrementi++ipre-incrementi--ipre-decrementii+=diaddditothevalueofii-=disubtractdifromix*=cmultiplyxbycx/=cdividexbycTOP7November18,2004StartLearningStepbyStep4.赋值的一些特殊形式(续)Assigningvaluestoseveralvariablesatatimex=y=valueassignthesamevaluetobothxandy{x,y}={value1,value2}assigndifferentvaluestoxandy{x,y}={y,x}interchangethevaluesofxandyAssignmentsformodifyinglistsPrependTo[v,elem]prependelemtothevalueofvAppendTo[v,elem]appendelemv={v,elem}makeanestedlistcontaningelemTOP8November18,2004StartLearningStepbyStep5.索引的定义Manipulatingindexedvariablesa[i]=valueaddoroverwriteavaluea[i]accessavaluea[i]=.removeavalue?ashowalldefinedvaluesClear[a]clearalldefinedvaluesTable[a[i],{i,1,n}]orArray[a,n]converttoanexplicitListTOP9November18,2004StartLearningStepbyStep6.函数的定义f[x]=valuedefinitionforaspecificexpressionxf[x_]=valuedefinitionforanyexpression,referredtoasxThedifferencebetweendefininganindexedvariableandafunctionTOP10November18,2004StartLearningStepbyStep7.定义的次序TreatmentofdefinitionsinMathematicaMathematicatriestoputspecificdefinitionsbeforemoregeneraldefinitionsTOP11November18,2004StartLearningStepbyStep8.即时和延时的定义lhs=rhs(immediateassignment)rhsisevaluatedwhentheassignmentismadelhs:=rhs(delayedassignment)rhsisevaluatedeachtimethevalueoflhsisrequestedThetwotypesofassignmentsinMathematicaInterpretationsofassignmentswiththe=and:=operatorslhs=rhsrhsisintendedtobethefinalvalueoflhs(e.g.,f[x_]=1-x^2)lhs:=rhsrhsgivesacommandorprogramtobeexecutedwheneveryouaskforthevalueoflhs(e.g.,f[x_]:=Expand[1-x^2])Definingfunctionsforevaluatingexpressionsf[x_]=exprdefineafunctionwhichgivesthevalueexprforanyparticularvalueofxTOP12November18,2004StartLearningStepbyStep8.即时和延时的定义(续)lhs-rhsrhsisevaluatedwhentheruleisgivenlhs:rhsrhsisevaluatedwhentheruleisusedTwotypesoftransformationrulesinMathematicaTOP13November18,2004StartLearningStepbyStep9.记住已知值的函数f[x_]:=f[x]=rhsdefineafunctionwhichremembersvaluesthatitfindsDefiningafunctionthatremembersvaluesitfindsTOP14November18,2004StartLearningStepbyStep10.定义联系不同的符号Associatingdefinitionswithdifferentsymbolsf[args]:=rhsdefineadownvalueforff[g[args],...]^:=rhsdefineanupvalueforgShorterwaystodefineupvaluesf[g]^=valueorf[g[args]]^=valuemakeassignmentstobeassociatedwithg,ratherthanff[g]^:=valueorf[g[args]]^:=valuemakedelayedassignmentsassociatedwithgf[arg1,arg2,...]^=valuemakeassignmentsassociatedwiththeheadsofalltheargiTOP15November18,2004StartLearningStepbyStep10.定义联系不同的符号(续)Possiblepositionsforsymbolsindefinitionsf[...]:=rhsdownvalueforff/:f[g[...]][...]:=rhsdownvalueforfg/:f[...,g,...]:=rhsupvalueforgg/:f[...,g[...],...]:=rhsupvalueforgTOP16November18,2004StartLearningStepbyStep11.定义数值值Definingordinaryandnumericalvaluesexpr=valuedefineavaluetobeusedwheneverpossibleN[expr]=valuedefineavaluetobeusedfornumericalapproximationDefiningnumericalvaluesthatdependonnumericalprecisionN[expr]=valuedefineanumericalvaluetobeusedwhendefalutnumericalprecisionisrequestedN[expr,n]=valuedefineanumericalvaluetobeusedwhenn-digitprecisionisrequestedTOP17November18,2004StartLearningStepbyStep12.修改内置函数ProtectionforfunctionsUnprotect[f]removeprotectionProtect[f]addprotectionTOP18November18,2004StartLearningStepbyStep13.使用赋值列表FindingandsettingvaluesofsymbolsDownValues[f]givethelistofdownvaluesoffUpValues[f]givethelistofupvaluesoffDownValues[f]=rulessetthedownvaluesoffUpValues[f]=rulessettheupvaluesoff