Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
11
votes
1answer
203 views

Proper Syntax highlighting for multi-argument With?

For the case of Block and Module one can choose to define values in the body for the variable names being scoped. So there is no ...
1
vote
0answers
51 views

Using `Block` with Conditions `/;`

I would like Block to run conditionally. I understand how to do one condition, but what about an AND operation? e.g ...
1
vote
0answers
36 views

How to return a variable name and value from within a self-contained function block?

Abstract: I have viewed other solutions on StackExchange but none actually address using a fully self-contained function to return name and value of a variable. The only function that works requires ...
1
vote
0answers
55 views

How to externally access local variables defined inside a With block

I am writing a package in which I have some constants defined inside a With block, which are then used by various functions defined inside that block. Separate to ...
3
votes
1answer
38 views

Using local variables inside WhenEvent

Using WhenEvent inside Module is quite strange. The local variables are not evaluated in ...
0
votes
1answer
46 views

Module[Monitor[ … ]] doesn't work when called by Manipulate?

My example function progressFunction[time_] := Module[{t}, Monitor[For[t = 0, t <= time, t++, Pause[1]], ProgressIndicator[t, {0, time}]]] does ...
28
votes
2answers
440 views

What causes Module variables to leak?

Sometimes, after writing code like f[] := Module[{a}, ...] and running f[] multiple times, there will be many ...
8
votes
1answer
91 views

Local variables in sums and tables - best practices?

Stumbled on Local variables when defining function in Mathematica in math.SE and decided to ask it here. Apologies if it is a duplicate - the only really relevant question with a detailed answer I ...
3
votes
1answer
43 views

New variable in With structure appear as global, and many other strange things I don't understand

I thought I know With and Module until I see this example on Wolfram Documentation. Below is a demo code: ...
3
votes
1answer
93 views

Again about Module inside other Module

Let me start with a short preamble. I have a large function (defined through other functions) which I need to integrate numerically. This works. Trying to speed up the process, I played with ...
0
votes
0answers
30 views

Evaluating global variable within a module

I'm trying to work with something like the following: a = b; f[x_, b_] := Module[{N}, N = Abs[a]; 10 N ] f[1, 1] The output of running this is: ...
1
vote
2answers
72 views

Problem with internal variable of a `Module` which returns the solution of an `NDSolve` command. y$25947 → InterpilatingFunction[…]

I will try to give a minimal example of my problem. Say I define the following function, spitting out an NDSolve result, using ...
1
vote
1answer
66 views

Solve variable inside Module

Suppose I have a Module in which I use Solve[]. If I make the solve variable local, I get this: ...
2
votes
1answer
70 views

Using Solve[] in Module

Clear[x, y]; function2[eqn3_, eqn4_] := Module[{x, y, sol}, sol = Solve[{eqn3, eqn4}, {x, y}]; {x, y} /. sol] function2[y == 3 x, y == x^2 - 4] I want it to ...
2
votes
1answer
63 views

LetL Breaks in ParallelEvaluate

This was a wild head-scratcher for me to hunt down, but I now am sure LetL is the culprit. Here's a small example: ...
1
vote
1answer
55 views

How to set same Variables with different Values [closed]

I have a pipe with different insulation sections and a fluid flowing through. I need to calculate the fluid Temperature at the end of every section, to estimate the temperature for the next section. I ...
0
votes
0answers
47 views

Is there a way to have local option values for Blocked variables?

Like I can set local attributes for a variable, ...
3
votes
1answer
71 views

ReplaceAll Wierdness

I've come across some weirdness in ReplaceAll. Replacement works fine outside of a module, but inside the module some replacements don't occur where one might expect them to. ...
0
votes
1answer
43 views

How to use module to assign functions to a set of imported text files?

