0
$\begingroup$

When I want to sum over indexed variables, indices being subscripts, the result works as expected, however, upon loading the Notation package, it doesn't. See below

enter image description here As one might have expected from the notation package, the subscript "n" does not change its values inside the sum. In this case however, it is clear that this would be wanted. Is there a way to achieve this?

I tried the following:

ClearNotations[]
mysum = Sum[Subscript[f,n],{n,0,2}]
Symbolize[ParsedBoxWrapper[SubscriptBox["_","_"]]]

where the Subscript[f,n] is done via Ctrl+_. Unfortunately, doing something like:

mysum /. Subscript[f,0] -> 1

where, again, I used Ctrl+_, then the rule does not apply! Looking at FullForm of test, I can see why, in that the output is:

Subscript[f, 0]

instead of:

   f\[UnderBracket]Subscript\[UnderBracket]0

In the worst case scenario, I can live with instead defining Summation "in the usual way" as:

mysumnew = Sum[f[n],{n,0,2}]

if there is a way to automatically "pretty print" the f[n] as subscripts. Unfortunately, it is not possible to do:

mysumnew /. f[n_] -> Subscript[f,n]

as the subscripts are all symbolized and Mathematica will therefore literally replace all the terms f[0], f[1] and f[2] with Subscript[f,n]!

$\endgroup$
0

3 Answers 3

2
$\begingroup$

It is generally a bad idea to use subscripts in this way in Mathematica. The use of indexed variables is at least as clear visually and does not cause many of the problems you will run into when using subscripts. For example, your problem does not happen when using the construction:

 s = Sum[f[n] x^n, {n, 0, 3}]
 f[0] + x f[1] + x^2 f[2] + x^3 f[3]

and there is no need to invoke a special package. In Mathematica, subscripts are a formatting tool, and Subscript[f, n] is not a symbol in the same way that f[n] is a symbol. In order to display everything in your final form, you can use:

s /. {f[n_] -> Subscript[f, n]}

enter image description here

$\endgroup$
1
$\begingroup$

The previous answers give very good reasons to avoid both subscripts and the Notation package. But if you must, you need to watch for pitfalls. Your first expression plus I have added an unsubscripted f.

sum = Sum[Subscript[f, n]*x^n, {n, 0, 2}] + f
(*Subscript[f, 2]*x^2 + Subscript[f, 1]*x + f + Subscript[f, 0]*)

which evidently is what you want. You probably don't want this, however:

sum /. f -> 1
(*Subscript[1, 2]*x^2 + Subscript[1, 1]*x + Subscript[1, 0] + 1*)

Yuch, and one of the main reasons to avoid subscripts.

I won't do it here, because it doesn't translate cleanly, but your Symbolize command makes each subscripted variable a single symbol such that f and n are no longer separate entities. It is like assigning a variable ab, then later assigning b=1 and expecting ab to become a1.

But sum /. f->1 now only changes the unsubscripted f as desired. Your n will stay n though.

$\endgroup$
0
$\begingroup$

I am not clear on what exactly you are trying to do, and the code

Sum[ x^n f_n /. {"n" -> ToString[n]}, {n, 0, 2}]

may do what you want but I can't tell. Note that f_n is $\texttt{f}_\texttt{n}$ your subscripted variable.

You may want to read some related questions. For example, question 120278 "Subscripted (superscripted) variables in Mathematica".

$\endgroup$
1
  • $\begingroup$ I have re-worked the question. Hopefully, it is clearer. $\endgroup$
    – Patrick.B
    Feb 23, 2019 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.