mathematica replacement rule subexpression -


i have using replacement rules:

first have defined simple formulas using setdelayed

a:=b+c d:=a/e 

evaluating following equation gives result expected:

in[20]:= (d^2 + something)/(d - 1) out[20]= ((b+c)^2/e^2+something)/((b+c)/e-1) 

but if want replace "d" term, let's "tmp", following:

in[26]:= (d^2+something)/(d-1)//.d->tmp out[26]= ((b+c)^2/e^2+something)/(tmp-1) 

it seems variable "d" replaced if it's not surrounded function "power". term d^2 not replaced tmp.

so doing wrong if want d replaced each time occurs during steps of evaluation?

in in[26] evaluator has rewritten d according rule established before replacerepeated (which you've written //.) sees it.

mathematica's evaluator quite greedy , //. has low precedence expression left of //. in in[26] has been rewritten to

((b+c)^2/e^2+something)/(tmp-1) 

before replacement rule has chance run. since expression contains no d replacement rule d->tmp not engaged.

you have number of choices.

a) write new expression d such d:=tmp or d=tmp in case evaluating

(d^2+something)/(d-1) 

will produce

(something + tmp^2)/(-1 + tmp) 

b) delete definition of d made evaluating d:=a/e , use replacement rule whenever want transform expression including d.

c) study definition , use of functions such hold[] , holdfirst[] , parts of documentation cover both mathematica's usual evaluation process , how modify it.

d) usual mathematica, there other ways achieve want, best choice depend on trying do.

to answer particular question what doing wrong if want d replaced each time occurs during steps of evaluation?: defining rewrite rule d:=a/e , putting set of rules tried during evaluation of expression evaluated thereafter in session.

edit in response comment

d:=a/e isn't temporary replacement, it's definition persists until removed or replaced or close session. generally, use rules (such d->tmp. of course, mathematica being can assign rules variables, eg

rule1 = d->a/e 

and use them in obvious way

expression /. rule1 

you can define lists of rules

rule_list = {d->tmp, e->tmp2, f-> tmp3} 

and on.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -