Questions on Mathematica's scoping constructs (Block, Module, With, etc.)

learn more… | top users | synonyms (1)

6
votes
1answer
71 views

How to reset the $ModuleNumber?

According to the documentation, we can reset $ModuleNumber to a positive machine integer. There is even an example in the doc: ...
6
votes
2answers
235 views

Variable scoping problem when mapping over delayed replacement

This is something that got me curious while I was playing around with Mathematica. Consider the following (contrived) example: ...
6
votes
1answer
148 views

How to inherit options by scoping?

Consider the following example: you have a function framed producing a cell with a button that, when pushed, calls framed again. ...
6
votes
1answer
231 views

Treat all variables as local in a module [duplicate]

In most languages, any variable defined inside a function is considered local. ...
6
votes
1answer
164 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
5
votes
4answers
256 views

Replacing parts of a matrix

I'm a beginner to Mathematica and to writing scripts in general. I'm currently trying to write a code that will take any element in a matrix higher than a certain threshold value n and set it equal to ...
5
votes
3answers
282 views

Notation for specifying transformation rules

I would like to have a compact notation for specifying conditions and results in transformation rules. Consider ReplaceAll: ...
5
votes
3answers
153 views

Prevent iterator name from being confused with symbol passed into function body

I have a massive amount of code with lots of Table and Sum inside a Module. Each with their ...
5
votes
1answer
233 views

Parts of Module body evaluated in external scope?

I have an expression that suggests that some expressions in a module body are dragging in definitions from outside the scope in a surprising way. First, consider a symbolic constant, ...
5
votes
2answers
137 views

Generating EventHandler using Table

I would like to change the edge color of a graph when double clicking on it, the following code works: ...
5
votes
2answers
133 views

Checkbox behaving weird when variables are local

I have a DynamicModule in which I need a checkbox with a Max in the dynamic part: ...
5
votes
1answer
95 views

Module does not remove a function definition

The following is a minor variant of the function uniqueTuples as given by Simon Woods in this answer. ...
5
votes
1answer
72 views

Dynamics and Block interfere

As mentioned by Albert Retey in a comment, we can unintentionally interfere with our dynamics as follows ...
5
votes
0answers
117 views

Are there any videos that show how different Mathematica expressions are evaluated?

Are there any videos that show how different Mathematica expressions are represented internally as object graphs and demonstrate how they are transformed during an evaluation? I am asking because I ...
4
votes
3answers
530 views

Defining a new Function using RegionPlot

I'm trying to make images like this: to illustrate 2D integrals to a calculus class. I used the code ...
4
votes
3answers
98 views

Creating a function after working out an example calculation

I have created this setup, it is giving me the correct answer, however I am interested in a function that does all this calculations and creates a output of mean and standard deviation. The function ...
4
votes
2answers
283 views

Comparing LetL and Module efficiency

I was recently introduced to the LetL macro thanks to Leonid's answer to one of my prior questions. I was, needless to say, impressed by the simplicity of its ...
4
votes
2answers
178 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
4
votes
3answers
226 views

Modify this code using Module and While

I have written a recursive function and would like to re-write the code using Module AND While to compare the timings. Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and ...
4
votes
1answer
216 views

How to avoid conflicts between local variable names and symbolic arguments in Block constructs?

I am using Block inside the definition of some recursive functions. If the Block definitions use short variable names, like ...
4
votes
1answer
98 views

Unwanted interaction between instances of a Manipulate function

The function trajectory below is defined in terms of Manipulate and calls the auxiliary function ...
4
votes
3answers
164 views

Altering values in Manipulate with dynamically generated controls

I have a Manipulate whose controls are generated based on a parameter in a containing Manipulate: ...
4
votes
2answers
169 views

TrackedSymbols affected by CurrentValue. Another problem with SetDelayed(:=)+OwnVales in Initialization

It is MWE, only a Disk[] that should print "Click" when clicked. Also "date... updating" ...
4
votes
1answer
110 views

Dial control for Manipulate?

I'm trying to make a control item in a Manipulate that is a simple dial. I'm trying to mimic Manipulate[theta, {theta, 0, 360}], ...
4
votes
1answer
194 views

Namespaces in Mathematica

In many programming languages one can define a namespace with namespace abc How can the similar be done in Mathematica? The ...
4
votes
0answers
178 views

