       Re msg16940 Simplifying expressions

• To: mathgroup at smc.vnet.net
• Subject: [mg17118] Re msg16940 Simplifying expressions
• From: "David Bailey" <dave-bailey at freeuk.com>
• Date: Sat, 17 Apr 1999 03:35:11 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Cyril Fisher and Ted Ersek suggested a number of mathematically
valid transformations which are hard to perform with Mathematica
either because the desired result is unstable (i.e. it is evaluated
further by Mathematica, destroying the desired effect) or because
it is hard to specify the desired transformation in the first place.

Clearly the action of Simplify or FullSimplify (even controlled by a few
options)
cannot possibly cater for the range of manipulations that may count as
'simplifications' in various contexts.

I think this is an important problem, because it is nice to be able to use
Mathematica as an algebraic calculator and ideally it should be possible to
specify any desired valid rearrangement of an expression.

At http://www.salford.co.uk/mathematica/cexp_file.html you will find a
package (plus
documentation) that I described at last year's conference which uses colour
to
attack this problem. The idea is that parts of an expression can be coloured
by
click selecting them and using a palette. The colour is represented as an
inert
function (e.g. Red) together with frontend rules which render Red[a+b] (say)
as 'a+b'
in red. Several different colours can be attached to an expression (as
required)
and used as handles to drive a variety of transformations. Obviously,
colouring
an expression also limits the ability of the Mathematica evaluator to do its
work, so
that for example Red^64 will be represented nicely rather than being
collapsed to a meaningless number.

What follows is a notebook which applies these ideas to the problems
suggested
by Fisher and Ersek. Of course, these methods hardly seem worth it in such
toy
examples, but they become more valuable as the complexity of the problem
increases.

David Bailey
db at salford-software.com

Notebook[{
Cell[BoxData[
\(<< "\<colour.m\>"\)], "Input"],

Cell["\<\
(1) In the first example we use colour both to specify the desired operation
\
and to protect the result from re-evaluation.:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
RowBox[{
RowBox[{"Exp", "[",
TagBox[
StyleBox[\(a\ \((b + c)\)\),
FontColor->RGBColor[1, 0, 0]],
Red], "]"}], "//", \(RR1[Expand]\)}]], "Input"],

Cell[BoxData[
\(E\^\(a\ b + a\ c\)\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
RowBox[{
SuperscriptBox["E",
RowBox[{\(a\ b\), "+",
TagBox[
StyleBox[\(a\ c\),
FontColor->RGBColor[0, 1, 0]],
Green]}]], "//", "Gout"}]], "Input"],

Cell[BoxData[
RowBox[{\(E\^\(a\ b\)\), " ",
TagBox[
StyleBox[\(E\^\(a\ c\)\),
FontColor->RGBColor[0, 1, 0]],
Green]}]], "Output"]
}, Open  ]],

Cell[TextData[{
"Removing the colour lets ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" attack the expression again:"
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
RowBox[{
RowBox[{\(E\^\(a\ b\)\), " ",
TagBox[
StyleBox[\(E\^\(a\ c\)\),
FontColor->RGBColor[0, 1, 0]],
Green]}], "//", \(GG1[]\)}]], "Input"],

Cell[BoxData[
\(\[ExponentialE]\^\(a\ b + a\ c\)\)], "Output"]
}, Open  ]],

