Re: Collect with Simplify destroys Hold

• To: mathgroup at smc.vnet.net
• Subject: [mg30708] Re: Collect with Simplify destroys Hold
• From: "Alan Mason" <amason2 at austin.rr.com>
• Date: Sat, 8 Sep 2001 02:24:04 -0400 (EDT)
• References: <9n18id\$26j\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Here is some code to get around the problem.  It's not as fast as the
built-in Collect[expr, patt, Simplify], but it should be correct in all
cases (unless I myself have a bug somewhere).

collectAndSimplify[expr_, patt_, h_:Simplify] :=
Module[{e, L, L2, xxt},
If[FreeQ[expr, Hold | HoldForm], Return[Collect[expr, patt, h]]
];
L = Union[Cases[expr, Hold[u__], {0, Infinity}]];
L2 = Table[{L[[i]], xxt[i]}, {i, Length[L]}];
e = expr /. ( Thread[Rule[#[[1]], #[[2]]]] & /@ L2);
e = Collect[e,  patt, h];
e = e /. (Thread[Rule[#[[2]], #[[1]]]] & /@ L2);
Return[e];
]
"Alan Mason" <amason2 at austin.rr.com> wrote in message
news:9n18id\$26j\$1 at smc.vnet.net...
> There is a serious bug in Mathematica 4.1 that affects the new extension
> Collect[expr, patt, h] to Collect[expr, patt].  It is supposed to collect
> patt in expr and then apply h to each collected part.  Unfortunately, if
> expr involves terms of the form Hold[expr2], the hold is destroyed (i.e.,
> expr2 is evaluated, which is an absolute no-no, and then Hold is rather
> absurdly applied to the result).  This is shown in the following short
> notebook.
>
> When working with differential operators that have been defined in terms
of
> other operators (e.g., in terms of the standard partial differential
> operators D_x, D_y, D_z), it is essential to keep them unevaluated until
the
> proper moment.  In the notebook,  J_y is the y component of the quantum
> mechanical angular momentum operator, expressed in terms of derivatives
with
> respect to the Euler angles.  The bug means that Collect[expr, patt,
> Simplify] cannot be used to collect and simplify expressions involving
these
> operators, which is too bad as it would be extremely useful.
>
> Does anyone know of a workaround? Since the error occurs with arbitrary
> heads h, the obvious idea (replacing Simplify by some head h, then
replacing
> h-> Simplify) does not work.
>
>
> In[1]:=
> \!\(\(\(J\_y\  = \ \(-\[ImaginaryI]\)\ \[HBar]\ Cos[\[Gamma]]\ Hold[
>           D\_\[Beta]] - \[ImaginaryI]\ \[HBar]\ Csc[\[Beta]]\ Hold[
>           D\_\[Alpha]]\ Sin[\[Gamma]] + \[ImaginaryI]\ \[HBar]\
> Cot[\[Beta]]\ \
> Hold[D\_\[Gamma]]\ Sin[\[Gamma]]\)\(\[IndentingNewLine]\)
>   \)\[IndentingNewLine]
>   test\  = \ Hold[J\_y]\ x\)
>
> Out[1]=
> \!\(\(-\[ImaginaryI]\)\ \[HBar]\ Cos[\[Gamma]]\ Hold[
>         D\_\[Beta]] - \[ImaginaryI]\ \[HBar]\ Csc[\[Beta]]\ Hold[
>         D\_\[Alpha]]\ Sin[\[Gamma]] + \[ImaginaryI]\ \[HBar]\
Cot[\[Beta]]\
> \
> Hold[D\_\[Gamma]]\ Sin[\[Gamma]]\)
>
> Out[2]=
> \!\(x\ Hold[J\_y]\)
>
> In[3]:=
> Collect[test, x, Simplify]
>
>
> Out[3]=
> \!\(x\ Hold[\(-\[ImaginaryI]\)\ \[HBar]\ Cos[\[Gamma]]\ Hold[
>             D\_\[Beta]] - \[ImaginaryI]\ \[HBar]\ Csc[\[Beta]]\ Hold[
>             D\_\[Alpha]]\ Sin[\[Gamma]] + \[ImaginaryI]\ \[HBar]\ \
> Cot[\[Beta]]\ Hold[D\_\[Gamma]]\ Sin[\[Gamma]]]\)
>
> In[4]:=
> Collect[test, x, h]
>
> Out[4]=
> \!\(x\ h[Hold[\(-\[ImaginaryI]\)\ \[HBar]\ Cos[\[Gamma]]\ Hold[
>               D\_\[Beta]] - \[ImaginaryI]\ \[HBar]\ Csc[\[Beta]]\ Hold[
>               D\_\[Alpha]]\ Sin[\[Gamma]] + \[ImaginaryI]\ \[HBar]\ Cot[\
> \[Beta]]\ Hold[D\_\[Gamma]]\ Sin[\[Gamma]]]]\)
>
>

```

• Prev by Date: Re: Sticky Previews? Mathematica Export, Illustrator, and Textures
• Next by Date: Re: = or := ???
• Previous by thread: SqrtMod function
• Next by thread: Good news from the Math Forum@Drexel