Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [pattern-matching]

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

21
votes
0answers
247 views

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

Bug introduced in 10.1 and persisting through 11.0 In making an attempt to answer this question, we ran into a version-dependent pattern-matching issue. An extended discussion in the comments ...
11
votes
0answers
111 views

Orderless breaks pattern match

I am perplexed by the effect of Orderless on pattern matching. Starting from a clean Kernel (i.e. after using Quit), I find the following expected match for ...
11
votes
0answers
155 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: ...
10
votes
0answers
139 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
0answers
136 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
141 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
309 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)$$ ...
6
votes
0answers
153 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 ...
6
votes
0answers
94 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
57 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 ...
4
votes
0answers
77 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
76 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
67 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
130 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 ...
4
votes
0answers
348 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 ...
3
votes
0answers
81 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
69 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
270 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
91 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
218 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
39 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
47 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
65 views

Automating generation of argument error messages

You can run into messages like DistributionFitTest["kk", "TestConclusion"] ...
2
votes
0answers
179 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
75 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
77 views

Why has FreeQ a quadratic complexity with high depth?

I am a little puzzled by the following test: ...
2
votes
0answers
82 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
27 views

Remove expression with matched braces from string

I am writing a LaTeX document where the custom command \chg{...} highlights changes in some color. Here I am, with a document of about 50 of those. Sure, one could ...
1
vote
0answers
46 views

Simplifying results of a vector function to be nested (SECOND UPDATE)

(Oddly enough, the title for another question that ended up dealing with an unrelated problem could have worked better here, but I didn't want to plagiarize.) Given a symbolic vector function ...
1
vote
0answers
89 views

Change of variables in a linear differential operator

Suppose I start with a differential operator in two variables x1 and x2 as follows: ...
1
vote
0answers
44 views

Position with Exception pattern matching

I have list {1, 2, 3, <|"a" -> 1|>, 4, 2}, and I would to find positions of all elements that match the case NOT INTEGER, namely ...
1
vote
0answers
80 views

Finding a compact formula to an analytical expression

I have an expression with 500 elements, each of which has a form similar to the next one: $\frac{Log\Big[\frac{f_{\{x_1,y_1\}}f_{\{x_2,y_2\}}}{f_{\{x_1,x_2\}}f_{\{y_1,y_2\}}}\Big]Log\Big[\frac{f_{\{...
1
vote
0answers
121 views

How does pattern matching in expression work?

I have been trying to understand how mathematica does pattern matching: {Sin[x] + 1} /. Sin -> Cos returns Cos[x] + 1 ...
1
vote
0answers
74 views

Apply a test function to variable arguments

I want to provide a function f[x_,y___] which applies a pattern matching test also on the optional parameters (e.g. request that they are vectors of numerical quantities). In case of one single ...
1
vote
0answers
162 views

Matching key-value pair without converting to List in KeyValueMap

I frequently use this method with KeyValueMap in order to match and refer to named parts (ie, "myKey", "myValue") EDIT: to refer to named parts in the function <...
1
vote
0answers
63 views

Expression Matching With MatchQ and HoldForm

I am learning to work with MatchQ for pattern matching in expressions. For instance: MatchQ[x + y, Plus[_,__]] returns True. However, ...
1
vote
0answers
115 views

How to “INJECT” with holding Evaluation, specifically into a WhenEvent

I am importing a set of differential and stochastic equations from an external package, and want to use Mathematica to analyse this. I am struggling building the set of equations that I can use with <...
1
vote
0answers
78 views

Unexpected result when trying to match against the arguments of Times

Consider the following question: ...
1
vote
0answers
434 views

Crosscorrelation for nonperiodic signals

I want to detect patterns (of an absorption spectrum which is nonperiodic) with Mathematica automaticly. I have found CorrelationFuncion but as far as I understand, ...
1
vote
0answers
102 views

Function behavior based on Head

I'm figuring how to implement a function behavior based on the Head of its argument. I mean ...
1
vote
0answers
56 views

Matching Root[…] objects with patterns, unexpected hidden argument

I needed to write patterns that could distinguish between arbitrary-function and polynomial forms of Root objects, for example, ...
0
votes
0answers
24 views

Define a function using pattern matching and repeated replacement to sum the elements of a list.(exercise in Paul Wellin's Book)

I am reading Paul Wellin "Essentials of Programming in Mathematica". I have a question about his answer to 4.1 Exercise 5. 4.1 Exercise 5: Define a function using pattern matching and repeated ...
0
votes
0answers
45 views

Simplify by defining new functions

if I have a formula like ...
0
votes
0answers
38 views

Unexpected behavior from RuleDelayed

RuleDelayed gives different results when list length is 3 than the results given when list length is 1 ,2 or 4. ...
0
votes
0answers
36 views

Limiting scope of StringPattern to first match for efficiency?

I have imported a Mathematica Notebook and assigned it to the variable file. In addition, I have gathered the strings corresponding to all Wolfram Language symbols:...
0
votes
0answers
50 views

Trouble with UpSet(Delayed) and IgnoringInactive

I'm having problems setting an upvalue for a symbol that is supposed to ignore Inactive: IgnoringInactive[f[g[x]]] ^:= match; ...
0
votes
0answers
24 views

Problems on MatchQ[-3*i[1], -x_*i[1]]

In my Mathematica 11.3. MatchQ[-3*i[1], -x_*i[1]] returns False. But MatchQ[-3*i[1], x_*i[1]] returns True. I don't ...
0
votes
0answers
51 views

Why does StringReplace wrap StringExpression around result?

The task is to separate a leading separator from subsequent text into a list. Why does StringReplace wrap StringExpression ...
0
votes
0answers
27 views

Defining functions with pattern as argument

I want to define a function such that f[18/17 h[y][z]] = 18/17 z for any function h[y][z]. I first tried with ...
0
votes
0answers
51 views

\[Prime]s not recognised as derivatives in some versions

This is more of a warning to others than a question, so feel free to delete if not appropriate, though I do have a question as well. I think there is a bug in some versions of Mathematica, in ...