Prev: Problem with a diagram
Next: First nonzero in list
From: Andrzej Kozlowski on 10 Jul 2010 04:00 On 10 Jul 2010, at 05:35, Richard Fateman wrote: > Andrzej Kozlowski wrote: > .... > >> Suppose in the expression 2/3 I + x/y I you wish to replace all fractions (that is 2/3 and x/y) by r and I by d. Without worrying about evaluation you can do this as follows: >> Unevaluated[Unevaluated[2/3 I + x/y I] /. HoldPattern[x_/y_] -> r] /. >> HoldPattern[I] -> d >> 2 d r >> ... > > >> All this is perfectly reasonable, logical and a great deal easier than almost anything in an undergraduate math syllabus at a reasonable university. >> > Really? > > So if someone gives you a basket of fruit and says to replace the apples with bananas, you explain that it is easy: First you tell that person to throw away that basket and come up with a new basket in which he has wrapped the whole basket in a layer of aluminum foil. You then replace > "apple-wrapped-in-foil" by banana. > > You then proclaim the problem solved. > > Unfortunately, if I give you an already unwrapped basket of fruit, it is impossible. (that is, an already simplified Mathematica expression) > > Which is presumably the usual state of the basket. Impossible? For example (just one of many ways to do this): expr = 1 + I; ReleaseHold[ ToExpression[ToString[expr, TraditionalForm], InputForm, Hold] /. HoldPattern[I] -> d] d+1 etc. > > > A discussion and partial definition of BetterRules, a Mathematica program that does things like this, is in > http://www.cs.berkeley.edu/~fateman/papers/better-rules.pdf It's amazing what some people call "papers" these days. Andrzej Kozlowski
From: Murray Eisenberg on 10 Jul 2010 04:00 I is not a "symbol"! Head[I] Complex 3+I /. I-> -I 3 + I Head[2] Integer (2/3) /. 2->5 2/3 Please stop beating the dead horse. On 7/8/2010 8:34 PM, AES wrote: > ...Is it ever a good idea to use ReplaceAll to do word-processing-type > "global find and replace" changes like this? > > Lengthy threads on this NG have pointed out the havoc that this can > wreak if the symbol I is among those replaced, particularly if one > attempts to do I -> -I. > > Or is I the _only_ symbol in the entire Mathematica vocabulary that > encounters this difficulty? > > (I really would like to know the answer to this question, even if this > is entirely out of curiosity, not any real need. I've asked it > previously, and never gotten an answer.) -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
From: Richard Fateman on 10 Jul 2010 03:59 Andrzej Kozlowski wrote: ..... > Suppose in the expression 2/3 I + x/y I you wish to replace all > fractions (that is 2/3 and x/y) by r and I by d. Without worrying about > evaluation you can do this as follows: > > > Unevaluated[Unevaluated[2/3 I + x/y I] /. HoldPattern[x_/y_] -> r] /. > HoldPattern[I] -> d > > 2 d r > >... > > > All this is perfectly reasonable, logical and a great deal easier than almost anything in an undergraduate math syllabus at a reasonable university. > > Really? So if someone gives you a basket of fruit and says to replace the apples with bananas, you explain that it is easy: First you tell that person to throw away that basket and come up with a new basket in which he has wrapped the whole basket in a layer of aluminum foil. You then replace "apple-wrapped-in-foil" by banana. You then proclaim the problem solved. Unfortunately, if I give you an already unwrapped basket of fruit, it is impossible. (that is, an already simplified Mathematica expression) Which is presumably the usual state of the basket. Here is another way. Fix the pattern matching in Mathematica by changing the way the rules are matched so you don't have to do this nonsense. Here is another way.(2) Provide an alternative matcher. Here is another way (3). Use the pattern matching as given, but run the rules through a transformer so that I->d becomes Complex[k_, j_] -> d j + k. And x_/y_ -> r becomes {Rational[x_/y_]->r, x_ /y_ -> r} A discussion and partial definition of BetterRules, a Mathematica program that does things like this, is in http://www.cs.berkeley.edu/~fateman/papers/better-rules.pdf And yes, this has been discussed previously, here.
From: Andrzej Kozlowski on 10 Jul 2010 06:33 Actually I is a Symbol, before evaluation: In[90]:= Head[Unevaluated[I]] Out[90]= Symbol but it is no loner one after evaluation: In[91]:= Head[I] Out[91]= Complex Andrzej Kozlowski On 10 Jul 2010, at 17:01, Murray Eisenberg wrote: > I is not a "symbol"! > > Head[I] > Complex > > 3+I /. I-> -I > 3 + I > > Head[2] > Integer > > (2/3) /. 2->5 > 2/3 > > Please stop beating the dead horse. > > > On 7/8/2010 8:34 PM, AES wrote: > >> ...Is it ever a good idea to use ReplaceAll to do word-processing-type >> "global find and replace" changes like this? >> >> Lengthy threads on this NG have pointed out the havoc that this can >> wreak if the symbol I is among those replaced, particularly if one >> attempts to do I -> -I. >> >> Or is I the _only_ symbol in the entire Mathematica vocabulary that >> encounters this difficulty? >> >> (I really would like to know the answer to this question, even if this >> is entirely out of curiosity, not any real need. I've asked it >> previously, and never gotten an answer.) > > > -- > Murray Eisenberg murray(a)math.umass.edu > Mathematics & Statistics Dept. > Lederle Graduate Research Tower phone 413 549-1020 (H) > University of Massachusetts 413 545-2859 (W) > 710 North Pleasant Street fax 413 545-1801 > Amherst, MA 01003-9305 >
From: Murray Eisenberg on 11 Jul 2010 06:18
I stand corrected. On 7/10/2010 6:23 AM, Andrzej Kozlowski wrote: > Actually I is a Symbol, before evaluation: > > In[90]:= Head[Unevaluated[I]] > > Out[90]= Symbol > > but it is no loner one after evaluation: > > In[91]:= Head[I] > > Out[91]= Complex > > Andrzej Kozlowski > > > On 10 Jul 2010, at 17:01, Murray Eisenberg wrote: > >> I is not a "symbol"! >> >> Head[I] >> Complex >> >> 3+I /. I-> -I >> 3 + I >> >> Head[2] >> Integer >> >> (2/3) /. 2->5 >> 2/3 >> >> Please stop beating the dead horse. >> >> >> On 7/8/2010 8:34 PM, AES wrote: >> >>> ...Is it ever a good idea to use ReplaceAll to do word-processing-type >>> "global find and replace" changes like this? >>> >>> Lengthy threads on this NG have pointed out the havoc that this can >>> wreak if the symbol I is among those replaced, particularly if one >>> attempts to do I -> -I. >>> >>> Or is I the _only_ symbol in the entire Mathematica vocabulary that >>> encounters this difficulty? >>> >>> (I really would like to know the answer to this question, even if this >>> is entirely out of curiosity, not any real need. I've asked it >>> previously, and never gotten an answer.) >> >> >> -- >> Murray Eisenberg murray(a)math.umass.edu >> Mathematics& Statistics Dept. >> Lederle Graduate Research Tower phone 413 549-1020 (H) >> University of Massachusetts 413 545-2859 (W) >> 710 North Pleasant Street fax 413 545-1801 >> Amherst, MA 01003-9305 >> > -- Murray Eisenberg murray(a)math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305 |