Skip to main content

Questions tagged [assignment]

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

Filter by
Sorted by
Tagged with
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, ...
rcollyer's user avatar
  • 34.1k
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? ...
stevenvh's user avatar
  • 6,978
56 votes
2 answers
3k views

How do you set attributes on SubValues?

SubValues, as discussed in a previous question, are declared as follows ...
rcollyer's user avatar
  • 34.1k
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: ...
Jon F.'s user avatar
  • 645
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 ...
Giovanni Resta's user avatar
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 ...
DSaad's user avatar
  • 1,173
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 ...
Pillsy's user avatar
  • 18.6k
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" ...
chris's user avatar
  • 23.1k
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. ...
Naitree's user avatar
  • 1,245
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 ...
Mr.Wizard's user avatar
  • 273k
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 ...
István Zachar's user avatar
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 ...
Mitchell Kaplan's user avatar
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: ...
celtschk's user avatar
  • 19.2k
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 (...
celtschk's user avatar
  • 19.2k
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-...
Fallible's user avatar
  • 793
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? ...
Ted Ersek's user avatar
  • 7,144
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 ...
Mike Bailey's user avatar
  • 1,925
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) ...
David's user avatar
  • 15k
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 ...
wxffles's user avatar
  • 14.3k
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 ...
BlueMac's user avatar
  • 711
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 ...
XiaoaiX's user avatar
  • 397
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 ...
telefunkenvf14's user avatar
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 ...
freddieknets's user avatar
  • 1,085
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.(...
Erdorath's user avatar
  • 243
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-...
Vicent's user avatar
  • 1,111
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:...
Eric Parker's user avatar
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 ...
πρόσεχε's user avatar
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 ...
Onizuka's user avatar
  • 349
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 &...
fhusb's user avatar
  • 123
12 votes
1 answer
605 views

Triggering actions when a variable is set

Some built-in variables trigger actions when their values are changed: ...
Szabolcs's user avatar
  • 236k
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 ...
geordie's user avatar
  • 3,703
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 ...
Ruben Garcia's user avatar
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 ...
Ian Hinder's user avatar
  • 2,125
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)) ...
Eric's user avatar
  • 1,191
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. ...
吴剑涛's user avatar
  • 123
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 ...
ZeitPolizei's user avatar
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. ...
Mr.Wizard's user avatar
  • 273k
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 ...
Ssdmitten's user avatar
  • 185
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 ...
Luis Fernando Moura's user avatar
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: ...
sakra's user avatar
  • 5,230
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 ...
user565739's user avatar
  • 1,139
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 ...
Matvei Kruglyak's user avatar
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: ...
kalix's user avatar
  • 352
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.: ...
verse's user avatar
  • 1,307
9 votes
2 answers
208 views

DownValue assignment using Apply

Why does assigning a DownValue using Apply, e.g., ...
Pisto's user avatar
  • 344
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 ...
cjwood's user avatar
  • 193
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 <...
bcegkmqs23's user avatar
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 ...
jjc385's user avatar
  • 3,493
8 votes
2 answers
455 views

Problem with function inside brackets. Bug? [duplicate]

This code adds random digits to lists, and it works fine: ...
Jerry Guern's user avatar
  • 4,632
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 ...
sjdh's user avatar
  • 7,817

1
2 3 4 5
7