# Tag Info

4

A few additional alternatives to inject patterns on the rhs: list = {a, b, c, d, e, f, g, h, i, j}; Replace[list, a_ :> (x_ /. x -> a), 1] {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} Replace[list, a_ :> (Pattern[#, _] &@a), 1] {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} Activate @ Replace[list, a_ :> Inactive[Pattern][a, _], 1] {a_, b_,...

8

Here is one way: Pattern[#, Blank[]] & /@ {a, b, c, d, e, f, g, h, i, j} (* {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} *) An inspection of the FullForm of a_ reveals why this works: a_ // FullForm (* Pattern[a, Blank[]] *) We can abbreviate slightly if we realize that the InputForm of Blank[] is _: Pattern[#, _] & /@ {a, b, c, d, e, f, g, h, i, j} ...

1

You can also select the ones with identical first and third entries, then use complement to get the rest: list = {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}}; {sel = Select[list, #[] == #[] &], Complement[list, sel]} {{{0, 0, 0}, {0, 1, 0}, {1, 0, 1}, {1, 1, 1}}, {{0, 0, 1}, {0, 1, 1}, {1, 0, 0}, {1, 1, 0}}}

5

GatherBy[list, #[[{1, 3}]] &] {{{0, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {0, 1, 1}}, {{1, 0, 0}, {1, 1, 0}}, {{1, 0, 1}, {1, 1, 1}}} Alternatively, Gather[list, #[[{1, 3}]] == #2[[{1, 3}]] &] same result or Values @ GroupBy[list, #[[{1, 3}]] &] same result

6

You can use Optional in your pattern, which has the short form of .: arr = Table[i*j a^i b^j, {i, 1, 5}, {j, 1, 5}]; arr /. a^i_. b^j_. :> RuleCondition[0, i + j > 5] //TeXForm \$\left( \begin{array}{ccccc} a b & 2 a b^2 & 3 a b^3 & 4 a b^4 & 0 \\ 2 a^2 b & 4 a^2 b^2 & 6 a^2 b^3 & 0 & 0 \\ 3 a^3 b & 6 a^3 b^2 &...

5

poly = x^5 + 6 x^4 + 2 x^3 - 8 x^2 + x + 10; With Cases Cases[poly, Alternatives @@ (a_. x^{_, 1, 0}) :> a] {10, 1, -8, 2, 6, 1} and to isolate the constant with DeleteCases DeleteCases[poly, Alternatives @@ (a_. x^{_, 1})] 10 Or with CoefficientList CoefficientList[poly, x] {10, 1, -8, 2, 6, 1} and the constant with Coefficient[poly, x, ...

4

Given that it's linear, I would probably do it this way: CoefficientList[ y'''' + y'' + x^2 y' - 6 y + 8 Cos[x] /. Derivative[n_][y] :> y^(n + 1), {y}] (* {8 Cos[x], -6, x^2, 1, 0, 1} *) Note that the output here has the coefficient of y''', which the desired output in the OP omits. I see every reason to include it, though. More straightforward: ...

5

One part of the problem has already been mentioned by @C.E. in his answer: Since Association is seen as atomic by the pattern matcher, no insertion of matched expressions happens within it. The reason this is a problem at all is that your postproc is not actually evaluated after the matches have been identified, but before. To prevent evaluation of the ...

3

A possible workaround: keys = ToString /@ syms; postproc[x_, h_: keys] := Thread[h -> x] Association @@@ Cases[exprs, # -> postproc[syms], Infinity] & /@ patts {{<|"x1" -> 14, "x2" -> x2, "y" -> y|>}, {<|"x1" -> x1, "x2" -> 12, "y" -> 3|>, <|"x1" -> x1, "x2" -> 2, "y" -> 8|>}}

2

("146d0" A + "-594d0" B) /. s_String?(StringStartsQ["-"]) :> -StringDrop[s, 1] (* "146d0" A - "594d0" B *)

-1

This pattern seems to be working ("146d0" A + "-594d0" B) //. Times[A_String, B_] :> -StringDelete[ToString[-ToExpression[A]], " "] B /; Negative[First@ToExpression[A]]

2

The second syntax example at the top of the documentation of Condition indicates to me that the test should be placed after the RuleDelayed. When you do that, it works: SparseArray[{{i_, j_} :> 1 /; IntegerDigits[i - 1, 2, 2][] == 0}, {4, 4}] // Normal (* Out: {{1, 1, 1, 1}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}} *) @glS is on to something odd ...

5

Update. The bug is confirmed by the tech support: [CASE:4352435]. For ReplaceList[{2, 3}, {u_, Except[1 | u_] ..} :> True] Mathematica 8.0.4 prints error: Except::named: Named pattern variables are not allowed in the first argument of Except[1|u_]. and returns {}, but versions 11.3 and 12.0 both return {True} without any messages. What seems to ...

6

Summary The exhibited behaviour is correct. In the last example, the expression under test is an inert expression of the form Derivative[...] * Derivative[...]. But the pattern evaluates to the form Derivative[...]^2. These two forms do not match. The fix is to use HoldPattern to prevent the pattern from evaluating. When doing this kind of work, it ...

3

In the same manner as yarchik's answer, but leveraging more symmetries. Also using ps as defined in the question. t = {2, 6}; u = {3, 6, 9}; eightfoldStarts = Thread /@ {{1, t}, {1, 5, {2, 3, 6}}, {1, 5, 9, t}, {2, u}, {2, 5, u}, {2, 5, 8, u}, {5, 1, t}, {5, 1, 9, t}, {5, 2, u}, {5, 2, 8, u}} // Catenate; finish[s : {__Integer}] := Join[...

6

It is possible to speed up your existing code even without compilation. The idea is to count not all configurations, but rather the topologically different ones. There are 3 topologically distinct possibilities to start a path Corner: 1, 3, 7, 9 Edge: 2, 4, 6, 8 Center: 5 Thus, it is sufficient to count the number of paths starting from 1 (n), ...

Top 50 recent answers are included