MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: question about HoldForm

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


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.


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
EvaluateAt[1][%]                   giving...

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

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

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
djmp at

From: Ruth Lazkoz [mailto:ruth.lazkoz at]
To: mathgroup at


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.



  • Prev by Date: Re:Re: Vectorization
  • Next by Date: Re: Importing (and dealing with) multiple files
  • Previous by thread: Re: question about HoldForm
  • Next by thread: Re: Re: WORD document