Hot answers tagged

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", ...


10

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


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: ...


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


5

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

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, ...


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} ...


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] ...


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}


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.


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

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, ...


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 ...


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 ...


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]}


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, #, ∞]] ...


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 ...



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