Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
2
votes
1answer
50 views

Why does Mathematica stay on an endless evaluating loop with Manipulate?

I have built Manipulate to see the impact of a change in time and volatility to the profit of an option strategy, bull spread. To calculate the price of the option, I built the good old Black-Scholes: ...
5
votes
1answer
144 views

With vs. Block scoping of parameters for Plot

A very basic question.. Why does Block work, but not With, to specify value of a parameter for a function to be plotted? ...
5
votes
2answers
105 views

HoldAll Attributes and Delayed Evaluation: ContourPlot with solutions to NDSolve

This is a general question on evaluating functions that are defined to be evaluated with delay. In particular, I am having difficulties working with the ...
1
vote
3answers
80 views

Prime number counting function

As part of an assignment for a calculus class, I have to write code for the prime number counting function $p(n)$, I have no problem creating the function itself, but for I want it as a function, ...
6
votes
0answers
63 views

Nested rules and automatic renaming of variables [duplicate]

I understand why $ happens here: ClearAll[foo] foo[a_] := (x_ :> a x) foo[1] ...
0
votes
0answers
40 views

K vs k in a module

Module[{K = 1, fn}, fn[p_] := Print[K]; ParallelTable[fn[p0], {p0, 2}]]; Prints K$316 (or whatever the module counter is on)...
1
vote
0answers
53 views

What use is empty binding list in `Block[{},…]`? [duplicate]

Spelunking the Export function, I stumbled upon the following definition: ...
2
votes
1answer
73 views
1
vote
1answer
59 views

Problem passing argument in hash table from global context to a module

I was experimenting with passing arguments via a hash table to a module. Found a very strange result. When I pass a function to be plotted inside the module, it comes out empty. Yet all argument ...
2
votes
1answer
88 views

Module and With do not work in some cases

I apologize if the title is too vague/general but I couldn't find a better one. I am trying to use either Module or With but it does not work if the object containing the local variables is defined ...
7
votes
0answers
131 views

A bug in Derivative?

Update This is a bug in v11.3 or earlier and is fixed in v12. Original Post Check this out: ...
0
votes
1answer
77 views

Want help with debugging a For-loop

I'm trying to run a simulation, which at the end gives a value 'qber'. Now I want to run this simulation multiple times with different values of 'p' each time. And these value of 'p' are actually the ...
2
votes
2answers
184 views

Clear variables inside Module

I have a function that uses Module. The function takes as input a .csv that can have as many columns as it wishes to, and it ...
6
votes
4answers
210 views

Create rule whose LHS is already defined

My question will be clearest if defined using an example. Consider the following code: Func[k_]:=Block[{a}, a[t_]:=t^k; {a[t]->a[t]} ]; However, calling ...
1
vote
0answers
44 views

Dependent variables in With operator [duplicate]

I want write something like this With[{x=1, y=x+1}, y] and get 2 as an answer. What is the correct way to do this?
0
votes
0answers
52 views

How to speed up a module function with a large number of arguments?

I'm setting up a fairly hefty numerical code dealing with matrices of variable dimension, but on the order of hundreds of rows and columns. I'm using a Module format: ...
1
vote
2answers
149 views

function fails to use local variable when it is called inside a Module

I have these two functions fun and microstep.Fun makes use of a Module construct within which I define the ...
0
votes
0answers
34 views

To find the Null Space of a matrix which is inside the module

...
0
votes
0answers
71 views

Iterating a Module

I have a Module that can act on other Modules, which is given as Kk[EE,p,z1,z2,z3,K_func] and I am trying to iterate it, such that ...
0
votes
0answers
25 views

How to define a global function dependent on a local pattern? [duplicate]

When g is in the local scope defined by Block, the following fails: ...
2
votes
2answers
64 views

Table and evaluation order

I thought Table should not evaluate the inner code until it inserts the value. I'm confused by test1[3]="a"; test2[4]="b"; Table[DownValues[i],{i,{test1,test2}}] ...
1
vote
1answer
49 views

Manipulating definitions while reading them with Get

In order to read in a definition with certain parameters replaced by appropriate values I can use for example Block[{a=1.1,b=2.2}, Get["file"]] where ...
2
votes
3answers
271 views

Use of Module With Functions

Really confused by how one should use the Module[] command in two variables. For example my function here should return the ...
0
votes
0answers
32 views

Tracking System function (Get) calls, and Stack[_]

Consider the following attempt and variations thereof to track (nested) calls to Get (reasoning of this approach below): ...
3
votes
1answer
87 views

ThrowAway Environment and Scoping

I want to be able to Get a .wl file. Run some functions (who will do their job working on some separate files that are being written away). And then return to some clean slate so that I can repeat ...
0
votes
1answer
33 views

