0
$\begingroup$

I'm really new with Mathematica, and I could not find a previously question similar to this one.

Is there a way that Mathematica can be made to recognize and treat a subscript letter as variable so that I could write equations like:

$\qquad a_{n} = n + 1 $

and have the evaluation of $a_{5}$ return 6.

Note: I'm actually trying to write equation for commutators like $[L_n,\, L_m] = n - m$

$\endgroup$
4
  • 4
    $\begingroup$ You can do it now, but problems occasionally occur. Consider, for instance, a[n] instead. $\endgroup$
    – bbgodfrey
    Commented Sep 6, 2019 at 1:09
  • 5
    $\begingroup$ Possible duplicate of Can we use letter with a subscript as a variable in Mathematica? Also see An alternative to subscripts? and Basic syntax issues (point #3). $\endgroup$
    – WReach
    Commented Sep 6, 2019 at 4:43
  • $\begingroup$ @WReach I'm afraid OP's question isn't a duplicate of that one. Though OP mentions "variable" in his post, according to the formula, OP is probably looking for something like Subscript[a, n_] = n + 1. $\endgroup$
    – xzczd
    Commented Sep 6, 2019 at 7:29
  • $\begingroup$ Such a functional assignment would require the redefinition of Subscript which will SURELY cause unwanted troubles elsewhere in Mathematica where subscripts are intended to use with no underlying definition. Modifying built-in symbols is not advised. On the other hand, you can make a[n] to format as a subscripted symbol that is still interpretable as a[n] or to whatever it evaluates due to its definition. $\endgroup$ Commented Sep 6, 2019 at 10:22

1 Answer 1

5
$\begingroup$

I would strongly recommend not using Subscript. One should think of Subscript as a typographical construct (which can be abused to use subscripting of a variable). Better is to use: L[n]

comm[L[n_], L[m_]] := n - m

Then you can call the function like this:

comm[L[15], L[8]]
7
$\endgroup$
1
  • $\begingroup$ Yeah, I actually got a way do use subscript as variable, but the program was 10 times slower, so I gave up and used your idea. Thanks for answering! $\endgroup$ Commented Sep 7, 2019 at 4:46

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