# Tag Info

7

From the documentation for ReplaceAll: ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part, or on any of its subparts. What happens in your example is that the first rule matches the whole ...

6

Okay, this has been a bit of a headache. The short answer is that it is all working as expected. Now for too much detail to put into a comment. To begin, the original question was a bit of a blind, though certainly not by intent. When one considers what has to happen in a match with an Orderless function such as Times, it seems quite plausible that this ...

13

There seems to be a bug regarding this, in version 10.1.0 under Windows. For a first evaluation in a fresh kernel I get: expr = Product[Unique["a"], {i, 1, 25}]; rep = {x_ f[y_] /; FreeQ[x, y] -> 0}; expr /. rep // Short // AbsoluteTiming {9.64113*10^-6, a15 a16 a17 a18 a19 a20 << 14 >> a35 a36 a37 a38 a39} But the second time I ...

2

Using replacement Rules collapse[lst_?VectorQ] := lst //. { {b___, S, S, e___} :> {b, e}, {b___, S, T, S, T, S, T, e___} :> {b, e}} collapse[{S, T, S, S, T}] (* {S, T, T} *) collapse[{S, S, T, S, T, S, T, S, T, S, S, T}] (* {T, T} *)

2

Starting in version 10 you can use Inactivate and Activate. w = Inactivate[D[f[x, y], {x, 2}], D] wh = w /. f[x, y] -> x^2 h[x, y] Activate@wh Hope this helps.

4

A standard way to do this is to Hold it, and Release when needed: r = Hold[D[f[x, y], {x, 2}]] Release[r] Release[r /. f[x, y] -> x h[x, y]] Compare the above to D[x h[x, y], {x, 2}] it is the same.

10

As far as I understand it, once a subexpression gets replaced, it can't get replaced again. Try this: a*b /. {a -> a, a -> 1, b -> c} resulting in a*c The first replacement just replaces a with a, but since that expression has already been changed, it is ignored after that. So the Rule a -> 1 is only applied to other parts of the expression ...

2

Try this: eq1 /. n1 -> (n[#1, #2, #3] - I*n2[#1, #2, #3] &) // Simplify yielding and eq2 /. n1 -> (n[#1, #2, #3] - I*n2[#1, #2, #3] &) // Simplify returning Have fun!

3

Your equations are filled with partial derivatives, and so your replacement rules also need to take this into account. You were trying to do this, Derivative[0, 0, 1][n1][x, y, t] /. {n1[x, y, t] -> Re[n[x, y, t]]} (* Derivative[0, 0, 1][n1][x, y, t] *) Instead, you need to format your replacement rule in terms of a pattern, Derivative[0, 0, ...

4

Here's a quick helper function to do so. It first finds the position of each $1$ in the original matrix using Position; it then selects $n$ of these positions randomly using RandomSample and replaces their value with $-1$ using ReplacePart: Clear[flipsome] flipsome[matrix_?MatrixQ, n_Integer] := ReplacePart[matrix, RandomSample[Position[matrix, 1], n] ...

3

Another possibility is sol = Solve[s^2 + a*s + b == 0, s]//Flatten; Sum[C[i] Exp[sol[[i, 2]] t], {i, Length[sol]}] But, why not just DSolve[y''[t] + a y'[t] + b y[t] == 0, y[t], t][[1, 1, 2]] both of which give (* E^(1/2 (-a - Sqrt[a^2 - 4 b]) t) C[1] + E^(1/2 (-a + Sqrt[a^2 - 4 b]) t) C[2] *) Addendum A bit more compact is ...

3

There are many possibilities. You could do something like sols = Flatten@Module[{i=1}, Solve[s^2 + a*s + b == 0, s] /. s :> s[i++]]; (* {s[1] -> 1/2 (-a - Sqrt[a^2 - 4 b]), s[2] -> 1/2 (-a + Sqrt[a^2 - 4 b])} *) Alternatively, sols = Solve[s^2 + a*s + b == 0, s]; exprs = {c1 Exp[s t], c2 Exp[s t]}; Plus @@ MapThread[#1 /. #2 &, {exprs, ...

3

This was somewhat too long for a comment. Not sure it answers the question but it might give a way of thinking about the elusive handling of Condition that proves helpful. The behavior in question is fairly well documented (I think people know this, I just wanted to state it for the record). The general idea to RuleDelayed is that it fires on a match and ...

4

The results we see are due to a subtle interaction between the Flat attribute of Dot and the outermost-in, left-to-right scanning strategy employed by the pattern matcher. The expression a . b . c . d . a . b /. x_ . y_ . x_ :> p[x,y] could legitimately return three different solutions depending upon how we decide to group the . (Dot) operators, ...

5

Look at the FullForm of the expression FullForm[a.b.c.d.a.b] Dot[a,b,c,d,a,b] and the replacement pattern FullForm[x_ . y_ . x_] Dot[Pattern[x,Blank[]], Pattern[y,Blank[], Pattern[x,Blank[]]] Clearly both the expression and the pattern are enclosed by Dot[...]. So what we need is for: Dot[a,b,c,d,a,b] and Dot[x_, y_, x_] to match. The only ...

0

Actually, both FullForm@Hold[lhs /. lhs :> (rhs /; test)] and FullForm@Hold[lhs /. lhs :> rhs /; test] yield the same: Hold[Replace[lhs,RuleDelayed[lhs, Condition[rhs,test]]]] and this Replace evaluates as the documentation says it does. I think you misunderstand Condition as an If construct that evaluates to something on its own. But it ...

3

is there some way to take the call ReplaceAll[var, rules]and make it forget contexts for a minute? BeginPackage["Test"]; replace::usage = "Returns var/.rules."; print; Begin["Private"]; replace[rules_] := ReplaceAll[Symbol[\$Context <> "var"], rules] End[]; EndPackage[]; Not general, but works. I would expect that replace[rules_] := ...

3

You can make use of Formal Symbols. BeginPackage["Test"]; replace::usage = "Returns \[FormalV]/.rules."; Begin["Private"]; replace[rules_] := \[FormalV] /.rules End[]; EndPackage[]; Then in the notebook. replace[\[FormalV] -> 0] (* 0 *) Hope this helps.

2

Try replace[rules_] := ReplaceAll[Globalvar, rules] in your function. Then replace[var -> 0] replace[TestPrivate`var -> 0] yields 0 var as I think you'd like.

1

So there are too many things missing from your code to try it, but we can answer the question anyway. When you do your FindRoot, you'll get an answer like {rzin -> -1.44} This is a replacement rule. You can use it like rzin - 4 /. {rzin -> -1.44} (* -5.44 *) So what you do in your loop is soln = FindRoot[{.....}, {rzin, 2}]; Integrate[...., ...

4

I described this use in Is there a faster way to Map an Association? and it is documented in the Mathematica Quick Revision History for 10.0.2, though as noted in my self-answer it existed in 10.0.0 as well: Replace, ReplaceAll, and ReplacePart now work with Associations Why these things are not more directly documented is a mystery, and rather ...

0

Normal converts an Association to a list of rules. Normal[<|x -> 123, y -> 456|>] {x -> 123, y -> 456} but evidently will evaluate the argument first: Normal[<|x -> 123, x -> 456|>] {x -> 456}

2

There is a function called Replace All: expr = a + a^2 + a^3; expr /. a^n_ -> D[u[t, x], {x, n}] But since a is not on the form a^1, it won't be replaced. An extra rule can be added: expr /. {a^n_ -> D[u[t, x], {x, n}], a -> D[u[t, x], {x, 1}]} This page shows how to extract parts from a polynomial, in case you'd like to do something with ...

Top 50 recent answers are included