Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
2 votes
0 answers
63 views

Why do notebooks by default put symbols in Global context where they can collide with all other notebooks?

This default setting can cause subtle and difficult to find problems, such as inconsistent or incorrect answers when symbols, variables or functions in different notebooks have the same name. I speak ...
NemoX's user avatar
  • 31
18 votes
2 answers
562 views

Garbage collection for lexical closures

Mathematica is known not to garbage collect temporary variables in certain cases. I am particularly interested in the very useful programming pattern known as a lexical closure. A somewhat trivial ...
user293787's user avatar
0 votes
1 answer
58 views

Program works, but then fails when I write it inside Module [duplicate]

I'm new to Wolfram Language; I'm trying to get used to it by writing a bunch of code and programs, being one of them the algorithm for the bisection method. When I write the algorithm outside of a ...
Daniel Bonilla Jaramillo Haase's user avatar
0 votes
0 answers
57 views

Block Behavior, but for Function Assignment instead of Variable Assignment

We can define a function using a variable b that does not appear as an argument, and then using Block, change the output of the ...
Just Some Old Man's user avatar
3 votes
0 answers
92 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 ...
Just Some Old Man's user avatar
2 votes
3 answers
93 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 ...
Just Some Old Man's user avatar
1 vote
1 answer
155 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 ...
Carrson Baldwin's user avatar
1 vote
0 answers
67 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.: ...
nate's user avatar
  • 455
3 votes
3 answers
240 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 ...
Tugrul Temel's user avatar
  • 6,409
0 votes
1 answer
131 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: ...
Zen 禅's user avatar
  • 23
1 vote
1 answer
293 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 ...
Michael Curry's user avatar
7 votes
2 answers
178 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. ...
c186282's user avatar
  • 1,412
32 votes
1 answer
964 views

Is the renaming mechanism of `With` flawed?

I'm confused about the renaming mechanism of With: ...
luyuwuli's user avatar
  • 2,814
6 votes
2 answers
1k views

What does empty {} mean in Module? [duplicate]

I have a question. I would like to know what an empty list ({}) means when used in Module[] as the code below shows? Why has a ...
amin bk's user avatar
  • 333
2 votes
1 answer
136 views

Code optimizations with functions inside Module

I wrote a small code by using functions inside Module: ...
Al Guy's user avatar
  • 1,630
5 votes
3 answers
700 views

Turning an expression into a function inside another function

Say I have an expression g=Sin[q]; It is easy to turn it into a function, and to perform manipulations on this function (this and variants of this were discussed ...
yohbs's user avatar
  • 7,075
1 vote
0 answers
191 views

Should I Remove the initial symbols of Module?

Often we use Module to construct complicate function. Consider that Module create new vars every time we run the function, should I remove the initial values at the end of Module-function?Or should I ...
Harry's user avatar
  • 2,745
4 votes
1 answer
528 views

ControlType -> None Vs. Module inside Manipulate (i.e. making everything local)

I'm building a program on another one made available in the Wolfram Demonstration Project. I've noticed that in the original code (working flawlessly) many variables were made local inside the ...
Luca M's user avatar
  • 1,079
6 votes
4 answers
651 views

Expressions containing globally undefined symbols inside a function where they are defined

Consider the following code. f[a_,b_]:=x x=a+b; f[1,2] (* a + b *) From a certain viewpoint, one might expect it to return 3 ...
imakhlin's user avatar
  • 177
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 ...
user5601's user avatar
  • 3,790
0 votes
0 answers
127 views

Best practices for breaking up functional blocks?

Say I have a main function that does some job, and I'm only interested in the final result of ans, i.e., the local functions are not important and produce no side-...
Chen Stats Yu's user avatar
5 votes
3 answers
238 views

Prevent iterator name from being confused with symbol passed into function body

I have a massive amount of code with lots of Table and Sum inside a Module. Each with their ...
QuantumDot's user avatar
  • 19.8k
1 vote
0 answers
98 views

Settings to disable global scope

I am building a fairly large application in Mathematica, and globally scoped variables are causing issues. I would like globally scoped variables to be out-of-scope for my expressions, except by ...
f23rt24tg3qf's user avatar
3 votes
1 answer
430 views

Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
William's user avatar
  • 7,655
8 votes
1 answer
176 views

Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?

Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation: ...
Vladimir Reshetnikov's user avatar
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 ...
Vladimir Reshetnikov's user avatar
3 votes
1 answer
149 views

Odd edge-case behavior of Block

In this example, Block is used to localize the variable cache as used in the function g when called from the function ...
Ymareth's user avatar
  • 4,751
57 votes
2 answers
2k views

How safe is the use of Block and Internal`InheritedBlock

For many questions on this site answers are given which suggest to use Block and, especially for more advanced topics, ...
Albert Retey's user avatar
  • 23.6k
4 votes
3 answers
522 views

Modify this code using Module and While

I have written a recursive function and would like to re-write the code using Module AND While to compare the timings. Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and ...
user71346's user avatar
  • 395
17 votes
1 answer
541 views

Is the sharing of variables in Module/Block within Compile documented behavior?

Today I noticed something, I think for the first time. When used inside Compile variable values within Module (and ...
Mr.Wizard's user avatar
  • 273k
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 ...
Murta's user avatar
  • 26.4k
17 votes
1 answer
564 views

Modules that initialize themselves on first call

I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
George Wolfe's user avatar
  • 5,482
5 votes
1 answer
636 views

How to avoid conflicts between local variable names and symbolic arguments in Block constructs?

I am using Block inside the definition of some recursive functions. If the Block definitions use short variable names, like ...
agravier's user avatar
  • 609
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?