1
$\begingroup$

I'm fairly new to the Wolfram Language and I'm having a problem with how packages work.

I have some function that are inside a package, the goal of those function is to create random polynomial functions and plot them.

I've tested those functions in a notebook file and I had no problem with it.

If I put those functions in a package the function gives me a strange result with Private`x instead of only x (img below).

(* Inside package*)
DrawRandomPoly[] := 
    Module[{p,x},
    p = MakeRationalPolynomial[0,1];
    Row[{"ƒ(\[FormalX]) ", p //DisplayForm, Plot[p,{x,-10,10},ImageSize->400, Epilog->{PointSize[Large],Point[{Last[FunctionDiscontinuities[p,x]],0}]}],"\!\(\*UnderscriptBox[\(\[Limit]\), \(\[FormalX]\[LongRightArrow]\[Alpha]\)]\) ƒ(\[FormalX])",Limit[p,x->Last[FunctionDiscontinuities[p,x]]]},"\[DoubleLongRightArrow]"]
    ]


MakeRationalPolynomial[n_,n2_] := 
    Module[{num,den,f},
        num=MakePolynomial[n,x];
        den=MakePolynomial[n2,x];
        Evaluate[num]/Evaluate[den]
        ]

MakePolynomial[n_Integer,x_Symbol] := Module[{z,c}, z = RandomChoice[{-1,1}] RandomInteger[{1,10}];
    c = Table[RandomInteger[{-10,10}], {n}];
    FromDigits[Reverse[AppendTo[c,z]],x]]

Private x

I don't understand what's the problem when I call the DrawRandomPoly function from an imported package. What can be the cause of this problem? Thanks

$\endgroup$
2
  • 1
    $\begingroup$ Just a note that you seem to be using Begin["Private`"] in your package when you should be using Begin["`Private`"] instead. $\endgroup$
    – Szabolcs
    Commented May 9, 2022 at 14:23
  • $\begingroup$ Probably not really an answer to your question, but as I see you are using it already, why don't you just use [\FormalX] everywhere in your code instead of x? That would be what I think it has been introduced for... $\endgroup$ Commented May 9, 2022 at 20:00

1 Answer 1

3
$\begingroup$

There's no real problem here. Symbols have a context, and so their actual name is a fully qualified "path" that includes the containing context. When the context of the symbol matches the current context, the short form of the name is displayed.

I assume that what happened here is that you referenced a variable x in the Private context of your package, and apparently, that variable was not "published" for use outside the Package (I'm being loose with terminology here but don't want to go into the whole BeginPackage versus Begin discussion). I'm guessing that you're doing the plot from within the Global context, and so Mathematica is being helpful by showing fully qualified names for symbols not in the current context.

There are probably several options here, but maybe the simplest is to make your published package functions accept the symbol that you want to use. That way, when you call them from Global, you'll (presumably) be passing a Global symbol, and Mathematica will display the short name. This means reworking your strings so they don't hard-code "x" as the assumed symbol name.

You might also find inspiration from SymbolName, which prints without the context.

I'd want to see more of how you structured your package code before offering other suggestions.

$\endgroup$
1
  • $\begingroup$ Thank you for the help, I tried adding x as a parameter passed during the call, as you suggested, and now it works fine. $\endgroup$
    – cgcg
    Commented May 9, 2022 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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