# Tag Info

## Hot answers tagged assignment

108

In Mathematica, all functions are really just patterns, and there are different kinds of those. Let's start with OwnValues, which is the pattern type of a variable as you know it from other programming languages. The symbol having the OwnValue has, as the name suggests, intrinsic, "own", value. In[1] := a = 2; OwnValues[a] Out[1] := {HoldPattern[a] :> ...

89

Preamble In addition to the differences between these global rules reflected in the patterns for which assignment operators put the global rule into one or another ...Value, there is another, and IMO,no less important difference, and that is in how these rules are used in the evaluation sequence. Evaluation: OwnValues The OwnValues represent symbols ...

35

Your question really is about how to make attributes of f affect also the evaluation of other groups of elements, like y and z in f[x___][y___][z___]. To my knowledge, you can not do it other than using tricks like returning a pure function and the like. This is because, the only tool you have to intercept the stages of evaluation sequence when y and z are ...

22

This seems to work: a = 1; b = 2; c = 3; d = 4; Scan[Function[p, p = 5, HoldAll], Hold[a, b, c, d]] Now, try evaluating {a, b, c, d}. Here's the version with slots: Scan[Function[Null, # = 5, HoldAll], Hold[a, b, c, d]]

20

Unique will do precisely this. Try for example Unique[x], which returns a symbol with a name similar to x$123. Here I should mention the Temporary attribute as well, which, when associated with a symbol, causes that symbol to be removed from the system when it's no longer referenced. This is occasionally useful when you need Unique. But whenever you do ... 20 You can do this : s = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}]; xx = s[[All, 1, 2]]; yy = s[[All, 2, 2]]; Now you can access solutions, this way xx[[1]], yy[[2]]. If you prefer to collect solutions in Array, there is another way : X = Array[ x, {Length@s}]; Y = Array[ y, {Length@s}]; x[k_] /; MemberQ[ Range[ Length @ s], k] := s[[k, 1, ... 19 Yes, at least in one place. x = {1, 2, 3} x[[2]] = 8; All right there, but y := {1, 2, 3} y[[2]] = 8 gives Set::noval: Symbol y in part assignment does not have an immediate value Credit to this old comment by Leonid. Also note the point on memory usage: [...] I'd guess that delayed definitions may use some intermediate internal variables, ... 19 Usually you don't want to actually assign values to x and y, and you would use replacement rules instead: sols = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}]; {x, y} /. sols[[1]] or for the second solution: {x, y} /. sols[[2]] If you really want to assign values to x and y globally, you could use: Set @@@ sols[[1]] but you must ... 19 Try for example SetAttributes[def, HoldAll] def[s_Symbol, v_] := Function[Null, s[x_] := #, HoldFirst][v] Unnamed functions just don't care :) Other alternatives that should also work (but I would use the previous approach) def[s_Symbol, v_] := Identity[SetDelayed][HoldPattern@s[x_], v]; def[s_Symbol, v_] := Unevaluated[s[x_] := "Hello"] /. "Hello" ->... 17 You can use Unset for this, like so: a[b_, c_] =. =. works with UpValues too (the full form of this has TagUnset): a /: Subscript[a,2] =. You need to use the same pattern in Unset that you used in the definition. Get this using Information (i.e. ?a). 17 General considerations To my mind, the only robust way to do this is to build some custom object model in Mathematica, and in particular to restrict the way values can be changed to some well-defined route you can control. Because, as it follows from one of the discussions you linked to, there seems to be no reliable way to intercept arbitrary value changes ... 16 How about this: list = {1, 2, 3}; ToExpression["list", InputForm, Hold] /. Hold[v_] :> AppendTo[v, 3] {1, 2, 3, 3} list {1, 2, 3, 3} 16 You can use Unset or =. to remove a single definition. For example, for your above function its DownValues are DownValues@a Out[1]= {HoldPattern[a[b_]] :> 2, HoldPattern[a[b_, c_]] :> 3} Unsetting the definition for a[b_, c_], a[b_, c_] =. DownValues@a Out[2]= {HoldPattern[a[b_]] :> 2} It works similarly for UpValues too, i.e., you can do a /... 15 Perhaps something like this: SetAttributes[localSet, HoldAll] localSet[lhs_, rhs_] := Union @@ Cases[ Unevaluated[lhs], Verbatim[Pattern][p_, _] :> HoldComplete[p], Infinity, Heads -> True ] /. _[x___] :> Block[{x}, lhs = rhs;] Test: var=3; localSet[f[var_],Normal[Series[Exp[var],{var,0,3}]]] DownValues[f] {HoldPattern[... 15 This is a fairly natural question and I feel it is worthy of attention. I am going to answer in two parts. First, I am going to show a method that is more appropriate for Mathematica programming and which I recommend you use instead. Then I will show how to force the action you are attempting. Better Alternatives The common way to accomplish ... 15 This might work as you expect and be save even if definitions for x exist: Block[{x}, f[x_] = D[Sin[x], x];] I would strongly suggest that you get familiar with Derivative and pure functions if you work with symbolic derivatives, though. This will make your life much easier in the long term. Your example would reduce to: f = Derivative[1][Sin] and a ... 15 The behaviour you observed is completely independent of NumericQ. It could also be seen with a function foo which has the initial definition foo[_]=False. Example 1: Initially you define NumerixQ[x]=True, which tells Mathematica that whenever it evaluates the expression NumericQ[x], it should evaluate to True. Since for symbols, it is pre-defined to return ... 15 The reason is that AppendTo and PrependTo actually replace the whole list with a new one, rather than only changing a single element. In that sense, they are not really "in-place". They are in-place in the sense that the result gets assigned back to the same variable. You can see using Trace, that AppendTo and PrependTo in fact expand: bar[1]={4,3,2,1}; ... 14 We can define a new "variable container" that can be used to assign the same value to multiple variables: ClearAll[vars] SetAttributes[vars, HoldAll] vars /: s:(_vars = _) := CompoundExpression @@ Thread[Unevaluated@s, vars, 1] It is used like this: In[4]:= ClearAll[a, b, c, d] vars[a, b, c, d] = 5 Out[5]= 5 In[6]:= {a, b, c, d} Out[6]= {5, 5, 5,... 14 Not to detract from the existing answers (particularly @WReach's suggestion, which was the same solution that came to my mind as I read your question, and which I will use here), but you may find it easier to define your own references rather than using strings. (In fact, I wouldn't necessarily recommend an approach based on building Mathematica expressions ... 13 TracePrint will show you what happens: PreIncrement takes it's argument x, evaluates it (let's call the result result), then evaluates x = result+1. Note that PreIncrement has HoldFirst. Now ++(++x) evaluates ++x first yielding 2, then evaluates (++x) = 2+1 resulting in an error (trying to assign to PreIncrement) and returning 3. This also explains why ... 13 There is a special-purpose function for this, Unique: In[270]:= Unique[] Out[270]=$3 In[271]:= Unique[a] Out[271]= a\$4318 However, these symbols will be guaranteed to be unique only within a given Mathematica session.

13

Utilizing ValueFunction as mentioned here, we could do something like this: SetAttributes[Observe, HoldFirst]; Observe[s_Symbol, fn_] := With[{VF := Experimental`ValueFunction[s]}, VF = If[ValueQ @ VF, ReplacePart[VF, {2, 2, 2} -> Append[VF[[2, 2, 2]], fn]], Function[Null, Table[f[##], {f, {fn}}], HoldAll] ]; ] We will need to ...

12

You could use myList = Hold[a,b,c,d] Function[x, x=5, {HoldAll}] /@ myList // ReleaseHold

12

If you insist on working with your list where you assemble variables, this will do it: setValues = Function[{vlist, val}, OwnValues[vlist] /. (_ :> vars_) :> Replace[Unevaluated@vars, var_ :> (var = val), {1}], HoldFirst]; For example: In[73]:= myList:={a,b,c,d} In[74]:= a=1;b=2;c=3;d=4; In[77]:= setValues[myList,5]; In[...

12

General thoughts I think that your mechanism is reasonably robust for common use cases, but not fully robust if one wants to take into account all possible ways that the value (or, generally, global properties) of the symbol can be changed in Mathematica. My current opinion is that making such triggering mechanism fully robust without new system support is ...

11

You could name the patterns DefFn[f_[args___], body_] := f[s : PatternSequence[args]] := WithStackFrame[{f, {s}}, body];

11

The error comes from the first line. I am not sure what the second does; finally, you differentiate with respect to s, but have a variable S, which is different. Perhaps you wanted to do this: v = v0*Sin[Pi*s/s0] D[v, s] which works. To see the problem with recursion, run this: ClearAll[v] v = Subscript[v, 0] What is happening is the same that ...

11

(a[[#]] = {1, 2, 3}) & /@ Range[4, 6]; You get: In[1]:= a Out[1]= {1, 2, 3, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, 7, 8, 9, 10} A convenient thing to remember is that if your elements are not sequential it is still easy to set up: (a[[#]] = {1, 2, 3}) & /@ {1, 3, 10}; In[2]:= a Out[2]= {{1, 2, 3}, 2, {1, 2, 3}, 4, 5, 6, 7, 8, 9, {1, 2, 3}}

Only top voted, non community-wiki answers of a minimum length are eligible