Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14141 > unrolled thread
| Started by | plank.in.sequim@gmail.com |
|---|---|
| First post | 2013-04-11 08:11 +0000 |
| Last post | 2013-04-14 00:36 +0000 |
| Articles | 9 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
"Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-11 08:11 +0000
Re: "Programming With Mathematica" Exercise help Peter Pein <petsie@dordos.net> - 2013-04-12 06:16 +0000
Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
Re: "Programming With Mathematica" Exercise help spamslayer19@gmail.com - 2013-04-12 06:15 +0000
Re: "Programming With Mathematica" Exercise help drallenbio@gmail.com - 2013-04-12 06:15 +0000
Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
Re: "Programming With Mathematica" Exercise help Roland Franzius <roland.franzius@uos.de> - 2013-04-13 06:02 +0000
Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
| From | plank.in.sequim@gmail.com |
|---|---|
| Date | 2013-04-11 08:11 +0000 |
| Subject | "Programming With Mathematica" Exercise help |
| Message-ID | <kk5r6r$ric$1@smc.vnet.net> |
I'm loving Paul Wellin's book "Programming with Mathematica: An Introduction" and am trying to diligently do all the exercises. Most of them have answers in the back but I'm stuck on Section 4.2, Exercise 2 and there's no answer given. It gives the following Mathematica code: z = 11; a = 9; z + 3 /. z -> a 14 So "z+3" is being evaluated to 14 and then the substitution has no effect. He asks how to "use the Hold function in the compound expression to obtain a value of 12". I don't seem to be able to get this to work. My original thought was to hold z+3, but then the z in the replacement part gets evaluated so the replacement is actually 11->3 which doesn't match in the held z+3 expression. In fact, if you replace "z+3" with Hold[11+3] then you'll end up with Hold[9+3]. Curiously, this works differently if you use replace. In: Replace[Hold[11+3],11->9] Out: Replace[Hold[11+3],11->9] In: Hold[11+3]/.11->9 out: Hold[9+3] I thought these two were supposed to be equivalent so I'm a bit confused here. In any event, I've tried all the commonsense ideas I've had and then spent some time flailing about randomly with Hold's but nothing seems to work correctly. Can anybody help me understand this? Thanks! Darrell
[toc] | [next] | [standalone]
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Date | 2013-04-12 06:16 +0000 |
| Message-ID | <kk88ru$c1p$1@smc.vnet.net> |
| In reply to | #14141 |
Am 11.04.2013 10:11, schrieb plank.in.sequim@gmail.com: > I'm loving Paul Wellin's book "Programming with Mathematica: An Introduction" and am trying to diligently do all the exercises. Most of them have answers in the back but I'm stuck on Section 4.2, Exercise 2 and there's no answer given. It gives the following Mathematica code: > > z = 11; > a = 9; > z + 3 /. z -> a > > 14 > > So "z+3" is being evaluated to 14 and then the substitution has no effect. He asks how to "use the Hold function in the compound expression to obtain a value of 12". I don't seem to be able to get this to work. My original thought was to hold z+3, but then the z in the replacement part gets evaluated so the replacement is actually 11->3 which doesn't match in the held z+3 expression. In fact, if you replace "z+3" with Hold[11+3] then you'll end up with Hold[9+3]. Curiously, this works differently if you use replace. > > In: Replace[Hold[11+3],11->9] > Out: Replace[Hold[11+3],11->9] > > In: Hold[11+3]/.11->9 > out: Hold[9+3] > > I thought these two were supposed to be equivalent so I'm a bit confused here. > > In any event, I've tried all the commonsense ideas I've had and then spent some time flailing about randomly with Hold's but nothing seems to work correctly. Can anybody help me understand this? Thanks! > > Darrell > Hi Darrell, maybe you're looking for Hold[z + 3] /. HoldPattern[z] -> a % // ReleaseHold ? Peter
[toc] | [prev] | [next] | [standalone]
| From | plank.in.sequim@gmail.com |
|---|---|
| Date | 2013-04-14 00:36 +0000 |
| Message-ID | <kkctm5$7er$1@smc.vnet.net> |
| In reply to | #14159 |
> Hold[z + 3] /. HoldPattern[z] -> a > % // ReleaseHold I would have thought this was the closest to what I would have called a "correct" answer since it actually uses hold and all the variables have their assigned values when the replacement takes place. I really think the most elegant way of doing it is the Unevaluated answer and the one that uses Block but neither uses Hold. Thanks - I guess HoldPattern is probably the magic I was looking for.
[toc] | [prev] | [next] | [standalone]
| From | spamslayer19@gmail.com |
|---|---|
| Date | 2013-04-12 06:15 +0000 |
| Message-ID | <kk88qi$c0u$1@smc.vnet.net> |
| In reply to | #14141 |
In[1]:= z = 11; a = 9; Hold@z + 3 /. Hold@z -> a Out[1]= 12 On Thursday, April 11, 2013 3:11:07 AM UTC-5, plank.i...@gmail.com wrote: > I'm loving Paul Wellin's book "Programming with Mathematica: An Introduction" and am trying to diligently do all the exercises. Most of them have answers in the back but I'm stuck on Section 4.2, Exercise 2 and there's no answer given. It gives the following Mathematica code: > > > > z = 11; > > a = 9; > > z + 3 /. z -> a > > > > 14 > > > > So "z+3" is being evaluated to 14 and then the substitution has no effect. He asks how to "use the Hold function in the compound expression to obtain a value of 12". I don't seem to be able to get this to work. My original thought was to hold z+3, but then the z in the replacement part gets evaluated so the replacement is actually 11->3 which doesn't match in the held z+3 expression. In fact, if you replace "z+3" with Hold[11+3] then you'll end up with Hold[9+3]. Curiously, this works differently if you use replace. > > > > In: Replace[Hold[11+3],11->9] > > Out: Replace[Hold[11+3],11->9] > > > > In: Hold[11+3]/.11->9 > > out: Hold[9+3] > > > > I thought these two were supposed to be equivalent so I'm a bit confused here. > > > > In any event, I've tried all the commonsense ideas I've had and then spent some time flailing about randomly with Hold's but nothing seems to work correctly. Can anybody help me understand this? Thanks! > > > > Darrell
[toc] | [prev] | [next] | [standalone]
| From | drallenbio@gmail.com |
|---|---|
| Date | 2013-04-12 06:15 +0000 |
| Message-ID | <kk88qc$c0n$1@smc.vnet.net> |
| In reply to | #14141 |
Darrell,
I have an older version of the text, and I believe the question is poorly worded. When you see Mr. Wellin's expected answer you will realize what he was really asking. Here's his answer at the back of the older text.
Clear[z,a];
Hold[z=11];
a=9;
z+3/.z->a
Don't forget Mathematica Stackexchange - there is alot of good stuff there to learn Mathematica. (http://mathematica.stackexchange.com/)
Todd
[toc] | [prev] | [next] | [standalone]
| From | plank.in.sequim@gmail.com |
|---|---|
| Date | 2013-04-14 00:36 +0000 |
| Message-ID | <kkctmg$7ev$1@smc.vnet.net> |
| In reply to | #14165 |
On Thursday, April 11, 2013 11:15:40 PM UTC-7, drall...@gmail.com wrote: > Darrell, > > > > I have an older version of the text, and I believe the question is poorly worded. When you see Mr. Wellin's expected answer you will realize what he was really asking. Here's his answer at the back of the older text. > > > > Clear[z,a]; > > Hold[z=11]; > > a=9; > > z+3/.z->a > > > > Don't forget Mathematica Stackexchange - there is alot of good stuff there to learn Mathematica. (http://mathematica.stackexchange.com/) > > > > Todd I just want to make sure I understand this. It's essentially negating the effect of the original assignment to z, correct? That's an interesting solution but it kind of seems like "solving" it by putting it in comments. I guess if that's his answer - well, it's just not the answer I would have marked as correct. Anyway, it's interesting to see his answer from an earlier edition. Thanks!
[toc] | [prev] | [next] | [standalone]
| From | Roland Franzius <roland.franzius@uos.de> |
|---|---|
| Date | 2013-04-13 06:02 +0000 |
| Message-ID | <kkasd1$ril$1@smc.vnet.net> |
| In reply to | #14141 |
Am 11.04.2013 10:11, schrieb plank.in.sequim@gmail.com: > I'm loving Paul Wellin's book "Programming with Mathematica: An Introduction" and am trying to diligently do all the exercises. Most of them have answers in the back but I'm stuck on Section 4.2, Exercise 2 and there's no answer given. It gives the following Mathematica code: > > z = 11; > a = 9; > z + 3 /. z -> a > > 14 > > So "z+3" is being evaluated to 14 and then the substitution has no effect. He asks how to "use the Hold function in the compound expression to obtain a value of 12". I don't seem to be able to get this to work. My original thought was to hold z+3, but then the z in the replacement part gets evaluated so the replacement is actually 11->3 which doesn't match in the held z+3 expression. In fact, if you replace "z+3" with Hold[11+3] then you'll end up with Hold[9+3]. Curiously, this works differently if you use replace. > > In: Replace[Hold[11+3],11->9] > Out: Replace[Hold[11+3],11->9] > > In: Hold[11+3]/.11->9 > out: Hold[9+3] > > I thought these two were supposed to be equivalent so I'm a bit confused here. > > In any event, I've tried all the commonsense ideas I've had and then spent some time flailing about randomly with Hold's but nothing seems to work correctly. Can anybody help me understand this? Thanks! In order to block evaluation during input use "Unevaluated" z = 11; a = 9; Unevaluated[z + 3] /. z -> a "Hold" blocks evaluation completely, so its use needs an additional "ReleaseHold" after. #z = 11; a = 9; Hold[z +3] /. z -> a //ReleaseHold Of course you may use any other self made Identity function with attributes, Hold, HoldAll, HoldComplete,HoldAllComplete SetAttributes[id,HoldAll] id=Identity id[z]/.z->a -- Roland Franzius
[toc] | [prev] | [next] | [standalone]
| From | plank.in.sequim@gmail.com |
|---|---|
| Date | 2013-04-14 00:36 +0000 |
| Message-ID | <kkctm0$7el$1@smc.vnet.net> |
| In reply to | #14170 |
> In order to block evaluation during input use "Unevaluated" > > z = 11; > a = 9; > Unevaluated[z + 3] /. z -> a This seems closest in spirit to what I thought the answer was meant to be. I had to put a HoldPattern on the z in the right half of the substitution or it would evaluate to 11 and so end up as 11->a. Nice, though, because it didn't require the ReleaseHold.
[toc] | [prev] | [next] | [standalone]
| From | plank.in.sequim@gmail.com |
|---|---|
| Date | 2013-04-14 00:36 +0000 |
| Message-ID | <kkctlr$7b8$1@smc.vnet.net> |
| In reply to | #14141 |
Wow - plenty of great answers to contemplate! Thank you all so much!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web