Questions tagged [scoping]

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

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

Evaluation control of nested With, If and Integrate

Can you explain the behaviour of the following? The first With outputs unevaluated If. So in the second ...
three777's user avatar
  • 118
0 votes
0 answers
77 views

Red colored font in code

To avoid affecting lamda in other part of my notebook, I have wrapped code with Module. After wrapping, red colored font appeared.Could I know the reason? ...
Soon's user avatar
  • 648
9 votes
2 answers
494 views

What's the difference between With and ReplaceAll?

A wolfram Tutorial mentions that ReplaceAll and With are similar: The way ...
ions me's user avatar
  • 881
3 votes
0 answers
49 views

Writing 'yield' style generator function

Just out of personal curiosity because I can't think of any solution. Probably, it's not possible, but I want a decisive answer. Is there some clever way of implementing the following function? ...
swish's user avatar
  • 7,801
1 vote
1 answer
82 views

Define list of local variables to be parsed to Module

I have a Module which has $n$ local variables, say $a_i$. Now I want to write my Module in such a way that I do not need to change the code for varying $n$. My approach is ...
Confuse-ray30's user avatar
1 vote
2 answers
82 views

SortBy does not work correctly when nest in Evaluate in With in Block

I have got some formula in Mathematica, then I want to pass value to verify them. full code ...
eason's user avatar
  • 197
4 votes
3 answers
147 views

With function replace rule does not work in

If I define an expression , and evaluate it in With function, replace rule does not work. sumT = TA + TE; With[ {TA = 0.1, TE = 0.2}, {TA + TE, sumT} ] output <...
eason's user avatar
  • 197
0 votes
0 answers
55 views

Using a module that turns the output of Findroot into a function of a variable

I am trying to create a module that finds the root b* of some equation with two variables b and ...
Paul Pezanis-Christou's user avatar
0 votes
2 answers
67 views

Using Dynamic string output inside Block

Inside a Block, I have (among other things) a string output that is updated during an internal for loop in the following (schematic) way: ...
michelangelov's user avatar
0 votes
1 answer
69 views

Using Manipulate for initial guess to nonlinear fitt, feed the result back to Manipulate (and repeat) for a general model with indexed parameters

I have been trying to put toghether a visual method to help fit experimental data with peaks. I have stumbled upon this answer which is very similar to where I got. But it is not sufficiently general ...
atapaka's user avatar
  • 3,874
1 vote
2 answers
61 views

How to extract manipulated indexed variables from Manipulate

I am struggling with getting values of variables that are manipulated with Manipulate: The code: ...
atapaka's user avatar
  • 3,874
0 votes
0 answers
37 views

Manipulate with With and without - scope of variables

I am struggling with some basic concepts: Why this works: ...
atapaka's user avatar
  • 3,874
4 votes
0 answers
78 views

Temporary symbol leaks into public context?

This phenomenon has confused me a long time. Consider the following package example, ...
Lacia's user avatar
  • 2,233
0 votes
0 answers
51 views

how to use subscripted variable such as a_i as the control of Manipulate?

I'd like to define a series of variables called $l_1$,$l_2$, etc., in Mathematica, using the "ctrl+_" shortcut. Moreover, I want to use these variables as the controls of a Manipulate[] ...
larry's user avatar
  • 715
0 votes
0 answers
86 views

Interactive control panel using dynamic is surprisingly slow

I am hoping to create a user interface for performing gaussian fits on two dimensional data. I made a basic grid of various controls and labels. The tool works, but it's much slower than I anticipated,...
Tapiocaweasel's user avatar
1 vote
1 answer
79 views

Dynamic text input of equation for graphing

I'd like a simple interface where the user can input (by text) a function $f(x)$, and then the function, its even part, and its odd part are all plotted on the same axes. I'd also like PlotLegend to ...
David G. Stork's user avatar
0 votes
1 answer
51 views

How to make this bisection code work for different equations?

