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

learn more… | top users | synonyms (1)

2
votes
1answer
52 views

How to protect function variables [duplicate]

I am sure this has been asked before, but I can't seem to find the right solution. I want a function to be the solution to a differential equation ...
4
votes
2answers
156 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
1
vote
1answer
42 views

Global variales, Paralell computation and Limiting notebook's contex

If I limit the context of each notebook: and use parallel computation: ParallelTable[ToExpression["b" <> ToString[j]], {j, 1, 10}] then after ...
3
votes
0answers
62 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 ...
3
votes
1answer
54 views

Should “counters” used in Do, Table, … within Module be declared as local?

I have little to no understanding of how Mathematica handles scoping, so the question may seem a little bit funny. What is the preferred way: ...
0
votes
0answers
35 views

Where to put manipulate brackets for slider effect to refresh page

In order to move the plane of a best fit around to alter the sum of the residuals, I have decided to pick on one point and attach a slider to it, this adds a little variance and moves the table ...
0
votes
1answer
33 views

Fit inside a module [closed]

I have here a MMA NB which works fine outside the MODULE. BUt in the module routine there comes up an warning that the solution is not real. Can anyone here help please? ...
1
vote
1answer
55 views

Strange interaction between Module and ListPlot

Why is the variable a ending up assigned to {1,2,3} after executing Module[{a}, a=Range[3]; ListPlot[a] ]
4
votes
2answers
268 views

Comparing LetL and Module efficiency

I was recently introduced to the LetL macro thanks to Leonid's answer to one of my prior questions. I was, needless to say, impressed by the simplicity of its ...
21
votes
2answers
910 views

What are some advanced uses for Block?

I read the answers to this question (What are the use cases for different scoping constructs?) and this one (Condition, Block, Module - which way is the most memory and computationally efficient?). ...
1
vote
1answer
61 views

NDSolve inside Module with local variables

I want to use NDSolve within a Module and output the solution at some times. (Mathematica 10) All works fine with global variables (symbols) ...
3
votes
1answer
55 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...
5
votes
1answer
218 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
3
votes
1answer
63 views

Hiding body of a module or function in mathematica

Is it possible to hide the body of a module or function in MMA? For example I would like to turn this: myModule[]:=[{}, Body of module ] to: ...
5
votes
1answer
85 views

Temporary variable not removed in Mathematica 10

In Mathematica 10, the documentation still states that a symbol with attribute Temporary will automatically be removed when it is no longer needed. Now consider ...
0
votes
2answers
39 views

How do I localize an assignment to expression (instead of symbol) within scoping construct?

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct? ...
4
votes
3answers
127 views

Altering values in Manipulate with dynamically generated controls

I have a Manipulate whose controls are generated based on a parameter in a containing Manipulate: ...
1
vote
2answers
57 views

Assigning to the “local” version of a symbol

Compare Manipulate[c[1] = 1; c[1], {c[1], 0, 1}] with Manipulate[Do[c[i] = 1, {i, 1}]; c[1], {c[1], 0, 1}] The first case ...
13
votes
1answer
296 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: ...
7
votes
4answers
438 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 ...
4
votes
0answers
135 views

Exhaustive examples of variable replacement due to nested scoping constructs

Consider the following examples, where I have replaced dollar signs in the output by a D. ...
0
votes
2answers
65 views

Use conditional expression inside With

I want to assign a number to an internal variable, depending if a specific option has been given, but it seems to fail. Something like: ...
2
votes
1answer
104 views

Odd problem with Module

I've been having a strange problem with Module. Oddly it seems to be linked to my use of Import and then applying a function such as Flatten to my Import. I pasted images of my code and output ...
0
votes
1answer
85 views

Return a local variable from a Module containing a Manipulate

I would like to create a set of custom controls that call Manipulate[] and return the value(s) set during the Manipulate session. I can easily make a version that uses something like: ...
1
vote
2answers
63 views

Is there a way to logically test an optional color input inside a module? [duplicate]

