Skip to main content

Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
2 votes
2 answers
36 views

How to fix parameter locally for iterating recursion equations

The following is a simplified version of a more detailed problem. I have two coupled recursion equations of two variables, x and y. One equation also depends on a parameter, c: ...
andring's user avatar
  • 163
0 votes
1 answer
46 views

How can I implement dependence between locally defined variables in With? [closed]

I have a procedure that randomly selects a list element and compares the element value to the value of its position. If a condition is met, the element is replaced. The following works ...
andring's user avatar
  • 163
-2 votes
2 answers
94 views

How to Block built-in functions conditionally in a single Block statement? [closed]

I have used the following method and it seems to be working. Just posting here since this way seems hacky. So if anyone can point a better way. ...
user13892's user avatar
  • 9,443
0 votes
1 answer
48 views

Programmatically establishing controllers for Manipulate inside a module

I am trying to create a module that allows users to cycle lists to look for a pattern and need a way to programmatically establish the number of controllers depending on the number of lists being used....
Chris Kennedy's user avatar
0 votes
1 answer
59 views

Why do I get a wrong data structure using Module? [closed]

I am using Module and reading rawlen.txt files from nb100, nb200 , ... folders. I have written a piece of program to calculate strain and I expect the results to have the following structure: {{0.}, {...
Delaram Nematollahi's user avatar
5 votes
1 answer
49 views

How does the scoping in Mathematica work using with blocks? [duplicate]

I'm attempting to understand the following scoping issue: ...
MBEmerson's user avatar
0 votes
0 answers
47 views

Substituting solution into an equation in a module

I have this model which gives me list as an output ...
Anshul Bokade's user avatar
5 votes
3 answers
155 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
  • 189
0 votes
0 answers
78 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
501 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
  • 991
3 votes
0 answers
52 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,921
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
83 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
148 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
58 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
77 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
81 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,962
1 vote
2 answers
65 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,962
0 votes
0 answers
38 views

Manipulate with With and without - scope of variables

I am struggling with some basic concepts: Why this works: ...
atapaka's user avatar
  • 3,962
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,263
0 votes
0 answers
55 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
  • 735
0 votes
0 answers
96 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
83 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
74 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
56 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,747
1 vote
1 answer
85 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
119 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
70 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,245
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
492 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,263
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,728
1 vote
2 answers
106 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
304 views

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

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

Dealing with locally defined functions in Module while exporting/importing

Consider the following example module: ...
John Taylor's user avatar
  • 5,827
2 votes
2 answers
224 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
83 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
  • 145k
2 votes
4 answers
90 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,467
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
89 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
90 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
  • 211
1 vote
2 answers
112 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

1
2 3 4 5
12