Questions tagged [pattern-matching]

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

18
votes
2answers
4k views

Should I use \\\“ or \” to match " in RegularExpression?

Both of the following return True: ...
3
votes
3answers
89 views

Find element in a list matching a pattern (using wildcards)

Somehow, I did not manage to find anything related to that. Suppose, in my Global list of variables, I would like to make a search such that I obtain all the ...
4
votes
0answers
61 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: ...
2
votes
1answer
29 views

Setting default function values for particular option values

I have a function with some optional arguments. How do I assign a value for the function when one of the optional arguments takes a particular value, regardless of any other argument values?
1
vote
2answers
62 views

How to use KeyValuePattern with individual replacement rules?

What's the syntax to use individual replacement rules for key-value patterns? This returns only the first match: ...
0
votes
1answer
42 views

Pattern ReplaceAll warning

Why does the following code give me a warning, but then still work? a1={{1,2},{3,1}} a2={"foo","bar"} a1/.{x_Integer,y_Integer}->{x,a2[[y]]} After a "Part: ...
1
vote
1answer
54 views

Function returning a pattern with condition - how to prevent condition to evaluate prematurely?

I figured it would be convenient to have a function that returns a pattern, that match equally nested brackets, of the specified type. Such a pattern is easy to create, but I get some error messages, ...
4
votes
1answer
50 views

Pattern matching repeated arguments of Times

I am trying to match against products of elements with a specific pattern. Since a product of symbols A B C is just Times[A,B,C],...
0
votes
1answer
101 views

`Plus` wrongly applies `OneIdentity` to eradicate itself in `Plus[Power[_, 2] ..]`, unreproducible

I was trying to match some polynomials, but this is weird: ...
5
votes
1answer
105 views

DeleteCases using two lists but with partial match?

I searched and tried different solutions on SE but nothing worked. I have two lists and would like to remove items from list1 that have a PARTIAL match with items ...
15
votes
4answers
472 views

Make mathematica treat $e_i^2$ as numeric

With NumericQ[symbol] = True, I can declare that a symbol is numeric. I want the expressions matching: $$e_{\text{i$\_$}?\text{IntegerQ}}^2$$ to be treated as ...
3
votes
1answer
128 views

Pattern test: _Integer vs _?IntegerQ [closed]

While checking out the different pattern replacement methods in this question, I have a query regarding the basic difference between the following two replacements: ...
1
vote
1answer
265 views

Replacement using pattern matching?

Starting with the expression eq1 = 5 Exp[1 + 2 x + 3 x^2 + 4 x^3]; I want to implement the following transformation: ...
10
votes
4answers
285 views

Pattern matching: Times[a_] vs Times[a__]

I don't understand why MatchQ[ HoldComplete[Times[3, 2, 2]], HoldComplete[Times[a_]] ] is returning True. I thought that ...
0
votes
3answers
36 views

Select elements from nested list where one element is largest

I'm trying to speed up the following, but implementing efficient pattern matching isn't intuitive to me yet, even with lots of examples. I have a list of ((x1,y1,z1), (x2,y2,z2)) pairs, and I want to ...
2
votes
1answer
64 views

Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...
31
votes
2answers
2k views

How to Combine Pattern Constraints and Default Values for Function Arguments?

EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are ...
3
votes
2answers
69 views

Using DeleteCases with a defined function with two arguments as a pattern

I am trying to manipulate some large datasets in Mathematica. I have plotted the data, and I would like to be able to easily remove points from the dataset, and it's fairly easy to identify the x-...
21
votes
0answers
264 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
126 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 ...
3
votes
3answers
68 views

Composition of polymorphic functions

Using pattern matching one can easily create polymorphic functions in Mathematica f[{a_, b_}] := {a^2, b^2}; f[c_Integer] := c^4; Now I want to create another ...
11
votes
1answer
168 views

Problem with DeleteCases

I'm about to report this as a bug but wondered what you would make of it. Works as expected in version 7. ...
3
votes
2answers
128 views

Can pattern assumptions be made reliable?

It is possible to use patterns in assumptions such as f[_]>0. This does not seem to be explained in the documentation but is for instance pointed out in Is it ...
3
votes
1answer
46 views

PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...
1
vote
1answer
69 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 ...
0
votes
2answers
194 views

Construct a pattern with an optional head

How can I construct a pattern that matches both f and h[f] without using the symbol f more ...
0
votes
1answer
30 views

