2
$\begingroup$

I have a function in scope that returns an expression with a variable t

BeginPackage["Pkg`"]
F1::usage = "";
Begin["scope`"]
F1[x_] := Sin[t + x];
End[]
EndPackage[]

When I use it from outside

In[159]:= F1[2]
Out[159]= Sin[2 + scope`t]

I can Replace scopettot` outside

In[169]:= F1[2] /. scope`t -> t
Out[169]= Sin[2 + t]

But is there any way to replace it from the inside of Package ?

I have tried replacing t with "t"

BeginPackage["Pkg`"]
F1::usage = "";
Begin["scope`"]
F1[x_] := Sin[t + x] /. t -> "t";
End[]
EndPackage[]

This looks like it works

In[223]:= F1[2]
Out[223]= Sin[2 + t]

However it is actually Sin[2 + "t"] So I cannot plot it with variable t

Plot[F1[2], {t, 0, 200}]

doesn't plot anything

$\endgroup$
  • 5
    $\begingroup$ You need to define t as a public variable as well as F1, with a usage message in the same way. $\endgroup$ – KraZug Nov 11 '16 at 13:02
  • $\begingroup$ @KraZug You should write your comment up as an answer, it seems like the most elegant solution & helped me out with a similar problem. $\endgroup$ – Chris K Nov 11 '16 at 19:45
  • $\begingroup$ @Chris The reason I didn't was because I assumed that the question would get closed. $\endgroup$ – KraZug Nov 11 '16 at 19:51
3
$\begingroup$

t needs to be an argument of your function F1 like so:

BeginPackage["Pkg`"]
F1::usage = "";
Begin["scope`"]
F1[x_, t_] := Sin[t + x];
End[]
EndPackage[]

The calling it:

F1[x, t]
(*Sin[t + x]*)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Is there any way to force t to be a variable not a value so that it doesn't conflict with a global variable t declared in user's notebook ? $\endgroup$ – Neel Basu Nov 12 '16 at 18:57
  • $\begingroup$ @NeelBasu, I don't understand what you are looking for. Can you show (code) what is conflicting in this case? $\endgroup$ – user21 Nov 12 '16 at 19:37
  • $\begingroup$ If I have t=42 in my Notebook prior invoking F1[x, t] it will use that t and return Sin[x + 42]. So is there any way such that user is forced to pass a variable only. or the symbol is used instead of its value even if a predefined variable is passed. $\endgroup$ – Neel Basu Nov 13 '16 at 13:41
  • $\begingroup$ Use something like 't_Symbol' in the definition of F1. Have look in the documentation under pattern matching. $\endgroup$ – user21 Nov 13 '16 at 20:51
2
$\begingroup$

Simply add a usage definition the variable t as well as F1 (and in general, anything you want to return from your package)

BeginPackage["Pkg`"]
F1::usage = "";
t::usage = "time variable";
Begin["scope`"]
F1[x_] := Sin[t + x];
End[]
EndPackage[]

Then the t that is returned works fine.

| improve this answer | |
$\endgroup$
  • $\begingroup$ If the user code uses a variable t it use that instead $\endgroup$ – Neel Basu Nov 12 '16 at 18:50
  • $\begingroup$ This code does not work when there is a global definition for t. E.g. try t=1 and then run your code. $\endgroup$ – user21 Nov 12 '16 at 19:35

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