Suppose I create a Module that is expected to return a graphic that I will later use in a plotting procedure (e.g., with ...
0
votes
1answer
67 views

Substitution of a function in a differential equation

Suppose we have a differential equation as follows: g[t_] := t/2 m[t_] := t/3 A[t_] := {{g[t], 1}, {1, m[t]}} p'[t] == A[t]. p[t], p[0] == {1, 0} ...
3
votes
1answer
66 views

Evaluating arguments of module (inside compile)

Note: this is a clarification of 2 earlier unanswered questions, Compile issues, scoping and order of evaluation and Optimising a simple computation with vectors . The problem I have a list of ...
2
votes
1answer
92 views

Manipulating parameters in a complex algorithm

I have a complex algorithm that depends on: A wavelet basis A number of sampling points out of a distribution A thresholding value I want to be able to play with different scenarios using ...
1
vote
0answers
57 views
2
votes
0answers
83 views

Compile issues, scoping and order of evaluation

I have a few questions about compiling functions, which I think are all related to scoping and order of evaluation. I will illustrate them by a minimal example of the problem I have. I'm sorry for ...
0
votes
1answer
48 views

Output a symbol from Module or Block

I have troubles to output an entire symbol, a function in this case, from a module or a block. For instance myf = Block[{f}, f[3] = 33; f[4] = 4; f] Then I want ...
1
vote
0answers
65 views

Building Reusable Custom Controls?

I would like to build an application using Mathematica 10. I would very much like to structure the application for maximum maintainability. To this end I would like to segregate controls and user ...
0
votes
2answers
108 views

Are there ways to create local variables analogous to a local variable in C++ and other languages? [closed]

There has been a great deal of discussion on local variables in Modules and Manipulate. If I am reading this correctly, local variables in a language such as C++ or C# are not exactly analogous to ...
2
votes
1answer
90 views

Implementing local variables of a recursive function with either Module or Block

First, some quick background: I have been eagerly learning Wolfram and Mathematica for two months now, and along the way have written about 250 pages of teaching material that has ballooned into a ...
2
votes
3answers
91 views

Dependency injection - use a specific implementation of one function within another

Consider... f[x_]:=Body containing h[some args]... g[x_]:=Body containing h[some args]... However I want to define something like... ...
5
votes
3answers
145 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 ...
0
votes
0answers
17 views

Trouble picking up package definitions [duplicate]

I open a new notebook and type ClearAll[test]; test[x_] := x + 42; I save it as Temp.nb and also as ...
1
vote
2answers
67 views

How can my Manipulate be aware of a local variable inside a nested Module

I have 3 sets of 5 x-y points: RandomInteger[10, {3, 5, 2}] I want to display only one set of points at a time, and use a slider to move on to show the next ...
1
vote
2answers
247 views

Error with “Newton's method” in Wellin's Mathematica book

In Wellin's Programming with Mathematica book, here's one of his implementations of the Newton method, where the iteration runs until the error tolerance is reached. ...
6
votes
4answers
523 views

Why do I see the error “only assignments to symbols are allowed” when using a Module and not otherwise?

When I type Remove["Global`*"] foo[] := Module[{Subscript[a, 1] = "x"}, 0]; foo[] I get the expected error "only assignments to symbols are allowed". I ...
10
votes
2answers
401 views

Syntax highlighter shows error for Manipulate when it's inside a Block or a Module

I'm putting my new copy of Mathematica 10 through its paces and I noticed a weird change in the colouring scheme that I find very annoying, and I'd like to get some insight into why it's happening. ...
1
vote
2answers
64 views

why the variable scope is not local when using evaluate to plot multicurve?

I noticed a surprising problem when using evaluate to plot multicurve, "evaluate" function actually consider global variable first, not the local! I can illustrate the problem using the following ...
-1
votes
2answers
73 views

How to fix the “Module” function, for implicit funcitons? [closed]

I wanted to make a function which gives the frequencies of a number in a specific list, when I came accros this problem. This is what I tried: ...
36
votes
4answers
850 views

How to avoid nested With[]?

With[ {v1 = #}, With[ {v2 = f[v1]}, g[v1, v2] ] ] How to avoid nested With[] like the above? I'd like to use ...
3
votes
2answers
418 views

Local variables

I'm trying to use Modules together with functions. tmp2 = x^2 + 1; f[y_] := Module[{x = 1}, Evaluate[y tmp2]] This works when ...
2
votes
1answer
46 views

Is the use of Block to temporarily override definitions inherently unsafe? [duplicate]

This may have been discussed before, if so, please let me know. Consider the following example: x = 5 Dynamic[{Clock[], x}] This will always display the ...
1
vote
1answer
48 views

How can I change the scoping of a pure function returned by a function in a package?

I have written a small Mathematica package that contains a module that solves some differential equation with initial conditions. Suppose that the solution to this differential equation defines some ...
1
vote
0answers
44 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 ...
1
vote
0answers
73 views

How to implement nested With as a single construct using functional programming (Fold)?

It is possible to define my own With construct where each local constant can depend on the previous defined local constants? I wish to replace something like this ...
7
votes
2answers
154 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 ...