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

*To*: mathgroup at smc.vnet.net*Subject*: [mg116412] Re: how to drop ContextPath when printing from a package?*From*: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>*Date*: Mon, 14 Feb 2011 04:26:25 -0500 (EST)

Hi Ted, I see. Your function and problem differ are a bit from what I assumed originally. The function you sent me actually works without problem. I think your problem pops up if you pass a variable name that is local to another scoping construction such as in (silly example): Module[{x}, fit1D[Range[10], RandomReal[{-0.3, 0.3}, 10] + 2 Range[10], a + b x, {a, b}, x, "test string", "test file", True] ] Passing a local variable is normally fine, but here you pass it as a symbol (without value) in order to use its name and not its value. I'd say that's a nono. But let's try to get this to work as you intend. Since the user is free to use any model and variable name replacement isn't as easy as before, but it can be done using ToString and string pattern matching: x -> StringReplace[ToString[x], a_ ~~ "$" ~~ ___ -> a] which uses the fact that all localized variables contain the "$-number" part. As far as I can see, the following update of your code now works even for localized variables (note that I changed the definition of f to include a formal parameter): fit1D[X_, Y_, model_, par_, x_, axesLabel_, fileName_, plotQ_] := Module[{XY, xyFit, label, gr1, gr2, gr, f, myVar},(*Clear[tyList, tyFit,par1,s1,s2,s3,label,gr1,gr2,gr,f];*) XY = MapThread[List, {X, Y}]; xyFit = FindFit[XY, model, par, x]; myVar = Symbol[StringReplace[ToString[x], a_ ~~ "$" ~~ ___ -> a]]; f[ToExpression[ToString[myVar] <> "_"]] = model /. x -> myVar /. xyFit; label = StringForm["y=``", f[myVar]]; If[plotQ, gr2 = LogLinearPlot[f[x], {x, Min[X], Max[X]}, PlotStyle -> {Thick}, PlotLabel -> label, GridLines -> Automatic, AxesLabel -> axesLabel, PlotRange -> {{Min[X], Max[X]}, {0., Max[Y]}}]; gr1 = ListLogLinearPlot[XY, AxesLabel -> axesLabel, PlotLabel -> label, Joined -> True, PlotMarkers -> Automatic, PlotStyle -> {Dotted(*,PointSize[.02]*)}]; gr = Show[{gr2, gr1}] (*saveImg[gr,fileName];*) ]; Return[{f[myVar], gr}]; ]; It's not elegant, but it works. By the way: XY = MapThread[List, {X, Y}] is correct, but I usually use Transpose[{X,Y}] which is somewhat more compact (even more so if you use the {X, Y}\[Transpose] notation, with the transpose symbol generated by esc-tr-esc). Cheers -- Sjoerd > -----Original Message----- > From: Ted Sariyski [mailto:tsariysk at craft-tech.com] > Sent: Saturday, 12 February 2011 15:00 > To: 'Sjoerd C. de Vries' > Subject: RE: how to drop ContextPath when printing from a package? > > Hi Sjoerd, > Thank you for the comments. Actually I also pass the model as an argument to > the module (attached): > > bArea[x_]=fit1D[time, area,a+c x^d,{a,c,d},x,{"time [sec]","area > [m^2]"},"bArea",plotQ]; > > I have bArea::usage="bArea[t (*sec*)]" in the Package header for further use of > bArea. > > The second suggestion is not applicable for this mode of use and I am not sure I > understand what do you mean with "... to use a different variable in your > module, so not x, or not to return the fit in terms of x." I tried something like > "label = StringForm["y=``", f/.x->t)]" but it did not do the job. Could you explain, > please? > > Thanks again, > --Ted > > > fit1D[X_,Y_,model_,par_,x_,axesLabel_,fileName_,plotQ_] := > Module[{XY,xyFit,label,gr1,gr2,gr,f}, > (*Clear[tyList,tyFit,par1,s1,s2,s3,label,gr1,gr2,gr,f];*) > XY = MapThread[List, {X, Y}]; > xyFit = FindFit[XY, model,par,x]; > f=model/.xyFit; > > label = StringForm["y=``", f(*/.x->t*)]; > > If[ plotQ, > gr2 = > LogLinearPlot[f,{x,Min[X],Max[X]},PlotStyle->{Thick},PlotLabel->label,GridLi > nes->Automatic, > > AxesLabel->axesLabel,PlotRange->{{Min[X],Max[X]},{0.,Max[Y]}}]; > gr1 = > ListLogLinearPlot[XY,AxesLabel->axesLabel,PlotLabel->label,Joined->True,Plot > Markers->Automatic,PlotStyle->{Dotted(*,PointSize[.02]*)}]; > gr = Show[{gr2,gr1}]; > saveImg[gr,fileName]; > ]; > > Return[f]; > ]; > > > -----Original Message----- > From: Sjoerd C. de Vries [mailto:sjoerd.c.devries at gmail.com] > Sent: Saturday, February 12, 2011 5:20 AM > To: Ted Sariyski > Subject: Re: how to drop ContextPath when printing from a package? > > Hi Ted, > > I assume your variable is local to a package that contains the variable. Apart > from the package context I further assume that your module basically does > something like: > > myFit[data_] := > Module[{x, f}, > f = FindFit[data, a + b x, {a, b}, x]; > a + b x /. f > ] > > which yields something like: > > In[276]:= myFit[{{1, 2.2}, {2, 3.8}, {3, 6}, {4, 8.4}}] > > Out[276]= -0.1 + 2.08 x$142961 > > In my opinion, it is never a good idea to return a local variable. One solution is, > of course, to use a different variable in your module, so not x, or not to return > the fit in terms of x. If returning a model in terms of x is necessary another > solution might be > > myFit[data_] := > Module[{x, f}, > f = FindFit[data, a + b x, {a, b}, x]; > a + b Symbol["x"] /. f > ] > > In[278]:= myFit[{{1, 2.2}, {2, 3.8}, {3, 6}, {4, 8.4}}] > > Out[278]= -0.1 + 2.08 x > > Cheers -- Sjoerd > > > On Feb 11, 10:14 am, Ted Sariyski <tsari... at craft-tech.com> wrote: > > 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`? > > Thanks in advance, > > --Ted

**Re: Nonorthogonal Eigenvectors**

**Re: k-permutations enumeration**

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

**ContourPlot confusion**