Am I allowed to use subscript in function arguments?

Mathematica seems to allow function arguments with subscript but the coloring (typesetting seems to be different). Does anyone have experience with that?

Please check the screenshot.
mathemticaRoomFactorLookup.png

Kind Regards,
Mathias Breuss

Hi Matthias,

This is generally not a good idea: WR_ (subscripted) is seen as:

Times[Blank[],Subscript[W,R]]

while WR_ (no subscript) is seen as:

Pattern[WR,Blank[]]

Which are fundamentally different. Moreover, the first argument of Pattern must be a symbol, i.e. not something with a Head different from Symbol (e.g. Subscript). Try not to use subscripts in Mathematica, it doesn’t work very nice. See for example:

r=2;
r+Subscript[r, a]

outputs:

2+Subscript[2, a]

I.e. the ra (subscript) is seen as variable r with a subscript, not a new variable by itself.

It can be done by putting a colon between the subscripted object and the underscore.

f[ Subscript[k, 1]:_, Subscript[k, 2]:_ ] := 1- (Subscript[k, 1] Subscript[k, 2] ) / (Subscript[k, 1] - Subscript[k, 2])

SubscriptedArguments.jpg

Subscripted objects can be used in executable code, but doing it robustly takes work.

See the attached notebook for a mini-tutorial.

If you can postpone subscripted variables for the presentations, after the calculations are finished, it might be much less trouble.

I tried your code in Mathematica 9 and 10 and both give me this error:

 Syntax::sntxf: "f[" cannot be followed by "Subscript[k,1]:_,Subscript[k,2]:_]".

But even if one could make it work, I still think one should try to avoid it. It is very easy to ‘break’ your code. For this example there is no reason to just write WR. I feel that one should avoid the added complexity and possibility to introduce (syntax) errors…

Sander, what Bruce didn’t mention in the text of his post (but which he does show in his notebook) is that one has to load the Notation` package and use the Symbolize function before using his approach.

I see now, thanks for clarifying!

Thank you all for your response.

@Bruce, I was wondering if there is something different (i.e. your suggestion still remains) if I define directly compiled function by using Compile[{a,b},a*b]. I assume that I still need to use Symbolize from the notation package? But not the colon anymore?

My intention is to later compile those function to C for later use in an C# application.

If you have Symbolize’d the variables, I expect it would work.

Feel free to experiment and let us know the result.