Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
1 vote
2 answers
79 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
143 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
52 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
59 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
52 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,733
1 vote
2 answers
54 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,733
0 votes
0 answers
36 views

Manipulate with With and without - scope of variables

I am struggling with some basic concepts: Why this works: ...
atapaka's user avatar
  • 3,733
4 votes
0 answers
76 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,208
0 votes
0 answers
45 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
  • 695
0 votes
0 answers
79 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
70 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
48 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
55 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
  • 61
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
51 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
96 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
420 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,577
1 vote
1 answer
79 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
109 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
65 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
87 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,177
2 votes
0 answers
143 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
476 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,208
1 vote
1 answer
55 views

Variable replacement not working in ParallelTable

The following code produces different results using Table and ParallelTable. K and ...
xiaohuamao's user avatar
  • 4,698
1 vote
2 answers
98 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
  • 4,642
2 votes
1 answer
97 views

Dealing with locally defined functions in Module while exporting/importing

Consider the following example module: ...
John Taylor's user avatar
  • 4,642
2 votes
2 answers
172 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
  • 137k
2 votes
4 answers
85 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
89 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,397
0 votes
0 answers
48 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
87 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
86 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
52 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
108 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
76 views

Condition-in-scope as a rule for SequenceReplace

Bug persisting through 13.1.0 [CASE:4841653] From ...
rnotlnglgq's user avatar
  • 3,690
2 votes
1 answer
236 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
25 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.4k
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
  • 899
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,208
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,114
18 votes
2 answers
504 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 ...
user293787's user avatar
  • 11.7k
1 vote
4 answers
121 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 ...
Ernesto Espinosa's user avatar
1 vote
1 answer
164 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 ...
Tim's user avatar
  • 21
1 vote
3 answers
124 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 ...
zltn.guba's user avatar
  • 113
1 vote
0 answers
68 views

Pattern matching and polluting the namespace

I am building a library. Inside of BeginPackage environment there is a function ...
fqrt's user avatar
  • 84

1
2 3 4 5
12