# Tag Info

11

Note that Times has the Flat and Orderless attributes, so the first replacement actually is performed. Both of the following ReplaceAll results are correct: -E^-y x /. {Times[-1, x] -> -x, -E^-y x -> -E^y x} (* -E^-y x *) where the whole expression is considered to match the first rule because of the Flat attribute, and the second rule is not ...

9

a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} ReplacePart can be used with explicit positions b = ReplacePart[a, {{2, 2}, {2, 3}, {3, 2}, {3, 3}} -> 30] or with positions matching a pattern b = ReplacePart[a, {(2 | 3), (2 | 3)} -> 30] {{1, 2, 3, 4}, {5, 30, 30, 8}, {9, 30, 30, 12}, {13, 14, 15, 16}} or b = ...

8

I think this may be what ilian is driving at, but I couldn't be sure in first answer. I thought that some elaboration would be helpful in any case. The behavior of flat and orderless functions in patterns is explained in tutorial/FlatAndOrderlessFunctions. While Orderless is significant here, I think it is the attribute Flat that one needs pay particular ...

8

The problem is that the function name f is substituted into Function (&) which is HoldAll. This means f[t] will not be evaluated until the Function is evaluated, such as in the OP's example problem[one][17]. So the trick is to evaluate the integrand before inserting it into the Function. Here is one way. one[x_] := 1 (*the argument*) problem[f_] := ...

4

It can be done with w.a.b.r + w.c.d.r /. Dot[FRONT_, AA__, BACK_] + Dot[FRONT_, BB__, BACK_] :> Dot[FRONT, Dot[AA] + Dot[BB], BACK] w.(a.b + c.d).r However, I like function argument destructuring, so I would probably write f[Dot[w_, a__, r_] + Dot[w_, b__, r_]] := w.(Dot[a] + Dot[b]).r f[w.a.b.r + w.c.d.r] w.(a.b + c.d).r

3

To answer the question as asked, we modify the code as follows: replacementRule = Plus[ Dot[FRONT__, AA__, BACK__] , Dot[FRONT__, BB__, BACK__] ] :> Dot[FRONT, Plus[Dot[AA], Dot[BB]], BACK]}] w.a.b.r + w.c.d.r /. replacementRule First, we have changed -> (Rule) to :> (RuleDelayed) so that when the expression is re-written, it will write it ...

3

The fastest way is usually an assignment on Part, and it generalizes well. (See my comments in Elegant operations on matrix rows and columns.) A = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; B = A; B[[2 ;; 3, 2 ;; 3]] = 30; B // MatrixForm \$\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 30 & 30 & 8 \\ 9 ...

3

The other answers have addressed the question well, but I'd like to try to illustrate one of the confusing points. Suppose we have the replacement operation x y z /. {x -> a, x y -> b, x y z -> c} Without a detailed understanding of the pattern matcher, it may be difficult to guess which rule will be applied. Knowing that the rules are applied ...

2

Please try: StringReplace[in, d1 : DigitCharacter .. ~~ "/" ~~ d2 : DigitCharacter .. :> "\!$$\*FractionBox[\(" ~~ d1 ~~ "$$, $$" ~~ d2 ~~ "$$]\)"]

2

Some insight can be obtained by using Trace. Trace[-E^(-I k) x /. {-x -> -x, -E^(-I k) x -> -E^(I k) x}]//InputForm The final few lines of the resulting lengthy expression are (* HoldForm[-(x/E^(I*k)) /. {-x -> -x, -(x/E^(I*k)) -> -(E^(I*k)*x)}], HoldForm[-x/E^(I*k)], HoldForm[-x/E^(I*k)], HoldForm[-(x/E^(I*k))] *) We see that the first ...

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