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

learn more… | top users | synonyms (1)

10
votes
1answer
113 views

How to correctly implement in a new function the scoping behavior of Table, Sum and other commands that use Block to localize iterators?

It is documented that "Block is automatically used to localize values of iterators in iteration constructs such as Do, Sum, and Table." Therefore the dummy index (iterator) in a Sum is shielded ...
0
votes
1answer
43 views

Module, block or whatever to scope variables without explicitly listing them? [duplicate]

In larger notebooks I have often trouble with conflicting variable names. To combat this there are Modules and blocks. But with those you always have to list the variables you are using. I don't ...
0
votes
0answers
36 views

Is it possible to declare a global variable in a package? [closed]

I have to create some functions in a package to solve inequalities step-by-step. I am having problems understanding some scope issues. For example, I have this function: ...
0
votes
0answers
18 views

How to plot eigenvalues as a function of parameters in a dynamic module?

I am trying to make a fancy interactive plot that shows how the eigenvalues of a matrix mat change under variation of parameters ...
1
vote
1answer
55 views

Subscripted variables give errors in Module expression [closed]

I have following problem. I want the R not to calculate before the integration but substitute later as a function of z. My Mathematica codes look like: Codes like big, (sorry) but most of them ...
6
votes
2answers
310 views

What does empty {} mean in Module? [duplicate]

I have a question. I would like to know what an empty list ({}) means when used in Module[] as the code below shows? Why has a ...
6
votes
1answer
154 views

Import another Notebook as if it were a Package

Is it possible to import the contents of another (existing) notebook into the current namespace in the same way that I can with a package? I know that it would be best to use a ...
1
vote
3answers
111 views

Multiple Plots inside a Manipulate with different dynamic variables on their x-axes

I have an expression with lots of variables, and I would like to make several plots of the expression, where the axis in each is a different variable. The idea is to put them side by side inside a ...
2
votes
1answer
65 views

Module vs. DynamicModule in a custom plot function [closed]

In my Mathematica work flow I move often used functions into a custom package (.m file) at some point. Since I like the features of the CustomTicks package, I have a collection of custom plot ...
1
vote
2answers
41 views

How do I Define a Module Which Displays Some Graphics and Returns Some Data?

I want to bundle some actions of a task in a module to call it a few times. The task shall display some Graphics and it shall return some data (as a list). But I can't do that (it would have been ...
2
votes
1answer
68 views

Code optimizations with functions inside Module

I wrote a small code by using functions inside Module: ...
3
votes
0answers
174 views

Is Mathematica's lexical scoping broken?

Why do these two lines output different things (b and 0, respectively)? Is it a bug? ...
1
vote
1answer
116 views

Is there any way to implement a “sequential” With[] in Mathematica? [duplicate]

I want the equivalent of Scheme's let*, or basically, a sequential With that works like this: ...
1
vote
1answer
39 views

How do I convert an expression that is a function of some variables into a functional?

If I have an arbitrary f[x, y, z], how do I convert it to f[#1, #2, #3] & without messing up scopes or causing scope ...
8
votes
0answers
72 views

What is the equivalent of $ModuleNumber for DynamicModule

Mathematica provides access to $ModuleNumber which can be enormously useful. Unfortunately I can't find the equivalent for ...
1
vote
1answer
48 views

How to write a function in the divide-and-conquer style [closed]

I'd like to vary a factor in a matrix calculation, so I have used Module to make it happened, but it doesn't work. Below is a code example. can someone tell me how ...
2
votes
2answers
78 views

Setting local variables with another function

I'm working on a package with a lot of functions using Module, which all begin by unpacking the arguments into local variables. I'd like to store this unpacking code in a separate function called ...
1
vote
0answers
48 views

ParallelTable gets slower when rerun in a module

Question: Why does the performance of ParallelTable within a Module drop so horribly when the module is RERUN several times? ...
0
votes
1answer
53 views

why URLFetch is slow when encapsulated by module and how to fix it [closed]

I'm using Mathematica 10.0.2 on Win7. I'm writing some program to crawl etymology of words on http://www.etymonline.com/. Below lies the example code: ...
1
vote
1answer
51 views

Declaring functions in NDSolve? [duplicate]

I have a simple enough series of coupled ODES, which is relatively easy to solve. It looks something like this. ...
1
vote
2answers
74 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
128 views

Plotting a function variable vs. an explicit function

Can someone tell me why this doesn't plot/animate? ...
3
votes
2answers
98 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 ...
1
vote
2answers
65 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
34 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
99 views

Working with ScheduledTasks inside DynamicModules

I have a question - is it possible to run ScheduleTask[] inside DynamicModule[]? What I have tried so far: ...
12
votes
0answers
126 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 ...
3
votes
1answer
88 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 ...
5
votes
1answer
66 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
138 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
100 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, ...
4
votes
2answers
80 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 ...
3
votes
1answer
66 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
107 views

Module, local variable

Lets say I define a Module, that internally use iterating in a Table or Do, say something ...
1
vote
1answer
81 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 ...
20
votes
1answer
216 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: ...
5
votes
3answers
223 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 ...
7
votes
2answers
110 views

Cannot Block Plus

It is customary to use the following idiom to partially evaluate something inside a held expression: ...
3
votes
2answers
108 views

Sections with unidirectionally shared variables

I have 2 sections is my notebook: ...
18
votes
1answer
249 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
72 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 ...
1
vote
0answers
19 views

Manipulate inside Module - “Order of evaluation conflicts” [duplicate]

In this example, Manipulate is contained within a Module: ...
3
votes
2answers
125 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 ...
1
vote
0answers
82 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 ...
3
votes
0answers
38 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
70 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 ...
-1
votes
1answer
105 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: ...