Questions tagged [assignment]
For questions relating to assignments to symbols, patterns, or expressions.
72
questions
221
votes
2
answers
14k
views
What is the distinction between DownValues, UpValues, SubValues, and OwnValues?
When reading through the documentation, you often encounter the phrases DownValues, UpValues, ...
64
votes
8
answers
108k
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?
...
46
votes
2
answers
10k
views
What is the difference between Set and SetDelayed?
I have a problem with defining a function and most of time I get confused by Set or = and ...
16
votes
3
answers
9k
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 ...
56
votes
2
answers
3k
views
How do you set attributes on SubValues?
SubValues, as discussed in a previous question, are declared as follows
...
25
votes
5
answers
6k
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 ...
23
votes
5
answers
1k
views
Reassign values to symbols
I've got a situation where I have, say 4 symbols, a, b, c and ...
52
votes
3
answers
6k
views
Changing Values in an Association using Map
I am trying to change the value of one key in an association based on the value of another key in that association. So if my association starts as:
...
19
votes
1
answer
599
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? ...
23
votes
3
answers
997
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:
...
14
votes
2
answers
3k
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 ...
41
votes
1
answer
2k
views
What is the story with Removed symbols?
The system function Remove evidently exists primarily to turn a fatal problem into an annoying one, by giving the user a (rather blunt) instrument with which to ...
11
votes
3
answers
553
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 ...
11
votes
3
answers
534
views
Part not behaving the way I expect it to
p = {{1, 0, 0}, {0, 1, 0}, {0, 1, 0}};
p[[All, 3]][[1 ;; 2]] = {1, 1};
p
I want to replace the first two elements of the third column of a 3x3 identity matrix.
...
11
votes
6
answers
1k
views
Generate list of strings from a list of assigned variables
How do I generate a list of strings from a list of assigned variables?
E.g. convert
...
8
votes
4
answers
2k
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 ...
11
votes
3
answers
725
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 ...
47
votes
1
answer
5k
views
++ is dangerous for C programmers
I noticed this fact, that may be misleading for programmers used to C language.
In Mathematica, if you have a function f[] and an array ...
26
votes
1
answer
682
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 possible ...
22
votes
2
answers
2k
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
3
answers
418
views
Why is the first argument of the SetDelayed evaluated?
In[1]: f=g;
In[2]: SetDelayed[f[x_],x^2];
In[3]: ?f
Global`f
f=g
In[4]: ?g
Global`g
g[x_]:=x^2
However, SetDelayed has the ...
39
votes
1
answer
1k
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" ...
20
votes
4
answers
2k
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 two-...
8
votes
2
answers
1k
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 ...
4
votes
2
answers
261
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 ...
12
votes
3
answers
11k
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 ...
12
votes
1
answer
605
views
Triggering actions when a variable is set
Some built-in variables trigger actions when their values are changed:
...
10
votes
2
answers
2k
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
...
17
votes
2
answers
450
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) ...
17
votes
4
answers
372
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
...
14
votes
2
answers
6k
views
Modifying parameters: Set::setps in the part assignment is not a symbol [duplicate]
Ok, this will probably be a silly question, but I can't get my head around it.
I'm making a simple module that, given a list and two integers, will swap the position of the elements in the list.(...
13
votes
1
answer
343
views
Why can AppendTo modify a referenced list in-place but Part cannot?
Part, AppendTo, PrependTo, AddTo, etc. allow in-place modification of a list, but only Part requires that the list be referenced through a simple symbol, e.g. the following all does what you'ld expect:...
13
votes
2
answers
6k
views
How to pass variables by reference and by value [duplicate]
I haven't seen this question specifically addressed before in this site, although some hints and traces have been given, for instance, here:
Pass by reference for an option argument
Does pass-by-...
12
votes
2
answers
2k
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 &...
9
votes
2
answers
401
views
Passing an unevaluated part of an association to a function
I'm making a UI widget that controls sound volume on a speaker using a slider, e.g.:
...
8
votes
1
answer
242
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]
<...
5
votes
1
answer
464
views
Is there any way to implement a "sequential" With[] in Mathematica? [duplicate]
I want the equivalent of Scheme's let*, or basically, a sequential With that works like this:
...
4
votes
3
answers
556
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 ...
1
vote
1
answer
936
views
Variable dependency...easy one
I'm brand new to Mathematica...should be an easy one. Instead of writing u[x] everywhere in the code, how do I assign the dependency early on like this:
u=u[x]
...
0
votes
2
answers
328
views
Associate the elements of a table
I have a table form as:
table = {{a, 6}, {b, ff}, {c, 2}}
I want to associate the first element of each pair from the table to the second element of the pair as ...
29
votes
3
answers
706
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.
...
10
votes
3
answers
500
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 2.
...
9
votes
1
answer
1k
views
How is LHS = RHS; ... ; LHS (nontrivially) different from ... ; RHS
I'm trying to understand how Mathematica thinks about the results of Set. Suppose we perform LHS = RHS. I'm focusing on when ...
9
votes
1
answer
141
views
Why Optional in Set is not held by the HoldFirst attribute?
When evaluatingf[x_:x0] := x, x0 will be evaluated, regardless of the HoldAll attribute of <...
9
votes
2
answers
208
views
DownValue assignment using Apply
Why does assigning a DownValue using Apply, e.g.,
...
6
votes
3
answers
794
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:
...
5
votes
1
answer
104
views
Set on symbol with OwnValues skips UpValues
Can anyone provide insight on what's going on here?
First I define a new Set behavior for a symbol:
...
4
votes
2
answers
136
views
Assignment with Span to Indexed Variable
If I use span to assign a value to every element in a "column", it miraculously works:
...
3
votes
1
answer
160
views
Using HoldFirst at the same time as Apply
I have some code which pops the first number from a list, displays that number, and updates the list:
...
1
vote
1
answer
303
views
direct assignment to the element in a Table
I have already asked a related question here why set values in this way doesn't work?
But I think I have to write the question which I encountered explicitly?
...