3
$\begingroup$

When I start a new notebook, I enter

Names["Global`*"]

I get this

{"list", "msgs", "rhs", "z"}

Then I enter,

mydummy[x_, y_, model_] := Module[
      {ans, j},
      ans = Sum[x[[j]], {j, 1, y}]

  ];
Names["Global`*"]

I get this:

{"ans", "ans$", "j", "j$", "list", "model", "model$", "msgs", "mydummy", "rhs", "x", "x$", "y", "y$", "z"}

I dont really know the difference between x and x$ here. I don't know any of the difference between the variables and the one with an extra $.

I check

?j
?j$

Ok, one of them is 'Temporary'.

Here are my questions:

1 - Are they all saved in RAM during the session? 2 - What's the point of saving these 'Temporary' (Global) variables? I thought when declaring them to be local using Module[], they would be thrown away after my execution.

3 - Why do we have x and y at first place? I only defined mydummy, which is a function. So I only want the 'final' value when it's evaluated.

4 - If the x and y are of any use, how do I access them? I tried

x

It does not tell me anything?

5 - If I define a new function,

mydummy2[x_, y_, model_] := Module[
  {ans, j},
  ans = Sum[x[[j]], {j, 1, y}]

]

Now

Names["Global`*"]

which 'x' is which?

6 - Sometimes, I get Global variables like

tmp$1
    tmp$20
tmp$300

To me, they are all 'local' to a Module. But it's in the list when I use 'Names["Global`*"]', why are they there? Can I remove them during a long calculation? How to remove and what's the downside of removing them?

7 - Do I need to declare $j$ within a Module, if it's a dummary index value, like this

ans=Table[Sum[j],{j,1,3}]
(* or *)
ans=Sum[x[[j]],{j,1,3}]

8 - Lastly, when i first started Mathematica, what are the "list,msgs,rhs,z" variables?

Sorry if this seems really silly. But I do want to understand this, as I have long codes, it seems sometimes it's using too much memory. I am not sure if it's caused by those 'local' variables.

$\endgroup$

1 Answer 1

2
$\begingroup$

First of all I suspect you are running Mathematica 10.0.0 and are experiencing these issues:

In version 10.0.1 I get:

Names["Global`*"]
{}

And then:

mydummy[x_, y_, model_] := Module[{ans, j}, ans = Sum[x[[j]], {j, 1, y}]];

Names["Global`*"]
{"ans", "ans$", "j", "j$", "model", "mydummy", "x", "y"}

The remaining names can all be explained as expected behavior. Every Symbol name that appears that does not correspond to an existing Symbol in the $ContextPath

is created in the current $Context. See:

This explains {"ans", "j", "model", "mydummy", "x", "y"}.

The remaining two: {"ans$", "j$"} are more tricky; I believe they are created by the automatic renaming mechanism that applies to nested scoping constructs, here SetDelayed and Module. See:

I shall try to address all of your question on a point-by-point basis if I have time. I think those links should give you plenty to read for the moment, and right now it is time for me to eat. :-)

$\endgroup$
2
  • $\begingroup$ You are absolutely right that I am using V10.0.0. As I am very new to Mathematica, I was not aware this was a bug related. I just thought it might be some of my code. Because I have scoping structure, and sometimes it is causing me memory overflow on my 8GB RAM. I will definitely take some time to read the links. $\endgroup$ Oct 14, 2014 at 13:55
  • $\begingroup$ Just tested on V 10.0.1. Names["Global*"]` works fine now. That's sorted! $\endgroup$ Oct 14, 2014 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.