[Date Index]
[Thread Index]
[Author Index]
Re: How to localize a symbol when using Manipulate?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg112609] Re: How to localize a symbol when using Manipulate?
*From*: Albert Retey <awnl at gmx-topmail.de>
*Date*: Thu, 23 Sep 2010 04:20:19 -0400 (EDT)
*References*: <i7c5u6$5g3$1@smc.vnet.net>
Am 22.09.2010 07:58, schrieb Nasser M. Abbasi:
> Mathematica experts:
>
> I came up against this problem when writing a demo.
>
> I simplified to it to the following:
>
> I want to make a call to a lower level function which makes an
> expression (say build a polynomial in s) and returns it back to be
> displayed.
>
> I want this function to use a symbol, say s, to build the polynomial.
>
> The problem is how to localize the symbol itself so it is not global.
>
> Since one dose not declare things in Mathematica, (sometime I wish I
> could) I can't declare the s to be a symbol, but must just use it.
>
> Here is a simple example to show the problem:
>
> If one does not pass a symbol for a lower level function during the call
> to be used in constructing an expression, then the expression will have
> $nnnn added to it.
>
> Manipulate[process[n], {n, 2, 10},
> Initialization :>
> (process[n_] := Module[{s}, s^n])
> ]
>
> If you run the above, the result shown will be s$nnn^2 where nnn is
> some number.
>
>
> To solve the above problem, typical solution is to pass the symbol name
> to be used. Example
>
> Manipulate[process[s, n], {n, 2, 10},
> Initialization :>
> (process[s_, n_] := Module[{}, s^n])
> ]
>
> If you run the above, the result shown will be s$^2
>
> There is a problem with the above solution. s in the above is global
> variable. Hence s needs to be localized. Since 's' is not a control
> variable (like 'n' is in this example), it needs to be explicitly
> localized (control variables are localized by default).
>
> The problem is how to localize 's'?
>
> One can not write the following, since 's' becomes Null
>
> Manipulate[process[s, n], {n, 2, 10},
> {s, ControlType -> None},
> Initialization :> (process[s_, n_] := Module[{}, s^n])
> ]
>
> If you run the above, the result shown will be Null^2
>
> I also can't use the Symbol[] trick
>
> Manipulate[process[Symbol["s"], n], {n, 2, 10},
> Initialization :> (
> process[s_, n_] := Module[{}, s^n])
> ]
>
> One way is not not pass 's' at all, and on return, strip the $$$n from
> the symbol, as follows, However using ToExpression is not allowed in a
> demo (for same reason as Symbol is not allowed, security). (someone
> helped me long time ago with providing the following nice strip function
> here in this newsgroup)
>
> Manipulate[ strip[process[n]], {n, 2, 10},
> Initialization :> (
> process[n_] := Module[{s}, s^n];
>
> strip[expr_] :=
> Module[{}, ToExpression[StringReplace[ToString[expr,
> FormatType -> TraditionalForm],
> c:LetterCharacter~~"$"~~DigitCharacter.. :> c]]]; )
> ]
>
>
> I have to use ToExpression, else the result will be String, which makes
> it hard to use later on, but is OK for just printing and display.
>
> Any one has other ideas I could try?
First of all, why do you think it is so important that s is localized if
the sole purpose of the Manipulate is to become a demonstration? It will
run in very well defined circumstances, so I think localization is
probably not important at all and maybe a Clear in the initialization
would be save enough.
Second: why do you think it is a problem to use the string "s" instead
of the symbol s in this case? I have done things like this and don't
really think that using a string instead of a symbol would always make
things more complicated. Where do you see problems?
Another possibility, if you really have good reasons for localization
and not using a string is to only replace the symbol with the string for
printout, e.g. with something like this:
Manipulate[
process[n] /. _Symbol?(Context[#] === "Global`" &) -> "s", {n, 2,
10}, Initialization :> (process[n_] := Module[{s}, s^n])]
hth,
albert
Prev by Date:
**Re: How to localize a symbol when using Manipulate?**
Next by Date:
**Re: How to interpret this integral?**
Previous by thread:
**Re: How to localize a symbol when using Manipulate?**
Next by thread:
**two questions about regression properties**
| |