Questions tagged [pattern-matching]

Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.

92 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
22
votes
0answers
306 views

What has changed in pattern matching functions with the Orderless attribute?

Bug introduced in 10.1 and fixed in 11.0(.1) In making an attempt to answer this question, we ran into a version-dependent pattern-matching issue. An extended discussion in the comments transpired, ...
13
votes
0answers
168 views

Alternatives and the Flat attribute

When a head has the attribute Flat then the pattern matcher is able to pick out a sublist of its arguments, even though the structure doesn't match: ...
12
votes
0answers
146 views

Orderless breaks pattern match

Bug introduced in 10.3 or earlier and persisting through 12.0.0 10.0.2 is not affected. I am perplexed by the effect of Orderless on pattern matching. Starting ...
10
votes
0answers
188 views

PatternSequence, Optional and _Symbol

Bug introduced in 9.0 or earlier, partially fixed in 10.1 and persisting through 11.3 [CASE: 4075992] confirmed Note: See (72290) for the fixed part of the bug. Consider the following example: <...
9
votes
1answer
224 views

Can we change a pattern once we throw them into pattern matcher?

Main Problem Mathematica's pattern matching mechanism seems fairly well in most cases but in some situations, I would like to ask for a bit more freedom. We can easily call for extra evaluation while ...
9
votes
0answers
155 views

Strange result of DeleteCases in an association

Bug introduced in 10.1.0 and fixed in 10.3.1 The function DeleteCases can be used for associations, also with level specification. Here is an example where some ...
8
votes
0answers
159 views

KeyValuePattern specificity in DownValues

Bug introduced in 10.4.0 and persisting through 11.3.0 CASE:3824590 [...] Developers are discussing this behavior. [...] I will let you know when I have definite information on this. [...] ...
8
votes
0answers
312 views

Convert logical relational expression to / from disjunctive and conjunctive forms?

I would like to convert logical relational expressions in disjunctive form, e.g., $$(x \lt -1) \lor (-1 \lt x \lt +1) \lor (x \gt +1)$$ into conjunctive form, e.g., $$(x \ne -1) \land (x \ne +1)$$ ...
7
votes
0answers
65 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
6
votes
0answers
100 views

Exceeding recursion limit with no obvious recursion: Optional pattern-matching causing nonlocal behavior

So, I've come across something strange, and wanted to get a deeper understanding of what was going on. Consider the following code: ...
6
votes
0answers
106 views

Difference between the infinite rewriting behavior of evaluation and ReplaceRepeated

In the documentation tutorial/Evaluation it's said that: Every time the expression changes, the Wolfram Language effectively starts the evaluation sequence over ...
6
votes
0answers
105 views

Examples of the single dot form of Optional?

I've known of the single dot form of Optional for a while, but never had occasion to actually use it: Are there any good examples of use cases for it other than in relation to ...
5
votes
0answers
66 views

Can't seem to match multiple KeyValuePatterns in one pattern

Let's define a couple patterns that we can use to match Associations. ...
5
votes
0answers
58 views

How to match -1 at arbitrary precision?

