Re: Help with output form?
- To: mathgroup at smc.vnet.net
- Subject: [mg60925] Re: [mg60902] Help with output form?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 4 Oct 2005 01:24:54 -0400 (EDT)
- References: <200510030806.EAA00162@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 3 Oct 2005, at 17:06, aufgang wrote: > I am struggling to get mathematica to output certain expressions the > way I'd like and i'm wondering if anyone could help me. > > normally, when you input an expression using the Set operator (=), > mathematica evaluates the expression, assigns the rhs to the lhs and > returns the evaluated rhs (eg. y=Expand[(a+b)^2] -> a^2+2 a b +b^2) > > what I would like to do is coax mathematica to output the lhs as > wel as > the evaluated rhs (ie. y=Expand[(a+b)^2] -> y=a^2+2 a b +b^2). > > I want the rhs to be evaluated like normal, and the value of the > lhs to > be set to rhs, so so Hold, HoldForm, etc wont work (they keep the lhs > but wont evaluate the rhs and the value isnt actually set) and I > know I > dont want to be using == instead of =. i just want the lhs to appear > in the output. > > the reson I want to do this is because I am calling the Mathematica > Kernel from a web app and I want to display this output format to the > users. > > I suspect I could do this somehow by redefining Set or something but i > can't seem to figure that out. > > Thanks for any help. > > What you are asking for is strictly speaking impossible, since if y has been assigned a^2+2 a b +b^2 as its value than whenever y is evaluated it will be replaced by a^2+2 a b +b^2. There is no way to do this without using some form of Hold. Probably the easiest approach is: y = Expand[(a + b)^2]; OwnValues[y][[1]] HoldPattern[y] :> a^2 + 2*b*a + b^2 If you do not want to see HoldPattern in your output you can replace it by HoldForm: y = Expand[(a + b)^2]; OwnValues[y][[1]] /. {HoldPattern -> HoldForm} HoldForm[y] :> a^2 + 2*b*a + b^2 In StandardForm or TraditionalForm you won't see the HoldForm. If you still do not like :> you cannot simply replace it by = (Set) because then Mathematica will attempt to set HoldForm[y] to the RHS (can't be done without unprotecting HoldForm) and in any case the output again would be just the RHS. If you really insist on having Set in your output you have to wrap everything in HoldForm, e.g. Block[{x}, y = Expand[(a + b)^2]; HoldForm[y -> x] /. {x -> y, Rule - > Set}] HoldForm[y = a^2 + 2*b*a + b^2] Andrzej Kozlowski
- Follow-Ups:
- Re: Re: Help with output form?
- From: Chris Chiasson <chris.chiasson@gmail.com>
- Re: Re: Help with output form?
- References:
- Help with output form?
- From: "aufgang" <aufgang@agile.net>
- Help with output form?