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

learn more… | top users | synonyms (1)

0
votes
0answers
46 views

Calculating Value-At-Risk (VaR) in Mathematica. A simple code [on hold]

Calculating Value-At-Risk in Mathematica Obtaining data (Microsoft. Daily returns 2014): msftreturn = FinancialData["MSFT", "Return", "2014"][[All, 2]]; Code: ...
0
votes
2answers
61 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
98 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 ...
1
vote
2answers
59 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
64 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
60 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
81 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

Why is it not allowed to define vectors of variables in a scoping construct?

The following command assigns 1 to a, 2 to b and 3 to c: ...
2
votes
0answers
79 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
45 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 ...
0
votes
2answers
102 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 ...
1
vote
0answers
62 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
1answer
73 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: ...
2
votes
1answer
73 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 ...
13
votes
1answer
274 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: ...
5
votes
3answers
143 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 ...
2
votes
3answers
89 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... ...
0
votes
0answers
16 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
58 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
224 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. ...
1
vote
2answers
59 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 ...
10
votes
2answers
381 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
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: ...
2
votes
1answer
43 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
47 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
42 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
72 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 ...
5
votes
2answers
125 views

Generating EventHandler using Table

I would like to change the edge color of a graph when double clicking on it, the following code works: ...
0
votes
0answers
44 views

Scoping problem with Manipulate [duplicate]

Consider the following simple code: f = Sin[a x]; Manipulate[ Plot[f, {x, 0, 10}], {a, 0, 10}] Manipulate takes f to be ...
3
votes
1answer
82 views

How to localize symbols on the right-hand side of Rule when using ReplaceAll?

I am frustrated by the following: {1, 2, 3.0} /. a_Integer -> a + 1 The a on the left side of the arrow is localized and ...
4
votes
0answers
121 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. ...
8
votes
1answer
106 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 ...
1
vote
1answer
48 views

Replicability of RandomVariate within a function?

I have a rather basic question but can't figure out a foolproof answer. In the following code, I build a function which returns the mean difference between successive elements of a vector created from ...
5
votes
2answers
119 views

Checkbox behaving weird when variables are local

I have a DynamicModule in which I need a checkbox with a Max in the dynamic part: ...
3
votes
0answers
137 views

Module in numerical model for NonlinearModelFit is slow and leaks memory

I am trying to make a NonlinearModelFit where the model is the result of a numerical calculation. My code works, but it is very slow and also seems to have a memory ...
0
votes
1answer
73 views

Change input variable in a function [duplicate]

I'd like to write a function, that would take a matrix as an input parameter and would change it by multiplying one of its rows by a factor. I try to do it in the following way: ...
0
votes
0answers
51 views

Fold Dynamic Function [duplicate]

I'm making on a demo working with differential equations as functions of two variables (x and y). The demo will have to Fold the differential equation with an initial y-value and a series of x-values. ...
3
votes
3answers
99 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
1
vote
2answers
99 views

Is it possible restore initial values after modify a control in manipulate?

I used to program in Visual Basic, but in Mathematica I have not seen some kind of restore default values after any user modification command in the Manipulate ...
0
votes
1answer
90 views

Multiple Simulations of a Markov Chain

I am trying to create a simple series of Bernoulli bits whose probabilities follow a two-state Markov chain. ...
1
vote
2answers
116 views

`Slot` (#) interfering with evaluation

In the following I am using one function makeFunc to assemble two functions a[t] and b[t] to ...
0
votes
3answers
142 views

Clean-up Code / Reduce variables

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example: ...
4
votes
1answer
93 views

Dial control for Manipulate?

I'm trying to make a control item in a Manipulate that is a simple dial. I'm trying to mimic Manipulate[theta, {theta, 0, 360}], ...
7
votes
1answer
197 views

Returning a value from a Module

In the following simple example MyMod := Module[{i}, Do[ Return[1], {i, 3} ]; Print["test"] ] I expected the module to exit on ...
0
votes
0answers
68 views

Accessing variables outside DynamicModule

I have this DynamicModule which is basically a form of input fields that are parameters. My two final functions are H1 and ...
4
votes
2answers
163 views

TrackedSymbols affected by CurrentValue. Another problem with SetDelayed(:=)+OwnVales in Initialization

It is MWE, only a Disk[] that should print "Click" when clicked. Also "date... updating" ...
0
votes
0answers
50 views

Dynamically Update Inset within Graphics with RunScheduledTask

I followed the instructions given below to play with RunScheduledTask within DynamicModule. ScheduledTask with scoped ...
5
votes
1answer
135 views

How to inherit options by scoping?

Consider the following example: you have a function framed producing a cell with a button that, when pushed, calls framed again. ...
0
votes
1answer
105 views

Why do InputFields not respond in Manipulate utilizing FindGeometricTransform?

In the following code changing the InputField does not result in observing changes in a result from FindGeometricTransform, but ...
0
votes
2answers
114 views

Defining Piecewise Functions in Modules

In short, my question is the same as my previous one except that everything is now wrapped up in a module. The relevant code I'm working with is: ...