I am very new to Mathematica. I have this code of Bisection Method for finding a root of a polynomial. ...
Learner's user avatar
  • 219
2 votes
0 answers
68 views

Most efficient way to define several local constants in a function -- Block or nested Withs?

Sorry if this is kind of a noob question but I'm wondering what the best-performing approach is to the following: I'd like to define several local constants in the body of a function, each of which ...
max's user avatar
  • 111
2 votes
0 answers
78 views

With[{a=1},{b=a},b] in 13.2 does evaluate to 1 [duplicate]

This isn't documented, and the front end highlights the second comma and third argument in red, but it's used in DownValues[Once][[3]] and is very convenient. Anyone know more?
Edward Huff's user avatar
1 vote
1 answer
52 views

Function definition inside Module with local symbol

Usually if I want to define a function using an expression I do: expr=x^2; f[x_]=expr; which usually works and give f a defination. But now if I want to do the ...
Cheng Tao's user avatar
  • 151
4 votes
1 answer
97 views

Passing control values out of nested Manipulates

I am building a "batch photo cropper" UI. I have a list of images to crop, and I can use Manipulate to make a nice cropping UI for each one: ...
ConvexMartian's user avatar
5 votes
1 answer
423 views

Is this buggy behavoir of Module solved? [closed]

I have just found out about this bug of Module, i.e. the creation of "zombie"-variables in some cases (see also this post). I couldn't find a post saying ...
mattiav27's user avatar
  • 6,637
1 vote
1 answer
82 views

how to achieve “for(int i,i<=3,i++){}” of C++ in Mathematica [closed]

when i use Mathematica to do some small programmings, i always need to pay attention to variables-namings, i just wonder is there a way to achieve “for(int i,i<=3,i++){}” of C++ in Mathematica. ...
Aerterliusi's user avatar
1 vote
1 answer
116 views

How to convert a function with named arguments to a pure function?

