Questions tagged [assignment]
For questions relating to assignments to symbols, patterns, or expressions.
303
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?
...
56
votes
2
answers
3k
views
How do you set attributes on SubValues?
SubValues, as discussed in a previous question, are declared as follows
...
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:
...
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 ...
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 ...
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 ...
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" ...
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.
...
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 ...
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 ...
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:
...
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 (...
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-...
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? ...
18
votes
4
answers
2k
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
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
...
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 ...
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 ...
16
votes
2
answers
632
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 ...
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 ...
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
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-...
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:...
12
votes
2
answers
839
views
Duplicate the swap function
Because defining the function:
swap[x_, y_] := Module[{}, t = x; x = y; y = t; Return[]]
and writing:
swap[x, y]
{x, y}
I ...
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
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 &...
12
votes
1
answer
605
views
Triggering actions when a variable is set
Some built-in variables trigger actions when their values are changed:
...
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
...
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 ...
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
2
answers
1k
views
Can't understand one paragraph in Power Programming with Mathematica
In the chapter 7 of the Power Programming with Mathematica, there is a paragraph discussing the behavior of Set- functions: (page 205. (pdf version p. 224))
...
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
2
answers
850
views
Is there a convention on which name to use for a throwaway variable? (Like underscore in Python) [duplicate]
I would prefer to write {_, foo} = LongFunctionName[arg1, arg2] instead of foo = LongFunctionName[arg1, arg2][[2]] or ...
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.
...
10
votes
6
answers
3k
views
How do I randomly divide individuals into groups?
I would like to assign 'x' individuals to 'y' groups, randomly. For example, I would like to divide 50 individuals into 100 groups randomly. Of course, with more groups than individuals many of the ...
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
...
10
votes
1
answer
371
views
Unset variable by its name given as a string
Unlike the function Clear, the function Unset does not work for string patterns. Given the name of a variable as a string, how can you unset the corresponding variable? Example:
Given:
...
10
votes
1
answer
4k
views
Clear and Unset doesn't free memory
Example: Try this example only if you have > 4G ram
d = 2000;
f[x_, y_, d_] =
Sum[Subscript[a, i, j]*x^i*y^j,
{i, 0, d - 1}, {j, 0, d - i}];
Then
...
9
votes
4
answers
2k
views
Looping through 3 million elements of a list to 'Set' values is slow
I am looping through the 3 million elements of a time-series (v1, o1) and corresponding values (v2, o2).
v1 contains a uniformly increasing timestamp (e.g. 1 to 3,000,000 in increments of 1)
o1 ...
9
votes
2
answers
256
views
Confusion about HoldFirst attribute of Set
Set has the attribute HoldFirst.
Expectedly the following does not work, i.e. it does not assign the OwnValue of 3 to x:
...
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.:
...
9
votes
2
answers
208
views
DownValue assignment using Apply
Why does assigning a DownValue using Apply, e.g.,
...
9
votes
1
answer
488
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 ...
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
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 ...
8
votes
2
answers
455
views
Problem with function inside brackets. Bug? [duplicate]
This code adds random digits to lists, and it works fine:
...
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 ...