Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Subscript on plus expression

96 views
Skip to first unread message

Dave Snead

unread,
Nov 18, 2012, 4:03:36 AM11/18/12
to
Hi,

Mathematica fails to parenthesize the a+b in
Subscript[Plus[a,b],k]

so the output looks identical to
Plus[a,Subscript[b,k]]

Is this a bug?
Is there an option to force the parentheses?

Thanks in advance,
Dave Snead



Murray Eisenberg

unread,
Nov 18, 2012, 5:16:12 PM11/18/12
to

On Nov 18, 2012, at 3:57 AM, Dave Snead <dsn...@charter.net> wrote:

> Mathematica fails to parenthesize the a+b in
> Subscript[Plus[a,b],k]
>
> so the output looks identical to
> Plus[a,Subscript[b,k]]
>
> Is this a bug?
> Is there an option to force the parentheses?

I don't know off hand of any such option, but is it so unreasonable to
insert parentheses explicitly in the first? After all, that's surely
what you'd do if writing it with pencil on paper!

---
Murray Eisenberg
mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2838 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305






Nasser M. Abbasi

unread,
Nov 18, 2012, 5:20:19 PM11/18/12
to
Actually, just typing

(a+b)
k

and then hitting ENTER will result in

a+b
k

i.e. the () are gone. Same effect you showed.

It might be because subscript are meant to be applied to an atomic symbol
and not combined expression like a+b. I do not know. I am not even sure
what (a+b)_k is supposed to mean, math wise. Is this meant to
be on the code or in a text cell? If it is in the code itself,
how will it be used?

--Nasser




Bob Hanlon

unread,
Nov 18, 2012, 5:21:13 PM11/18/12
to
Format[Subscript[arg_Plus, k_]] :=
Subscript["(" <> StringJoin @@
Riffle[ToString /@ (List @@ arg),
" + "] <> ")", k]

Subscript[a + b + 2, k]


Bob Hanlon

Bob Hanlon

unread,
Nov 19, 2012, 5:05:06 PM11/19/12
to
Not clear what you are trying to accomplish. The following example
provides some approaches

