Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8616 > unrolled thread

Why HoldForm[] shows this expression in red in notebook?

Started by"Nasser M. Abbasi" <nma%12000.org@gtempaccount.com>
First post2012-02-19 11:32 +0000
Last post2012-02-20 07:57 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Why HoldForm[] shows this expression in red in notebook? "Nasser M. Abbasi" <nma%12000.org@gtempaccount.com> - 2012-02-19 11:32 +0000
    Re: Why HoldForm[] shows this expression in red in notebook? David Bailey <dave@removedbailey.co.uk> - 2012-02-20 07:51 +0000
      Re: Why HoldForm[] shows this expression in red in notebook? "Nasser M. Abbasi" <nma@12000.org> - 2012-02-21 11:09 +0000
        Re: Why HoldForm[] shows this expression in red in notebook? David Bailey <dave@removedbailey.co.uk> - 2012-02-23 10:46 +0000
    Re: Why HoldForm[] shows this expression in red in notebook? Szabolcs Horvát <szhorvat@gmail.com> - 2012-02-20 07:57 +0000

#8616 — Why HoldForm[] shows this expression in red in notebook?

From"Nasser M. Abbasi" <nma%12000.org@gtempaccount.com>
Date2012-02-19 11:32 +0000
SubjectWhy HoldForm[] shows this expression in red in notebook?
Message-ID<jhqml4$f9t$1@smc.vnet.net>
foo[x_, v_] := Module[{z = v},
   {x^z, Hold[x^z], HoldForm[x^z]}
   ];

Any one knows why the notebook shows the 'z' in the last term above in
the red and but not the other z's?

I am using V 8.04 on windows

thanks
--Nasser

[toc] | [next] | [standalone]


#8639

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-02-20 07:51 +0000
Message-ID<jhsu1o$nmq$1@smc.vnet.net>
In reply to#8616
On 19/02/2012 11:32, Nasser M. Abbasi wrote:
> foo[x_, v_] := Module[{z = v},
>     {x^z, Hold[x^z], HoldForm[x^z]}
>     ];
>
> Any one knows why the notebook shows the 'z' in the last term above in
> the red and but not the other z's?
>
> I am using V 8.04 on windows
>
> thanks
> --Nasser
>
>
I think the last z is coloured red because you are returning an 
expression involving a local variable:

In[2]:= foo[a,b]

Out[2]= {a^b,Hold[a^z$6385],a^z$6385}

Notice the strange variable z$6385 - which is local. I can't see why the 
middle term isn't red as well, but the left term evaluates to something 
that doesn't involve a local variable.

Perhaps you really wanted to write this:

foo[x_, v_] := Module[{z = v},
    {x^z, Hold[Evaluate[x^z]], HoldForm[Evaluate[x^z]]}
    ];

In[4]:= foo[a,b]
Out[4]= {a^b,Hold[a^b],a^b}

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [next] | [standalone]


#8655

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-02-21 11:09 +0000
Message-ID<jhvu1f$7ua$1@smc.vnet.net>
In reply to#8639
On 2/20/2012 1:51 AM, David Bailey wrote:
> On 19/02/2012 11:32, Nasser M. Abbasi wrote:
>> foo[x_, v_] := Module[{z = v},
>>      {x^z, Hold[x^z], HoldForm[x^z]}
>>      ];
>>
>> Any one knows why the notebook shows the 'z' in the last term above in
>> the red and but not the other z's?
>>
>> I am using V 8.04 on windows
>>
>> thanks
>> --Nasser
>>
>>

> I think the last z is coloured red because you are returning an
> expression involving a local variable:
>

But we return expressions involving local variables all the time?

foo[] := Module[{x},
   x + 3
   ]

no red color any where.

> In[2]:= foo[a,b]
>
> Out[2]= {a^b,Hold[a^z$6385],a^z$6385}
>
> Notice the strange variable z$6385 - which is local. I can't see why the
> middle term isn't red as well, but the left term evaluates to something
> that doesn't involve a local variable.
>
> Perhaps you really wanted to write this:
>
> foo[x_, v_] := Module[{z = v},
>      {x^z, Hold[Evaluate[x^z]], HoldForm[Evaluate[x^z]]}
>      ];

No, that would not work for me, may be I should explain
more: I have an internal function, where I want to
make a plot, and want to make a plot label for the plot.

The plot label will be an analytical expression of
the equation solved.

I am not supposed to use Strings to formulate the
equations for labels. So what other options are there?

The way I do things now is to make labels using this
method:

Text@Row[{"equation ",Style[TraditionalForm[HoldForm[...]]]}]

And in place of ... I write the equation, using standard
Mathemtica code, as I would write it in the main cell itself
as code. i.e. no strings.

For example, to render this equation: u''[x]=f(x), I would
write

