New answers tagged

6

The following code defines AndRuleDelayed to take a number of patterns followed by a (single) replacement and build a rule. The rule matches if and only if all patterns match, and the replacement can involve variables from any of the patterns. If the same variable name appears in two patterns, the combined pattern will only match if all occurrences of that ...


3

As a general case where you want to replace all the arguments, (b'[x y/z] + z b''[x y/z]) /. Derivative[n_][b_][x_] :> Derivative[n][b][z] z b''(z)+b'(z) No matter what was your previous argument, it will be replaced by z.


1

b'[xy/z] + z b''[xy/z] /. {xy/z -> z} gives me an output of b'[z]+z b''[z] Is that what you meant?


1

Activate@Replace[Inactive[f][3],3->1, Infinity] 1 or Replace[Hold@f[3], 3 -> 1, Infinity] // ReleaseHold 1


3

There are two related bugs. SubstituteSingleReplace calls RulesComplement to construct a list of rules and their opposites (so in your case b_ ** a_ :> -a ** b and -b_ ** a_ :> a ** b). Unfortunately, the function incorrectly uses Rule when given a RuleDelayed (my In[1] was loading the package): In[2]:= RulesComplement[b_ ** a_ :> -a ** b] ...


7

Put the underscore after the name of the Head instead of after the final square brackets, h[1, k[2, x]] /. {f_[n_, g_[m_, x] ] :> f[0, g[m + n, x]]} (* h[0, k[3, x]] *) f[3, g[5, x]] /. {f_[n_, g_[m_, x] ] :> f[0, g[m + n, x]]} (* f[0, g[8, x]] *)


9

I recently needed to do something like this. Here's a simplified version of what I came up with: DynamicModule[{elem = "Hydrogen"}, Panel[Row[{(ColorData["Atoms", "Panel"] /. HoldPattern["MouseClicked" :> rhs_] :> ("MouseClicked" :> (elem = CanonicalName[ElementData[Cases[Unevaluated[rhs], _Rule, ∞][[1, ...


4

I propose the use of ArrayComponents and Unitize: array = {{{{0, -a Sqrt[a^2 + c^2], -c Sqrt[a^2 + c^2]}, {-a Sqrt[a^2 + c^2], 0, 0}, {-c Sqrt[a^2 + c^2], 0, 0}}}, {{{0, 0, 0}, {0, -2 a^2, -2 a c}, {0, -2 a c, -2 c^2}}}, {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}}; array // ArrayComponents // Unitize {{{{0, 1, 1}, {1, 0, 0}, {1, 0, ...


3

mat = {{{{0, -a Sqrt[a^2 + c^2], -c Sqrt[a^2 + c^2]}, {-a Sqrt[a^2 + c^2], 0, 0}, {-c Sqrt[a^2 + c^2], 0, 0}}}, {{{0, 0, 0}, {0, -2 a^2, -2 a c}, {0, -2 a c, -2 c^2}}}, {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}} You can use f1 = Replace[#, Except[0 | _List] -> 1, Infinity] &; f2 = SparseArray[SparseArray[#]["NonzeroPositions"] ...


5

The problem is that g[x1, x2] returns 0 with your definition and this is evaluated before the substitutions are applied. You could define g2[x1_?NumericQ, x2_?NumericQ] := Count[{x1, x2}, _?Negative] to hold off evaluation until values has been given to x1 and x2. Then g2[x1, x2] just returns g2[x1, x2] and g2[x1, x2] /. {x1 -> 1, x2 -> -2} return 1 ...


1

Using upper case letters for the beginning of a symbol is frowned upon so I am going to replace A and B with u and v. So your equilibrium system becomes: dA = c u + b v - a u dB = d v + a u - b v Now when we replace a with 1 and b with 2 we get dA /. {a -> 1, b -> 2} (* -u + c u + 2 v *) dB /. {a -> 1, b -> 2} (* u - 2 v + d v *) Below is ...


1

One way to debug what's going on is to limit how many times FixedPointList can iterate (which the OP shows in another form). FixedPointList[# /. {{s___List, w_String, x_String, r___} /; StringTake[w, 1] == StringTake[x, 1] :> {s, {w, x}, r}, {s___List, w_String, x_String, r___} :> {s, x, r}(*, {s___List}\[RuleDelayed]{s}*)} &, ...


2

There are many ways to accomplish what you want to do in Mathematica. This answer will discus just one -- defining prc as a function of v. Function is one of core concepts of Mathematica, so this approach has wide application beyond this specific case. v = Table[i, {i, 1, 6.8, 0.19}]; prc[v_] := (v^2 + 1)/(v^2 - 5) Then to get the values of prc over v ...


0

I am not sure if I still understand your question properly. I will delete it later if it is completely irrelevant. Let there be a function tab[x_] := # x & /@ Range[5] which gives an list for a variable tab[g] {g, 2 g, 3 g, 4 g, 5 g} Now I define a new function lin[f__] := Variables[f][[1]] -> f Which gives you the result as rule ...



Top 50 recent answers are included