Tag Info

Hot answers tagged

68

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] :> ...


56

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 ...


22

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 ...


19

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]]


18

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 ...


17

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" ...


16

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).


16

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, ...


15

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

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 ...


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 ...


14

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 ...


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 ...


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]; ...


12

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] ...


12

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 ...


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

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 ...


10

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, ...


10

(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}}


10

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, ...


10

You can use injector pattern for that: {Hold[{a = 2, b = 3}], Hold[{a = 4, b = 6}]}[[1]] /. Hold[init_] :> With[init, a + b] (* 5 *) {Hold[{a = 2, b = 3}], Hold[{a = 4, b = 6}]}[[2]] /. Hold[init_] :> With[init, a + b] (* 10 *) but you can see that Hold is preferred over Unevaluated in such cases. As an alternative, you can define your own ...


9

Perhaps a = Range[10] a[[4 ;; 6]] = Sequence@{1, 2, 3}; Returns {1, 2, 3, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}, 7, 8, 9, 10} However, as Mr.Wizard noted in a comment, this is a LIIIEEE. If you do Information[a] you see that the Sequence is actually being saved. I find this weird, but anyway, can be solved with an extra a=a


9

Unique[] is the function that does exactly what you want. However, do note that Unique uses $ModuleNumber and increments it, so if your code depends on the value of $ModuleNumber or if you mess with it, you should be aware of the consequences.


9

This is because you are using = (the assignment operator) in the condition (not the body) of While. It is a typical beginner mistake to use = where == is meant, so Mathematica warns about this. Since you also use several ; in the condition, it gets a little confused and only highlights one of the = signs, not all of them.


9

You could use Formal Symbols: f[\[FormalX]_] = D[Sin[\[FormalX]], \[FormalX]] Which looks like this in the Notebook: Formal Symbols are entered with Esc$xEsc where x is any regular letter. Formal Symbols cannot be assigned a global value, avoiding collisions: Set::wrsym: Symbol [FormalX] is Protected. >> I also wrote a function localSet to ...


8

One could use Outer for this purpose: {a, b, c, d} = {1, 2, 3, 4}; Outer[Set, Hold[a, b, c, d], Hold[5], 1] /. Hold -> List or: {a, b, c, d} = {1, 2, 3, 4}; Outer[Set, Unevaluated[{a, b, c, d}], {5}, 1] Thread also works: {a, b, c, d} = {1, 2, 3, 4}; Thread[Hold[{a, b, c, d}, 5]] /. Hold -> Set


8

I think avoiding ToExpression is important, so here is a solution using Symbol: MapThread[With[{var = (Clear[#1]; Symbol[#1])}, var = #2] &, data] I use Clear to be sure that the symbol has not a previous value defined, which would generate an assigment error. You can also use Evaluate instead With as @Andy answer: MapThread[(Clear[#1]; ...


8

I believe it is because Set has attribute HoldFirst. The FullForm of what you are attempting would look like... Set[ToExpression[data[[1,1]]],1] The ToExpression doesn't get a chance to evaluate before trying to assign the value. You can use Evaluate if you insist on doing it this way. Evaluate[ToExpression[data[[1, 1]]]] = data[[2, 1]]



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