Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
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
vote
0answers
45 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
55 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
101 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
317 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
75 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
67 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
215 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
116 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
162 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
315 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
96 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
159 views

Iterator order breaks Table

Evaluating the following Table expression ...
26
votes
1answer
493 views

Bug with Block & Lookup: Scope Variable Leak

After debugging a large chunk of code, I could identify this MWE. ...
0
votes
1answer
91 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
26 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
30 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
75 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 ...
5
votes
3answers
885 views

Returning a function built with local (block) variables

I would like to build a block that outputs a pure function. Something like: ...
0
votes
1answer
183 views

Extract data from plots created inside the module environment

I need to extract the data from plots created inside the module environment to re draw my plots in a better plotter that Mathematica. I have a code that includes several matrix multiplications, and ...
0
votes
0answers
69 views

function from .wl produces no output [duplicate]

I'm trying to learn to reuse functions. I have created a .wl file with the following content. As I understand the last line without semicolon is the function output. Should each function have it's ...
0
votes
2answers
35 views

Dropping \$NUMBER from local var$number when passing results from a Module [duplicate]

I'm sure this has been asked, but I can't seem to find the correct terms to find it. I have a function that looks something like this ...
6
votes
2answers
139 views

How to localize symbols in 'Manipulate'

I'm confused about the best approach to localizing symbols used in a Manipulate. I understand that Manipulate does a good job by ...
3
votes
2answers
502 views

Module inside Module. Conflict of names in arguments

I have a module which calls number of other smaller helper modules. But those other helper modules are not used by another module, so there is no needed to have them in global context. So I wanted to ...
2
votes
1answer
104 views

Variable range maximum for a Manipulate expression inside a module

Let's say I have a function defined as a module where at the end I call up "Manipulate" as below: a[x_]:= Module[{k},k = 2 x; Manipulate[Something[y], {y, 0, k}] ...
15
votes
2answers
488 views

Why can't I inject expressions in Compile using (only) With

Generally speaking, With can be used to inject into arbitrarily held expressions, for example: ...
0
votes
0answers
52 views

Error in storing the output of a module (subroutine) into an array

For a physics application, I have to compute components of a rank-4 tensor, the Riemann tensor. I do this using a module (subroutine) which looks like this ...
4
votes
3answers
276 views

Understanding Block and Definition

The code Block[{f}, f := test; Definition[f]] Outputs Null. Why? I would have expected it to output the definition of ...
1
vote
2answers
86 views

Refering to local module variables

How can I do the following? I have code inside a Module such as Module[{a1,a2,a3,a4}, code...]. In this code I want to refer to these constants efficiently (for a ...
1
vote
2answers
454 views

Replacement rules are not effective in a module

I am trying to bracket the roots of a function with the variable p. The assumptions are: the root exist between 0 and 1, the value of the function is positive at <...
2
votes
3answers
241 views

Pure function within another function

I am writing a function along the lines of the following: func[basis_,coordinates_,vars_]:=Table[Function[vars,monomial]@@@coordinates,{monomial,basis}] The idea ...
1
vote
1answer
67 views

Weird behaviour of If inside of Block definitions?

I am on Mathematica 9 and I have discovered, that, when I use If inside of the definitions of the Block variables, then other, ...
2
votes
1answer
142 views

How to define an auxiliary function within a function [duplicate]

I would like to define a function within a function to serve as an auxiliary function. In essence, something like this: f[x_,y_]:=Module[{g[z_]:=2z},x+g[y]] ...
3
votes
1answer
47 views

Dynamic divide as number rather than fraction

I'm practising making Dynamics so I made a simple solution measurement converter but I'm having trouble getting a numerical output at ...
1
vote
1answer
198 views

call function to trigger update to dynamic — what's the right way

I want to create a plot using Dynamic. Ideally, I'd have some persistent, locally scoped state variables living in a Module; as ...
2
votes
1answer
196 views

Passing a function as a parameter to a module [closed]

I'm currently trying to introduce myself to some programming with Mathematica. I'm not the most familiar with programming in general, but I work best by just trying to make stuff. In this code, I'm ...
3
votes
2answers
749 views

How to set all variables in function as local

Here is an example code: a = 1; b = 1; c = a + b; f[x_, y_] := Module[{a = 3, b = 2, c}, c = (x + y)*(a + b)]; a,b,c are ...
3
votes
2answers
217 views

Plot and an error

I have set of matrices(multiplied to give U123[q, p, r]) and would like to diagonalize U123[q, p, r] and plot the eigenvalues. ...
5
votes
2answers
175 views

Defining pure function using module

I am creating a function fun which takes a parameter p and returns a pure function: ...
5
votes
1answer
130 views

Unique[] and special handling of symbols ending with a single dollar sign

Example Unique[x] generates a new symbol, with a name of the form x$nnn. If so then this is not correct: ...
4
votes
0answers
173 views

What is the best way to localize variables in a custom control

I am trying to build a custom control that is just a combination of a group of smaller controls that combine to give the final value, but with the final value only updating when the controls stop ...
6
votes
1answer
668 views

Begin[“`Private`”] vs Module

So, if you have a package formatted like so: ...
1
vote
0answers
80 views

Odd problem with `Module` and `Block` [duplicate]

I found some bizarre phenomena in my code. While I'm using Module in my function, the result is incompatible with which I did outside ...
7
votes
2answers
161 views

How to create Functions that have Compiled functions effectively

I'm about to build a package that will provide lots of simple functions. I want these functions to run as fast as possible when evaluating numerically but still provide symbolic results when needed. ...
0
votes
1answer
266 views

Problem with Module, Function, Association and Dataset

I have struck a combination of the above-mentioned functions that causes evaluation to fail. I wonder whether someone can help me create a more minimal example in order to understand the issue better. ...
6
votes
1answer
213 views

Explaining error when using Part inside Module in Dataset

I have a vague recollection of seen an explanation for this, but I can not find it, so it may be a false memory. Will delete if duplicated. Let ds be a simple <...
3
votes
2answers
284 views

How to access externally defined symbols without polluting the current namespace?

NB: In this post, by fully qualified names I mean names like Global`x or SomePackage`y, which include their complete context ...
0
votes
1answer
158 views

How to avoid namespace leaks?

Below is a toy package that illustrates what I mean by "namespace leaks". ...
1
vote
1answer
203 views

Defining local variables in a dynamic way in Module

I have a function whose variable are specified by a input parameters of function: ...
0
votes
0answers
428 views

Module: local variable specification error [duplicate]

I am developing a module which does some vector algebra, and I have added an image of the code in the link below. (I did not find a good way to add formatted code in this pane). If the image ...

1 2 3
4
5
10