Exhaustive examples of variable replacement due to nested scoping constructs

Consider the following examples, where I have replaced dollar signs in the output by a D. ...
3
votes
2answers
102 views

Is there a way to localize Off[some::message]?

Is there a way to temporarily suppress certain messages, so that I could write for example (with made-up syntax for that feature): ...
3
votes
1answer
154 views

Issue with scope of variables

For the definitions, t = 1; f[x_, y_] := t (x + y); v[3] := {1, 1}; g[u_] := f[v[u][[1]], v[u][[2]]]; the following two expressions yield different results, ...
3
votes
3answers
105 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
3
votes
2answers
255 views

How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
3
votes
2answers
463 views

Local variables

I'm trying to use Modules together with functions. tmp2 = x^2 + 1; f[y_] := Module[{x = 1}, Evaluate[y tmp2]] This works when ...
3
votes
2answers
308 views

Why does the syntax highlighter color a local symbol red inside DynamicModule?

I am adding a Module inside DynamicModule here is an example (thanks to @rm-rf for this simple example) ...
3
votes
1answer
61 views

Should “counters” used in Do, Table, … within Module be declared as local?

I have little to no understanding of how Mathematica handles scoping, so the question may seem a little bit funny. What is the preferred way: ...
3
votes
1answer
87 views

Hiding body of a module or function in mathematica

Is it possible to hide the body of a module or function in MMA? For example I would like to turn this: myModule[]:=[{}, Body of module ] to: ...
3
votes
1answer
80 views

Evaluating arguments of module (inside compile)

Note: this is a clarification of 2 earlier unanswered questions, Compile issues, scoping and order of evaluation and Optimising a simple computation with vectors . The problem I have a list of ...
3
votes
1answer
91 views

How to localize symbols on the right-hand side of Rule when using ReplaceAll?

I am frustrated by the following: {1, 2, 3.0} /. a_Integer -> a + 1 The a on the left side of the arrow is localized and ...
3
votes
1answer
189 views

How can I set multiple values to local variables in a Module?

Multiple assignment is supported by Set. How can do something similar in Module expressions? If I could do it, whould there be ...
3
votes
2answers
83 views

Memory Problem with Modules and NDSolve

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is: ...
3
votes
2answers
158 views

Strange interaction between Module and ListPlot

Why is the variable a ending up assigned to {1,2,3} after executing Module[{a}, a=Range[3]; ListPlot[a] ]
3
votes
1answer
74 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...
3
votes
1answer
64 views

Pathological behaviour when evaluating a sum

When I declare the function $f$ as follows: f[n_] := Sum[x/Sum[Sqrt[y], {y, 1, x}], {x, 1, n}]/n the input ...
3
votes
1answer
151 views

Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
3
votes
1answer
103 views

ControlType -> None Vs. Module inside Manipulate (i.e. making everything local)

I'm building a program on another one made available in the Wolfram Demonstration Project. I've noticed that in the original code (working flawlessly) many variables were made local inside the ...
3
votes
0answers
63 views

Decrease computation time of module [closed]

I need to compute the exponential of a matrix (in this caseR) by the summation method where R is a square matrix with symbolic ...
3
votes
0answers
87 views

Why is it not allowed to define vectors of variables in a scoping construct?

The following command assigns 1 to a, 2 to b and 3 to c: ...
3
votes
0answers
200 views

Module in numerical model for NonlinearModelFit is slow and leaks memory

I am trying to make a NonlinearModelFit where the model is the result of a numerical calculation. My code works, but it is very slow and also seems to have a memory ...
2
votes
1answer
129 views

Odd edge-case behavior of Block

In this example, Block is used to localize the variable cache as used in the function g when called from the function ...
2
votes
2answers
610 views

Scoping inside of Module and Manipulate

This is driving me nuts: I'm trying to control the parameters for a relatively large system of ODEs using Manipulate. ...
2
votes
3answers
95 views

Dependency injection - use a specific implementation of one function within another

Consider... f[x_]:=Body containing h[some args]... g[x_]:=Body containing h[some args]... However I want to define something like... ...
2
votes
2answers
117 views

DynamicModule with intermediate expressions

I am trying to generalize a Manipulate construct into a DynamicModule one to try and include smoother controls, but I seem to be ...