Message Boards Message Boards

1
|
20792 Views
|
7 Replies
|
10 Total Likes
View groups...
Share
Share this post:

Am I allowed to use subscript in function arguments?

Posted 11 years ago

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.enter image description here

Kind Regards, Mathias Breuss

POSTED BY: Mathias Breuss
7 Replies

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.

POSTED BY: David Reiss

I see now, thanks for clarifying!

POSTED BY: Sander Huisman

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

StandardForm version of above code

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.

Attachments:
POSTED BY: Bruce Miller

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...

POSTED BY: Sander Huisman
Posted 11 years ago

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.

POSTED BY: Mathias Breuss

If you have Symbolize'd the variables, I expect it would work.
Feel free to experiment and let us know the result.

POSTED BY: Bruce Miller

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.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract