New answers tagged

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]] *)


0

That's not really an answer, the main point is left totally ununderstood, but it's an useful workaround. Suppose that xml data are: <test> <example>An example</example> <project>A project</project> <projectName>A project name</projectName> <projectDate>A project date</projectDate> </test> This ...


3

You should use Condition with StringMatchQ: Cases[dataXML, XMLElement[tag_String /; StringMatchQ[tag, "project*"], __], Infinity] because Cases doesn't support string patterns. As to why it is designed in such a way, I would cite Leonid Shifrin: I would say that the reason is dead simple <…>. Cases and DeleteCases work on parsed expressions, ...


10

StringCases["ABCaDEFbcABCdefDEF", "ABC" ~~ Shortest[a__] ~~ "DEF" :> a] (* Out: {"a", "def"} *)


3

Short answer You should use RuleDelayed: "foo" /. x_String :> Head[x] Othewise, the r.h.s of your rule is computed before the match, when x is still a symbol, not bound to anything. Longer answer Let's enumerate the main steps the main evaluator goes through, when evaluating your original code. The case of Rule The head ReplaceAll is evaluated ...


10

You can first compare two of the strings, get the longest common string, and then take the result and compare it to the third string. And keeping do it until the last string in the list will give you the longest common string for all the strings. This can be achieved using Fold, for example: ls = {"home/dir1/dir2/jmoasd.txt", ...


2

Quick note: I was not familiar with the term "reciprocal equations", so I looked it up. These are polynomial equations $f(x)=0$ such that $f(x)=\pm \ x^n f(1/x)$, which can be true if and only if the coefficients satisfy $a_i=\pm \ a_{n-i}$. These are interesting because they can be reduced to equations of a lower degree by substituting $t = x+1/x$ in the ...


0

The reason it doesn't work is because you are using the same multiplier for the x_ and x_^2 term (i.e.,b) and the same is true for constant and x_^3 term (i.e., a). In the second expression 3 x^3 + 7 x^2 - 7 x - 3 7 doesn't match -7 and 3 fails to match -3. If you only want the absolute value you could use MatchQ[3 x^3 + 13 x^2 + 13 x + 3, a_ x^3 + ...


0

The reason it doesn't work is because the pattern -b_ x_ doesn't match the expression - 7 x, since the latter one has a full form of Times[-7, x]. So you can do something like Cases[3 x^3 + 7 x^2 - 7 x - 3 == 0, a_ x_^3 + b_ x_^2 + c_ x_ + d_ /; c == -b && d == -a] (*{-3 - 7 x + 7 x^2 + 3 x^3}*)


2

Since you insist on using Cases: list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c}, {e,f}, {g}, {}, {Sin[a], Cos[b]}}; Cases[list, sub : {_, __} :> Total@sub] {3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]}


3

You may use Total and Select. Total /@ Select[Length@# >= 2 &]@list {3, 8, 9, 6, a + b + c, e + f, Cos[b] + Sin[a]} Total /@ list {3, 2, 8, 9, 6, a + b + c, e + f, g, 0, Cos[b] + Sin[a]} Hope this helps.


2

Applying a pattern to a list of expressions to transform the expressions doesn't make much sense. Perhaps there is a language issue here and you mean to ask what pure function you could map over your list to do the transformation. If so, kglr's answer in a comment is what you want. Here is a slightly simplified version: D[#, FirstCase[#, _Symbol, #, ∞]] ...


3

Here are two ways to do it taken from comments to the question. kglr and J.M. With[{expr = 3 Sqrt[2 x + 9] + Sqrt[a + 2] + 23/7 x^2 >= 0}, Cases[expr, Sqrt[something_] /; ! FreeQ[something, x], ∞]] kglr With[{expr = 3 Sqrt[2 x + 9] + Sqrt[a + 2] + 23/7 x^2 >= 0}, Cases[expr, Power[something : __, Rational[1, 2]] /; Not[FreeQ[something, x]], ...


3

Here's a fast way if you have general values, like @ciao's: SeedRandom[0]; sa = SparseArray[(# -> RandomChoice[Join[{x^2 + y^2, x^3}, Range@200]]) & /@ RandomInteger[{1, 2000}, {4000, 2}]]; Extract[sa["NonzeroPositions"], Position[sa["NonzeroValues"], x^2 + y^2, 1]] // RepeatedTiming {0.00015, {{22, 1644}, {165, 37}, {207, 910}, {332, ...


0

This is unlikely to be efficient, due to the Complement, and it's quite possible that the very first operation we do on the array "unpacks" it, but here's one possibility. Let's suppose we have a sample array, array = SparseArray@RandomInteger[{0, 5}, {10, 10}]; array // MatrixForm We set a new background for this SparseArray by doing array1 = ...


4

expr = (-1 + (E^(w[1, 0] - 1.55432 w[1, 1]) - E^(-w[1, 0] + 1.55432 w[1, 1]))^2 / (E^(w[1, 0] - 1.55432 w[1, 1]) + E^(-w[1, 0] + 1.55432 w[1, 1]))^2) expr /. Power[E, Plus[x_, y__]] :> Inactive[Times][Power[E, x], Power[E, y]]/. {Power[E, Times[c_, w[i_, j_], ___]] :> Power[T[i, j], Sign[c]], Power[E, w[i_, j_]] ...


4

This is very similar to the answer by Mr.Wizard, but avoids to introduce a named expression and therefore rewriting the function by using Condition: Replace[_, {_ /; IntegerQ[#] :> # (# - 1), _ :> #}] & /@ {1, 2, 3, x} {0, 2, 6, x} A more direct way is to use Switch: Switch[#, _Integer, # (# - 1), _, #] & /@ {1, 2, 3, x} ...


1

I came up with another way to do this: expr /. b[a_]*c_f :> Block[{$patternMatched}, With[{result = c /. (d[a] :> ($patternMatched = True; e))}, result /; $patternMatched]] Once the outer pattern matches (b[a_]*c_f), a boolean is set up $patternMatched. Then, if the inner pattern matches (d[a] appears inside c) then $patternMatched is ...


9

This works: Block[{Pick}, Thread@Pick[{1, 2}, {0, 1}, Except[0]]] This is due to the fact that Pick[1,0,Except[0]] also works and gives Sequence[]. It appears, the pattern is working first on the whole expression. As {0, 1} matches Except[0], it gives true and the corresponding element is the entire list {1, 2}. Another quick workaround is this: ...


3

Looks like Except doesn't work in Pick pattern matching. Here's a workaround. Pick[{1, 2, 3, 4}, {0, 1, 0, 2}, _?(# != 0 &)] {2, 4} Or similar to the form in Pick: Possible Issues (last example) list = {1, 2, 3, 4}; selector = {0, 1, 0, 2}; newSelector = Map[MatchQ[#, Except[0]] &, {0, 1, 0, 2}]; Pick[list, newSelector] {2, 4}



Top 50 recent answers are included