Questions tagged [scoping]
Questions on Mathematica's scoping constructs (Block, Module, With, etc.)
518
questions
0
votes
0
answers
63
views
Counting multiple $-sign string indices in output and replacing them by normal indices
I'm using the package xTensor in xAct. My issue is that inside a module indices are replaced by strings of the form $##### which make the final output unreadable. In a related post Print expressions ...
1
vote
3
answers
67
views
Module returns multiple functions
I want to write a module that returns two functions which then can be evaluated later on. I know how to do it with a single function, but I don't know how to make it work for multiple functions.
An ...
1
vote
0
answers
50
views
Pattern matching and polluting the namespace
I am building a library. Inside of BeginPackage environment there is a function
...
0
votes
0
answers
61
views
Why does my function for LocalSubmit successfully create a TaskObject with no errors but ignores my simple Print task?
tldr: for lazy readers (don't blame you im sometimes that way too) or those who found my details below a little bit vague my question is: why is my Print not ...
3
votes
3
answers
167
views
How to speed up this Manipulate with Show?
I'm doing this manipulation where there is a mapping between functions as follows.
However, why does it not work when I substitute ...
0
votes
0
answers
43
views
Why does local sigma escape its Block in Mathematica 13.0.1?
My objective is to utilize Traditional Form in .NB files. But I've run into a problem where variables I intend to be local to a Block "escape" with an assigned value causing unexpected &...
0
votes
0
answers
39
views
How to implement recursive function in While loop Module, calling on other functions
I am trying to implement a module such that it " takes a distance matrix, Q, and iterates
the corresponding “Bellman operator” module written above until a final cost-to-go
function (i.e. J) is ...
0
votes
0
answers
31
views
Inputting a data file into a module
I'm currently writing a module that converts a data source into a distance matrix. So far, the code I have works:
...
0
votes
0
answers
42
views
How do I use a Do loop on a Manipulate?
How do I use Table or Do Loop iterators inside Manipulate? Is there a way to use Iterators inside a Manipulate function?
...
1
vote
2
answers
122
views
Replacement with equivalent local variable and global variables give apparently different results using the same NDSolve output
I am solving a system ODEs which Mathematica quickly gives me a result varsol and dvarsol. Up to this point, everything is good.
...
5
votes
2
answers
128
views
Local symbols and variables in Module
Is it allowed to define local functions inside the Module in Mathematica? For example, out of the two codes below which one is correct?
First:
...
8
votes
1
answer
420
views
Rule rewriting behavior
This is kind of meta-programming attempt. Below are two snippets:
Snippet 1:
...
3
votes
1
answer
78
views
Let Monitor show the result returned by Module?
I wrote a simple solver to solve an equation with two parameters a and b. At the same time, ...
1
vote
0
answers
78
views
how to check inside a module if a symbol is local to the current module or global?
One way to check if a symbol is global is to do Context[x] and this will give Global if the symbol is in the global context. But ...
0
votes
1
answer
45
views
Program works, but then fails when I write it inside Module [duplicate]
I'm new to Wolfram Language; I'm trying to get used to it by writing a bunch of code and programs, being one of them the algorithm for the bisection method.
When I write the algorithm outside of a ...
3
votes
0
answers
109
views
Why does changing this code in a Compile make it work?
This is a follow up question to this one. In that question I asked why the following code didn't work:
...
0
votes
2
answers
30
views
How to turn FindFit result into function within a Module context
I'm trying to construct a 'normal' function (with formal parameter) from the FindFit result within a Module context.
While this works on a global level:
In :=
...
6
votes
1
answer
81
views
unexpected StringCases behaviour inside function module
First, Let use see
a = {"wx", "wy", "wz"};
AssociationThread[Flatten[StringCases[a, "w" ~~ d_ :> d]] -> a]
gives
<...
3
votes
1
answer
62
views
Condition block scoping
I have a list of functional rules of the type
F[x_]:=y;cond[x] with always the same condition. The list is long and rule 1 of programming says: "Do not copy!&...
1
vote
0
answers
61
views
"With" use in a (implicit) nested loop
Symbolic code:
Do[g[longcat[i,k],longcat[i,k]],{i,1,f[k]},{k,1,m}]
Longcat is loooong and occurs several times in my expression g...
0
votes
1
answer
64
views
Module counting number of $1$'s in a vector
I wrote a module which hopes to count the number of $1$'s in a given vector
mycount[v_]:=Module[{v0=v},s=0;Do[If[v0[[i]]=1,s=s+1],{i,1,Length[v0]}];s]
However, no ...
0
votes
0
answers
53
views
Block Behavior, but for Function Assignment instead of Variable Assignment
We can define a function using a variable b that does not appear as an argument, and then using Block, change the output of the ...
1
vote
1
answer
72
views
Dynamic influenced by a countdown Clock ignores actions in second argument
I stripped a Dynamic down to its bare essentials. Dynamic doesn't execute any of the functions asked for in second argument. ...
0
votes
0
answers
40
views
Using the for loop in the Module function
Good afternoon, I need some help. I have compiled a function that calculates the flight of the ball, step by step I made an analytical solution first, then a numerical one. But in numerical it is not ...
0
votes
1
answer
53
views
Using function definition under Module
In a multistep code, I am trying to use module to define a function such that (working example)
CR[i_] := {{i*5, 3, 5, i, 2 i, 5 i}, {1, 2, 3, 4, 5, 6}}
A[EF_] := Module[{cr = CR[EF]}, c[t_] = ...
0
votes
1
answer
82
views
How to use result of NDSolve within a module?
I want to numerically solve a differential equation and use the resulting interpolating function for further calculations:
...
1
vote
1
answer
123
views
My dynamic button evaluates as soon as it's created. How do I stop this behavior?
I think I finally hit on the holy grail of buttons. With a very small footprint I coded a dynamic AutoSave button whose duration and frequency can be set all the while showing very useful dynamic real ...
4
votes
0
answers
56
views
Preventing $-renaming of pattern names in lexically-scoped constructs
When you use a lexically-scoped construct like With or Function to perform replacements in an expression, Mathematica might ...
3
votes
1
answer
59
views
Please help with dynamic autosave button that only halfway works?
Ive been working on this from morning to midnight (primarily studying the docs that never seem to have useful examples) only to to make very little ground. I am trying to code a dynamic button that ...
1
vote
1
answer
77
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 ...
1
vote
3
answers
102
views
Access to variables in surrounding context from within Manipulate
Suppose you want to access a variable defined in a "surrounding" context (scoped via Begin and End) from within ...
2
votes
1
answer
119
views
Parallelization of two procedures inside Module
I am new in parallel programming and I am trying to speed up an old code using parallelization when possible. So I am (re)writing a function that computes two lists with the same dimensions (called <...
8
votes
1
answer
144
views
OptionValue doesn't work if a local variable has the same name as the function itself
The following innocent-looking code results in error.
...
3
votes
3
answers
225
views
How to solve these algebraic identities?
I need help in finding/solving these types of algebraic equations using Mathematica.
here is the problem/code. I tried using 'With' command but no use. Thanks in advance.
...
2
votes
1
answer
44
views
Why are the inputs not being applied to the function in this MWE?
Consider the following MWE
...
3
votes
0
answers
79
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 ...
2
votes
1
answer
49
views
Manipulate scoping issue?
I'm running Mathematica 12.2 and have encountered the following unexpected behavior with Manipulate:
...
1
vote
1
answer
59
views
Variables I defined in Module are not evaluated as I expected
I want to make and use object like stuff of Object-Oriented Programming language(like JavaScript) in Mathematica and am trying it by referring to the answer of this question using ...
7
votes
1
answer
134
views
How to define a version of Table[] which localizes iteration variable by Module[]?
For reusable code it's good practice to use Module[] to localize iteration variables used by Table[], as explained by this post.
...
0
votes
1
answer
51
views
It would be possible to visualize the value of the integral together with the graph (in the same Manipulate command)
f[x_, y_] := x - 3 y Manipulate[RegionPlot[ 0 <= x <= a && 0 <= y <= b (1 - (x/a)), {x, 0, a + 2}, {y, 0, b + 2}], {a, 1, 20, 1}, {b, 1, 20, 1}]
...
0
votes
1
answer
30
views
Create a table of plots in manipulate with redundant variables
I'm trying to create a table of diagrams of which each shows a different variable on the x axis and fetches the remaining parameters from the enclosing scope, a Manipulate object.
So I have this code ...
1
vote
1
answer
62
views
Using With[] vs. Using a Pure Function for Constants
There are many cases where I am conflicted over using With[] versus using pure functions when dealing with constants, knowing the output is the same either way. For ...
0
votes
0
answers
52
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 ...
2
votes
1
answer
56
views
How to Show Velocity and Time Measurements in Manipulate (Wolfram Demonstrations)
I am working with a Wolfram Demonstration that uses the Manipulate function (which I am not yet familiar with).
The code is
...
0
votes
1
answer
41
views
How can I create a PasteButton that changes its "contents" dynamically?
I'm trying to make a units palette to help with my various engineering courses. I thought about creating a "meta-palette" of sorts, maybe one that changes on the fly, or one where I can ...
0
votes
1
answer
69
views
Passing symbols, variables and defining functions within functions
Moderators state: “You will be brief and straight to the point”, and so do I.
All codes test whether a polynomial is even or odd, using different approaches.
This works:
...
1
vote
1
answer
52
views
Unique symbol for parallel kernels
For each kernel, a unique symbol can be generated with Unique[]. However, since it's basically defined by the growing ...
4
votes
0
answers
44
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 ...
1
vote
0
answers
67
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 ...
4
votes
0
answers
121
views
DynamicModule Error when assigning Nothing to variable
Assigning Nothing to a variable inside Module works as expected:
...