The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [scoping]

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

50 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
23
votes
0answers
401 views

Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0, fixed in 10.4, intentionally reintroduced in 11.0 and persisting through 12.0. In the following example, inside the minimumX's ...
15
votes
0answers
181 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 ...
10
votes
0answers
834 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 ...
7
votes
0answers
128 views

A bug in Derivative?

Update This is a bug in v11.3 or earlier and is fixed in v12. Original Post Check this out: ...
5
votes
0answers
152 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: ...
5
votes
0answers
331 views

Exhaustive examples of variable replacement due to nested scoping constructs

Consider the following examples, where I have replaced dollar signs in the output by a D. ...
5
votes
0answers
480 views

Module in numerical model for NonlinearModelFit is slow and leaks memory

I am trying to make a NonlinearModelFit where the model is the result of a numerical calculation. My code works, but it is very slow and also seems to have a memory ...
5
votes
0answers
143 views

Are there any videos that show how different Mathematica expressions are evaluated?

Are there any videos that show how different Mathematica expressions are represented internally as object graphs and demonstrate how they are transformed during an evaluation? I am asking because I ...
4
votes
0answers
141 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 ...
4
votes
0answers
127 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 ...
4
votes
0answers
217 views

Why is it not allowed to define vectors of variables in a scoping construct?

The following command assigns 1 to a, 2 to b and 3 to c: ...
3
votes
0answers
126 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, if ...
2
votes
0answers
25 views

Unexpected symbol::shdw warning during NotebookEvaluate

Consider the following code: ...
2
votes
0answers
85 views

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

Protect[x]; Internal`LocalizedBlock[{x[0]}, x[0] = 1; x[0]] ...
2
votes
0answers
155 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 ...
2
votes
0answers
131 views

Compile issues, scoping and order of evaluation

I have a few questions about compiling functions, which I think are all related to scoping and order of evaluation. I will illustrate them by a minimal example of the problem I have. I'm sorry for ...
1
vote
0answers
100 views

Transferring a function between two modules in a package

I have a first module which outputs as forth output a function G I want to transfer to another module. The specific function is ...
1
vote
0answers
53 views

ConditionalExpression still resulting from Assuming a Module's local variables

I have a memoizing, recursive function whose MWE looks like this (the two fake inputs/ICs are from 'K' and 'Bfun2', the latter just a quick way to check the results coming out of the Residue.: ...
1
vote
0answers
42 views

How to define a symbollic matrix with variables and numbers

Newbie here. I want to define a (user-defined)function to return symmetric matrices, as I need to define quite a few tensors. I am able to do this using strings. But I need the matrix entries to be ...
1
vote
0answers
84 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? ...
1
vote
0answers
142 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 ...
1
vote
0answers
197 views

Building Reusable Custom Controls?

I would like to build an application using Mathematica 10. I would very much like to structure the application for maximum maintainability. To this end I would like to segregate controls and user ...
1
vote
0answers
68 views

Settings to disable global scope

I am building a fairly large application in Mathematica, and globally scoped variables are causing issues. I would like globally scoped variables to be out-of-scope for my expressions, except by ...
1
vote
0answers
96 views

How to implement nested With as a single construct using functional programming (Fold)?

It is possible to define my own With construct where each local constant can depend on the previous defined local constants? I wish to replace something like this ...
1
vote
0answers
289 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
0answers
17 views

Referencing a control from within a module

Below is a simple Manipulate that references a module via a button, which resets a slider and value. I would like to reference and reset the slider control for the slider "n" from within the module "...
0
votes
0answers
30 views

Problem in solving a trancedental equation which is embedded in module

I have written a small program to compute the resonance frequency, for that I am solving a transcendental equation, which comes from the determinant of the matrix ...
0
votes
1answer
26 views

Turning an equation into a function in a module

I have defined a function like this: f[q_] := Module[{g}, g[x_] := Evaluate[q]; ... ] I am calling the function like this: ...
0
votes
0answers
30 views

Can I still use global variables when I choose the default context to be the cell group?

To avoid accidentally reuse the same variables in different sections of my notebook, I go to Evaluation menu and choose Notebook's default context > Unique to each cell group. But sometimes I do want ...
0
votes
0answers
24 views

Gradient method in vectorfield on surface table does not work, module is slow

For teaching purposes I want to generate a vectorfield on a surface an visualize the path of the gradient method. The surface and the gradient are generated by ...
0
votes
0answers
52 views

Assigning values to variables inside Module using user defined function

Say I'm defining a function with some local variables, for which I'm using Module. Now, I want to assign some value (or perhaps a list of values or a list of list, ...
0
votes
1answer
110 views

Finding the inverse of a function

I am to solve for $r(\rho)$ given the function, \[Rho]Asymp[r_,b_,q_] := 1/(1 - q) Gamma[1/(1 - q)]/Gamma[(q - 2)/(q - 1)] r Sqrt[1 - (b/r)^(1 - q)] This can be ...
0
votes
0answers
40 views

K vs k in a module

Module[{K = 1, fn}, fn[p_] := Print[K]; ParallelTable[fn[p0], {p0, 2}]]; Prints K$316 (or whatever the module counter is on)...
0
votes
0answers
48 views

How to speed up a module function with a large number of arguments?

I'm setting up a fairly hefty numerical code dealing with matrices of variable dimension, but on the order of hundreds of rows and columns. I'm using a Module format: ...
0
votes
0answers
34 views

To find the Null Space of a matrix which is inside the module

...
0
votes
0answers
68 views

Iterating a Module

I have a Module that can act on other Modules, which is given as Kk[EE,p,z1,z2,z3,K_func] and I am trying to iterate it, such that ...
0
votes
0answers
32 views

Tracking System function (Get) calls, and Stack[_]

Consider the following attempt and variations thereof to track (nested) calls to Get (reasoning of this approach below): ...
0
votes
0answers
38 views

Imitate Integrate's behavior with regard to the integrand parameter (scoping/naming)

Using Integrate, I can do any of the following: ...
0
votes
0answers
28 views

Defining functions while evaluating a module

This might be a very basic question, so apologies in advance, but I couldn't find a direct solution. Suppose I have a module that takes function(s) and expression(s) as its arguments. For the sake ...
0
votes
0answers
182 views

Parallel computing issues when running a Module

I'm running a heavy simulation and try to use Parallel computing to run a Module case. I have been working on this parallel computation for more than three months, but still haven't come up with a ...
0
votes
0answers
37 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 ...
0
votes
0answers
108 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 ...
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
83 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 ...
0
votes
0answers
93 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 ...
0
votes
0answers
138 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. ...
0
votes
0answers
98 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 ...
0
votes
0answers
80 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 ...
0
votes
0answers
110 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 side-...