Question about Initialization Cells

  • Follow


I downloaded the source code for a Wolfram Demonstrations Project; this one:
http://demonstrations.wolfram.com/ComparingAlgorithmsForTheTravelingSalesmanProblem/

The first three cells, all initialization cells, each contain a simple
one-liner, for example:
         dist[p1_, p2_] := Norm[p1 - p2]
When I evaluate any of these cells, it appears to define the function, but
when I try (in a second, new notebook window) to evaluate the function
(e.g., to evaluate "dist[4.5]}), the function is undefined. It is blue i
the second window, even though it is black in the first window.

But If I type "dist[4,5]" in the demonstration project window itself, all
works as expected.

The demo project does not appear to be a package; 'dist' does not seem to
be private. What am I missing?

0
Reply james4271 (10) 5/5/2012 8:16:14 AM

Evaluate the following in the notebook:

Options[EvaluationNotebook[], CellContext]

If the result is

{CellContext -> Notebook}

then that means that the notebook has been set so that any function
defined in it is defined in a Context unique to that notebook.  If so,
then those functions will not be in the context path for Mathematica
to find them in another notebook and would need to be called there by
their full context path.

--David


On May 5, 4:16 am, James Stein <ja...@stein.org> wrote:
> I downloaded the source code for a Wolfram Demonstrations Project; this one:http://demonstrations.wolfram.com/ComparingAlgorithmsForTheTravelingS...
>
> The first three cells, all initialization cells, each contain a simple
> one-liner, for example:
>          dist[p1_, p2_] := Norm[p1 - p2]
> When I evaluate any of these cells, it appears to define the function, but
> when I try (in a second, new notebook window) to evaluate the function
> (e.g., to evaluate "dist[4.5]}), the function is undefined. It is blue i
> the second window, even though it is black in the first window.
>
> But If I type "dist[4,5]" in the demonstration project window itself, all
> works as expected.
>
> The demo project does not appear to be a package; 'dist' does not seem to
> be private. What am I missing?


0
Reply dbreiss (537) 5/8/2012 8:07:27 AM


I downloaded the source code for a Wolfram Demonstrations Project; this one:

http://demonstrations.wolfram.com/ComparingAlgorithmsForTheTravelingSalesmanProblem/

The first three cells, all initialization cells, each contain a simple
one-liner, for example:

         dist[p1_, p2_] := Norm[p1 - p2]

When I evaluate any of these cells, it appears to define the function, but
when I try (in a second, new notebook window) to evaluate the function

(e.g., to evaluate "dist[4.5]}), the function is undefined. It is blue in
the second window, even though it is black in the first window.

But If I type "dist[4,5]" in the demonstration project window itself, all
works as expected.

The demo project does not appear to be a package; 'dist' does not seem to
be private. What am I missing?

The function, dist[p1,p2] that you describe here has two variables according to definition. Mathematica distinguishes functions not only by their heads (i.e. the name "dist"), but also by the number of variables. For Mathematica, therefore, the functions dist[1,2] and dist[1] are two absolutely different functions. In your case the first of them is already defined, but not the second. Finally 4.5 is for Mathematica one rational number equal to four and a half, rather than two numbers. Thus, Mathematica understands dist[4.5] as a new, yet undefined function of a one variable.

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.

IEE S.A.

ZAE Weiergewan,

11, rue Edmond Reuter,

L-5326 Contern, LUXEMBOURG



Office phone :  +352-2454-2566

Office fax:       +352-2454-3566

mobile phone:  +49 151 52 40 66 44



e-mail: alexei.boulbitch@iee.lu<mailto:alexei.boulbitch@iee.lu>






0
Reply Alexei.Boulbitch (367) 5/8/2012 8:13:04 AM

That's something good to know: from time to time there have been posts 
to this list asking about how to arrange exactly the same restriction -- 
typically by somebody used to another CAS system and expecting 
definitions in one notebook, by default, to be confined to that 
notebook's context.

On 5/8/12 4:06 AM, David Reiss wrote:
> Evaluate the following in the notebook:
>
> Options[EvaluationNotebook[], CellContext]
>
> If the result is
>
> {CellContext ->  Notebook}
>
> then that means that the notebook has been set so that any function
> defined in it is defined in a Context unique to that notebook.  If so,
> then those functions will not be in the context path for Mathematica
> to find them in another notebook and would need to be called there by
> their full context path.
>
> --David
>
>
> On May 5, 4:16 am, James Stein<ja...@stein.org>  wrote:
>> I downloaded the source code for a Wolfram Demonstrations Project; this one:http://demonstrations.wolfram.com/ComparingAlgorithmsForTheTravelingS...
>>
>> The first three cells, all initialization cells, each contain a simple
>> one-liner, for example:
>>           dist[p1_, p2_] := Norm[p1 - p2]
>> When I evaluate any of these cells, it appears to define the function, but
>> when I try (in a second, new notebook window) to evaluate the function
>> (e.g., to evaluate "dist[4.5]}), the function is undefined. It is blue i
>> the second window, even though it is black in the first window.
>>
>> But If I type "dist[4,5]" in the demonstration project window itself, all
>> works as expected.
>>
>> The demo project does not appear to be a package; 'dist' does not seem to
>> be private. What am I missing?
>
>

-- 
Murray Eisenberg                     murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

0
Reply murray (1862) 5/9/2012 7:52:58 AM

3 Replies
30 Views

(page loaded in 0.174 seconds)

Similiar Articles:













7/8/2012 11:15:43 PM


Reply: