Questions on assigning and unassigning definitions to names that represent them, including issues raised by context and localization. Use tag [assignment] for issues relating to Set versus SetDelayed.

learn more… | top users | synonyms

93
votes
7answers
3k views

What are the use cases for different scoping constructs?

Mathematica includes three functions that I know of which can be used to effectively (if not actually) define a variable and give it a value within a local scope: ...
34
votes
3answers
1k views

Why modules with no variables?

I was reading some code, in particular, recipe 4.13 on unification pattern-matching in Sal Mangano's Mathematica Cookbook, and there were many instances of Modules with no variables in them, such as ...
28
votes
3answers
1k views

Is there an equivalent of MATLAB's Workspace window in Mathematica?

For example, in MATLAB, a panel is available where one can see straightaway which variables are used and their dimension sizes. Is such a feature available in Mathematica? I really find it hard to ...
22
votes
5answers
9k views

How do I clear all user defined symbols?

Is there some way to do this other than going to Evaluation -> Quit kernel and firing a new one up?
20
votes
5answers
2k views

Clear complains that a subscripted variable is not a symbol or a string?

Here's a small listing where I've used EscqEsc to typeset θ in the notebook: ...
19
votes
3answers
364 views

Why x = x doesn't cause an infinite loop, but f[x_] := f[x] does?

If I execute: In[1] := x = x Out[1] = x and then I evaluate the symbol x: ...
18
votes
2answers
1k views

Best practice of passing a large number of parameters to functions

I have a number of functions which all take a large number of parameters. I am wondering which is the best practice of passing these parameters to those functions. I could of course simply specify the ...
18
votes
2answers
317 views

The dangers of SaveDefinitions — should this really happen?

I have been bitten hard by SaveDefinitions -> True. I'll describe in detail what happened below. My questions are: Is this a bug? What is the most convenient ...
18
votes
1answer
2k views

How to define a global variable in Mathematica?

I have restricted the context of my notebooks to each individual notebook. So variables in each notebook are local and are not seen in another notebook. But in two of my notebooks I have two ...
16
votes
3answers
638 views

Create an adaptive amount of local variables for error propagation

I intend to write a function which calculates the result and the error for any formula with any amount of variables using the Gauß Error Propagation. The error $\mathrm{d}R$ for a function $R(a,b,c)$ ...
15
votes
4answers
611 views

Making a symbol's new definitions be tried before all previously defined ones

Is there any way to "close" a package (or a symbol, or a context) in that if a user of the package adds definitions to the symbol they will be tried before the package defined ones, just like what ...
14
votes
6answers
905 views

How to clear all variables except one?

I have a lot of variables: $a,b,c,d, \ldots $ I want to clear the values of all of them except $b$. It's really lengthy to write Clear[a]; Clear[c]; ... Is ...
14
votes
6answers
2k views

Why can't I define vector v as having subscripted elements v_1, v_2, v_3…?

I'm a very beginner in Mathematica 8 programming. While trying to play a little with tensor analysis I encountered a problem. How to define for example vector in such manner: v={$v_1,v_2,v_3$} For ...
14
votes
4answers
1k views

How to implement the Observer design pattern?

As a programmer with a history with procedural languages, the Observer design pattern inmediately springs to my mind when attacking certain class of problems, mostly UI/notifications related. A ...
13
votes
1answer
611 views

What is $ sign in Mathematica?

Is it a special sign or it can be used as anything else? For creating variables for example. $x=4
12
votes
7answers
863 views

How to Set parts of indexed lists?

I would like to assign a list to an indexed variable and then change it using Part and Set like this: ...
12
votes
4answers
1k views

how to differentiate formally?

I have been wrapping my head around this for a while now and I have not found a solution so far. I want to work with an arbitrary number of variables in mathematica and use some built in functions. ...
12
votes
5answers
1k views

Define a mathematical set

I have a terribly short question: Is there a way to define a pure mathematical set? I tried a bit of googling, but the word "set" has two meanings and the other one (as in setting) as by far more ...
12
votes
1answer
239 views

Is it possible to Clear all variables (but not functions)?

I have written a Mathematica script in which I define functions and variables. Here is a vastly simplified example: ...
12
votes
3answers
290 views

Variable scope similar to other languages

Mathematica, unlike many other languages, defines the variables in the global scope unless it is explicitly asked to define a variable in a local scope, e.g., using ...
10
votes
3answers
867 views

Elegant manipulation of the variables list

I have a list of global variables (some of them are indexed variables), for example: varsH = Hold[U0[1], U0[2], B0, V0[1], V0[2]] Or (if it is easier to handle) ...
10
votes
4answers
390 views

How to use pattern matching to assign values to Subscript[f,x_]?

I want to define two subscripted functions Subscript[f,1] and Subscript[f,2]. To keep the assignments local, I would like to ...
10
votes
3answers
303 views

How can I hold UpValues but evaluate other expressions?

