Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
12
votes
2answers
330 views

RecurrenceTable iteration variable not localized? Bug or user error?

I recently lost a lot of time on this and need to understand whether I have misunderstood MMA operations or there's a problem. I am using MMA 10.1 on Windows 7 64-bit. The essence of the issue is ...
0
votes
1answer
103 views

Is there a way to apply Module and LocalizedVariables -> False simultaneously?

In my project, there are some variables that I want to define locally so that my defined function has no Manipulate dependency with each other when calling the function twice. By using ...
2
votes
0answers
106 views

How does Table behave differently? [closed]

I have some functions, that I cannot break down to a reasonable MWE, so I'm going to try for an abstract question without providing the code. If need be, I'll include the extra code. I have some ...
21
votes
1answer
422 views

Bug with iterated With: Referring to function arguments invalidates previous definitions

Thank you for your report. I have forwarded your report to the appropriate team for review. - WRI ...
2
votes
0answers
95 views

Replacing a built in function with my own one [closed]

I'd like to replace a built in Mathematica function with my own function. In the particular case I'd like to have: ...
1
vote
1answer
97 views

Replacement in Module[] after importing a list

I have a function defined by its series expansion $$ \begin{align} F(\vec{x}=(x_1,\dots x_l);\vec{y}=(y_1,\dots,y_l);q)=\frac{1}{\sum_{k=1}^l y_k}\sum \limits_{j=0}^{\infty} (\prod\limits_{i=1}^{l} ...
14
votes
1answer
115 views

Bug when combining Dataset with a scoping construct and Map

The following fails in v11: ...
2
votes
0answers
86 views

Internal`LocalizedBlock doesn't support Protected symbols and emits weird messages

Protect[x]; Internal`LocalizedBlock[{x[0]}, x[0] = 1; x[0]] ...
21
votes
1answer
437 views

ClearAll::clloc - “Cannot clear local variable x” in Block[{x}, …] - why?

Block[{x}, ClearAll@x] ClearAll::clloc: Cannot clear local variable x. The documentation just says An error occurs because ClearAll cannot be used with a ...
12
votes
2answers
174 views

Bug: With[] pattern-variable renaming misses symbols within `Except`

Bug introduced in 10.1 and fixed in 11.1 Thank you for taking the time to send in this report. It does appear that pattern-variable renaming misses symbols within Except when using With. I will ...
5
votes
3answers
166 views

Why does `Block` reset `Attributes`?

Not even "formal symbols" such as \[FormalX] are guaranteed to always be symbolic. Their Protected argument is forgotten when ...
14
votes
2answers
243 views

Is anonymous pure function a scoping construct?

In recent thread was raised the question: why anonymous pure functions Function[body] (or body &) do not rename symbols in ...
11
votes
4answers
541 views

Using predefined expressions as free variables in a pure function [duplicate]

I want to have a function value of an expression where some variables are solutions to some set of equations, with some values of parameters. I had an idea to use pure functions for that. However, ...
8
votes
2answers
342 views

Unexpected variable renaming depending on form of a pure function

I encountered unexpected difference in variable renaming behavior depending on whether a pure function with a named parameter, or a pure function with a slot is used: ...
0
votes
0answers
94 views

Problem Plotting `Module` Output

I have code that automatically outputs a parametric function that's found by using ParametricNDSolveValue: I'm using a Module ...
1
vote
1answer
75 views

Define a function inside a function when the function is passed as a parameter

I'm trying to pass a function of a variable (t) into a module where I will do lots of manipulation (such as taking the derivitave). But I'm not sure what the right way is. I have a workaround (f2), ...
13
votes
1answer
146 views

Unexpected optimization with With

I do not understand why the function With[{y = #}, {y, y, y}] & maps faster than the function {#, #, #} &. The ...
5
votes
2answers
790 views

Compute Hessian of function symbolically

This question could equally apply to the computation of other symbolic transformations of a function, but I use the Hessian as an example here. Consider a two-variable function ...
6
votes
3answers
124 views

Condition usage clarification

ref / Condition / Details says: lhs := Module[{vars}, rhs /; test ] allows local variables to be shared between test and <...
5
votes
1answer
62 views

Manipulate switching datasources when pasted in new notebook

I have a notebook to build a Manipulate. I perform an evaluation of the notebook and copy & paste the Manipulate into ...
16
votes
0answers
186 views

Why are delayed definitions in scoping functions NOT documented?

As outlined here, := may be used in scoping constructs like With to give delayed definitions, but this is undocumented. This ...
0
votes
0answers
147 views

Module and memory use

Introduction My question is related to this one: https://stackoverflow.com/questions/6867892/reducing-memory-usage-in-an-extended-mathematica-session I have a lot of Mathematica code in which I ...
0
votes
0answers
42 views

Deleting variable assignments in a sub-notebook which is called from my main notebook

I have two notebooks; one main notebook which calls another notebook several times in a loop. I want to delete the assignments of variables in the sub-notebook which is called from the main one. ...
13
votes
1answer
274 views

Working with DynamicModule: SetDelayed + OwnValues

Shortly, definitions in form of e.g. x:= RandomReal[] have to be avoided inside DynamicModule[{x}, ...]. Why?, this ...
5
votes
3answers
151 views

Using a function from inside a module [duplicate]

I try to plot a function for special values of some parameters which I don't want to set globally: ...
1
vote
1answer
122 views

Dynamic Plot of non-linear function within DynamicModule yields dynamically changing constant line instead of expected behavior

I'm having a problem with DynamicModule; the code below produces a plot with a line that changes value, based on the slider positions of the parameters, which is very nearly what I want; I would ...
0
votes
1answer
68 views

why does this function fail to return? [closed]

I have written 2 functions to calculate the stress and strain of a material, the strain function works perfectly, but the stress function which very similar to the Strain function did not return what ...
3
votes
2answers
412 views

Using Solve[ ] inside a Module

I want to use Solve[] inside a Module. If I make the variables solved for local to the module, they are treated differently than ...
1
vote
0answers
43 views

Dynamic fails to update after evaluating `Pause[]` within `Block` [duplicate]

When evaluating this, the final value of a is 0 as it should be, but Dynamic[a] shows a <...
5
votes
0answers
156 views

Why patterns names aren't scoped by default?

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get: ...
3
votes
1answer
84 views

Why does syntax highlighting in `Set` and `Rule` not color pattern names on the RHS?

The mathematica front-end colors x on the right hand side of x_ :> x and f[x_] := x but ...
5
votes
4answers
221 views

How to scope `Pattern` labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
30
votes
1answer
844 views

Is the renaming mechanism of `With` flawed?

I'm confused about the renaming mechanism of With: ...
1
vote
2answers
739 views

Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the ...
0
votes
0answers
106 views

How to use a numerical integral as an input function

I am new to mathematica. I am trying to use a numerical indefinite integral as an input function of a module. I have a function $$r^* \equiv r^*(r)=\int_0^{1/r} B(z)dz$$ with $B(z)$ being a analytic ...
4
votes
1answer
225 views

Using previous result inside Module

I enjoy using "%" to capture the previous output as it allows me to split big tasks into chunks without having to introduce lots of intermediate variable names. Consider an overly simple example: <...
2
votes
0answers
157 views

How to use Compile? Bivariate Gibbs Sampler

I am trying to use compile to speed up my Gibbs sampler. I know I can use Reap and Sow to speed up tremendously. I have a more complicated Bayesian posterior that I want to speed up with compile so I ...
0
votes
1answer
38 views

How to turn a function's input argument into a new function

I want to write a function via a module that takes a user's input and creates a function from the input within the module. That is something like Input: ...
5
votes
1answer
131 views

Using delayed evaluation on module variables called through an association

I came up with a construct that works well enough for my current use case but has some flaws (at least I suspect it does). Say we have a function that calculates some stuff that takes a while to ...
18
votes
4answers
461 views

Internal DynamicModule steals scope of external DynamicModule

I have two dynamic objects, one nested in the other. Both have their own functions defined, as follows. I have simplified them to the point where they don't make much sense, but can still illustrate ...
9
votes
1answer
288 views

Why does the global variable exist here, where it was declared only in Module?

Why does Mathematica choose the mechanism that makes the global variable a here while parsing? In ...
-3
votes
1answer
132 views

Animate Spring Pendelum

Is there ANYBODY who can solve this exercise??? Visualize the oscillation of a spring pendulum . Write to a function ( modules ) "spring mass" [ eg , nzigzag ] , which visualizes the spring ( Line ) ...
8
votes
1answer
380 views

Where does a package have to be loaded?

I'm trying to solve a very tricky issue in my function. The function needs the NDSolve`FEM` framework. Here is a minimal example: ...
11
votes
2answers
208 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
79 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
1answer
3k 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 ...
8
votes
1answer
459 views

Package functions and symbolic calculations

I'm writing my package (.m) in Mathematica and when I call the function it returns 0 instead of a right output. Here's my code: Package.m ...
1
vote
1answer
112 views

Subscripted variables give errors in Module expression [duplicate]

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
859 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 ...
9
votes
1answer
1k 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 ...