A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
4
votes
3answers
587 views

Returning a function built with local (block) variables

I would like to build a block that outputs a pure function. Something like: ...
0
votes
1answer
157 views

Extract data from plots created inside the module environment

I need to extract the data from plots created inside the module environment to re draw my plots in a better plotter that Mathematica. I have a code that includes several matrix multiplications, and ...
0
votes
0answers
56 views

function from .wl produces no output [duplicate]

I'm trying to learn to reuse functions. I have created a .wl file with the following content. As I understand the last line without semicolon is the function output. Should each function have it's ...
0
votes
2answers
32 views

Dropping \$NUMBER from local var$number when passing results from a Module [duplicate]

I'm sure this has been asked, but I can't seem to find the correct terms to find it. I have a function that looks something like this ...
6
votes
2answers
125 views

How to localize symbols in 'Manipulate'

I'm confused about the best approach to localizing symbols used in a Manipulate. I understand that Manipulate does a good job by ...
3
votes
2answers
271 views

Module inside Module. Conflict of names in arguments

I have a module which calls number of other smaller helper modules. But those other helper modules are not used by another module, so there is no needed to have them in global context. So I wanted to ...
2
votes
1answer
92 views

Variable range maximum for a Manipulate expression inside a module

Let's say I have a function defined as a module where at the end I call up "Manipulate" as below: a[x_]:= Module[{k},k = 2 x; Manipulate[Something[y], {y, 0, k}] ...
15
votes
2answers
474 views

Why can't I inject expressions in Compile using (only) With

Generally speaking, With can be used to inject into arbitrarily held expressions, for example: ...
0
votes
0answers
38 views

Error in storing the output of a module (subroutine) into an array

For a physics application, I have to compute components of a rank-4 tensor, the Riemann tensor. I do this using a module (subroutine) which looks like this ...
4
votes
3answers
253 views

Understanding Block and Definition

The code Block[{f}, f := test; Definition[f]] Outputs Null. Why? I would have expected it to output the definition of ...
1
vote
2answers
73 views

Refering to local module variables

