Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: How to print/display symbolic expressions from inside a Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104247] Re: How to print/display symbolic expressions from inside a Module
  • From: Hannes Kessler <HannesKessler at hushmail.com>
  • Date: Sun, 25 Oct 2009 01:05:27 -0400 (EDT)
  • References: <hbr599$rh2$1@smc.vnet.net>

Hello Nasser,

you can do a string replacement as you already mentioned:

foo[] := Module[{x, y, expr}, expr = x^2 + y;
  Print[" expression now is ", StringReplace[
    ToString[expr, FormatType -> TraditionalForm],
    c : LetterCharacter ~~ "$" ~~ DigitCharacter .. :> c]]]
foo[]

Best regards,
Hannes Kessler

On 23 Okt., 04:43, "Nasser M. Abbasi" <n... at 12000.org> wrote:
> Hello;
>
> I use Print[] allot while debugging, and trace how the expression evolve
> etc... while it is still symbolic, before I plug in numerical values.
>
> But when I have a Module, and use Print[] on expression which involves lo=
cal
> variables, they all print with a $ sign ofcourse.  This makes it hard t=
o
> read the expressions.
>
> foo[] := Module[{x, y,expr},
>                  expr= x^2 + y;
>                  Print[" expression now is ",
>                     ToString[expr,FormatType->Traditi=
onalForm]]
>           ]
> foo[]
>  expression now is x$589^2+y$589
>
> A standard way to solve this is to pass the symbols themselves as in
>
> foo[x_, y_] := Module[{expr}, expr = x^2 + y;
>                        Print[" expression now is =
",
>                            ToString[expr,Form=
atType -> TraditionalForm]]
>                       ]
>
> Remove[x, y]
> foo[x, y]
>  expression now is x^2+y
>
> But the problem is that I am calling foo[] itself from inside yet another
> Module, and I also can't have global variables at all.  So, everything =
is
> wrapped inside a Module, and every symbol much have a scope inside, nothi=
ng
> global.
>
> So, the only option I see (which I am going to try next, do not know if i=
t
> will work) is to make a pattern which strips these $nnn from the symbols =
for
> display purposes?
>
> Or Am I missing some other simple solution to this common problem?
>
> Thanks,
> --Nasser



  • Prev by Date: Re: ODE with in a piece wise plot
  • Next by Date: Re: Using Mathematica 5.2 and 7.01 with separate settings
  • Previous by thread: Re: How to print/display symbolic expressions from inside a Module
  • Next by thread: Re: How to print/display symbolic expressions from inside a Module