We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [pattern-matching]

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

56 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
21
votes
0answers
269 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, ...
12
votes
0answers
129 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 ...
11
votes
0answers
160 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
154 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
145 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
144 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
311 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
67 views

Repeated string pattern: difference between the FrontEnd and WolframScript?

This code works fine in a string based interface like WolframScript or Mathematica Kernel. Run this through a string based interface ...
6
votes
0answers
159 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
97 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
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
352 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
69 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
83 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
85 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
68 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
132 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
0answers
85 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
73 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
339 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
96 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
222 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
25 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
72 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
40 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
49 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
76 views

Automating generation of argument error messages

You can run into messages like DistributionFitTest["kk", "TestConclusion"] ...
2
votes
0answers
213 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
76 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
78 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
34 views

Replacing factors that are only dependent on a single specific variable

I have expressions with terms that are products of factors. Some of the factors are expressions that depend on a single specific variable. I would like to replace those factors with constants and ...
1
vote
0answers
64 views

Graph of a list of rules with coefficient

I am trying to transform an expression to a list of rules and after that plot its graph. The expression has the following form ...
1
vote
0answers
30 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
1answer
93 views

Replacing one of the integrals in the multiple integral with an analytical solution

Problem consist in simplifying complicated multiple numerical integral, where I know analytical solution for a subset of them. Integrand is of the form ...
1
vote
0answers
52 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
95 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
46 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
85 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
127 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
88 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
175 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
74 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
118 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
80 views

Unexpected result when trying to match against the arguments of Times

Consider the following question: ...
1
vote
0answers
462 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
103 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
58 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
35 views

making replacements when patterns don't match exactly

I want to simplified some mathematical expressions, and they are similar to ...
0
votes
0answers
22 views

Check whether the argument of a function is a matrix with numerical values

I know that MatrixQ[expr,NumericQ] tells whether expr is a matrix with numerical values or not. How do I implement such a ...