A function is defined with named arguments. How can this function be converted to a pure function that uses slots (#). As a simple example, how can the following: <...
feynman's user avatar
  • 229
0 votes
1 answer
68 views

Problem with functions defined in Module in mathematica package

Might be very naive; I am trying for the first time to develop a Mathematica package as shown below. The intended usage is that the user will load the package, specify the input functions 'Model' and '...
madmiKe's user avatar
2 votes
1 answer
88 views

Finding the output of the nested CompoundExpression

I am trying to catch the output expression of a nested CompoundExpression like this ...
Kirill Vasin's user avatar
  • 1,235
2 votes
0 answers
144 views

What is the usage of other "*Block" and "With*"? [closed]

There exist some "kernel functions" that are related to the built-in scoping constructs: Assumptions`AssumptionsBlock, ...
user688486's user avatar
13 votes
3 answers
486 views

Why does Module slow down StringMatchQ by 180 times?

I knew that Module is slower than With or Block, often by several times. But it surprises me ...
Lacia's user avatar
  • 2,233
1 vote
1 answer
59 views

Variable replacement not working in ParallelTable

The following code produces different results using Table and ParallelTable. K and ...
xiaohuamao's user avatar
  • 4,708
1 vote
2 answers
102 views

Module returning Null even with Module as a Return parameter

I'm currently trying to implement Newton's method of approximating roots to approximate the root of 2. I looked into some possible reasons as to why it would return Null, such as the scope of the ...
Jason Ham's user avatar
5 votes
3 answers
303 views

How can I replace the locally defined function inside If with its value?

Consider the following module: ...
John Taylor's user avatar
  • 5,513
2 votes
1 answer
101 views

Dealing with locally defined functions in Module while exporting/importing

Consider the following example module: ...
John Taylor's user avatar
  • 5,513
2 votes
2 answers
204 views

about Return value of Module[]

questions are in the below code. Although I'm pretty sure of the answer, I still feel a little uncomfortable, because I feel that the format of the body of Module[] ...
Aerterliusi's user avatar
1 vote
3 answers
81 views

Local variables in a module don't show their actual values

I need to do the following transformation that pick[a,b] -> ex[c|->{c[a],c[b]}] ex is a function that accepts a function as parameter which returns a list ...
Gqqnbig's user avatar
  • 307
4 votes
1 answer
71 views

problem using package to verify solution of ode with Association, Naming scope/context handling [duplicate]

I learning how to use packages. I found a big problem. Here is description of the problem is, then a MWE is given. This small package takes in an ode and ...
Nasser's user avatar
  • 143k
2 votes
4 answers
88 views

Always the same function in function table

Let's say, we have a list of points such as Points = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}; This is nothing but the corners of a square. List Plot: Now I ...
jabru's user avatar
  • 205
0 votes
0 answers
91 views

Map-ed programmatic Style-s (based on user-input) are not evaluating. Scoping constructs are suspected

The top function manages several sub-functions which set visual properties over a list of Options. Inside stylerules the ...
Jules Manson's user avatar
  • 2,457
0 votes
0 answers
50 views

Efficiency in a use of ParallelMap[]

Suppose M is a large and relatively dense symbolic matrix. I am interested in applying F[] (...
DaveWasHere's user avatar
5 votes
1 answer
88 views

Automatic scoping of input to function

I'd like to write a function whose arguments contains symbols that are automatically localized, just like some built-in functions, like Sum. Even better, I'd like to see the symbols' color change, to ...
Ben's user avatar
  • 53
3 votes
1 answer
88 views

Antiderivative of a piecewise function evaluates wrong inside a subroutine

It's very helpfull to use antiderivative of a given function in the form Derivative[-1][fun][t]! Here my example with a simple piecewise function: ...
Ulrich Neumann's user avatar
0 votes
1 answer
53 views

How to use Assuming and With together

(I am sorry for the ASCII form, I use the free WolframEngine which works in text mode.) I have these assumptions ...
scriptfoo's user avatar
  • 201
1 vote
2 answers
111 views

Symbolic manipulations of predefined variables without numerical substitution

As a novice in Mathematica I would like to expand an expression without substituting its variables so that x = 4; y = 4; Expand[(x^2 + y^2)/x^2] generates the same ...
Joost Keuskamp's user avatar
0 votes
0 answers
44 views

How do I build a module that takes in graph data? [duplicate]

This is a homework problem I'm really struggling with. I'm supposed to program the shortest path problem in Mathematica. There are 100 nodes, each node has corresponding distances to 2-3 more nodes. ...
FlashWayneArrow's user avatar
4 votes
0 answers
77 views

Condition-in-scope as a rule for SequenceReplace

Bug persisting through 13.1.0 [CASE:4841653] From ...
rnotlnglgq's user avatar
  • 3,740
2 votes
1 answer
280 views

How to assign a value to something inside a module globally? [closed]

Let's say I created a function that calculates something inside a module. How do I assign that result to a global value? Let's say I have this: ...
Victor Nielsen's user avatar
6 votes
2 answers
1k views

Unexpected result with sums in set delayed functions

I don't know if I've just found a bug (and if so, whether there's a workaround), or if I'm just losing my mind. Without further ado, here is my code (really, a minimal working example), run on ...
stuart's user avatar
  • 63
1 vote
0 answers
28 views

Sequential vs Nested Module for closing tagged cell

I have the following simple line of code in my notebook Plot[x^2, {x, 0, 2}] I add a CellTag with the string "Plot" to that cell. Sequential Module I ...
Jack LaVigne's user avatar
  • 14.5k
1 vote
2 answers
170 views

Problem regarding "With" function

I am trying to calculate the effective potential using this code in Mathematica: ...
codebpr's user avatar
  • 2,179
1 vote
0 answers
65 views

Is it safe to name the local variables by ending with "$"?

In many cases I need to copy the Global` symbols foo into Module/Block, and to memory the ...
Lacia's user avatar
  • 2,233
0 votes
0 answers
29 views

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

Consider the simple code ...
lineage's user avatar
  • 1,144

1
2 3 4 5
12