Text@Row[{"equation ",
    Style[TraditionalForm[HoldForm[u''[x] == f[x, y]]]]}]

I do not want to Evaluate u''[x] ofcourse. I want just
to display the equation itself as is.

The problem comes when I do the above inside a function, which
already has x and y as local symbols being used, like this:

foo[] := Module[{x, y},
   x = 1; y = 2;
   {Text@Row[{"equation is ",
     Style[TraditionalForm[HoldForm[u''[x] == f[x, y]]]]}]
    }
]

Now the red comes back and the $$ show up in the label which
ofcourse I do not want.

So, I have to change the code in the function, which already
uses these symbols to use someother symbols. not good.

Again, I can't use strings to make up the equation in
the label. So, what else is one to do? What is the
canonical way to do this sort of thing in Mathematica, without
using strings? Something else better than HoldForm around to use?

I do not want to use global symbols. I like to render an
expression from inside a function using TraditionalForm
and have the freedom to use any Mathematica symbol, like \[Del],
and others as well. Since I am using HoldForm, I thought I could
do that.

thanks,
--Nasser

[toc] | [prev] | [next] | [standalone]


#8705

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-02-23 10:46 +0000
Message-ID<ji55dl$764$1@smc.vnet.net>
In reply to#8655
On 21/02/2012 11:09, Nasser M. Abbasi wrote:
> On 2/20/2012 1:51 AM, David Bailey wrote:
>> On 19/02/2012 11:32, Nasser M. Abbasi wrote:
>>> foo[x_, v_] := Module[{z = v},
>>>       {x^z, Hold[x^z], HoldForm[x^z]}
>>>       ];
>>>
>>> Any one knows why the notebook shows the 'z' in the last term above in
>>> the red and but not the other z's?
>>>
>>> I am using V 8.04 on windows
>>>
>>> thanks
>>> --Nasser
>>>
>>>
>
>> I think the last z is coloured red because you are returning an
>> expression involving a local variable:
>>
>
> But we return expressions involving local variables all the time?
>
> foo[] := Module[{x},
>     x + 3
>     ]

I think the syntax colourer just fails to recognise what is going on 
here. It is not fool proof.
>
> no red color any where.
>

>
> No, that would not work for me, may be I should explain
> more: I have an internal function, where I want to
> make a plot, and want to make a plot label for the plot.
>
> The plot label will be an analytical expression of
> the equation solved.
>
> I am not supposed to use Strings to formulate the
> equations for labels. So what other options are there?
>
> The way I do things now is to make labels using this
> method:
>
> Text@Row[{"equation ",Style[TraditionalForm[HoldForm[...]]]}]
>
> And in place of ... I write the equation, using standard
> Mathemtica code, as I would write it in the main cell itself
> as code. i.e. no strings.
>
> For example, to render this equation: u''[x]=f(x), I would
> write
>
> Text@Row[{"equation ",
>      Style[TraditionalForm[HoldForm[u''[x] == f[x, y]]]]}]
>
> I do not want to Evaluate u''[x] ofcourse. I want just
> to display the equation itself as is.
>
> The problem comes when I do the above inside a function, which
> already has x and y as local symbols being used, like this:
>
> foo[] := Module[{x, y},
>     x = 1; y = 2;
>     {Text@Row[{"equation is ",
>       Style[TraditionalForm[HoldForm[u''[x] == f[x, y]]]]}]
>      }
> ]
>
> Now the red comes back and the $$ show up in the label which
> ofcourse I do not want.
>
> So, I have to change the code in the function, which already
> uses these symbols to use someother symbols. not good.

I'd say the simplest answer is to use global variables inside a Block 
construct instead of a Module construct.

David Bailey
http://www.dbaileyconsultancy.co.uk


[toc] | [prev] | [next] | [standalone]


#8651

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2012-02-20 07:57 +0000
Message-ID<jhsud9$nqj$1@smc.vnet.net>
In reply to#8616
On 2/19/2012 1:32 PM, Nasser M. Abbasi wrote:
> foo[x_, v_] := Module[{z = v},
>     {x^z, Hold[x^z], HoldForm[x^z]}
>     ];

Hi Nasser,

I assume it is a warning that z (the Module variable) will not be evaluated.

One might expect foo[x,2] to give {..., HoldForm[x^2]}.  It will not 
because z never evaluates to 2 (the expression that was substituted for v)

Why this warning is not shown for Hold?  I don't know.  I guess the 
results from HoldForm might be more confusing because if how it is 
printed.  I fell into this trap with HoldForm before (when trying to use 
it in plot labels) but not with Hold (where I typically focus on the 
hold-properties rather than formatting).

So again, this is just a guess.

-- 
Szabolcs Horvát
Visit Mathematica.SE:  http://mathematica.stackexchange.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web