Questions tagged [scoping]

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

68 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
25
votes
0answers
459 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 ...
16
votes
0answers
192 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 ...
11
votes
1answer
207 views

Proper Syntax highlighting for multi-argument With?

For the case of Block and Module one can choose to define values in the body for the variable names being scoped. So there is no ...
10
votes
0answers
893 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 ...
8
votes
0answers
156 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: ...
6
votes
0answers
345 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
164 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
534 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
144 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
42 views

`Function` does something bizarre with formal parameters

I think this may overlap with existing questions, including one I asked years ago, but I'm not sure how, because the behavior I'm seeing is confusing enough I can't even really describe it except to ...
4
votes
0answers
99 views

DynamicModule Error when assigning Nothing to variable

Assigning Nothing to a variable inside Module works as expected: ...
4
votes
0answers
175 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
131 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
230 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
75 views

Module inside With, With inside Module, or Just Module?

In general, if one wants to define local variables and local constants, which of the following is the best programming practice in terms of performance and code "safety"? (Assume ...
3
votes
0answers
131 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
1answer
84 views

Why some variables get substituted and others don't

I was running into some trouble with my math and I whittled it down to this nugget: ...
2
votes
0answers
32 views

Unexpected symbol::shdw warning during NotebookEvaluate

Consider the following code: ...
2
votes
0answers
92 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
167 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
110 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? ...
2
votes
0answers
154 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
56 views

Autoloading MaTeX` and Rubi` via DeclarePackage[] in the user init.m does not work

Former question: Autoloading and adding code to a package in the user init.m I am using MaTeX, and I found the updateMaTeX[] function] at the GitHub repo. I tried ...
1
vote
0answers
34 views

How to Inherit Arrowheds size?

I'm trying to write a Module that produces some arrows with custom Arrowhead shapes. However, I want the code external to the <...
1
vote
0answers
45 views

Why is my code for counting function calls not working?

I am tryin to calculate how many calls to a function my code is using. ...
1
vote
0answers
50 views

Recursiveness in backtracking algorithm

In order to familiarise with using basic algorithms in Mathematica, I am coding a sudoku solver using a backtracking algorithm. I am treating the sudoku itself as an association (I want to learn to ...
1
vote
0answers
52 views

Using `Block` with Conditions `/;`

I would like Block to run conditionally. I understand how to do one condition, but what about an AND operation? e.g ...
1
vote
0answers
38 views

How to return a variable name and value from within a self-contained function block?

Abstract: I have viewed other solutions on StackExchange but none actually address using a fully self-contained function to return name and value of a variable. The only function that works requires ...
1
vote
0answers
56 views

How to externally access local variables defined inside a With block

I am writing a package in which I have some constants defined inside a With block, which are then used by various functions defined inside that block. Separate to ...
1
vote
0answers
69 views

How to iterate a function

I have written a custom function called f1[r_] and I have tried iterating this function using Table. I work nicely for the first ...
1
vote
0answers
112 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
61 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
45 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
125 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 ...
1
vote
0answers
164 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
206 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
75 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
104 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
311 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
1answer
61 views

How to force Mathematica throwing an error box intentionally?

When building a Module function, I try to throw an error box when this function facing some cases. But I don't know how to output this error box with ground methods. This picture is the Mma error bar ...
0
votes
0answers
31 views

Numerical errors in compiled function involving real and complex numbers as output

I am trying to use the Compile command in Mathematica to reduce the computation time because of its fast execution within Mathematica and ability to use the ...
0
votes
0answers
28 views

Display more outputs inside Module and Which

As it can be seen in the following piece of code, i would like to display more outputs which are inside a Module, part of a function definition. In turn this is ...
0
votes
0answers
32 views

Replace the output of a module containing local variables by their actual value

I want to construct a function as the output of a Module, but I obtain the local variables as part of the output. This is a problem when I try to construct many of these functions in a parallel table, ...
0
votes
0answers
40 views

Locally remove definition of function

Consider the following code g[x_]:=x^2 f[x_]:=2*g[x] f[x] (* returns 2 x^2*) test[x_]:=Module[{g},f[x]] test[x] (* returns 2 x^2 as well*) What I would like to ...
0
votes
0answers
63 views

NDSolve and NIntegrate with parameters

I have a problem with a numeric computation. I should solve a numeric ODE and the rhs is a numeric parametric function that I find with several NIntegrate. I'd like ...
0
votes
0answers
32 views

Evaluating global variable within a module

I'm trying to work with something like the following: a = b; f[x_, b_] := Module[{N}, N = Abs[a]; 10 N ] f[1, 1] The output of running this is: ...
0
votes
0answers
48 views

Is there a way to have local option values for Blocked variables?

Like I can set local attributes for a variable, ...
0
votes
0answers
56 views

help with improving my code

I am fairly new to MMA-12 and have written the following code to calculate possible coordinates (for given constraints [see If[]] in the code) and get the ...
0
votes
0answers
25 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
37 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 ...