Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to drop ContextPath when printing from a package?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116409] Re: how to drop ContextPath when printing from a package?
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sun, 13 Feb 2011 05:51:05 -0500 (EST)
  • References: <200812020542.AAA19141@smc.vnet.net> <ij2uks$7j8$1@smc.vnet.net>

Hi,

> I have a module, which make use of  FindFit[data,model,par,x] and 
> returns the model and a plot of data and the fit curve. I want to use 
> the model as a PlotLabel but
> instead of
> 
> y=1.19623+1048.91 x^0.733809
> 
>   I  get
> 
> y=1.19623+1048.91 initData`Private`x$^0.733809
> 
> How can I  get rid of  the ContextPath  initData`Private`?

If it is just the PlotLabel that is a problem for you, I would suggest
to replace the symbol with a string. If you are also returning the
model, that will also contain the symbol in your private context, which
is not very useful as a result. I see two ways to solve that:

1) return the model in form of a Function, which localizes the
independent variable of the model.

2) let the caller provide the symbol to be used as independent variable
of the model.

Both have their pros and cons: case 2 is probably more consistent with
what Mathematica internal functions usually do and probably best fits
the use within a notebook, case 1 is something that a programmer using
the function might prefer since it has less implications concerning
unwanted evaluations of the independent variables (consider what the
myFit or FindFit will do when you supply a variable which has a
value...). So personally I would probably use 2 if the function is meant
to be used directly by users of your package, but prefer 1 if I mailny
would make use of the package in other code.

Here are examples based on Sjoerds example function:

Case 1): replace x with a string for the plot label, return a function
(note that I would suggest to use Block instead of Module in this case
to not create a new symbol for each Function to be returned):

BeginPackage["myFit`"];
myFit::"usage" = "myFit[data] makes a fit.";
Begin["`Private`"];
myFit[data_] := Block[{x, a, b, f, model, plot},
   f = FindFit[data, a + b x, {a, b}, x];
   model = a + b x /. f;
   plot = ListPlot[data, PlotLabel -> (model /. x -> "x")];
   {Function @@ {{x}, model}, plot}
   ];
End[];
EndPackage[];

Case 2): replace x with a string for the plot label, return an
expression built with a symbol defined by the caller:

BeginPackage["myFit`"];
myFit::"usage" = "myFit[data,var] makes a fit.";
Begin["`Private`"];
myFit[data_, x_Symbol] := Module[{f, a, b, model, plot},
   f = FindFit[data, a + b x, {a, b}, x];
   model = a + b x /. f;
   plot = ListPlot[data, PlotLabel -> (model /. x :> SymbolName[x])];
   {model, plot}
   ];
End[];
EndPackage[];


hth,

albert


  • Prev by Date: Re: Bug in Mathematica 7.0.1 multiple integration?
  • Next by Date: Re: new Graph function + combinatorica: various problems
  • Previous by thread: Re: how to drop ContextPath when printing from a package?
  • Next by thread: Re: how to drop ContextPath when printing from a package?