Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12483 > unrolled thread

Superscript on plus expression

Started by"Dave Snead" <dsnead6@charter.net>
First post2012-11-27 08:39 +0000
Last post2012-12-03 08:18 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Superscript on plus expression "Dave Snead" <dsnead6@charter.net> - 2012-11-27 08:39 +0000
    Re: Superscript on plus expression Roland Franzius <roland.franzius@uos.de> - 2012-11-28 08:15 +0000
    Re: Superscript on plus expression awnl <awnl@gmx-topmail.de> - 2012-12-03 08:18 +0000

#12483 — Superscript on plus expression

From"Dave Snead" <dsnead6@charter.net>
Date2012-11-27 08:39 +0000
SubjectSuperscript on plus expression
Message-ID<k91u8h$5l1$1@smc.vnet.net>
Hi,

Mathematica fails to parenthesize the a+b in Superscript[Plus[a,b],k]
so the output looks identical to
Plus[a,Superscript[b,k]]

Previous suggestions that worked to fix a similar problem for the Subscript 
case, i.e.,

Format[Subscript[arg_Plus, k_]] :=
Subscript[
  "(" <> StringJoin @@ Riffle[ToString /@ (List @@ arg), " + "] <>
   ")", k]

and

MakeBoxes[Subscript[(head : (Plus | Times))[args__], k_],
  form : (StandardForm | TraditionalForm)] :=
InterpretationBox[#1, #2] & @@ {SubscriptBox[
    RowBox[{"(", ToBoxes[head[args], form], ")"}], ToBoxes[k, form]],
   Subscript[head[args], k]}

don't carry over analogously to the Superscript case!

Format[Superscript[arg_Plus, k_]] :=
Superscript[
  "(" <> StringJoin @@ Riffle[ToString /@ (List @@ arg), " + "] <>
   ")", k]

and

MakeBoxes[Superscript[(head : (Plus | Times))[args__], k_],
  form : (StandardForm | TraditionalForm)] :=
InterpretationBox[#1, #2] & @@ {SuperscriptBox[
    RowBox[{"(", ToBoxes[head[args], form], ")"}], ToBoxes[k, form]],
   Superscript[head[args], k]}

don't work!


How can I get Mathematica to parenthesize the sum in
Superscript[Plus[a,b],k] ?

Thanks in advance,
Dave Snead




[toc] | [next] | [standalone]


#12487

FromRoland Franzius <roland.franzius@uos.de>
Date2012-11-28 08:15 +0000
Message-ID<k94h68$bfi$1@smc.vnet.net>
In reply to#12483
Am 27.11.2012 09:39, schrieb Dave Snead:
> Hi,
>
> How can I get Mathematica to parenthesize the sum in
> Superscript[Plus[a,b],k] ?

Seems to be impossible to intercept output of Superscript by Format. 
Defining

Format[superscript[a_Plus,b_]]:= Superscript[MatrixForm[{a}],b]

is working.

One may catch Superscript[Plus ,..] by  postprocessing output:

$Post=
If[ MatchQ[#,Superscript[_Plus,_],
     (MapAt[MatrixForm[{#}]&), #, 1],
     #]&

Superscript is a special text case because the input parser reads it as 
power, which is a pain for index gynm. On the other hand even in text 
cells it is not possible to avoid brackets in mixed tensor index 
super/subscripts.

Probably, the best way is to write a tensor index Format routine with
a List of {index-x-position,index-y-position, indexvalue} for each 
index, a task not a too difficult with MakeBoxes.

-- 

Roland Franzius

[toc] | [prev] | [next] | [standalone]


#12505

Fromawnl <awnl@gmx-topmail.de>
Date2012-12-03 08:18 +0000
Message-ID<k9hn83$j0$1@smc.vnet.net>
In reply to#12483
Hi,

> Mathematica fails to parenthesize the a+b in
> Superscript[Plus[a,b],k] so the output looks identical to
> Plus[a,Superscript[b,k]]

it's probably interesting to note that this specific behavior seems to
vary between version versions 5 and 7 will show the parentheses for
Superscript[Plus[a,b],k], while 6 and 8 don't. Considering that I'm not
convinced these changes and the behavior in version 8 were made on
purpose (no changes are mentioned for version 8 in the documentation,
too). Probably what you see really is a bug...

> Previous suggestions that worked to fix a similar problem for the
> Subscript case, i.e.,
>
> Format[Subscript[arg_Plus, k_]] := Subscript[ "(" <> StringJoin @@
> Riffle[ToString /@ (List @@ arg), " + "] <> ")", k]
>
> and
>
> MakeBoxes[Subscript[(head : (Plus | Times))[args__], k_], form :
> (StandardForm | TraditionalForm)] := InterpretationBox[#1, #2] & @@
> {SubscriptBox[ RowBox[{"(", ToBoxes[head[args], form], ")"}],
> ToBoxes[k, form]], Subscript[head[args], k]}
>
> don't carry over analogously to the Superscript case!
>
> Format[Superscript[arg_Plus, k_]] := Superscript[ "(" <> StringJoin
> @@ Riffle[ToString /@ (List @@ arg), " + "] <> ")", k]
>
> and
>
> MakeBoxes[Superscript[(head : (Plus | Times))[args__], k_], form :
> (StandardForm | TraditionalForm)] := InterpretationBox[#1, #2] & @@
> {SuperscriptBox[ RowBox[{"(", ToBoxes[head[args], form], ")"}],
> ToBoxes[k, form]], Superscript[head[args], k]}
>
> don't work!

Roland has already mentioned that this is most probably due to the early
interpretation of Superscript as Power. I guess they use some kind of 
shortcut for that case which circumvents the user accessable functions 
for performance reasons...

> How can I get Mathematica to parenthesize the sum in
> Superscript[Plus[a,b],k] ?
>

Here is another workaround which maps Superscript to Subsuperscript and 
then uses the above trick to format the Subsuperscript which does work 
as expected:

Superscript[arg_Plus, k_] := Subsuperscript[arg, "", k]

ormat[Subsuperscript[arg_Plus, "", k_]] :=
  Interpretation[Subsuperscript[
    Row[{
      Style["(", ShowStringCharacters -> False],
      arg,
      Style[")", ShowStringCharacters -> False]
      }],
    Style["", ShowStringCharacters -> False],
    k
    ], Subsuperscript[arg, "", k]]

you could use anything that doesn't show instead of the empty string, 
e.g. a Spacer[0]. The Interpretation and ShowStringCharacters options 
are there so you can copy and paste and use % for the generated output 
as usual. With that workaround you would of course need to make all 
definitions you planed to make for Superscript now for Subsuperscript 
with an empty string as second argument instead. If you are only needing 
this to produce nicely formatted output that shouldn't be a problem, 
though...


cheers albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web