Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
17 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
26 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 ...
0
votes
1answer
26 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
28 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
111 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
52 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
67 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
97 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
76 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} ...
0
votes
0answers
30 views

Can I still use global variables when I choose the default context to be the cell group?

To avoid accidentally reuse the same variables in different sections of my notebook, I go to Evaluation menu and choose Notebook's default context > Unique to each cell group. But sometimes I do want ...
4
votes
1answer
113 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 ...
5
votes
2answers
140 views

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

If you have some defined function, say f[a_, b_ c_, x], one can initialize this by using With[...] as ...
3
votes
2answers
73 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
116 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
107 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? ...
0
votes
0answers
26 views

Why are unrelated symbols localized in Module? [duplicate]

As I understand, an expression like Module[{var},expr[var]] localizes var and evaluates ...
1
vote
1answer
94 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
41 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 ...
1
vote
1answer
52 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
58 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
57 views

NonlinearModelFit of an NDSolve result

I have some data : ...
0
votes
0answers
24 views

Gradient method in vectorfield on surface table does not work, module is slow

For teaching purposes I want to generate a vectorfield on a surface an visualize the path of the gradient method. The surface and the gradient are generated by ...
10
votes
4answers
546 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
119 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
75 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: ...
0
votes
0answers
52 views

Assigning values to variables inside Module using user defined function

Say I'm defining a function with some local variables, for which I'm using Module. Now, I want to assign some value (or perhaps a list of values or a list of list, ...
1
vote
1answer
113 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
74 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
25 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
83 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
52 views

Function returned from module doesn't work [closed]

I have a sequence of steps that work in the main notebook: ...
0
votes
1answer
110 views

Finding the inverse of a function

I am to solve for $r(\rho)$ given the function, \[Rho]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
33 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
138 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
131 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
64 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
53 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
84 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
103 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
62 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
220 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
47 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
140 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
93 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
73 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
51 views

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

Spelunking the Export function, I stumbled upon the following definition: ...