MathGroup Archive 2006

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

Search the Archive

RE: Re: Symbolize Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69750] RE: [mg69682] Re: [mg69678] Symbolize Problem
  • From: "David Park" <djmp at earthlink.net>
  • Date: Fri, 22 Sep 2006 01:04:21 -0400 (EDT)

Chris,

I was not able to understand what the original poster wanted to do, so I put
off a response. But your posting stimulated me to respond.

I would like to present a construction that is very useful and quite easy
for formatting expressions into common textbook forms. (I learned this
method from Ted Ersek who said that he in turn had learned it from Neil
Soiffer of WRI.)

The construction involves MakeBoxes and InterpretationBox. The trick is that
InterpretationBox has the attribute HoldAllComplete (I'm not certain why)
and so a construction has to be used that allows the specific formatting to
be computed when necessary. This is achieved by writing InterpretationBox as
a pure function and applying it to the list of computed arguments.

As an example, suppose we want to format MatrixExp so it will format as a
regular exponential when it holds a symbolic argument, that is when it is
unevaluated. We can use the following construction.

MakeBoxes[MatrixExp[x_], form : StandardForm | TraditionalForm] :=
  InterpretationBox[#1, #2, SyntaxForm -> "^"] & @@
    {SuperscriptBox["\[ExponentialE]", MakeBoxes[x, form]], MatrixExp[x]}

Then we can use this as follows:

MatrixExp[\[Theta]*J]
% /. J -> {{0, -1}, {1, 0}}

Exp[J x]
{{Cos[x], -Sin[x]}, {Sin[x], Cos[x]}}

The first argument of IntepretationBox gives the format for the displayed
output and the second argument gives the internal representation. If you
look at FullForm, for example, you will get the internal representation,
MatrixExp[J x] in this case. The output can be copied and pasted. It can be
edited, unless you block it, but the editing will have no effect.

The SyntaxForm option has the default value of Automatic, which will
probably be good enough in most cases. In any case, I believe it gives the
precedence grouping for the formatted output, but am not entirely clear
about it.

This is a much better method than using Format, which can't usually be
copied and pasted. Note that the input is still a standard expression
without box format. But using box format for input is not actually very
convenient. You have to have code to bring up a box structure and then you
have to tab around it in. So having standard input with formatted output is
far easier. I find that this method works for all the formatting I have to
do - and I do a lot of it.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

From: Chris Chiasson [mailto:chris at chiasson.name]
To: mathgroup at smc.vnet.net

I went through a phase with the Notation package too. It is quite
useful, but I recommend just working directly with Format + MakeBoxes
and MakeExpression if you're going to change things. Think of the
Notation package as a graphical wrapper for defining
MakeBoxes/MakeExpression rules via the notebook interface instead of
typing everything out. Anyway, you could probably guess how to define
the correct MakeExpression rule just from looking at the source code
you provided to us.

MakeExpression[RowBox[{SuperscriptBox["\[Del]","2"],whatever_String}],_]:=
  MakeExpression["delSquared"<>whatever]

I strongly caution you to stay away from MakeExpression. Just think of
the problems WRI has with TraditionalForm input - and they invented
the Mathematica box model. Format & MakeBoxes, OOTH, are nice
functions for formatting your output.

On 9/20/06, Peter <pjcrosbie at attglobal.net> wrote:
> I am attempting to write more readable code by defining some custom
> symbols.  I can get simple constructions to sumbolize but I fail on
> anything too complicated.  For example,
>
> Symbolize[$B"&(B_]
>
> Cell[BoxData[
>     RowBox[{"Symbolize", "[",
>       TagBox["\[EmptyDownTriangle]_",
>         NotationBoxTag,
>         TagStyle->"NotationTemplateStyle"], "]"}]], "Input"]
>
> Works fine and symbolizes things like $B"&(Bf without problems.
>
> However, if I add a superscript to the $B"&(B as in
>
>  Cell[BoxData[
>     RowBox[{"Symbolize", "[",
>       TagBox[
>         RowBox[{
>           SuperscriptBox["\[EmptyDownTriangle]", "2"], "_"}],
>         NotationBoxTag,
>         TagStyle->"NotationTemplateStyle"], "]"}]], "Input"]
>
> then the symbol following the $B"&(B^2 is always interpreted as a
> multiplication.
>
> Any ideas would be really helpful.
>
>


--
http://chris.chiasson.name/



  • Prev by Date: Re: does the following code shut down anyone else's kernel? - why?
  • Next by Date: Re: Re: Symbolize Problem
  • Previous by thread: Re: Re: Symbolize Problem
  • Next by thread: Iterative constants and variables definitions