For questions relating to assignments to symbols, patterns, or expressions.

learn more… | top users | synonyms

115
votes
2answers
5k views

What is the distinction between DownValues, UpValues, SubValues, and OwnValues?

When reading through the documentation, you often encounter the phrases DownValues, UpValues, ...
32
votes
1answer
649 views

How to augment the realm of functions Mathematica thinks it knows how to integrate symbolically

My question involves extending the functionality of Integrate over specific integrals in the most generic manner. Specifically, is it possible to "hack into" ...
27
votes
3answers
422 views

Assign a randomly generated list to variable

Bug introduced in 10.0.0 and fixed in 10.0.2 I don't know if this is a silly question, but it really baffles me. So please help me understand this behavior. ...
26
votes
1answer
835 views

How do you set attributes on SubValues?

SubValues, as discussed in a previous question, are declared as follows ...
22
votes
1answer
468 views

Why does list assignment with a packed array result in unpacked values?

Bug introduced in 5.2 or earlier and fixed in 10.1.0 This is another question on the design choices in Mathematica. I understand that without direct reply from the developers it may not be ...
20
votes
5answers
598 views

Reassign values to symbols

I've got a situation where I have, say 4 symbols, a, b, c and ...
20
votes
3answers
411 views

Nested definition: How can I define a function with a passed-in expression?

Here's a simplified version of what I'm trying to do: ...
19
votes
5answers
2k views

How to pass a symbol name to a function with any of the Hold attributes?

Given a function with the attribute HoldFirst, HoldAll or similar, and a variable, list, how ...
19
votes
5answers
16k views

Assign the results from a Solve to variable(s)

I understand Mathematica can't assign the results of a Solve to the unknowns because there may be more than 1 solution. How can I assign the 4 values of following result to variables?
18
votes
4answers
899 views

How can I randomly generate a unique, unused symbol?

In a previous question on collecting terms for a multivariable polynomial, I answered with a solution that required a unused symbol to be used for a temporary substitution and back substitution. It ...
17
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 ...
17
votes
4answers
317 views

Setting parts of a list

Suppose I have list a = Range[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} in which I want to set some elements to be a list ...
17
votes
2answers
501 views

Does Set vs. SetDelayed have any effect after the definition was done?

I was always assuming that the only difference between Set (=) and SetDelayed (...
16
votes
2answers
211 views

Clearing a specific definition

Consider the case where a symbol has multiple definitions attached to it, a /: Subscript[a,2] := 1 a[b_] := 2 a[b_, c_] := 3 How does one clear (/unset/remove) ...
15
votes
1answer
281 views

How to make a function like Set, but with a Block construct for the pattern names

How can we define a function that works like f[x_]=ComputeSomething[x] and treats x as a variable that does not have a value? ...
14
votes
2answers
350 views

How to make use of NumericQ[x] = True (and use it safely)?

Basic Issue: I'm trying to understand the proper use of NumericQ's "magical" capabilities. Please consider the examples below. Actual question and some links are ...
11
votes
3answers
376 views

MapThread gives different results from ToExpression when trying to assign variables from a list

I have the following data set: data = {{"Jakarta","Surabaya","Bandung"},{1,2,3}} and I'd actually like to assign the numerical values to the categorical data so ...
11
votes
1answer
373 views

Triggering actions when a variable is set

Some built-in variables trigger actions when their values are changed: ...
10
votes
2answers
825 views

Scoping in assigning a derivative

While answering another question, I stumbled upon a problem I cannot easily resolve. To assign the derivative of a function to another function, typically one can do this with a ...
10
votes
3answers
346 views

Why does ++++x return an increment of 2 when the value of x is only incremented by 1?

This line returns 3: x = 1; ++++x However, the value of x after the increment is only ...
9
votes
2answers
125 views

DownValue assignment using Apply

Why does assigning a DownValue using Apply, e.g., ...
8
votes
3answers
339 views

How to write a function-defining function which stores the function arguments in a stack?

I have a function-defining function SetAttributes[DefFn, HoldAll]; DefFn[f_[args___], body_] := f[args] := body; I am trying to modify this function to record ...
8
votes
2answers
297 views

How to replace a variable that was assigned a value before

Question How to make the replacement $f[x] \to f[y]$ for an abitrary function $f$ if $x$ was assigned a value before? Example 1 How can I make the replacement $x +1 \to y + 1$ if I have assigned ...
8
votes
3answers
3k 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 ...
8
votes
2answers
238 views

How to ignore list elements when extracting with pattern matching

If I have a simple list, say {1, 2, 3}, and I want to extract a subset of the elements, what is the correct syntax? e.g. { i, _, j } = {1, 2, 3} I want i = 1 ...
8
votes
1answer
161 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] ...
8
votes
1answer
116 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. ...
7
votes
2answers
164 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 ...
7
votes
1answer
192 views