Consider these definitions: own = "OwnValue"; down[_] = "DownValue"; sub[_][_] = "SubValue"; N[n] = 3.14; _[___, up, ___] ^= "UpValue"; The attribute ...
10
votes
4answers
416 views

Refering to global variables inside modules

How do I refer to global variables inside modules if the same name is used for a local one? In code, I want that ...
9
votes
3answers
801 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
3answers
343 views

Problem when defining variable in Mathematica

I'm trying to run this: domain := {n, 0, 10}; Plot[n, domain] but it doesn't work. Instead, it generates the message ...
9
votes
2answers
221 views

How to take derivative of parameterized coordinate?

Suppose I have a vector in $\mathbb{R}^n$ but $n$ is not known in advance. I want to be able to write functions which operate on the components of that vector, and then I'd like to be able to take ...
8
votes
3answers
564 views

Variable variable names

I have two variables, $ u \in \{0,40\}$ and $\gamma \in \{0,1\}$. I take $10$ values of $u$ and $5$ values of $\gamma$ and using their combinations as a pair of input parameters, I perform some ...
8
votes
3answers
153 views

Protecting function definitions only for particular inputs

I would like to be able to define the values of a particular function for new inputs. f[x1]=f1; f[x2]=f2; I would also like to be able redefine the values for ...
8
votes
2answers
185 views

Representing a value in an output as a self defined variable

I'm trying to find a way to have Mathematica always represent a numerical value as a self defined variable that I define using lhs=rhs. For example, if I execute ...
8
votes
1answer
120 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 ...
8
votes
2answers
214 views

Distribute and analyze large datasets

I need to analyze very large datasets, and they obviously don't fit in a computer memory. I need to produce some statistics on these datasets, and this brings me here: How can I make some statistical ...
8
votes
1answer
151 views

Different behaviours of Default Argument

I don't really understand the behaviour of Default Argument. If I execute this command in Mathematica: In: {f[a], f[a + b]} /. f[x_ + y_.] -> p[x, y] ...
7
votes
6answers
596 views

Using a built-in symbol as a variable

I want to produce a Mathematica Computable Document in which N appears as a variable in my formulae. But N is a reserved word in ...
7
votes
1answer
143 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
7
votes
2answers
188 views

Why doesn't DumpSave work for elements created within a local notebook context?

I recently noticed that when dealing with notebooks with local context, I cannot successfully DumpSave variables, or, rather, when I call ...
7
votes
1answer
229 views

Defining my own invisible operator

There are some invisible operators in Mathematica (\[ImplicitPlus],\[InvisibleTimes],...
7
votes
0answers
67 views

Maximum number of literal values for SetDelayed?

Under what circumstances should I expect Mathematica to turn a delayed assignment into an immediate assignment? I have put together a minimal example (available here) which demonstrates this behavior. ...
6
votes
2answers
319 views

Obtaining the name of a variable as a string [duplicate]

Suppose that I have an arbitrary variable myVariable. Is there some function fun -- either built-in or one that I can define -- ...
6
votes
2answers
189 views

Can I redefine a list as a set of variables?

Suppose I have a list mydata = {3, 14, 15, 92, 65} and in some cases I want to manipulate it as a list, like for reading from and writing to a file, or for ...
6
votes
3answers
2k views

Assigning values to a list of variable names

As part of a calculation I need to do something like this Evaluate[{aaa, bbb, ccc}[[ index]]] = {1, 2, 3, 4, 5} so if index is ...
6
votes
4answers
450 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 ...
6
votes
1answer
145 views

How can I define a ⊕ operator?

What is wrong with the following code? CirclePlus := SparseArray[Band[{1, 1}] -> {##}] & a_ ⊕ b_ := CirclePlus[a, b] SetDelayed::write: Tag Function in ...
6
votes
2answers
95 views

How to name a variable with the value of another variable?

A simple but very important (at least for me) question. In some languages for defining a variable, for example, we can do: i=10 "d"&i=30 So I have defined the variable d10, and... d10=30 ...
6
votes
2answers
260 views

How to choose variables from a list for a function and then use the solution in a subsequent function?

I am trying to calculate heating degree days and cooling degree days and output that information to a table. I am using mathematica's curated data to do this. In text this is what I would like to ...
5
votes
1answer
1k views

Module with “local functions”

I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module. So I tried this: ...
5
votes
1answer
134 views

Reloading package after accidental Remove[] of symbol

I was using Needs["PlotLegends"] By chance I removed ShowLegend by Remove[ShowLegend] How do ...
5
votes
1answer
317 views

Using external variable as a variable inside WhenEvent & NDSolve

I'm trying to use the new WhenEvent functionality of NDSolve in Mathematica 9, in order to perform an action when a variable in the system reaches a certain value. I would like this variable to come ...
5
votes
1answer
272 views

Proper use of arbitrary number of variables

So, I'm working on a project where the number of independent variables is not fixed. Consider a problem of $N$ independent variables, $\boldsymbol{r}$. I want to perform different things with them. ...