Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
1
vote
1answer
219 views

Why are there local variable conflicts in my recursive function definition?

Sorry to post a long piece of code. There are two places in here where I Print[] 'path2' when the count == 984, because for some reason, that list is getting ...
0
votes
1answer
85 views

Different result in ContourPlot function using different version of Mathematica (?)

I tested "Elasticity with Mathematica" notebook (Mathematica 6) in Mathematica 11.3 and tested C05_stress_kirsch.nb, I added some trigger code to protected function in the tensor2analysis.m file, and ...
2
votes
0answers
32 views

Unexpected symbol::shdw warning during NotebookEvaluate

Consider the following code: ...
1
vote
0answers
46 views

Why doesn't `ReplaceAll` work in this situation? [closed]

I can work around this by using With, but it seems really odd to get the following: ...
0
votes
1answer
120 views

Multiple functions within a Module

I have a function that extracts a numerical value from two vectors, written as Spa[i_,j_]:=Flatten[LaU[i].LaD[j]][[1]]. (Note, I'm sure that this isn't the most ...
1
vote
0answers
75 views

Function returned from module doesn't work [closed]

I have a sequence of steps that work in the main notebook: ...
3
votes
2answers
214 views

Finding the inverse of a function

I am to solve for $r(\rho)$ given the function, ρAsymp[r_, b_, q_] := 1/(1 - q) Gamma[1/(1 - q)]/Gamma[(q - 2)/(q - 1)] r Sqrt[1 - (b/r)^(1 - q)] This can be ...
1
vote
1answer
38 views

Evaluation of Extract and use the result in a Sum

I am experiencing a problem in the evaluation of a sum. I hope that someone can help me, I am not an expert Mathematica programmer. I'm posting just the portion of code that gives problems, I know ...
4
votes
2answers
213 views

Scoping of pattern variables [duplicate]

I have run into a problematic behavior, and then came to find that it is a "possible issue" called out in the documentation, but I guess I am just baffled that this IS an issue, and am wondering why. ...
0
votes
1answer
143 views

Can I bake my own PI using your Wolfram language oven and prebuilt utensils? [closed]

How might I override the value of the Greek letter PI within various builtin language functions in the Wolfram language? Specifically and without having to reinvent the wheel or code equivalent ...
2
votes
1answer
191 views

How to define a function inside a module through Evaluate [duplicate]

I am trying to define a function inside a module as following ...
0
votes
0answers
47 views

Symbol created in Module not seen outside of Module scope [duplicate]

I am using a function defined with a Module[]. In the Module I am creating a new variable name using a ...
1
vote
0answers
61 views

ConditionalExpression still resulting from Assuming a Module's local variables

I have a memoizing, recursive function whose MWE looks like this (the two fake inputs/ICs are from 'K' and 'Bfun2', the latter just a quick way to check the results coming out of the Residue.: ...
1
vote
2answers
107 views

How do I localize changes to a variable inside a Do-loop?

I want to change the increment value inside the do loop . But i dont know why it remains for the first declared value? ...
3
votes
3answers
133 views

MostOccuringWeekdays Wolfram Challenge [closed]

I am working this challenge from Wolfram Challenge. I believe that I have solved the problem with the following code (note it is not very efficient - I am new to Mathematica!) ...
3
votes
1answer
92 views

Module - Symbols out of scope of lexical scoping

The Problem Consider the following code (1): In[1]:= x = 2; Module[{x, y}, y[x_] = x; y[1] ] Out[2]= 2 I expected the answer ...
3
votes
3answers
225 views

What is wrong with Module? or me? [closed]

The following code does not give me an answer. It looks all is working when each command inside Module is individually run. But ...
2
votes
1answer
80 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
152 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
134 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
88 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
64 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
44 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
56 views

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

Spelunking the Export function, I stumbled upon the following definition: ...
2
votes
1answer
80 views
1
vote
1answer
65 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 ...
3
votes
1answer
118 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 ...
8
votes
0answers
153 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
107 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
391 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
211 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
45 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
83 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
206 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
37 views

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

...
0
votes
0answers
106 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
27 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
128 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
314 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
35 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): ...
4
votes
1answer
92 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
38 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
39 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
701 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
83 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
33 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
110 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
45 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
90 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 2
3
4 5
10