0

I have a very basic syntax question for declaring $X_{hb}$ a variable.

f[Subscript[X, hb_]] := Subscript[X, hb]

In mathematica instead of writing the Subscript[] code, I simply use the ctrl+_ shortcut to place whatever I want in the subscript. However, after doing this I do not know how make mathematica consider $X_{hb}$ as a variable since placing the _ after it is considered to be an unrealted character.

See here: https://i.stack.imgur.com/JGz8F.png

How do I fix this issue?

Ozera
  • 125
  • 5
  • 8
  • Don't use Subscripts. 2. If you won't listen to rule 1, then see this answer to your question (http://mathematica.stackexchange.com/questions/1004/can-we-use-letter-with-a-subscript-as-a-variable-in-mathematica)
  • (http://mathematica.stackexchange.com/questions/13741/how-to-create-symbol-whose-name-has-subscript)

    – Searke Apr 08 '16 at 18:13
  • Try f[hb_]:=Subscript[X, hb]. – Kagaratsch Apr 08 '16 at 18:20
  • @Searke It seems like a very basic thing to do. Why is declaring variables qith subscripts a big no-no in mathematica? It's very common in math to write variables with subscripts. In any case, I am going to follow the "rule 1" as that is a lot of stuff to do for a work around. – Ozera Apr 08 '16 at 18:33
  • @Ozera Good programming notation is not good math notation. For example, using greek characters for variables in your program is also almost always a terrible idea. – Searke Apr 08 '16 at 18:36
  • 1
    @Ozera There is a logic behind how Subscripts work in Mathematica. Since subscripts are vague and often interpreted in contradictory ways, Mathematica treats them like generic expressions and requires that you tell it what they mean. Most people don't realize how vague and contradictory their different uses of subscripts tend to be. – Searke Apr 08 '16 at 18:38
  • @Searke Ah. I thought in Mathematica using greek letters would be okay. Thank you for your help and information about how subscripts actually work! – Ozera Apr 08 '16 at 18:38
  • 2
    @Ozera You can use greek letters as variables just fine in Mathematica. It's just terrible form to program with undescriptive variable names. Write descriptive variable names. Write "variance" instead of using a sigma. This is a rule to follow in practically any programming language, not just Mathematica. – Searke Apr 08 '16 at 18:40
  • 2
    I think Greek letters are not a problem in mathematica itself (but is a problem with stack exchange). Instead Subscripts are really a problem. The warning about subscripts is therefore a very good advice. – andre314 Apr 08 '16 at 18:43
  • okay. Thank you all for the information ^.^! – Ozera Apr 08 '16 at 18:46
  • 1
    @andre undescriptive variable names are one of the worst parts of debugging other people's Mathematica code. In no other language do people so often use single letter variable names. My worst experience was a notebook where someone has used "t", "tau", a gothic version of "t", and an ancient aramaic character that looks like "t" just because he wanted several different variables that all looked liked "t". – Searke Apr 08 '16 at 18:50
  • Use Format to display the variable X[hb] in a subscripted form. Format[X[hb_]] := Subscript[X, hb]; {X[hb], X[a], X[1]} – Bob Hanlon Apr 08 '16 at 19:00
  • 1
    @Searke Descriptive variables and above all descriptive functions are in the philosophy of Mathematica since the beginning (1980s !) and it's a very good thing (long function names were no more a problem thanks to word completion, at this time other langages were full of cryptic names that make them unusable except for the specialist). I'm surprised by your experience. – andre314 Apr 08 '16 at 19:02
  • Finally, note that you don't declare variables in Mathematica. Rather, you assign to them OwnValues, DownValues, UpValues, and SubValues. – march Apr 08 '16 at 21:09