s1[lambda_, t_] =
x[t] /. DSolve[{x'[t] == lambda, x[0] == 0}, x[t], t][[1]]

lambda t


t1[lambda_, value_] = t /. Solve[s1[lambda, t] == value, t][[1]]

value/lambda


s1[lambda, t1[lambda, value]]

value


Manipulate[
Module[
{ti = 0,
tf = 100,
pt = {t1[lambda, value], value}},
Plot[s1[lambda, t], {t, ti, tf},
PlotRange -> {0, 0.7},
Epilog -> {
Text[
{NumberForm[pt[[1]], {4, 1}],
NumberForm[pt[[2]], {4, 2}]},
pt, {1.5, -1.5}],
LightGray,
Line[{{ti, value}, pt}],
Line[{
{t1[lambda, value], 0}, pt}],
Red,
AbsolutePointSize[5],
Point[pt]}]],
{{value, 0.1}, 0, 0.7, 0.01,
Appearance -> "Labeled"},
{{lambda, 0.001}, 0.001, 0.007, 0.00025,
Appearance -> "Labeled"}]


Bob Hanlon


On Sat, Nov 17, 2012 at 3:49 AM, William Duhe <wjd...@loyno.edu> wrote:
> Bellow is a program that solves a diff eq, then finds a particular valued root for that eq. What I want to do is be able to plot how the root changes as a function of Lambda.
>
>
>
>
>
> m = Manipulate[Module[
> {ti, tf, s1},
> imgSize = 375;
>
> ti = 0;(*initial time*)
> tf = 100;(*final plot time*)
>
>
> s1 = NDSolve[{x'[t] == lambda, x[0] == 0}, x, {t, ti, tf}][[1]];
>
> t1 = FindRoot[x[t] == .1 /. s1, {t, tf}];
>
> delta[lambda] :=
> Module[{times, v, s = s1[lambda]},
> times = Quiet[
> Chop[FindRoot[x[t] == .1 /. s, {t, #}] & /@ {tf - 1, ti + 1}]];
> v = Flatten[x[t] /. s /. times];
> Subtract @@ v];
> Plot[delta[lambda], {lambda, 0, .007}, Frame -> True,
> Axes -> False,
> FrameLabel -> {"lambda", "Change in value of root"},
> ImageSize -> imgSize,
> Epilog -> {Text[ToString[pt, TraditionalForm], pt, {-1.25, 1}],
> Red, AbsolutePointSize[4], Point[pt]}]
>
> Column[{Plot[Evaluate[{Tooltip[x[t] /. s1, "x[t]"]}], {t, ti, tf},
> PlotRange -> Automatic, AxesLabel -> {"Time", "functions"},
> ImageSize -> 350, Exclusions -> Automatic],
> Row[{"x[t] = 1 when ",
> t1 =
> Quiet[Chop[
> FindRoot[x[t] == .1 /. s1, {t, #}] & /@ {tf, ti}]]}],
>
> "\n\n"}]], {{lambda, .001}, 0.001, 0.007,
> Appearance -> "Labeled"}]
>

Dave Snead

unread,
Nov 19, 2012, 5:05:27 PM11/19/12
to
Nasser --,

I'm using these as tensor indices.

Although Bob Hanlon offered a workaround (thanks Bob)
Format[Subscript[arg_Plus, k_]] :=
Subscript["(" <> StringJoin @@
Riffle[ToString /@ (List @@ arg),
" + "] <> ")", k]
Subscript[a + b + 2, k],

I think the folks at Mathematica should fix this and insert the parentheses.
The output of Subscript[Plus[a,b],k] should not be represented as
Plus[a,Subscript[b,k]]!

-- Dave

-----Original Message-----
From: Nasser M. Abbasi
Sent: Sunday, November 18, 2012 2:13 PM
Subject: Re: Subscript on plus expression

On 11/18/2012 3:03 AM, Dave Snead wrote:

Bill Rowe

unread,
Nov 19, 2012, 5:06:43 PM11/19/12
to
On 11/18/12 at 5:15 PM, mur...@math.umass.edu (Murray Eisenberg)
wrote:

>On Nov 18, 2012, at 3:57 AM, Dave Snead <dsn...@charter.net> wrote:

>>Mathematica fails to parenthesize the a+b in Subscript[Plus[a,b],k]
>>so the output looks identical to Plus[a,Subscript[b,k]]
>>Is this a bug? Is there an option to force the parentheses?

>I don't know off hand of any such option, but is it so unreasonable
>to insert parentheses explicitly in the first? After all, that's
>surely what you'd do if writing it with pencil on paper!

That won't help since Mathematica will evaluate (a+b) to a+b
removing the parentheses before the full expression is
evaluated. The only way I can think of to create something that
at least looks like a subscripted sum would be to enter it in a
manner where Mathematica will not evaluate any part of it.

For example, I could set the input cell format to be a text cell
then create the expression. Of course the end result isn't
something that can be used in other computations.


djmpark

unread,
Nov 19, 2012, 7:18:41 PM11/19/12
to
How about this?

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]}

Tests:

Subscript[a + b, k]

Subscript[a x, k]


David Park
djm...@comcast.net
http://home.comcast.net/~djmpark/index.html



From: Dave Snead [mailto:dsn...@charter.net]

Hi,

Mathematica fails to parenthesize the a+b in Subscript[Plus[a,b],k]

so the output looks identical to
Plus[a,Subscript[b,k]]

Is this a bug?
Is there an option to force the parentheses?

Dave Snead

unread,
Nov 27, 2012, 3:39:45 AM11/27/12
to
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] ?

Roland Franzius

unread,
Nov 28, 2012, 3:15:04 AM11/28/12
to
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

Bob Hanlon

unread,
Nov 28, 2012, 3:16:21 AM11/28/12
to
You could use a replacement rule.

ssr = ss_?(
# === Subscript || # === Superscript&)[arg_Plus,k_] :>
ss["(" <> StringJoin@@Riffle[ToString/@
(List@@arg), " + "] <> ")", k];

{Subscript[a+b, k], Superscript[a+b, k]}/.ssr


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

awnl

unread,
Dec 3, 2012, 3:18:11 AM12/3/12
to
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


0 new messages