Questions tagged [scoping]

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

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

How to force Mathematica throwing an error box intentionally?

When building a Module function, I try to throw an error box when this function facing some cases. But I don't know how to output this error box with ground methods. This picture is the Mma error bar ...
1
vote
3answers
91 views

Access to variables in surrounding context from within Manipulate

Suppose you want to access a variable defined in a "surrounding" context (scoped via Begin and End) from within ...
2
votes
1answer
82 views

Parallelization of two procedures inside Module

I am new in parallel programming and I am trying to speed up an old code using parallelization when possible. So I am (re)writing a function that computes two lists with the same dimensions (called <...
8
votes
1answer
126 views

OptionValue doesn't work if a local variable has the same name as the function itself

The following innocent-looking code results in error. ...
3
votes
3answers
211 views

How to solve these algebraic identities?

I need help in finding/solving these types of algebraic equations using Mathematica. here is the problem/code. I tried using 'With' command but no use. Thanks in advance. ...
2
votes
1answer
42 views

Why are the inputs not being applied to the function in this MWE?

Consider the following MWE ...
3
votes
0answers
75 views

Module inside With, With inside Module, or Just Module?

In general, if one wants to define local variables and local constants, which of the following is the best programming practice in terms of performance and code "safety"? (Assume ...
2
votes
1answer
45 views

Manipulate scoping issue?

I'm running Mathematica 12.2 and have encountered the following unexpected behavior with Manipulate: ...
1
vote
1answer
52 views

Variables I defined in Module are not evaluated as I expected

I want to make and use object like stuff of Object-Oriented Programming language(like JavaScript) in Mathematica and am trying it by referring to the answer of this question using ...
7
votes
1answer
126 views

How to define a version of Table[] which localizes iteration variable by Module[]?

For reusable code it's good practice to use Module[] to localize iteration variables used by Table[], as explained by this post. ...
0
votes
1answer
45 views

It would be possible to visualize the value of the integral together with the graph (in the same Manipulate command)

f[x_, y_] := x - 3 y Manipulate[RegionPlot[ 0 <= x <= a && 0 <= y <= b (1 - (x/a)), {x, 0, a + 2}, {y, 0, b + 2}], {a, 1, 20, 1}, {b, 1, 20, 1}] ...
0
votes
1answer
24 views

Create a table of plots in manipulate with redundant variables

I'm trying to create a table of diagrams of which each shows a different variable on the x axis and fetches the remaining parameters from the enclosing scope, a Manipulate object. So I have this code ...
1
vote
1answer
57 views

Using With[] vs. Using a Pure Function for Constants

There are many cases where I am conflicted over using With[] versus using pure functions when dealing with constants, knowing the output is the same either way. For ...
0
votes
0answers
32 views

Numerical errors in compiled function involving real and complex numbers as output

I am trying to use the Compile command in Mathematica to reduce the computation time because of its fast execution within Mathematica and ability to use the ...
2
votes
1answer
42 views

How to Show Velocity and Time Measurements in Manipulate (Wolfram Demonstrations)

I am working with a Wolfram Demonstration that uses the Manipulate function (which I am not yet familiar with). The code is ...
0
votes
1answer
32 views

How can I create a PasteButton that changes its “contents” dynamically?

I'm trying to make a units palette to help with my various engineering courses. I thought about creating a "meta-palette" of sorts, maybe one that changes on the fly, or one where I can ...
0
votes
0answers
28 views

Display more outputs inside Module and Which

As it can be seen in the following piece of code, i would like to display more outputs which are inside a Module, part of a function definition. In turn this is ...
0
votes
1answer
68 views

Passing symbols, variables and defining functions within functions

Moderators state: “You will be brief and straight to the point”, and so do I. All codes test whether a polynomial is even or odd, using different approaches. This works: ...
1
vote
1answer
46 views

Unique symbol for parallel kernels

For each kernel, a unique symbol can be generated with Unique[]. However, since it's basically defined by the growing ...
4
votes
0answers
42 views

`Function` does something bizarre with formal parameters

I think this may overlap with existing questions, including one I asked years ago, but I'm not sure how, because the behavior I'm seeing is confusing enough I can't even really describe it except to ...
1
vote
0answers
56 views

Autoloading MaTeX` and Rubi` via DeclarePackage[] in the user init.m does not work

Former question: Autoloading and adding code to a package in the user init.m I am using MaTeX, and I found the updateMaTeX[] function] at the GitHub repo. I tried ...
4
votes
0answers
100 views

DynamicModule Error when assigning Nothing to variable

Assigning Nothing to a variable inside Module works as expected: ...
2
votes
0answers
39 views

Strange Replace problem within RuleDelayed within function [duplicate]

I've run into a strange anomaly with Replace when it is performed inside the first argument of RuleDelayed within a function ...
0
votes
1answer
82 views

Combination of Block and With - Unexpected results

I'm trying to use Block to prevent a function from executing. In this example, the function I'm trying to block will simply be ...
6
votes
1answer
83 views

Context Manipulation within Scoping Constructs

In the following the full context form of Test is not required as "Aaa`" is added to ...
1
vote
1answer
35 views

Strange behaviour of SubValues of a definition in a context

First we define a context: Begin["Ctx`"]; ClearAll[f]; f[m_][n_] := n // (Range /* Append[m]); End[]; We can check that ...
6
votes
1answer
74 views

Compile - Optimization of function calls - does it matter?

I was surprised to see that, in the results of CompilePrint for a function made with Compile, calls to ...
2
votes
2answers
186 views

Wolfram modules returning functions

I am trying to get a With / Module pair to return a set of functions. ...
4
votes
1answer
110 views

Scoping problem? [duplicate]

Why (f[1, 0] /. {x : 0 | 1 :> 1 - x}) -> 0 returns (expectedly) f[0, 1] -> 0, while ...
4
votes
2answers
61 views

Functions within modules within Compile

I've noticed that if I try to define a Function within a Module within Compile, I get errors ...
2
votes
1answer
77 views

More control over plotstyle in TwoAxisListPlot

I don't understand how to modify the ColorData in the mapIndexed for the code from another post found here and is copied below for reference. What I want to do is control the color of each trace as ...
1
vote
0answers
34 views

How to Inherit Arrowheds size?

I'm trying to write a Module that produces some arrows with custom Arrowhead shapes. However, I want the code external to the <...
0
votes
0answers
32 views

Replace the output of a module containing local variables by their actual value

I want to construct a function as the output of a Module, but I obtain the local variables as part of the output. This is a problem when I try to construct many of these functions in a parallel table, ...
2
votes
2answers
63 views

Implement Table and Sum replacements scoped with Module

Functions like Table and Sum are scoped with Block, rather than ...
3
votes
3answers
109 views

Memory isolation autochecker

I'm working on a project in which I'm putting everything in modules to make sure I can run multiple instances without them interfering with each other, however I'd like to have it automatically let me ...
1
vote
2answers
70 views

Manipulate not showing the four curves I want to manipulate

I've tried to display four functions in a single plot without success and I don't know how I should change my code in order to see the function. Indeed when I run the command I just see the axises but ...
1
vote
0answers
45 views

Why is my code for counting function calls not working?

I am tryin to calculate how many calls to a function my code is using. ...
-1
votes
1answer
63 views

How can I prevent my special symbols to conflict with other global symbols?

I am building a tool to manipulate Feynman diagrams and depict them using the Graph functionality. A diagram might have the following syntax ...
0
votes
1answer
49 views

Getting error messages form Set while trying to implement the bisection method with a For-loop [duplicate]

My objective is to build a function using modules that runs the bisection method. I have a working bisection method but when I try to implement it as a module I keep getting this error and I can't ...
2
votes
1answer
84 views

Why some variables get substituted and others don't

I was running into some trouble with my math and I whittled it down to this nugget: ...
3
votes
2answers
164 views

How to use a list of indexes as a variables inside a Module?

This is a trivial function, just to show that Module does not recognize the list generated by Table. ...
0
votes
0answers
41 views

Locally remove definition of function

Consider the following code g[x_]:=x^2 f[x_]:=2*g[x] f[x] (* returns 2 x^2*) test[x_]:=Module[{g},f[x]] test[x] (* returns 2 x^2 as well*) What I would like to ...
3
votes
1answer
72 views

Problem reusing Dynamic variables illustrated by synchronized Panes code

Suppose I have several plots that represent different views of a common set of time sequence data but each Plot contains too much data (all of which I want ...
0
votes
1answer
65 views

Getting error when I substitute PDE for ODE

The following code works perfectly: ...
2
votes
1answer
48 views

How to “Hold” variables assigned value inside DSolve?

Consider the following ODE: a = 0.8; b = 8; v = DSolveValue[{ u[x] == a*(b* u'[x] + u''[x]), u[4.925] == 4, u'[4.925] == 0.625 }, u[x], x] which gives ...
1
vote
0answers
51 views

Recursiveness in backtracking algorithm

In order to familiarise with using basic algorithms in Mathematica, I am coding a sudoku solver using a backtracking algorithm. I am treating the sudoku itself as an association (I want to learn to ...
3
votes
1answer
45 views

How to use two separate colours for two lines in a single 'Plot' within the 'With' command?

'''With[{k = {1, 10}}, Plot[k*x^2, {x, 0, 1}]]''' I want to use two separate colours for two k values. How to do that? Thanks in advance.
1
vote
1answer
43 views

Returning (not displaying) graphics from a Module

I want to create a Module which puts several parametric surfaces together as one object. For example, say I want to create a module which returns a cylinder that has a top, bottom, and cylindrical &...
1
vote
1answer
88 views

Is it a good idea to unprotect standard commands?

I often see in code by other people some standard functions like Sum, NonCommutativeMultiply etc get unprotected, supplemented ...
2
votes
3answers
85 views

Strange Behavior of Compile[] inside With[]

Consider, in the examples below, the difference between defining $n$ as a global variable or as an argument to function comp[], which includes a compiled function ...

1
2 3 4 5
10