Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
30 views

Dataset column names not showing and entire dataset renders in closed form [closed]

My row names show just fine hut Unsure why column names no6 displaying and dataset renders in a closed-looking elided form. I want the entire dataset rendering open. If you do not have the time or the ...
user avatar
  • 1,837
0 votes
0 answers
29 views

Set keeps RHS unevaluated when RHS is a Module variable [duplicate]

Consider the simple code ...
user avatar
  • 902
14 votes
2 answers
346 views

Garbage collection for lexical closures

Mathematica is known not to garbage collect temporary variables in certain cases. I am particularly interested in the very useful programming pattern known as a lexical closure. A somewhat trivial ...
user avatar
1 vote
4 answers
111 views

Trouble understanding the local variables of the Block command

Example one: This is said in the documentation Block localizes values only; it does not create new symbols I don't understand the previous sentence ...
user avatar
1 vote
1 answer
77 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 ...
user avatar
  • 21
1 vote
3 answers
70 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 ...
user avatar
  • 113
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 ...
user avatar
  • 74
3 votes
3 answers
170 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 ...
user avatar
  • 1,958
0 votes
0 answers
48 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 &...
user avatar
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 ...
user avatar
  • 65
0 votes
0 answers
33 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: ...
user avatar
0 votes
0 answers
45 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? ...
user avatar
1 vote
2 answers
125 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. ...
user avatar
5 votes
2 answers
133 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: ...
user avatar
  • 51
8 votes
1 answer
422 views

Rule rewriting behavior

This is kind of meta-programming attempt. Below are two snippets: Snippet 1: ...
user avatar
3 votes
1 answer
79 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, ...
user avatar
  • 1,197
1 vote
0 answers
80 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 ...
user avatar
  • 109k
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 ...
user avatar
3 votes
0 answers
110 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: ...
user avatar
  • 6,134
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 := ...
user avatar
6 votes
1 answer
82 views

unexpected StringCases behaviour inside function module

First, Let use see a = {"wx", "wy", "wz"}; AssociationThread[Flatten[StringCases[a, "w" ~~ d_ :> d]] -> a] gives <...
user avatar
  • 15.7k
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!&...
user avatar
1 vote
0 answers
64 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...
user avatar
0 votes
1 answer
65 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 ...
user avatar
  • 199
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 ...
user avatar
1 vote
1 answer
74 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. ...
user avatar
  • 1,837
0 votes
0 answers
44 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 ...
user avatar
  • 1
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_] = ...
user avatar
  • 973
0 votes
1 answer
88 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: ...
user avatar
  • 197
1 vote
1 answer
124 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 ...
user avatar
  • 1,837
4 votes
0 answers
57 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 ...
user avatar
  • 8,208
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 ...
user avatar
  • 1,837
1 vote
1 answer
78 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 ...
user avatar
  • 81
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 ...
user avatar
2 votes
1 answer
122 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 <...
user avatar
  • 163
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. ...
user avatar
  • 489
3 votes
3 answers
227 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. ...
user avatar
2 votes
1 answer
44 views

Why are the inputs not being applied to the function in this MWE?

Consider the following MWE ...
user avatar
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 ...
user avatar
2 votes
1 answer
49 views

Manipulate scoping issue?

I'm running Mathematica 12.2 and have encountered the following unexpected behavior with Manipulate: ...
user avatar
  • 349
1 vote
1 answer
62 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 ...
user avatar
  • 113
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. ...
user avatar
  • 489
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}] ...
user avatar
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 ...
user avatar
  • 115
1 vote
1 answer
63 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 ...
user avatar
0 votes
0 answers
55 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 ...
user avatar
  • 13
2 votes
1 answer
57 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 ...
user avatar
  • 109
0 votes
1 answer
43 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 ...
user avatar
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: ...
user avatar
1 vote
1 answer
53 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 ...
user avatar
  • 988

1
2 3 4 5
11