How can I do the following? I have code inside a Module such as Module[{a1,a2,a3,a4}, code...]. In this code I want to refer to these constants efficiently (for a ...
1
vote
2answers
326 views

Replacement rules are not effective in a module

I am trying to bracket the roots of a function with the variable p. The assumptions are: the root exist between 0 and 1, the value of the function is positive at <...
2
votes
3answers
210 views

Pure function within another function

I am writing a function along the lines of the following: func[basis_,coordinates_,vars_]:=Table[Function[vars,monomial]@@@coordinates,{monomial,basis}] The idea ...
1
vote
1answer
66 views

Weird behaviour of If inside of Block definitions?

I am on Mathematica 9 and I have discovered, that, when I use If inside of the definitions of the Block variables, then other, ...
2
votes
1answer
104 views

How to define an auxiliary function within a function [duplicate]

I would like to define a function within a function to serve as an auxiliary function. In essence, something like this: f[x_,y_]:=Module[{g[z_]:=2z},x+g[y]] ...
3
votes
1answer
47 views

Dynamic divide as number rather than fraction

I'm practising making Dynamics so I made a simple solution measurement converter but I'm having trouble getting a numerical output at ...
1
vote
1answer
131 views

call function to trigger update to dynamic — what's the right way

I want to create a plot using Dynamic. Ideally, I'd have some persistent, locally scoped state variables living in a Module; as ...
2
votes
1answer
113 views

Passing a function as a parameter to a module [closed]

I'm currently trying to introduce myself to some programming with Mathematica. I'm not the most familiar with programming in general, but I work best by just trying to make stuff. In this code, I'm ...
3
votes
2answers
335 views

How to set all variables in function as local

Here is an example code: a = 1; b = 1; c = a + b; f[x_, y_] := Module[{a = 3, b = 2, c}, c = (x + y)*(a + b)]; a,b,c are ...
3
votes
2answers
187 views

Plot and an error

I have set of matrices(multiplied to give U123[q, p, r]) and would like to diagonalize U123[q, p, r] and plot the eigenvalues. ...
4
votes
2answers
141 views

Defining pure function using module

I am creating a function fun which takes a parameter p and returns a pure function: ...
5
votes
1answer
121 views

Unique[] and special handling of symbols ending with a single dollar sign

Example Unique[x] generates a new symbol, with a name of the form x$nnn. If so then this is not correct: ...
4
votes
0answers
149 views

What is the best way to localize variables in a custom control

I am trying to build a custom control that is just a combination of a group of smaller controls that combine to give the final value, but with the final value only updating when the controls stop ...
6
votes
1answer
435 views

Begin[“`Private`”] vs Module

So, if you have a package formatted like so: ...
1
vote
0answers
77 views

Odd problem with `Module` and `Block` [duplicate]

I found some bizarre phenomena in my code. While I'm using Module in my function, the result is incompatible with which I did outside ...
7
votes
2answers
152 views

How to create Functions that have Compiled functions effectively

I'm about to build a package that will provide lots of simple functions. I want these functions to run as fast as possible when evaluating numerically but still provide symbolic results when needed. ...
0
votes
1answer
207 views

Problem with Module, Function, Association and Dataset

I have struck a combination of the above-mentioned functions that causes evaluation to fail. I wonder whether someone can help me create a more minimal example in order to understand the issue better. ...
6
votes
1answer
151 views

Explaining error when using Part inside Module in Dataset

I have a vague recollection of seen an explanation for this, but I can not find it, so it may be a false memory. Will delete if duplicated. Let ds be a simple <...
3
votes
2answers
253 views

How to access externally defined symbols without polluting the current namespace?

NB: In this post, by fully qualified names I mean names like Global`x or SomePackage`y, which include their complete context ...
0
votes
1answer
143 views

How to avoid namespace leaks?

Below is a toy package that illustrates what I mean by "namespace leaks". ...
1
vote
1answer
147 views

Defining local variables in a dynamic way in Module

I have a function whose variable are specified by a input parameters of function: ...
0
votes
0answers
265 views

Module: local variable specification error [duplicate]

I am developing a module which does some vector algebra, and I have added an image of the code in the link below. (I did not find a good way to add formatted code in this pane). If the image ...
4
votes
2answers
134 views

Can a Dynamic be attached to the single elements of a list?

Consider the following example: ...
5
votes
1answer
496 views

Parallelization inside a module: Proper use of local variables

I have the following Module which includes a ParallelDo call. Unfortunately, ParallelDo is ...
0
votes
2answers
338 views

Print expressions using local variables in Module without Dollar sign: is that possible

In a Module, I want to print the result of some data fit. I use something like this: ...
23
votes
4answers
728 views

SetOptions locally?

Is there a way to SetOptions locally? For example, I have a Module and inside it I am doing a lot of ...
0
votes
2answers
250 views

Using recursion to find the trace of gamma matrices

All you need to know about the gamma matrices for this problem is that there are four of them, $\gamma_{\mu}$ with $\mu = 0, 1, 2, 3$, and that the trace of the product of n gamma matrices (n even) ...
0
votes
0answers
112 views

Performance tuning with memoisation, compilation and vectorization - further possibilities?

In order to tune the performance of noticeably slow code of mine, I had a look at many different excellent posts on this topic here on M.SE, e.g. here to name the most interesting one. Now I ask ...
3
votes
2answers
176 views

Problem with Evaluate inside Module

My problem I have a large system of differential equations which is automatically created, but to give you an easy example let's just say it is ...
0
votes
1answer
115 views

Plotting: substitute before evaluation

When plotting, the Mathematica seems to substitute the value of the variable only after evaluating the plotted function. To use a simple example where I'm trying to examine how changing certain ...
4
votes
2answers
117 views

Why does my Block[]ed variable not apply?

Consider the following example: ...
4
votes
0answers
130 views

Why does `… := With[{var = …}, …]` create `var$`?

EDIT This issue is specifically different from the "duplicate" question. The creation of variables in the active context during parsing is of no surprise and is the expected behavior. What I'm ...
0
votes
0answers
56 views

Hidden localization of symbols via Module and Interpretation

I want to hide Module's symbol names (such as x$23118) from the end user of a package I am developing. To give some context: The ...
0
votes
0answers
119 views

How do I declare global constants and local constants? [duplicate]

Many times I have a Mathematica code like this ...
0
votes
1answer
58 views

Using expressions in Plot with Manipulate [duplicate]

The following works exactly as advertised: Manipulate[ Plot[5 Cos[Pi t] Sin[Pi x], {x, 0, 2}, PlotRange -> {-5, 5}], {t, 0, 4}] However, if I use an ...
0
votes
1answer
43 views

Locally (de)assigning a value

I have failed in adapt the methods discussed in $\qquad$Pass function or formula as function parameter to my work. I would like to know if there is a way to say the following to Mathematica: If <...
0
votes
0answers
84 views

Scope and loops - displaying data

I am having trouble understanding Mathematica's scoping and what it does with loops. For example, if you would like to display a bunch of images, in python you may construct a loop and show the ...
2
votes
1answer
317 views

Using Manipulate and Module for Riemann Sum

I'm trying to write a program using the Manipulate function that shows the leftmost, rightmost, and midpoint Riemann sums for a function. I've successfully coded ...
6
votes
1answer
1k views

How to prevent name clash between arguments of different functions?

Per comments, including here the description of the problem lifted from another site: Mathematica Pure Function Scope Problem Consider this snippet: ...
21
votes
2answers
557 views

Should I always be using Module in functions unless I explicitly want global variables?

I define functions a ton. In the past, I've mostly just defined my functions like: myFunc[var1_]:=( var2=var1^2; Return@var2; ) And I haven't run into too ...