Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
3
votes
3answers
204 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. ...
0
votes
0answers
41 views

Return is not returning a value in a recursive module [closed]

I have a recursive function executing a search in a family tree to find lines of descent. I have a line If[curnode == pers2, Return[curpath]]; This functions as expected in cases 1,2,or 3 ancestors ...
0
votes
0answers
29 views

How to use NMinimize with two different restrictions?

I have an energy function which depends on x1,x2,x3,x4,...,xne in which ne is the number of electrons. x1, x2,...,xne are the angle of the spin of each electron (x (theta) which can change between ...
2
votes
1answer
41 views
3
votes
0answers
73 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
44 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
125 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
23 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
54 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
31 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
67 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: ...
2
votes
1answer
40 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
31 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: ...
3
votes
2answers
1k views

Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the ...
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 ...
1
vote
0answers
55 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
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 ...
4
votes
0answers
95 views

DynamicModule Error when assigning Nothing to variable

Assigning Nothing to a variable inside Module works as expected: ...
2
votes
0answers
37 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 ...
26
votes
4answers
921 views

SetOptions locally?

Is there a way to SetOptions locally? For example, I have a Module and inside it I am doing a lot of ...
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
73 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
184 views

Wolfram modules returning functions

I am trying to get a With / Module pair to return a set of functions. ...
4
votes
1answer
107 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
76 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
30 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
69 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
48 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 ...
3
votes
2answers
157 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. ...
5
votes
2answers
497 views

Using block or module to free cache

I'm writing some code at the moment that schematically looks like: (set initial conditions for some differential equation for given parameters (M,R,...)) ...
0
votes
0answers
40 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
68 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 ...
2
votes
1answer
45 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
48 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
42 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
87 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 ...

1
2 3 4 5
10