Tag Info

New answers tagged replacement

4

As expressed in the comments, the Replace functions are not merely "syntactic sugar" for Map. The two are quite different. One primary difference is the order in which expressions are visited. See: How to perform a depth-first preorder traversal of an expression? Another is that Replace will go inside held expressions, while Map does not evaluate: Hold[1 ...

6

The general issue, as mentioned by xzczd, is that Manipulate only "notices" explicit visible parameters. This is because when you evaluate something like Manipulate[x, {x, 0, 1}] and start waggling the slider, you are not changing the value of the global symbol x, but instead a temporary symbol called something like x15. You can see this like so: ...

2

If this really can be expressed linearly, then expressing in Matrix form is going to be the easiest thing and the best from a computational perspective. For example, with your definitions: R6 = kf*(z[1] + mu*z[5]) - 2*z[6] + mu*kd^2*z[7] - kd^2*z[6]; R7 = kf*(z[1] + mu*z[6] - 2*z[7]) + kd^2*z[8] + kd^2*z[7]; R8 = kf*z[1] + (mu*z[7] - 2*z[8]) + kd^2*z[9] + ...

1

Does this do the job? poly=(kz py + py + kr kx) variables = Variables[poly]; reprule =DeleteCases[If[StringTake[ToString[#], 1] == "k", # -> Subscript[StringTake[ToString[#], 1], StringTake[ToString[#], {2, -1}]]] & /@ variables, Null] poly/.reprule This will take all the variables in an expression, work out if they are a combination of k ...

1

I don't know why it works that way, and you have the same issue with f[x_]={x}. You will find code here that defines new operators that work like x_?IntegerQ->{X} and f[x_]={x} except the new operators do what you expected.

12

I don't think there's a contradiction. The documentation for Rule says Symbols that occur as pattern names in lhs are treated as local to the rule. and as you've already pointed out, lhs->rhs evaluates rhs immediately In ClearAll@x; {1, 3.5} /. x_?IntegerQ -> {x} the rule's rhs evaluates first, but there's no global rule associated with x, ...

6

Perhaps it will seem clearer if the FullForm of the rule is examined. In the first example below, we see that the value of x is the RHS of the rule. So we should expect any integer to be replaced by {2}. x = 2; x_?IntegerQ -> {x} // FullForm Rule[PatternTest[Pattern[x, Blank[]], IntegerQ], List[2]] Here, if we clear x, the RHS of the rule is {x}. ...

0

Note that your first example is not contradictory with the immediate evaluation of the rhs. For example, if you try : ClearAll[x, y] x = y {1, 3.5} /. y_?IntegerQ -> {x} you get {{1}, 3.5}. This means that the rhs is really evaluated

1

I just got a little mistake with my code. The countP[l_list] should be : countP[l_List]

2

You can do this: a = {1, 2, 3} x^2 + y^2 + z^2 /. Rule @@@ Transpose[{{x, y, z}, a}] You might also use MapThread and the like. But anyways, make sure you understand what @@@ does.

6

This can be found in many answers, but I can't find the same question on this site or in the documentation. So: x^2+y^2+z^2 /.Thread[{x, y, z} -> a] (* 14 *)

3

ReplaceRepeated is probably what you want. Something like: G //. {{a, c} -> 1, {a, c, x} -> 2} //. is the shortcut for this. ReplaceAll probably also works: G /. {{a, c} -> 1, {a, c, x} -> 2}

1

Second try Perhaps this is the behavior you are interested in. If a definition does not match the original expression is returned, with Unevaluated intact. If however the definition is applied the Unevaluated is stripped. f[a_, b_] /; NumericQ[a] := {a, b} f[Unevaluated["inert"], 2] f[Unevaluated[2 + 2], 2] f[Unevaluated["inert"], 2] {4, 2} This ...

Top 50 recent answers are included