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

learn more… | top users | synonyms (1)

0
votes
0answers
44 views

Fold Dynamic Function

I'm making on a demo working with differential equations as functions of two variables (x and y). The demo will have to Fold the differential equation with an initial y-value and a series of x-values. ...
3
votes
3answers
91 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
1
vote
2answers
64 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 ...
4
votes
2answers
152 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" ...
0
votes
1answer
55 views

Multiple Simulations of a Markov Chain

I am trying to create a simple series of Bernoulli bits whose probabilities follow a two-state Markov chain. ...
1
vote
2answers
86 views

`Slot` (#) interfering with evaluation

In the following I am using one function makeFunc to assemble two functions a[t] and b[t] to ...
0
votes
3answers
117 views

Clean-up Code / Reduce variables

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example: ...
4
votes
1answer
78 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}], ...
5
votes
1answer
141 views

Returning a value from a Module

In the following simple example MyMod := Module[{i}, Do[ Return[1], {i, 3} ]; Print["test"] ] I expected the module to exit on ...
0
votes
0answers
46 views

Accessing variables outside DynamicModule

I have this DynamicModule which is basically a form of input fields that are parameters. My two final functions are H1 and ...
0
votes
0answers
42 views

Dynamically Update Inset within Graphics with RunScheduledTask

I followed the instructions given below to play with RunScheduledTask within DynamicModule. ScheduledTask with scoped ...
4
votes
1answer
102 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. ...
0
votes
1answer
86 views

Why do InputFields not respond in Manipulate utilizing FindGeometricTransform?

In the following code changing the InputField does not result in observing changes in a result from FindGeometricTransform, but ...
0
votes
2answers
54 views

Defining Piecewise Functions in Modules

In short, my question is the same as my previous one except that everything is now wrapped up in a module. The relevant code I'm working with is: ...
2
votes
0answers
32 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 ...
2
votes
2answers
82 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 ...
7
votes
1answer
199 views

Variable scoping in Module

I don't understand why $a$ and $b$ are not local in the following code: ...
0
votes
0answers
82 views

Local variable names - Is there an easy way?

I apologize first, because this has been asked, like in Treat all variables as local in a module [duplicate] Variable scope similar to other languages But I don't get how to use them. Some codes ...
0
votes
0answers
23 views

How to pass rules to packages [duplicate]

EDIT: After helpful comment from @Mr.Wizard I added the working implementation below for future reference. I wrote a module that does something with variables provided in form of a rule. It works ...
6
votes
1answer
50 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: ...
0
votes
2answers
71 views
0
votes
1answer
64 views

Circumventing the default scoping: Assign a variable inside a function [duplicate]

I'm currently trying very hard to assign a variable inside a function. Therefore I'm using a function like this, which should reassign a predefined variable. ...
6
votes
1answer
110 views

Strange behaviour of Internal`InheritedBlock

I was going to post an answer for fast way to replace all zeros in the matrix. I was even quite happy because timings were the same order of magnitude as others. The idea was to overwrite ...
3
votes
1answer
57 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 ...
5
votes
1answer
128 views

Treat all variables as local in a module [duplicate]

In most languages, any variable defined inside a function is considered local. ...
13
votes
2answers
181 views

Module trash collection behaviour

I have asked this question before on the Wolfram community forum (http://community.wolfram.com/groups/-/m/t/83845?p_p_auth=Gxoxe65B) but not with great success. Module creates temporary variables of ...
2
votes
2answers
95 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 ...
0
votes
1answer
186 views

Passing lists to functions in a manner that works like pass-by-reference in other languages?

Is it possible to use a list as a variable, i.e., to pass it by reference to a function? In particular, I have a two-dimensional array and a function to get one element specified by its position: ...
5
votes
4answers
386 views

Why do I see the error “only assignments to symbols are allowed” when using a Module and not otherwise?

When I type Remove["Global`*"] foo[] := Module[{Subscript[a, 1] = "x"}, 0]; foo[] I get the expected error "only assignments to symbols are allowed". I ...
1
vote
1answer
124 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 ...
3
votes
1answer
147 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 ...
11
votes
4answers
278 views

Using With to scope over pure functions

Consider this example: given an option for a function func as "x" :> (a&), how can one assign different values to ...
0
votes
0answers
71 views

Programming a Meijer G related result

I am not that new in Mathematica, but I do not remember I have ever used it for programming (like we may do in Java). I have an important result given here by this integral: $$ \int_0^{+\infty} ...
3
votes
2answers
161 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 ...
4
votes
3answers
89 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 ...
2
votes
1answer
107 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
220 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 ...
1
vote
2answers
142 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: ...
11
votes
3answers
258 views

Variable scope similar to other languages

Mathematica, unlike many other languages, defines the variables in the global scope unless it is explicitly asked to define a variable in a local scope, e.g., using ...
6
votes
1answer
105 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 ...
7
votes
0answers
202 views

Compile recursive function modifying global variables

How to compile recursive formula when it relies on more than a few global variables (global to the topmost compiled function)? It is unreasonable to pass on all such variables to each recursive ...
10
votes
2answers
209 views

Avoiding MainEvaluate in a CompiledFunction to fetch global variables

I'm trying to write a simulation using Mathematica 8.0. Since I will most likely be doing the same operation over and over again, I'm trying to Compile whatever I ...
0
votes
1answer
84 views

Arbitrary number of variables in Module, Block, etc

How do I specify an arbitrary number of variable names and initialize them inside of a Module or Block? For example, I want to do something like: ...
0
votes
2answers
72 views

Scope of expression in a user-defined function

I have the following code: fbasic = A + R + Z*Z *Z*Z; f[A_, Z_, R_] := fbasic + R; f[0, 2, 3] The answer I am getting is ...
1
vote
0answers
100 views

Variable scope in nested NMaximize, NMaxValue

I'm trying to use NMaxValue inside other statements (FindRoot and NMinimize). Those statements should bind variable names, but I'm getting weird warnings which seem to indicate that the variables ...
0
votes
3answers
314 views

Constructing a list with a Module [closed]

I have build this simple module trying to construct a matrix via Table ...
4
votes
3answers
358 views

What is the right way to construct DynamicModule

When I'm creating DynamicModules for CDF purposes I'm usualy basing on my experience with that, particular goal and content rather than on deep insight. I want to ...
0
votes
1answer
181 views

Why the dollar signs when I Solve inside a Module [closed]

I would like the solutions of my equations to be local only, so I did a Solve inside a Module: Module[{a, b, c}, Solve[{a == 3* c, b == 2 *a}, {a, b}]] and the ...
9
votes
1answer
118 views

What are the scoping rules for function parameters shadowing System` symbols?

Here are some very contrived code snippets, highly unlikely to appear in real code, but still I am curious why they behave like this: ...
0
votes
2answers
182 views

Why does my code return {Null, Null, Null}? [closed]

I have a module created which makes what I want, but at the end there are {NULL, NULL, NULL}. Why that? How can I prevent this NULL list? ...