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

learn more… | top users | synonyms (1)

5
votes
1answer
134 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. ...
5
votes
1answer
153 views

Treat all variables as local in a module [duplicate]

In most languages, any variable defined inside a function is considered local. ...
5
votes
0answers
112 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
92 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
3answers
157 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
180 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
2answers
163 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
93 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
0answers
101 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
94 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
144 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
98 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
3
votes
3answers
369 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 ...
3
votes
2answers
189 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
247 views

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 ...
3
votes
2answers
388 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
232 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
55 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
82 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
148 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
1answer
61 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
140 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
162 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 ...
3
votes
0answers
129 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
124 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
495 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
89 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
99 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 ...
2
votes
1answer
249 views

Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
2
votes
1answer
139 views

Effect of Block[] on iterators in Manipulate[]

The following is a toy example that hopefully shows the problem clearly enough. Why does the following code create a nice, manipulable output: ...
2
votes
1answer
66 views

Implementing local variables of a recursive function with either Module or Block

First, some quick background: I have been eagerly learning Wolfram and Mathematica for two months now, and along the way have written about 250 pages of teaching material that has ballooned into a ...
2
votes
2answers
115 views

Getting variables out of Dynamic Modules after restoring the program

The scrap of code below [from Mathematica's Help on DynamicModules] generates a Pane of ...
2
votes
1answer
114 views

Module Inside ToBoxes or MakeBoxes

Does someone mind explaining why Module doesn't seem to work while inside ToBoxes or MakeBoxes. ...
2
votes
1answer
84 views

How to make a code line dynamic in a Module?

The code line If[StreamPosition>0,Read[str,Record]] should be executed in a Module at first. The Module consists of a Slider and a Pane. The Slider ...
2
votes
1answer
77 views

Manipulating parameters in a complex algorithm

I have a complex algorithm that depends on: A wavelet basis A number of sampling points out of a distribution A thresholding value I want to be able to play with different scenarios using ...
2
votes
1answer
43 views

Is the use of Block to temporarily override definitions inherently unsafe? [duplicate]

This may have been discussed before, if so, please let me know. Consider the following example: x = 5 Dynamic[{Clock[], x}] This will always display the ...
2
votes
0answers
77 views

Compile issues, scoping and order of evaluation

I have a few questions about compiling functions, which I think are all related to scoping and order of evaluation. I will illustrate them by a minimal example of the problem I have. I'm sorry for ...
2
votes
0answers
35 views

Differences in SetDelayed (:=) in Module and DynamicModule [duplicate]

I have a question about the differences in SetDelayed assignment functions in a Module and in a ...
1
vote
2answers
151 views

Confused about scoping, not sure what to ask

I'm grading for a class using Mathematica right now, and one of the students ran into a weird issue. When he ran code like this: ...
1
vote
2answers
218 views

Error with “Newton's method” in Wellin's Mathematica book

In Wellin's Programming with Mathematica book, here's one of his implementations of the Newton method, where the iteration runs until the error tolerance is reached. ...
1
vote
1answer
135 views

Passing a joined list of symbols for Module or Block to treat as its own local symbols

The Question I'd like to write a function which itself takes a list of symbols, e.g. Foo[{x, y, z}] to pass to Module ...
1
vote
2answers
97 views

Is it possible restore initial values after modify a control in manipulate?

I used to program in Visual Basic, but in Mathematica I have not seen some kind of restore default values after any user modification command in the Manipulate ...
1
vote
2answers
111 views

`Slot` (#) interfering with evaluation

In the following I am using one function makeFunc to assemble two functions a[t] and b[t] to ...
1
vote
2answers
55 views

How can my Manipulate be aware of a local variable inside a nested Module

I have 3 sets of 5 x-y points: RandomInteger[10, {3, 5, 2}] I want to display only one set of points at a time, and use a slider to move on to show the next ...
1
vote
1answer
45 views

How can I change the scoping of a pure function returned by a function in a package?

I have written a small Mathematica package that contains a module that solves some differential equation with initial conditions. Suppose that the solution to this differential equation defines some ...
1
vote
1answer
178 views

Using block or module to free cache

I'm writing some code at the moment that schematically looks like: (set initial conditions for some differential equation for given parameters (M,R,...)) ...
1
vote
2answers
55 views

Is there a way to logically test an optional color input inside a module? [duplicate]

Suppose I create a Module that is expected to return a graphic that I will later use in a plotting procedure (e.g., with ...
1
vote
1answer
47 views

Replicability of RandomVariate within a function?

I have a rather basic question but can't figure out a foolproof answer. In the following code, I build a function which returns the mean difference between successive elements of a vector created from ...
1
vote
0answers
56 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: ...
1
vote
0answers
58 views

Building Reusable Custom Controls?

I would like to build an application using Mathematica 10. I would very much like to structure the application for maximum maintainability. To this end I would like to segregate controls and user ...