Notation package: StringMemberQ in conditional notation definition

Suppose that I have a list, allSpecies, that contain a set of symbols, each of which has been assigned a new notation using Notation[...]. As an example, the internal representation is C60s, and the ...
2
votes
1answer
70 views

How to trace value assigned to local variable(variables in Module)?

Bug introduced in v10.1 or earlier and persists through v11.3 CASE:4240757 Here is my code: ...
9
votes
1answer
391 views

Does Mathematica use first order or second order order unification?

When Mathematica is pattern-matching expressions, does it use first order or second order unification? Just to clarify the difference between first order and second order unification: In second order ...
51
votes
1answer
2k views

How to generally match, unify and merge patterns?

This question was split from this one. While that question is now about how to match two particular patterns (mostly using Verbatim or ...
1
vote
0answers
90 views

Design completion idea for Position [closed]

This is not exactly a question but rather a suggestion for a design which I think would greatly improve Mathematica. Before I put this to Wolfram Research I would like to see what other people think ...
2
votes
2answers
70 views

Replacing with pattern on built-in symbols?

I have: f[a, b] /. f[x__] -> g[x, 0] // InputForm (* Out: g[a, b, 0] *) Which is what I expect, but when I use a built in symbol instead of ...
0
votes
1answer
59 views

Parsing output of a reduce operation

This question is a broader question than this. The output of Reduce can be of different forms. The solutions proposed for that question works well when the output of Reduce is as provided in that ...
0
votes
1answer
36 views

How to get all the expression with pattern match? [closed]

The FullForm of my equation final(obtained from System Modeler) is: ...
4
votes
2answers
62 views

Identifying total derivatives

I would like to identify which terms in my expression are a total derivative and which ones are not. For expressions in which all terms are a total derivative, one can simply use integrate to find ...
4
votes
1answer
101 views

A single argument pattern definition applies to multiple-argument patterns?

Consider defining a pattern rule, such as myFun[x_]:=x As far as I understand Mathematica syntax, this rule means ...
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 ...
4
votes
2answers
79 views

Partitioning string into longest substrings of given characters

I want to partition string into longest substrings that each contain only specific characters, beginning from left to right with no overlaps, always choosing the longest one possible at current ...
5
votes
2answers
221 views

Manipulating a general length function

As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like ...
8
votes
1answer
287 views

Is there a defined priority for pattern matching?

If you define a function g, like so: g[x_?EvenQ] := "even" g[x_?NumberQ] := "number" why will Mathematica always return ...
0
votes
2answers
67 views

How can I extract the parameters from my expression automatically?

I have an expression of this form $\qquad e^{-x^2b_0+xb_1+b_2}(c_0+c_1x+c_2x^3+c_3x^3+c_4x^4)$ However, it has complicated parameters. ...
5
votes
1answer
146 views

Can someone please explain the code from another question

Source: Position of a pattern-matched part of an expression Can someone please explain the following code by Leonid Shifrin. ...
5
votes
4answers
92 views

Finding where a specific number appears in an expression

I would like to see how many times (and where) the number "91" appear in a set of formulae. I thought of using Position[expr , 91 , Infinity], but it seems not to ...
0
votes
2answers
88 views

Learning to work with core pattern only [closed]

I am trying to learn "core" patterns so I can start using them more often. Consider I am trying to solve the following problem. data = {2,{4,2,3},5,{7,3},{},4,1} (...
1
vote
1answer
72 views

Pattern matching on Orderless functions

I'm trying the following pattern matching command: ReplaceList[Hold[b + a], Hold[x___ + y___] -> {{x}, {y}}] And Mathematica returns: ...
2
votes
0answers
29 views

Using Min & Max of an Interval in a replacement [closed]

I want to use a replacement rule to transform a list of equations. The following works as expected: ...
2
votes
1answer
46 views

FreeQ checking for several variables [closed]

I am using FreeQ to define a replacement rule. However, it doesn't seem to be possible to specify a list of variables as the second argument, which is why I came up ...
1
vote
1answer
40 views

Commutativity of Times and pattern matching

I understand that Attributes of Times make a bit non intuitive the Pattern Matching of its arguments. However I do not understand why this gives False: ...
4
votes
1answer
50 views

String Pattern to match “x by y”

I'm trying to create a string pattern which will match either (with quotes included): "x by y" x by y 'x by y' I've tried to use the following pattern: ...
5
votes
1answer
78 views