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

learn more… | top users | synonyms (1)

10
votes
4answers
841 views

Subscripts - 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
2answers
39 views

Evaluating While Loops inside Modules [duplicate]

I am trying to compute some objects inside while loops within modules, but I keep getting the following error: ...
3
votes
2answers
86 views

Problem with PlotLegends in Manipulate

Please consider this simple code: Manipulate[ Plot[{a Sin[x], a Cos[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"], {a, 1, 2}] Mathematica returns this ...
3
votes
2answers
120 views

Plotting a function variable vs. an explicit function

Can someone tell me why this doesn't plot/animate? ...
1
vote
2answers
58 views

Dynamic Updating

I want to take some points, place them onto a graph and then use the locator pane. I then want to take the new locations that the user has moved the points too and remove the y coordinate and use just ...
1
vote
0answers
27 views

Using DynamicModule and Locator with an external Module [closed]

I wish to call an external Module, called PCA inside a DynamicModule. Essentially, this ...
3
votes
4answers
88 views

Working with ScheduledTasks inside DynamicModules

I have a question - is it possible to run ScheduleTask[] inside DynamicModule[]? What I have tried so far: ...
22
votes
3answers
472 views
18
votes
0answers
253 views

Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0 and persisting through 10.3.1 or later In the following example, inside the minimumX's Module, ...
3
votes
1answer
83 views

How to inject segments of code into functions defined by SetDelayed [duplicate]

I need to define a set of functions that are written in terms of common but lengthy expressions. To effect this, I want to inject abbreviations for such expressions into the RHS of ...
12
votes
0answers
114 views

Integrate - Why is the integration variable not local

I have noticed something odd: Let f[t_]:=Integrate[x, {x, 0, t}] h[t_]:=Sum[x, {x, 0, t}] Now, enter ...
13
votes
3answers
237 views

How does $CellContext` work?

The only official info about $CellContext` I was able to find is: placeholder for the context of a symbol inside Dynamic ...
5
votes
1answer
64 views

How to solve an equation system whose equations are based on Mod[x,y]

I'm trying to find the parameters of a specific linear congruential generator (pseudo random number generator) based on a list of the first five numbers generated by it. The list is this one: ...
4
votes
2answers
118 views

How to take derivative of a interpolated function inside the module

When handling derivatives of a interpolated function, I often use the following function makeDeriv[f_InterpolatingFunction] = D[ f[x], x] Then, I can define ...
4
votes
2answers
97 views

How can I pass arguments to Module without being reinterpreted each time they appear inside it?

I'm trying to understand how to save time when calling functions in Mathematica. I wrote this example, ...
0
votes
0answers
72 views

What is the right way to combine a Package, Module, ParallelTable and NIntegrate?

I am stuck with a curious problem. I would like to to have a package from which i can call a function test[path], which then performs a time consuming numerical ...
3
votes
0answers
109 views

Assign variables from list inside With or Module

I have found that it is possible to assign variables from lists, as follows. r = {1, 2, 3}; {a, b, c} = r; b 2 However, ...
4
votes
2answers
75 views

Assigning function values in a memoized recursive module

I have a complicated recursive module which I have memoized. I realised that certain isomorphs of inputs to this function will give the same output. Therefore I decided to memoize all such ...
9
votes
2answers
211 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
3
votes
1answer
51 views

Blocking Dataset`$ElisionThreshold doesn't work

I would like to define a simple function to automatically show all rows in a Dataset. Based on Taliesin Beynon's answer, I tried the following trivial snippet: ...
3
votes
2answers
91 views

Module, local variable

Lets say I define a Module, that internally use iterating in a Table or Do, say something ...
12
votes
1answer
222 views

Temporary variable not removed in Mathematica 10

Bug introduced in 10.0.1 and fixed in 10.0.2 In Mathematica 10, the documentation still states that a symbol with attribute Temporary will automatically be ...
17
votes
2answers
402 views

Messages leak out from Block

Bug introduced in 8.0 or earlier and fixed in 10.1 When assigning message to a symbol localized by Block, I would expect that this message is not accessible ...
1
vote
1answer
70 views

Module inside module in `Compile`

How can I make compilation to go through with a module inside module? I tried this, failed, a few other ways failed too. ...
0
votes
0answers
62 views

Is tensor argument passing to module expensive?

I wrote a simple module which is supposed to do recursive computation, however it turns out to be expensive even for 300 points. I am not sure why is it slow, all the computations seem to be ...
1
vote
0answers
80 views

Should I Remove the initial symbols of Module?

Often we use Module to construct complicate function. Consider that Module create new vars every time we run the function, should I remove the initial values at the end of Module-function?Or should I ...
20
votes
1answer
209 views

Strange behavior with Cases and Position with SetDelayed

I feel like I must be missing something simple and obvious here, but this has me scratching my head. This works as expected: ...
3
votes
2answers
227 views

Strange interaction between Module and ListPlot

Bug introduced in 10.0 and fixed in 10.1 Why is the variable a ending up assigned to {1,2,3} after executing Module[{a}, a=Range[3]; ListPlot[a] ]
0
votes
0answers
102 views

Best practices for breaking up functional blocks?

Say I have a main function that does some job, and I'm only interested in the final result of ans, i.e., the local functions are not important and produce no ...
5
votes
3answers
203 views

Turning an expression into a function inside another function

Say I have an expression g=Sin[q]; It is easy to turn it into a function, and to perform manipulations on this function (this and variants of this were discussed ...
49
votes
5answers
1k views

How to avoid nested With[]?

With[ {v1 = #}, With[ {v2 = f[v1]}, g[v1, v2] ] ] How to avoid nested With[] like the above? I'd like to use ...
7
votes
2answers
108 views

Cannot Block Plus

It is customary to use the following idiom to partially evaluate something inside a held expression: ...
3
votes
2answers
105 views
18
votes
1answer
239 views

A With expression local variable specification without head List

I just noticed a new form of With in its documentation: With[$x=x_0$, $expr$] specifies that all occurrences of the symbol $x$ in $expr$ should be replaced ...
0
votes
0answers
21 views

How to get rid of numerical scope error? [duplicate]

I am trying to perform a numerical maximization routine on a setdelayed function which involves a numerical integral and I'm getting an error message. The evaluation continues and gives the right ...
2
votes
0answers
70 views

Scoping all symbols

After working Mathematica for a couple weeks to write a larger program, beyond doing integrals, I am starting to gain an appreciation for the Wolfram Language. There are a couple issues that drive me ...
9
votes
2answers
201 views

How to define UpValues in a different context from within a package

I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but ...
1
vote
0answers
19 views
3
votes
2answers
108 views

How to use locally scoped dynamic variables in Manipulate for expression and control parameters

I have a Manipulate that has dynamic variables that I want to use in the displayed expression and use in the parameters for the manipulate controls. I only ...
3
votes
0answers
37 views

Cross referencing symbols in different packages [duplicate]

I have problems with name space shadowing which I do not know how to resolve. I have two packages which depend on each other. Here is example of two files fooPackage.m: ...
2
votes
1answer
62 views

How to write a function that returns the combined output of several built-in functions

I am trying to make a module that takes a Boolean function, and prints the following: the function's truth table the function's NOR epxression the function's minimal Disjunctive normal form So far ...
2
votes
2answers
146 views

Expressions containing globally undefined symbols inside a function where they are defined

Consider the following code. f[a_,b_]:=x; Clear[a,b]; x=a+b; f[1,2] It returns a+b. However, from a certain viewpoint, one ...
-1
votes
1answer
94 views

Module whose input is a function of 2 variables [closed]

I need to make a Module whose input is a function of 2 variables and 2 variables m,n so i can make a Matrix with Array. Basicaly, I need this: ...
0
votes
1answer
40 views

Behavior of custom file chooser code changes when placed inside DynamicModule

My primary objective is to build a file chooser that aides a user in selecting files into one of two categories. Files can be individually selected, or based on the clicking of a specific checkbox, ...
139
votes
7answers
6k views

What are the use cases for different scoping constructs?

Mathematica includes three functions that I know of which can be used to effectively (if not actually) define a variable and give it a value within a local scope: ...
5
votes
1answer
94 views

Dynamics and Block interfere

As mentioned by Albert Retey in a comment, we can unintentionally interfere with our dynamics as follows ...
0
votes
1answer
84 views

Basic (?) dynamic module questions (If clause and refresh problems)

I'm building a dynamic module, but I'm stuck with a few things i don't know how to deal with. In particular I'm stuck with the "If" sentence and the refresh inside the dynamic module. Here's a ...
1
vote
0answers
34 views