• To: mathgroup at smc.vnet.net
• Subject: [mg60806] Re: [mg60777] question about HoldForm
• From: "David Park" <djmp at earthlink.net>
• Date: Thu, 29 Sep 2005 05:41:16 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Ruth,

If you try the ExpressionManipulation package on the Mathematica page at my
web site you will find some tools for evaluating held expressions in a
controlled manner. The package extends the existing EvaluateAt command. Such
a routine was originally posted by Allan Hayes a long time valuable
contributer to MathGroup. Ted Ersek also helped with some of the code.

Needs["Algebra`ExpressionManipulation`"]

Don't set a value for x. Then here is one path of evaluation.

HoldForm[x^2 + 4*x + 4]
EvaluateAt[1, Factor][%]
% /. x -> 2
EvaluateAtPattern[Plus[__]][%]
EvaluateAt[1][%]                   giving...

x^2 + 4*x + 4
(2 + x)^2
(2 + 2)^2
4^2
16

Here is another path of evaluation.

HoldForm[x^2 + 4*x + 4]
% /. x -> 2
EvaluateAt[{{1, 1}, {1, 2}}][%]
EvaluateAt[1][%]				giving...

x^2 + 4*x + 4
2^2 + 4*2 + 4
4 + 8 + 4
16

There was another question on MathGroup yesterday that this package also
helps with.

The poster wanted to find the position in

aa = 1 + x + x y + Log[Sin[z]];

That contains x + Log[Sin[z]], and perhaps operate on it. The part can be
obtained by

Part[aa, {2, 4}]

but you can't use this, say, with ReplacePart (as far as I know).
ExpressionManipulation has an ExtendedPosition command which will treat a
subset of level parts as a 'position'.

ExtendedPosition[aa, x + Log[Sin[z]]]
{eP[{}, {2, 4}]}

where eP is a wrapper for extended positions, the first entry gives the top
level for the position (in this case the entire expression) and the second
entry gives the subset of level parts. We could then use this to operate on
the particular parts.

aa // EvaluateAt[ExtendedPosition[aa, x + Log[Sin[z]]], f]
1 + x y + f[x + Log[Sin[z]]]

In this case we could have used a replacement rule almost as easily. But
there is also an ExtendedPattern command that will find positions and
extended positions that fit some pattern.

David Park

From: Ruth Lazkoz [mailto:ruth.lazkoz at ehu.es]
To: mathgroup at smc.vnet.net

Hi,

I have this expression

x = 2; HoldForm[x^2 + 4x + 4]

Is there a way to operate on HoldForm so that I get (x^2+2)^2? If I cut
a paste the result and operate on it I obviously get 16.

Thanks,

Ruth

```

• Prev by Date: Re:Re: Vectorization
• Next by Date: Re: Importing (and dealing with) multiple files