New answers tagged

5

Well, as @WReach has already explained the process of Thread clearly, I think I'll simply give a new function to solve this problem and make both examples work. After knowing the behaviour of Pick, a simple change can solve this problem------Whenever the two parts are not of the same shape, simply pass it and go to the next. The code is shown below: ...


25

This is not a bug. It is a consequence of the manner in which Pick scans its arguments. The Pick Process The documentation for Pick calls its arguments list, sel and patt. Pick scans the list and sel expressions lock-step in a top-down, left-to-right fashion. It proceeds roughly as follows: The entire sel expression is checked to see if it matches ...


5

This is a form of memoization by dynamically defining a Pattern with the results of the executed Expression as clarified by @ciao. The standard form is documented in Functions that remember values they have found and looks like: f[x_]:=f[x]=rhs So as an example: f[x_] := f[x] = f[x - 1] + f[x - 2] behaves the same as i:f[x_] := i = f[x - 1] + f[x - ...


1

For (6) you need to specify two patterns, one for each ordering: words = {"abcd", "aydx", "axdy", "adx"}; Select[ words, StringContainsQ[("x" ~~ ___ ~~ "y") | ("y" ~~ ___ ~~ "x")] ] (* Out: {"aydx", "axdy"} *) StringContainsQ, StringStartsQ and StringEndQ can be used for some of your patterns and may either give a performance boost or at the very ...


0

As of version 10.1 there is TextWords that will achieve this for you easily TextWords[",,temp sp.a tiral - dump NV-A rambo.6833. 16,rgcht"] (*{"temp", "sp.a", "tiral", "dump", "NV-A", "rambo.6833", "16,rgcht"}*) Note that the implementation of the function is available to you with ??TextWords It relies on a bunch of stuff from the ...


2

Do you mean: ClearAll[A] Attributes@A = HoldAll; A[f[n_]] := A[f[n]] = Module[{d}, d = Numerator@f[n - 1]] I've removed the redundant Return. Module is also irrelevant here actually. Anyway, I suppose you need Module in your real problem so don't take it away. Or you need f to be arbitrary, too? Then: ClearAll[A] Attributes@A = HoldAll; A[f_[n_]] := A[f[...


3

I don't think that StringMatchQ["x", Except[{"*"}]] works as expected as well as StringMatchQ["x", Except["*"]]: StringMatchQ["x", Except[{"*"}]] StringMatchQ["x", Except["*"]] True True The string pattern "*" is an abbreviated string pattern consisted from the only metacharacter * which corresponds to zero or more characters according to the first ...


1

Thank you everyone! Eventually I found the solution at Dropping Higher Order Terms in symbolic evaluation. The one using Normal[Series[expr /. Thread[vars -> t*vars], {t, 0, 10}]] /. t -> 1 My final code (for getting the components of the Riemann tensor of a certain weak gravitational field) became. Needs["GREATER2`"]; X = {t, x, y, w}; ds2 = -(1 ...


12

For large lists, this should be snappy: Pick[Range@Length@dat, StringTake[dat, UpTo@4], "star"] and actually, taking advantage of listability, Pick[Range@Length@dat, StringMatchQ[dat, "star*"]] is a bit faster it seems...


10

Position[dat, _String?(StringMatchQ[#, "star" ~~ ___] &)] or Position[dat, _String?(StringMatchQ[#, "star*"] &)] or Position[dat, _?(StringMatchQ[#, "star*"] &), Heads -> False] or Position[StringMatchQ[dat, "star*"], True] (*thanks: @TomD *) {{1}, {7}, {13}, {19}} Alternatively, Pick[Range@Length@dat, StringMatchQ[#, "star*"] ...


3

If you Trace the evaluation sequence of this expression, you get the following: Trace@MatchQ[x y, (x | y) (x | y)] (* {{(x|y) (x|y), (x|y)^2}, MatchQ[x y, (x|y)^2], False} *) This shows that (x|y) (x|y) gets evaluated to (x|y)^2 before the pattern matching occurs, and x y doesn't match (x|y)^2, although x^2 and y^2 will: MatchQ[#, (x | y) (x | y)] & /...


1

I guess what you want is to create a function that can match something like $x x$ or $x y$ in a simple way. So, here's my answer and hope this can help you: f = Function[{t, l}, With[{pat = Alternatives @@ l},MatchQ[Unevaluated@t, HoldPattern[pat pat]]],HoldFirst]; f[x x, {x, y, z}] f[x z, {x, y, z}] Also, I think you would love to use something like Map:...


6

It seems like a glitch in the pattern matcher, occuring whenever named patterns appear in a head and PatternSequence appears at level 1 in the body. Null /. f_[PatternSequence[x, y]] -> 0 Pattern::patvar : First element in pattern Pattern[1, _] is not a valid pattern name. >> When there are multiple named patterns in the head there are additional ...


0

In this case, the use of PatternSequence seems to be unnecessary. I think that the following achieves the desired result: rule2 = HoldPattern[func[a | b, c_][({x_} | x_), z_]] :> {c, x, z} func[a, 2][1, 3] /. rule2 func[a, 2][{1}, 3] /. rule2 In response to the edited question, I think the following works rule3 = func[a | b, c_][{x__} | (x__)] :> {...


4

Get the relation graph of your example graph = SimpleGraph[RelationGraph[StringStartsQ, example], VertexLabels -> "Name"] Get the end of the vertex in every weaky connected graph First@*TopologicalSort/@WeaklyConnectedGraphComponents[graph] {"Clothing/Accessories/Belts", "Watches/Accessories/Pocket Watch Chains"} Hope this can help. :)


3

EDIT: My original answer, while quite a bit quicker than the current answer, dropped some words that should not be dropped, as does the other current answer, e.g.. {"pa","paperweight"} fails on both (unless that is the intent of the OP, which I doubt). This led me to the following, which does not suffer from the problem and is also quite a bit quicker yet: ...


4

Your solution doesn't seem to work when there are duplicates. Anyway, could you test the performance of the following method? example = RandomChoice[{"Clothing", "Clothing/Accessories", "Clothing/Accessories/Belts", "Watch", "Watches/Accessories", "Watches/Accessories/Pocket Watch Chains" }, 15000 ]; Split[Sort@example, StringStartsQ[#2, #] &...


0

I like this question and I know I have played with the concept before, either for a question here or for my own interest. (I cannot recall.) I also like your method but I am immediately struck by the fact that it will not work with held expressions, for example: Hold[1 + 2] /. AndRuleDelayed[a_ + b_, c_?NumericQ, {a, b, c}] Hold[1 + 2] /. (a_ + b_) :> {...


5

General The real problem with objects is that sooner or later you really start to want some of the fields mutable. And once you want it, you really open a can of worms. Also, for methods, you will find very soon that you want some nice features such as pattern-based method overloading and the syntax similar to how we use functions in Mathematica. I do have ...


10

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



Top 50 recent answers are included