Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When defining packages, can I put parameter values alone or along with functions into a package in subscripted form?

BeginPackage["Myfunction`"]; 
Subscript[q, 1] = 0;
Subscript[q, 2] = 0.5;
myfunction::usage ="...";
Begin["`Private`"];
myfunction[ak__List] := blablabla...;
End[];
EndPackage[];

Because these global subscripted parameters in the package are needed in many programs, I separated them from the main programs and put them into a single package. In the main programs, I only need to load the package rather than bothering to redefine these parameters again. For plain symbols, it works fine. But whem it comes to subscripted symbols, it fails. And I found that is because I used

  Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]];

in the main program. subscripted symbols like

  Subscript[q, 1] 

are transformed into

  q_Subscript_1

I also tried to use Symbolize function in the package, but it fails. The clarity for using subscripted symbols made me reluctant to avoid them.

share|improve this question
    
This is one of those question where there is most likely a deeper issue. I'm not sure whether simply answering it will really help you. –  halirutan Apr 10 '13 at 8:27
    
You should mention your main issue (that seems to have to do with Symbolize) in the question and esp. question title... –  Yves Klett Apr 10 '13 at 8:57
3  
You should avoid using sunscripted variables (see details here, point #4); assign values directly to symbols and not to Subscript-ed forms, exactly like halirutan did in his anwer, or use functional forms like dq[1]=0; dq[2]=0.5. –  István Zachar Apr 10 '13 at 8:58

3 Answers 3

up vote 2 down vote accepted

The question is, why not?

BeginPackage["Myfunction`"];
myfunction::usage = "blub";
Begin["`Private`"];
a = 3;
b = 10;
myfunction[] := Table[i, {i, a, b}];
End[];
EndPackage[];

Now you can just try it

myfunction[]

(* {3, 4, 5, 6, 7, 8, 9, 10} *)

or if you want to set one of your variables to another value, you can do that too. You just have to be aware, that you created it inside your package and it is in your private context:

Myfunction`Private`a = 0;
myfunction[]

(* {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
share|improve this answer
    
Thanks, halirutan. a little problem arose when I am using subscripted variables. In my main program, I used Symbolize[ParsedBoxWrapper[SubscriptBox["", ""]]] to transform all subscripted variables into symbols, I find that I can not use the subscripted variables in the package any more. –  novice Apr 10 '13 at 8:38
    
I've edited the @novice's post according to his intention (I hope), so you might want to modify your invocation. Sorry for any inconvenience :) –  István Zachar Apr 10 '13 at 9:02
    
@novice Can you please edit your question and give a short example what you try to do and what doesn't work? Is it stands, I don't understand what you are really asking. –  halirutan Apr 10 '13 at 10:38
    
@halirutan,I modified my question. I want to put all parameters (including subscripted ones) into a single package, then I call these data in the main program. –  novice Apr 11 '13 at 8:24
    
I found a way to use subscripted variables, that is transforming them into symbols first, then use symbol like q[UnderBracket]Subscript[UnderBracket]1 in the packages. –  novice Apr 15 '13 at 7:45

Generally I agree with what István Zachar wrote, which I'll quote here for permanence:

You should avoid using sunscripted variables (see details here, point #4); assign values directly to symbols and not to Subscript-ed forms, exactly like halirutan did in his anwer, or use functional forms like dq[1]=0; dq[2]=0.5.

However, I don't really understand the question as I don't know how you are using these subscripted variables. Perhaps you want to attach the rules to the symbol rather than Subscript. If that is the case please read these:

Clear complains that a variable is not a symbol or a string?

Can we use letter with a subscript as a variable in Mathematica?

share|improve this answer
    
I deleted my answer since it was for the original question. I'm not up for another "please don't use Subscript" answer, so I leave it for you instead of editing mine ;-) +1 –  halirutan Apr 10 '13 at 10:11
    
@halirutan I don't see how the question has substantially changed. Frankly I don't understand it before or after the edit as it is never shown how these subscripted values are used. I'm just blindly throwing darts here hoping to hit something the OP wants. –  Mr.Wizard Apr 10 '13 at 10:14
    
I could show you in chat what he probably means.. –  halirutan Apr 10 '13 at 10:17

Since the Notation package cannot be used inside another packages, I have to transform all the subscripted variables into symbols, then copy the symbols into the packages. For example:

<< Notation`;
Symbolize[ParsedBoxWrapper[SubscriptBox["_", "_"]]];
Subscript[q, 1];

then Subscript[q, 1] becomes q\[UnderBracket]Subscript\[UnderBracket]1 In the package, you can define q\[UnderBracket]Subscript\[UnderBracket]1=0;

In the main program, use Symbolize first due to the difference between Subscript[q, 1] and q\[UnderBracket]Subscript\[UnderBracket]1

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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