Issues with Manipulate and dynamic variables in plotting [duplicate]

I'm trying to make a manipulate that shows a parametric plot of a cycloid with manipulators that affect the shape of the curve. My problem seems to be my scoping of the variable ...
0
votes
0answers
38 views

Imitate Integrate's behavior with regard to the integrand parameter (scoping/naming)

Using Integrate, I can do any of the following: ...
0
votes
1answer
448 views

Using Random walk on a region [duplicate]

I want to simulate a random walk on two dimension equation using module. Checked the input so the path works ...
3
votes
1answer
76 views

Variable scoping confusion

Forgive me if this question has been asked prior (I wouldn't even know where to start looking for an answer to this problem to be honest). I know the following code in Mathematica works: ...
0
votes
0answers
28 views

Defining functions while evaluating a module

This might be a very basic question, so apologies in advance, but I couldn't find a direct solution. Suppose I have a module that takes function(s) and expression(s) as its arguments. For the sake ...
3
votes
1answer
89 views

When does Slot evaluate?

I looked up the related answers, it must have to do with the scope. Toy example similar to mine in structure: ...
2
votes
1answer
37 views

Why is the variable x not locally scope in the StringExpression?

Clear[x] StringCases["abcadcacb","a"~~x_~~"c"->x] {b,d} ...
2
votes
1answer
88 views

Converting strings to expressions in a package

I have a function that generates an equation in the form of a String. I want to be able to turn that string into an Expression, and evaluate it in another function. For example: ...
1
vote
0answers
42 views

How to define a symbollic matrix with variables and numbers

Newbie here. I want to define a (user-defined)function to return symmetric matrices, as I need to define quite a few tensors. I am able to do this using strings. But I need the matrix entries to be ...
0
votes
1answer
52 views

Error when calculating more than 1 input

I'm trying to solve a problem where I can't do more than 1 calculation inside a Module here. I have a list of numbers where ...
0
votes
1answer
95 views

Declaring global variable in module/ Looping in mathematica [closed]

I'm new and still learning about mathematica. I'm having some trouble with my code. This is my current code that i'm having trouble with: ...
1
vote
1answer
122 views

A function which performs multiple steps

I'm trying to figure out how I can define a function which takes an input, performs an operation on it, then produces a table from the result. For example, I have a function ...
2
votes
2answers
71 views

How to block list of variable that is of string form?

For example, I have varList={"a1","a2",...} a1, a2,... are all variables, they maybe ...
2
votes
0answers
55 views

Immediate Function Definition inside a Module [duplicate]

In mathematica, it is possible to perform immediate definition of a function where the dependent variable is not explicitly mentioned on the RHS. For example: ...
3
votes
2answers
172 views

Sow in WhenEvent can't extract parameter value of ParametricNDSolve

This is a problem I noticed when trying to answer this question. Just consider the following toy example: ...
4
votes
1answer
108 views

Using Position[] inside a Module

I have a dataset with missing or wrong values. A module should replace those values so that the dataset length is preserved. For identifying the positions to be replaced I use ...
5
votes
1answer
109 views

Import expression for function definition inside a Module of a Package

I've been struggling on this problem for quite a while, but I still don't understand why Mathematica is unable to recognize a function definition where the rhs is an ...
0
votes
0answers
207 views

Parallel computing issues when running a Module

I'm running a heavy simulation and try to use Parallel computing to run a Module case. I have been working on this parallel computation for more than three months, but still haven't come up with a ...
1
vote
0answers
92 views

Why does With return a temporary symbol? [duplicate]

Why does With[{x0 = 1, y0 = 2}, f[x_] = a + b x /. First[ Solve[{y0 == a + b x0, b == 0.1}, {a, b}]]] evaluate to ...
1
vote
1answer
105 views

Iterator order breaks Table

Evaluating the following Table expression ...
24
votes
1answer
454 views

Bug with Block & Lookup: Scope Variable Leak

After debugging a large chunk of code, I could identify this MWE. ...
0
votes
1answer
86 views

passing system of equations as argument

I am trying to solve a 6-equation system with one self-built function. Problem is, 3 equation must be passed as argument while the other 3 are defined inside the function. I am quite new with Wolfram ...
0
votes
0answers
22 views

character string in file name inside module [duplicate]

I have a module in which at the end a table is exported. That works well. Now I want to use that module such that I can transfer a part of a file name as an argument, such as ...
0
votes
0answers
27 views

Patterns in module [duplicate]

I have this weird behavior of patterns inside the Module construct. Note that we have In: Module[{}, a_->x] Out: a_ -> x ...
3
votes
1answer
67 views

Algebra, Local variables and the Module function

Algebra is so much fun but I'm struggling to understand how MMA executes local variables within Module. Reading the Module ...