All Questions
589 questions
13
votes
1
answer
164
views
Unexpected optimization with With
I do not understand why the function With[{y = #}, {y, y, y}] & maps faster than the function {#, #, #} &. The ...
13
votes
1
answer
407
views
Is garbage collection broken in version 10.0.0?
Bug introduced in 10.0.0 and fixed in 10.0.1
In version 10.0.0 under Windows I am observing the following behavior:
...
12
votes
3
answers
381
views
What is the equivalent of $ModuleNumber for DynamicModule
Mathematica provides access to $ModuleNumber which can be enormously useful.
Unfortunately I can't find the equivalent for ...
12
votes
1
answer
575
views
A smarter nested With?
I often find myself writing code that looks a bit like this:
...
12
votes
2
answers
183
views
Bug: With[] pattern-variable renaming misses symbols within `Except`
Bug introduced in 10.1 and fixed in 11.1
Thank you for taking the time to send in this report. It does appear that pattern-variable renaming misses symbols within Except when using With. I will ...
12
votes
1
answer
553
views
Temporary variable not removed in Mathematica 10
Bug introduced in 10.0.1 and fixed in 10.0.2
In Mathematica 10, the documentation still states that a symbol with attribute Temporary will automatically be removed ...
11
votes
2
answers
650
views
Strange results while nesting scope constructs?
I did a handful of tests with Block, With, and Module and had a couple of new surprises. ...
11
votes
2
answers
517
views
What's the difference between With and ReplaceAll?
A wolfram Tutorial mentions that ReplaceAll and With are similar:
The way ...
11
votes
2
answers
527
views
Localizing variables within a Manipulate
I have a notebook with several examples of very similar computations, each involving the same variable/parameter names of the ingredients used in a final Manipulate....
11
votes
4
answers
724
views
Using predefined expressions as free variables in a pure function [duplicate]
I want to have a function value of an expression where some variables are solutions to some set of equations, with some values of parameters. I had an idea to use pure functions for that.
However, ...
11
votes
3
answers
731
views
Find variable name from DumpSave on a scoped variable
I saved an InterpolationFunction in a ".mx" files using DumpSave on a variable that was scoped by a ...
11
votes
2
answers
478
views
Unexpected variable renaming depending on form of a pure function
I encountered unexpected difference in variable renaming behavior depending on whether a pure function with a named parameter, or a pure function with a slot is used:
...
11
votes
1
answer
488
views
Variable scoping in Module
I don't understand why $a$ and $b$ are not local in the following code:
...
11
votes
2
answers
264
views
How to correctly implement in a new function the scoping behavior of Table, Sum and other commands that use Block to localize iterators?
It is documented that "Block is automatically used to localize values of iterators in iteration constructs such as Do, Sum, and Table." Therefore the dummy index (iterator) in a Sum is shielded ...
11
votes
1
answer
1k
views
Treat all variables as local in a module [duplicate]
In most languages, any variable defined inside a function is considered local.
...
11
votes
0
answers
946
views
Compile recursive function modifying global variables
How to compile recursive formula when it relies on more than a few global variables (global to the topmost compiled function)? It is unreasonable to pass on all such variables to each recursive ...
11
votes
1
answer
242
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 ...
10
votes
4
answers
1k
views
How to set Block local variables by code?
I need to create a user-defined Block function where the Block variables' values are defined by code. For example, imagine I ...
10
votes
3
answers
240
views
With/Function(/etc) replacement behaviour
Can anyone explain to me this behaviour? I've been having more than a couple of similar doubts these last weeks.
For example
f[_?NumericQ] := 8;
Now, if I do
<...
10
votes
2
answers
556
views
Avoiding MainEvaluate in a CompiledFunction to fetch global variables
I'm trying to write a simulation using Mathematica 8.0. Since I will most likely be doing the same operation over and over again, I'm trying to Compile whatever I ...
10
votes
4
answers
531
views
Working with DynamicModule: ScheduledTasks with local variables
I have a question - is it possible to run ScheduleTask[] inside DynamicModule[]?
What I have tried so far:
...
10
votes
1
answer
204
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,...
10
votes
1
answer
688
views
Package functions and symbolic calculations
I'm writing my package (.m) in Mathematica and when I call the function it returns 0 instead of a right output.
Here's my code:
Package.m
...
10
votes
1
answer
187
views
What are the scoping rules for function parameters shadowing System` symbols?
Here are some very contrived code snippets, highly unlikely to appear in real code, but still I am curious why they behave like this:
...
10
votes
1
answer
177
views
Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?
Consider the following expression:
Block[{Print = CopyToClipboard}, doSomething[]]
Now all calls to Print from ...
10
votes
1
answer
285
views
Difficulty catching thrown errors in asynchronous tasks
In this Question, Leonid explained that module variables can get garbage collected from a chain of asynchronous tasks unless their Temporary attribute is cleared ...
9
votes
6
answers
956
views
How can you give a Module a context and have its local variables and Modules belong to that context?
What would be the best way to give a module its own context and have its local variables and Modules belong to that context?
9
votes
1
answer
379
views
Why does the global variable exist here, where it was declared only in Module?
Why does Mathematica choose the mechanism that makes the global variable a here while parsing?
In
...
9
votes
3
answers
2k
views
How to use local variables with indices in a Mathematica Block environment?
I have the following code:
Subscript[f, i_][x_] := Block[{a},
Subscript[a, i] = 3 x;
Subscript[a, 1]
]
If I evaluate for example ...
9
votes
2
answers
258
views
How to define UpValues in a different context from within a package
I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but ...
9
votes
3
answers
888
views
How do I avoid scoping collisions of iterator variables?
Yesterday I had an issue that kept for quite a while. My code wasn't producing the right results. Now I know why and I am wondering how to avoid the issue in future. Consider the following code.
<...
9
votes
1
answer
515
views
Where does a package have to be loaded?
I'm trying to solve a very tricky issue in my function. The function needs the NDSolve`FEM` framework.
Here is a minimal example:
...
9
votes
1
answer
2k
views
Import another Notebook as if it were a Package
Is it possible to import the contents of another (existing) notebook into the current namespace in the same way that I can with a package?
I know that it would be best to use a ...
9
votes
1
answer
156
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.
...
9
votes
1
answer
394
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 ...
9
votes
1
answer
216
views
Strange behaviour of Internal`InheritedBlock
I was going to post an answer for fast way to replace all zeros in the matrix. I was even quite happy because timings were the same order of magnitude as others.
The idea was to overwrite ...
9
votes
2
answers
425
views
How to assemble and disassemble a function into steps
I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
9
votes
0
answers
191
views
A bug in Derivative?
Update
This is a bug in v11.3 or earlier and is fixed in v12.
Original Post
Check this out:
...
8
votes
2
answers
271
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
...
8
votes
1
answer
458
views
Rule rewriting behavior
This is kind of meta-programming attempt. Below are two snippets:
Snippet 1:
...
8
votes
1
answer
199
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.
...
8
votes
2
answers
183
views
Question about scoping data in a multi-level Manipulate construction
Background: In a module containing an outer / inner manipulate I select the key of data with a dropdownlist in the outer manipulate. Data is read and displayed in the inner manipulate for edit ...
8
votes
2
answers
154
views
Cannot Block Plus
It is customary to use the following idiom to partially evaluate something inside a held expression:
...
8
votes
1
answer
286
views
Dynamics and Block interfere
As mentioned by Albert Retey in a comment, we can unintentionally interfere with our dynamics as follows
...
8
votes
1
answer
357
views
Module variable scoping in Scheduled Tasks?
I ran across a scoping puzzle while experimenting with ScheduledTasks, and I'd be grateful for an explanation from the sages here. I found a workaround by intuitive horse sense, but was unable to ...
8
votes
2
answers
209
views
Possible to scope an anonymous function within Module?
This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial.
I'm working on a problem that requires a function to be redefined ...
8
votes
1
answer
120
views
Does the syntax coloring I see indicate a real problem?
I need a behaviour provided by:
x = 2;
Block[{x = HoldForm[x]}, x]
x
FrontEnd marks ...
8
votes
1
answer
228
views
How to reset the $ModuleNumber?
According to the documentation, we can reset $ModuleNumber to a positive machine integer. There is even an example in the doc:
...
8
votes
1
answer
380
views
Creating a Block from a list of rules
How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments?
The function needs to be ...
8
votes
1
answer
347
views
FullDefinition and Module local variable
I have a question about FullDefinition and how it interacts with the local variables of Module.
I've created a few functions (...