Is this a good way to assign a Solve result to a function?

My code: ...
7
votes
1answer
323 views

Assignment rule to distribute matrix-multiplication over custom notation

I'm trying to write some code to do basic algebraic manipulations in BraKet notation. Thus far I have a function KetToVec which will convert any expression of the form a1 Ket[s1]+ b Ket[s2] +... to a ...
6
votes
2answers
95 views

Force evaluation of the right-hand side of a local variable definition

One can do expression = a + 1; f[a_] := Evaluate[expression] but how would one do something like this ...
6
votes
3answers
396 views

Understanding Function and Variable in Mathematica for beginner [closed]

I'm reading the book of Leonid Shifrin, and I love the way the book explains the core in Mathematica. As everything is an expression, now I understand that, there ...
5
votes
4answers
376 views

Is there any way to assign multiple values?

I am looking for something like "byref" assignment in programming languages. Let's assume I have: x1=x2=....=xn if I want to change their values ( say to 3 ) I need to set all of them like: ...
5
votes
3answers
376 views

Assign output of solve to variables [duplicate]

I have this input: Solve[x^2 + 3 x + 2 == 0, x] which gives this output: {{x -> -2}, {x -> -1}} I want the first x ...
5
votes
3answers
169 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 ...
5
votes
4answers
539 views

Delayed evaluation of assignment

I have a set of PDE's that depend on parameters. They depend on many parameters, but for simplicity we can here assume that they depend only on one, say a. I want ...
5
votes
1answer
91 views

Edit built in symbol

I would like to add new attributes to built in symbols (e.g. ElementData or PolyhedraData) so that I can still call the symbol ...
5
votes
1answer
285 views

Assign variables automatically to a function

Lets consider that I've this function: fn = a*x^2 + b*x + c; The idea here is to assign automatically the variables a, ...
4
votes
3answers
189 views

Using Block with a list of variables

I have a list of variables var={v1,v2}; which I want to protect/localize during a particular operation. I have decided that I want to use ...
4
votes
2answers
115 views

Choosing an initializer list in With

I'm trying to have several initializer lists in a With function and choose which one I want to use. So far I have this: ...
4
votes
2answers
117 views

Initializing several values of a function

How do I initialize several values of a function at once? Consider the function f[0] := 1; f[1] := 2; f[2] := 3; f[n_] := f[n] = f[n-1] + f[n-2] + f[n-3]; Is ...
4
votes
2answers
845 views

How to store value in a For loop

I am using two For loops. When the inner loop finishes, I don't want to print but I want to store the result from the inner loop in some variable so that I can call ...
4
votes
2answers
209 views

why set values in this way doesn't work?

question is as follows define a list ttt={1,2}; and if I set values in this way {ttt[[1]],ttt[[2]]}={3,4} then the value ...
4
votes
3answers
190 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: ...
4
votes
1answer
848 views

Integral of Lorentzian yields different results depending on when parameter assigments are made

I'm evaluating the integral of a Lorentzian, which I know equals one. First I define the function and evaluate the integral in two slightly different ways. Surprisingly, I do not get the right answer ...
3
votes
2answers
451 views

How to copy a function by value?

I need to change a function symbol multiple times by assigning another symbol to it, but when I'm trying to do that the symbols overwrite each other. I'm assuming this is because the assignment is by ...
3
votes
3answers
133 views

Using a variable to tell Mathematica which existing variable to manipulate

I have an estimation routine I have coded up, I want now to do some tests on it where I pick a particular parameter and run the estimation for differing values of that parameter. What I want is to be ...
3
votes
1answer
96 views

Syntax Coloring for “Possible Unwanted Assignment” Issue

Apparently Mathematica's syntax coloring engine believes that the second assignment in the following code excerpt (concocted for illustration purposes only) is a "Possible Unwanted Assignment" since ...
3
votes
2answers
118 views

Assignments in ParallelDo when using indexed variables

For convenience I use an indexed variables syntax to hold results of long computations for different integer parameters: ...
3
votes
1answer
156 views

Set promotes Rule to RuleDelayed?

In mentally building a model of how Mathematica works, I naively expected Set to create a Rule in a symbol's ...