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

MathGroup Archive 2010

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

Search the Archive

How to localize a symbol when using Manipulate?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112602] How to localize a symbol when using Manipulate?
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Wed, 22 Sep 2010 01:58:11 -0400 (EDT)
  • Reply-to: nma at 12000.org

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?

thanks
--Nasser



  • Prev by Date: Re: Summing above diagonal in a tensor
  • Next by Date: Re: Help with Solve
  • Previous by thread: Re: How to localize a symbol when using Manipulate?
  • Next by thread: Re: How to localize a symbol when using Manipulate?