# Tag Info

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

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

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

7

Per request by the Pope: Think about it - what are the cases that are tested? It's the elements of the list. Try, e.g., Cases[list, x_ /; (Print@x; True)] and see what you get (it will show that the test is done on an element-by-element basis). Cases[Split@list, {Repeated[a | b, {2}]}] will spit out a list of matches, MemberQ[Split@list, {Repeated[a | b,...

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

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

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

5

Why part is explained nicely in @ciao's answer. This post deals with the part: I want to detect whether this list has repeated element a,a or b,b You can get what you expected to get from Cases[...] using the new-in-v-10 function SequenceCases: list={a,b,a,b,b,b,a,b,a,a,b}; SequenceCases[list,{Repeated[x_,{2}]}] {{b, b}, {a, a}} Or, define a ...

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

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

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

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

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)] & /...

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

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

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

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

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