Cell[TextData[{
"(2) Here the desired end form is unstable - i.e. ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" will automatically evaluate it to something else. We threfore use colour
\
to protect it:"
}], "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\(Simplify[2/\((1 - w^2*x^2*\((y*z)\)^2)\)]\)], "Input"],

Cell[BoxData[
\(2\/\(1 - w\^2\ x\^2\ y\^2\ z\^2\)\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
RowBox[{
FractionBox["2",
RowBox[{"1", "-",
TagBox[
StyleBox[\(w\^2\ x\^2\ y\^2\ z\^2\),
FontColor->RGBColor[0, 1, 0]],
Green]}]], "//", \(GG1[Red[PowerExpand[Sqrt[#]]]^2&]\)}]],
"Input"],

Cell[BoxData[
FractionBox["2",
RowBox[{"1", "-",
SuperscriptBox[
RowBox[{"(",
TagBox[
StyleBox[\(w\ x\ y\ z\),
FontColor->RGBColor[1, 0, 0]],
Red], ")"}], "2"]}]]], "Output"]
}, Open  ]],

Cell[TextData[{
"(3) To simplify the next example in the way desired we have to supply a \
function that 'recognises' certain types of integers and represents them\n\
using colour to prevent ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" reversing the process:"
}], "Text"],

Cell[BoxData[
\(Special[x_Integer] :=
Red\^Log[2, x] /; \((x > 2\  && \ IntegerQ[Log[2, x]])\); \n
Special[x_] := x\)], "Input"],

Cell[CellGroupData[{

Cell[BoxData[
\(FullSimplify[\((18446744073709551616 - n)\)/5] /.
k_Integer :> Special[k]\)], "Input"],

Cell[BoxData[
RowBox[{\(1\/5\), " ",
RowBox[{"(",
RowBox[{\(-n\), "+",
SuperscriptBox[
TagBox[
StyleBox["2",
FontColor->RGBColor[1, 0, 0]],
Red], "64"]}], ")"}]}]], "Output"]
}, Open  ]],

Cell["\<\
(4) This problem is rather different in that it is only the order of the top
\
level summation which is 'wrong'. Colour cannot help hee, but
at least puts the powers in ascending order (descending order was
specified).\
\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\(poly =
2\ e\^2 - 4\ e\ h + 2\ h\^2 + 3\ e\ s - 3\ h\ s + 5\ k \((e - h)\)\^3;
\nnewpoly = Series[poly, {e, h, 5}] // Normal\)], "Input"],

Cell[BoxData[
\(2\ \((e - h)\)\^2 + 5\ \((e - h)\)\^3\ k + 3\ \((e - h)\)\ s\)],
"Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell[BoxData[
\(3\ s\ \((e - h)\) + 2\ \((e - h)\)\^2 + 5\ k\ \((e - h)\)\^3 //

Cell[BoxData[
\`5\ k\ \((e - h)\)\^3 + 2\ \((e - h)\)\^2 + 3\ s\ \((e - h)\)\)],
"Output"]
}, Open  ]],

Cell["\<\
(5) The final example is a bit unfair, since I doubt if the Series function
\
ever represents the result in the terms desired.  What is required is the \
n'th term of a series (whee 'n' is a variable). The function SeriesTerm in \
the package Rsolve provides this, but unfortunately it does not produce a \
result in this case. The only option
therefore is to check that the result is of the desired form and to leave it
\
(using colour) in this form:\
\>", "Text"],

Cell[CellGroupData[{

Cell[BoxData[
\(ser =
\((Series[BesselI[0, x], {x, 0, 8}] // Normal)\) /.
x\^p_ ->
\(x^p\ 2\^p\ \((\(\((p/2)\)!\))\)\^2\)\/\(\(Green\^p\)
\((\(Red[p/2]!\))\)\^2\)\)], "Input"],

Cell[BoxData[
RowBox[{"1", "+",
FractionBox[\(x\^8\),
RowBox[{
SuperscriptBox[
RowBox[{
TagBox[
StyleBox["4",
FontColor->RGBColor[1, 0, 0]],
Red], "!"}], "2"], " ",
SuperscriptBox[
TagBox[
StyleBox["2",
FontColor->RGBColor[0, 1, 0]],
Green], "8"]}]], "+",
FractionBox[\(x\^6\),
RowBox[{
SuperscriptBox[
RowBox[{
TagBox[
StyleBox["3",
FontColor->RGBColor[1, 0, 0]],
Red], "!"}], "2"], " ",
SuperscriptBox[
TagBox[
StyleBox["2",
FontColor->RGBColor[0, 1, 0]],
Green], "6"]}]], "+",
FractionBox[\(x\^4\),
RowBox[{
SuperscriptBox[
RowBox[{
TagBox[
StyleBox["2",
FontColor->RGBColor[1, 0, 0]],
Red], "!"}], "2"], " ",
SuperscriptBox[
TagBox[
StyleBox["2",
FontColor->RGBColor[0, 1, 0]],
Green], "4"]}]], "+",
FractionBox[\(x\^2\),
RowBox[{
SuperscriptBox[
RowBox[{
TagBox[
StyleBox["1",
FontColor->RGBColor[1, 0, 0]],
Red], "!"}], "2"], " ",
SuperscriptBox[
TagBox[
StyleBox["2",
FontColor->RGBColor[0, 1, 0]],
Green], "2"]}]]}]], "Output"]
}, Open  ]],

Cell["\<\
OK, this is a bit of a kludge, but it illustrates the fact that you could \
(say) store a range of series expansions in terms of the n'th term and then
apply them without destroying the structure of the coefficients using
colour.\
\
\>", "Text"]
},
FrontEndVersion->"Microsoft Windows 3.0",
ScreenRectangle->{{0, 1024}, {0, 740}},
WindowSize->{846, 605},
WindowMargins->{{40, Automatic}, {Automatic, 5}}
]

```

• Prev by Date: FW: Re: Plot & BoldPlot
• Next by Date: Re: List of function-heads
• Previous by thread: Re: Display (was: psfix)
• Next by thread: Stellar Oscillations