I have a lot of text files in my folder (in this example I'm using only 8). I am trying to import all of them and then do calculation on each individual file; I want to use the same notebook without ...
1
vote
0answers
69 views

How to iterate a function

I have written a custom function called f1[r_] and I have tried iterating this function using Table. I work nicely for the first ...
0
votes
0answers
56 views

help with improving my code

I am fairly new to MMA-12 and have written the following code to calculate possible coordinates (for given constraints [see If[]] in the code) and get the ...
1
vote
1answer
55 views

NDSolve with Events that reevaluate with different Discrete Variables

I have the following problem: I have a set of ODEs and some discrete variables which I can solve successfully. Now I want mathematica to check at every $0.1 \,t$ time step, wether it would increase ...
4
votes
3answers
97 views

Is it possible to use a previously defined value in subsequent definitions of a `Block`?

Is it possible to use a previously defined value in subsequent definitions of a Block? According to syntax coloration, this should work as expected, that is, ...
1
vote
2answers
77 views

How to keep symbol bindings local to a Notebook section?

I'm using the Notebook on Wolfram-Cloud as a ... well ... notebook to keep notes about the topic I'm studying and to answer exercises. I usually keep one notebook file per chapter, with course notes ...
2
votes
0answers
42 views

Does passing patterns between packages alter their name unbeknownst to the user? [duplicate]

This question is a partial followup to my previous question. Mainly, after using With to inject the proper argument in the function at the rhs of ...
1
vote
1answer
50 views

How can I make parameters set by an array of buttons known to other quantities in my model?

With help from @m_goldberg, I developed the following model: ...
2
votes
1answer
60 views

Handling a list of variable assignments to be used with `With` [duplicate]

Is there a way to use With as follows? variables := {a = 2, b = 3}; With[variables, a^b] Desired output ...
0
votes
0answers
37 views

Mathematica Function Taxicab Numbers [duplicate]

I have spent days trying to compile this code and I cannot generate a list of taxicab numbers for the Hardy-Ramanujan Theory. Here is the module I wrote earlier: ...
0
votes
0answers
25 views

Referencing a control from within a module

Below is a simple Manipulate that references a module via a button, which resets a slider and value. I would like to reference and reset the slider control for the slider "n" from within the module "...
0
votes
0answers
37 views

Problem in solving a trancedental equation which is embedded in module

I have written a small program to compute the resonance frequency, for that I am solving a transcendental equation, which comes from the determinant of the matrix ...
2
votes
2answers
80 views

Turning an equation into a function in a module

I have defined a function like this: f[q_] := Module[{g}, g[x_] := Evaluate[q]; ... ] I am calling the function like this: ...
0
votes
1answer
43 views

For different value of input module is giving same result

I have a simple module written to extract a certain shape for a certain value of ω, but I am getting the same result for different input values into the module. 1.mode[1] and mode[2] looks exactly ...
10
votes
1answer
166 views

Memory leak with FindMaximum/Minimum inside of Module

This is my first question here. I have to solve coupled first order ODE-s and find the maximum of a function which depends on the solution of the ODE. I have to do this calculation in large quantities,...
0
votes
0answers
53 views

Is it possible to create complicated packages from modules which work fine by copy paste? — A counterexample [duplicate]

Here is a module which constructs essentially several functions in a chain [Phi], sct, ks, WE, G: ...
-1
votes
1answer
73 views

Facing error while solving a determinants with the use of NSolve inside module

I have written a small program to find the natural frequencies of some system using an energy approach. v1 and t1 are the potentials and kinetic energy of the beam. v2 and t2 are the potentials and ...
1
vote
0answers
112 views

Transferring a function between two modules in a package

I have a first module which outputs as forth output a function G I want to transfer to another module. The specific function is ...
1
vote
2answers
95 views

Sum of n sums, Permutations of the indices, how to write them in Mathematica?

I was wondering how to write a function $ F (r, q, n, f) $ in Mathematica, defined in this way: $$F(r,q,n,f):=\sum_{i_0=1}^q f(i_0) \Biggl(\sum_{i_1=i_0+1}^{q+1} f(i_1)\biggl(\sum_{i_2=i_1+1}^{q+2} ...
4
votes
1answer
249 views

Best practice with Return in Module or Block

I am a bit confused on the use of Return in either Block or Module when defining a custom ...
8
votes
2answers
189 views

Using `With[…]` with a list specification as a variable [duplicate]

If you have some defined function, say f[a_, b_ c_, x], one can initialize this by using With[...] as ...
3
votes
2answers
76 views

Scoping construct for dependent constants

I sometimes want to write a function which needs to apply a bunch of transformations that are not functions of a single parameter only: ...
3
votes
1answer
121 views

function evaluation - I don't get it

this function returns Pi/4 for a[1,4] In[...]:= a[r_, n_] := Module[{an = Pi/n, h = r Cos[an], w = 2 r Sin[an]}, an]; a[1, 4] Out[...]= Pi/4 so the kernel ...
5
votes
1answer
118 views

why DSolve in V 12 returned an internal module variables with $ on them in this example?

Fyi, Reported to WRI in case it is a bug. [CASE:4288939] Is this supposed to happen? ...
1
vote
1answer
127 views

How to use Module function?

Here is my issue; I want to iterate a fit to this data for a polynomial of degree 2 up till degree 6. However, at each iteration I drop certain data points that have a poor residual value. So each ...
0
votes
1answer
54 views

Modifying/optimizing a double sum with an If condition

I would like to better understand double summations where one of the sums depends on the upper limit of the previous sum. This appears frequently in representation theory (to the extent of my ...
0
votes
1answer
58 views

Variables referring to other variables within Module

The following doesn't work: Lol[x_, y_] = Module[ {w=x+1, z = w + 1}, z] because z refers to ...
1
vote
1answer
72 views

Using rules inside Module or Block

I have a rather long piece of code inside a Module, and I want to provide different parameters to the different pieces inside the ...
0
votes
1answer
120 views

NonlinearModelFit of an NDSolve result

I have some data : ...
12
votes
4answers
766 views

What is the accessibility of a package's `Private` context variables?

I've been reading up on how Mathematica handles contexts, $Context, $ContextPath, and a few of the tutorials they have on ...
6
votes
1answer
133 views

RegionPlot3D no longer localizes its arguments?

Bug introduced after 9, fixed in 12.0. Just now, I was astonished to find the behavior of RegionPlot3D changes somewhere after v9 . Consider the following sample: ...
3
votes
1answer
170 views

How to trace value assigned to local variable(variables in Module)?

Bug introduced in v10.1 or earlier and persists through v11.3 CASE:4240757 Here is my code: ...

1
2
3 4 5
10