Suppose a want a pattern to match: -1 -1. -1.`42 (or any other value for the precision) with a pattern. What would be the best pattern to use? I guess one could ...
5
votes
0answers
361 views

Syntax error for optional pattern, but only in debugger

This expression (explained here by Leonid) evaluates fine unless Evaluation->Debugger is enabled: h[x : (_Integer) : 1] := x Syntax::sntxf: "x:" cannot be ...
4
votes
0answers
85 views

Why does Shortest not work inside SequencePosition?

Why does Shortest not work in a SequencePosition call? The following two commands return the same answer: ...
4
votes
0answers
91 views

`Optional` with non-empty list requires PatternTest/Condition?

Arbitrary lists are easy to use with Optional: a[0]~MatchQ~a[0, Optional[x_List, {}]] a[0]~MatchQ~a[0, Optional[x:{___}, {}]] ...
4
votes
0answers
92 views

Pattern match Shortest BlankNullSequence in SequencePosition

Apologies if this has an answer somewhere but I can't find it. From the examples for SequencePosition we are shown that ...
4
votes
0answers
73 views

How to get f[x] to match with f[c_., x] when OneIdentity doesn't match

After setting: SetAttributes[f, OneIdentity] Default[f] = 1; We have the following successful matches: ...
4
votes
0answers
134 views

Why the flat f[x] is treated as ghostHead_[f[x]]?

While reading this answer I feel confused. If we do not care any specific detail of the original question, then ybeltukov's answer essentially makes use of the following fact ...
3
votes
2answers
84 views

Built-in functions on the LHS won't pattern-match

I have this pattern-matching problem: F[X_,myminus[X_]]:=0 F[1,myminus[1]] Works. ...
3
votes
0answers
45 views

Preventing $-renaming of pattern names in lexically-scoped constructs

When you use a lexically-scoped construct like With or Function to perform replacements in an expression, Mathematica might ...
3
votes
0answers
64 views

What's the time / space complexity of pattern matching?

For example, in ReplaceAll[expression, pattern], what are the main algorithms used internally? How does the asymptotic time / space complexity scale with the length ...
3
votes
0answers
32 views

Unexpected behaviour with Coefficient

If I run expr = (CF G[0, zb])/(1 - zb) + (CF zb^2 G[0, zb])/(1 - zb) + (2 CF G[1, zb])/(1 - zb) + (2 CF zb^2 G[1, zb])/(1 - zb); Coefficient[expr, zb, -1] in ...
3
votes
0answers
93 views

Select certain term containing some patterns and confusing pattern Alternatives in FreeQ

I am considering a function termsContain[func_,list_] which select in an expression func the terms containing the ones in the ...
3
votes
0answers
76 views

Match Polish letters

I am quite confused about the following cases, in Mathematica 9.0.1.0: LetterQ["ł"] StringMatchQ["ł", WordCharacter ..] The first case gives ...
3
votes
0answers
389 views

Generating a list of random numbers that follows a pattern

I'm a beginner to Mathematica. I'm writing a code to design a 4-bar linkage mechanism that traces a path in 2D space. I got the modelling done right, now I'm writing my own implementation of a genetic ...
3
votes
0answers
103 views

Is pattern matching just systematic guessing and checking?

How does the pattern matcher work? how does it match this example? f[3, h[4]] /. f[x_, z_[y_]] :> {x, y, z} My guess is that the pattern matcher collects all ...
3
votes
0answers
233 views

I am missing a function like HoldPattern

Temporary message: I am planning to edit this question, making the examples less confusing and by moving the things that are confusing to a separate question For any confusion about expressions that ...
2
votes
0answers
50 views

Simple questions about Pattern and MatchQ

1st Question : Note that ...
2
votes
0answers
37 views

What sorts of expressions can be Solve'd for?

We can Solve for expressions that match _Symbol, as in Solve[x^2+1 == 0, x]—but we can also ...
2
votes
0answers
38 views

Pattern with alternative heads

This works: ...
2
votes
0answers
58 views

How to produce an error message for each argument?

I want to print a helpful message for every wrong input. How do you compactly implement a function with custom pattern constraints, default values and error messages for each argument separately? For ...
2
votes
0answers
34 views

recursively find StringCases

Can I recursively extract StringCases[] that match a specific pattern? Here is a minimalist example (not my actual problem): input ...
2
votes
0answers
60 views

How does Mathematica choose which function definition to use?

I was wondering what the process of matching function arguments arguments against different function definitions actually looks like for Mathematica. (Just to be clear, I'm not simply looking for an ...
2
votes
0answers
46 views

KeyUnion by equivalent patterns

I have associations whose keys are patterns. For example, ass1 = <|a_ -> 1, f[] -> 3|>; ass2 = <|b_ -> 2|>; I would like to construct the ...
2
votes
0answers
47 views

How to find consecutive things in nested expressions?

To find things that are consecutive you can use SequenceCases: ...
2
votes
0answers
42 views

Pattern matching and NonCommutativeMultiply

I am trying to define some functions using NonCommutativeMultiply and I am experiencing some strange behavior. Consider the following expression ...
2
votes
0answers
107 views

Lambda calculus reduction in Mathematica

Are there any built-in functions for reducing lambda terms in Mathematica? If not, how might one do it? I've heard of Function, which is good for $\beta$-reduction, ...
2
votes
0answers
41 views

Match an expression that has a descendant pattern

I have a complicated expression in which I have another expression p which, when it’s present, I want to match the outer expression and all of its children. I was hoping for something like ...
2
votes
0answers
83 views

Fast way to check if an expression matches any of the rules in a list?

I have a function defined roughly like so. This is a minimum recreation. foo[expr_, rules: {__Rule}] := "some fancy result" /; AnyRuleMatchesQ[expr, rules]; What ...
2
votes
0answers
91 views

Automating generation of argument error messages

You can run into messages like DistributionFitTest["kk", "TestConclusion"] ...
2
votes
0answers
296 views

Replacement rule for summation with Kronecker delta

I want to perform some symbolic computations in index notation, without explicit reference to sums, so that in expressions of the type f[i,j]g[j,k] summation over <...
2
votes
0answers
77 views

Good coding for integral substituions

This question is aimed at improving both my coding and my understanding of MMa's capabilities. A lot of my code is still hacks and workarounds for things I don't know how to do properly. I want to ...
2
votes
0answers
79 views

Why has FreeQ a quadratic complexity with high depth?

I am a little puzzled by the following test: ...
2
votes
0answers
87 views

Hashing by Equals instead of SameQ

I have some numerical code involving complex numbers representing points on a plane. Some of these are equal but not identical, ie ...
1
vote
0answers
48 views

"Compile-time" patterns

If I define a function like f[x : {_, _Integer}] := (* function body *) then, as I understand it, every time f[expr] is ...
1
vote
0answers
69 views

Alternatives not working with Sequence[]?

In the following piece of code I've specified two possibilities to match via Alternatives, but only one of them seems to be considered: ...
1
vote
0answers
81 views

The danger of pattern matching?

I'm reading the book Mathematica® programming: an advanced introduction by Leonid Shifrin and there is a very nice evaluation. Here is: This, plus a large number of quite generic and efficient ...