Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
6 votes
3 answers
220 views

Apply pattern using a Slot (#) to each list element using a second slot

I am trying to find a neat way of applying an operation to each list element, but do not know how to properly use multiple slots or whether it is actually possible to solve this with multiple slots. I ...
3 votes
1 answer
193 views

Test whether an expression contains a particular pattern

How to test whether an expression contains a particular pattern ? I made a function for myself that tests whether an expression contains Plus or not. ...
  • 3,529
4 votes
0 answers
52 views

How to use Shortest[___] with Sequence pattern matching?

Shortest[] doesn't appear to work with Sequence s. Is this a bug or a feature? What can I do to force the following ...
0 votes
1 answer
22 views

Confusing result when pattern matching a BlankSequence (__) to a list, then calling unary function on the list [duplicate]

The following makes sense to me: In[1] = f[a, b] /. f[x__] -> x Out[1] = Sequence[a, b] In[2] = f[a, b] /. f[x__] -> {x} Out[2] = {a, b} However, the ...
  • 181
4 votes
3 answers
167 views

How to combine multiple named patterns into one Cases?

In this toy example, lis = {C[1]*3, C[2]*9, 123}; I want to change, using Cases, the pattern C[x__]*y_ to ...
  • 127k
2 votes
2 answers
100 views

Cases without repetition

What would be the fastest way to find all cases in a list that match a pattern but without repetition in the result? Cases[ list, pattern ] // DeleteDuplicates does ...
  • 1,216
3 votes
2 answers
117 views

How to use Position to select based on absence of a pattern?

I need to use Position to obtain positions of entries in list of lists that do not contain a certain pattern. The problem is that ...
  • 127k
4 votes
1 answer
153 views

RuleDelayed (:>) involving Piecewise leads to Piecewise::pairs warning in ReplaceAll (/.)

When I write a replacement rule like this: Piecewise[{{x^2, x < 0}}] /. Piecewise[a_, b___] :> Piecewise[a, b] It works fine, but I get the unexpected ...
  • 2,923
1 vote
1 answer
51 views

Plus[__] matches everything in MatchQ [duplicate]

I understand that Clear[a,b,c] MatchQ[a+b+c, Plus[__]] True Because a+b+c is in fact ...
  • 3,529
5 votes
0 answers
57 views

Why does MatchQ[Hold[c + b + a], Hold[x_ + b + a]] return False?

Can anyone explain why the following pattern matches: [In1]:MatchQ[Hold[a + c + b], Hold[x_ + b + a]] [Out1]:True But this one fails: ...
3 votes
1 answer
38 views

Match first n elements where n is matched before

Can we use a matched integer to determine how many elements in a sequence should be matched? In other words, do you think it is possible to generalize foo for any n?...
  • 630
5 votes
1 answer
48 views

Assumptions about indexed variables

I am interested in the most efficient way to impose assumptions on indexed variables. In the following stylized problem, the following code ...
  • 187
1 vote
2 answers
60 views

How to perform item shifting deformation?

How to subtract the two equations. That is, the left subtraction of the two equations is equal to the right subtraction of the equation Continue to the previous question, how to perform the term shift ...
  • 793
6 votes
0 answers
85 views

Slow performance of SequenceCases

While working on a solution to another question, I noticed some performance regression slow performance of SequenceCases and friends. Consider a problem of finding zeroes of a function, given a sorted ...
  • 5,021
2 votes
1 answer
256 views

How to calculate infinitesimal analytic continuation?

Many problems in science and engineering are related to the analytic continuation and in particular infinitesimal analytic continuation to the upper or lower complex plane, i.e., a generic complex ...
  • 4,564
10 votes
7 answers
636 views

Select data from a structured list

Let's suppose we have the following structure of data ...
  • 8,520
1 vote
1 answer
130 views

how to understand f[{1,2,3,4}]? [closed]

As far as my understanding of function in Mathematica, its core is pattern match, but why f[{a,b,c}] (when f is defined) can work correctly? in my understanding, <...
0 votes
1 answer
79 views

Operating on specific case given a general case match

I have an expression a which include parts of the form A[i,j], where i,j take a range of ...
  • 21
2 votes
2 answers
61 views

Finding Missing Values in TimeSeries

With TimeSeries it is all too common to end up with something like this: ...
  • 307
5 votes
1 answer
82 views

Extract system of linear equations from the linearly independent vectors

Dear StackExchange Community, I have a lot of linearly independent vectors, e[i]e[j], e[i]e[j]h[i], f[i]e[j], etc, and I have an equation for example ...
  • 53
3 votes
3 answers
200 views

How do I adjust the form of the inequality?

I have some inequality ,like this $$ \left\{\begin{array}{l} x+y+z \geqslant 1 \\ 2 x+6 y+3 z \geqslant 6 \\ a x+b y+c z \geqslant d \end{array}\right. $$ I want to put all the y's in this inequality ...
  • 1,118
3 votes
1 answer
105 views

Pattern matching does not work as expected for Max [closed]

h[1, x, y] /. h[b___] -> {b} This code outputs {1,x,y}. But replacing h with Max (as below), it outputs {Max[1, x, y]}. ...
2 votes
1 answer
103 views

How to replace Divide in an expression?

It seems that a/b is always replaced with the form: a * b^-1, Times[a, Power[b, -1]]. I ...
6 votes
2 answers
180 views

"Cases" can not find "EllipticPi" inside expression

Why the following code does not find any of EllipticPi while at the same time it finds all EllipticE or ...
  • 5,915
3 votes
2 answers
113 views

Pattern matching with vector

I have an expression: {((-2*m*n + B*R0^2)*Sin[t])/(4*Pi*R0), -1/4*((-2*m*n + B*R0^2)*Cos[t])/(Pi*R0), 0} And I would like it to recognize this pattern ...
4 votes
3 answers
123 views

Rule can not work? [duplicate]

For example I have this equation,I want to use c->a/b ,but it can not work $$ \frac{a^2}{b^2}+\frac{b^2}{a^2}+\frac{a}{b}+e^{a/b}+\frac{b}{a}+\log \left(\frac{a}{b}\right) $$ ...
  • 1,118
0 votes
0 answers
64 views

ReplaceAll on a list of strings

Does replacing a list of strings have to be this convoluted (with a warning too)? Why can't ReplaceAll natively use string matching patterns? ...
2 votes
1 answer
71 views

`SequencePosition` doing unnecessary work

SequencePosition returns only the first sequence in a list that matches a given pattern with SequencePosition[list,pattern,1] ...
  • 1,706
0 votes
1 answer
53 views

How can I distinguish between different structures of actual function parameters?

Overloading functions for different types of parameters is very powerful. I can easily write different versions of a function depending on the function parameters being Integer, Real, Complex, Sring ...
3 votes
1 answer
53 views

Using Contains* symbols to create implicitly defined entity classes

The following line finds the higher ed institutions in New York without a Carnegie classification: ...
0 votes
1 answer
51 views

How to write a Replace Rule that will Iconize all Rules in a List whose rhs are long String-s?

First, let me illustrate what I'm trying to do. FormatOptions is a programming utility that will Echo a given ...
  • 2,337
3 votes
1 answer
31 views

SequenceCases with OverLaps [closed]

SequenceCases is not performing as expected. Here's an example to illustrate: Find all the triples where the pair {hi, mid} precedes {hi,lo} and {hi,lo} precedes {mid,lo} in an ordered list, where hi &...
  • 593
1 vote
2 answers
65 views

Pattern matching with powers

The elements in my list look like A^a B^b C^c D^d , but some powers are zero or one. I am trying to write a matching pattern that selects elements such that ...
  • 630
9 votes
1 answer
201 views

How do I correctly apply patterns to function arguments using Pattern (:), PatternTest (?), and Condition (/;)

Given: Clear[func]; listOfLists = {{a, b}, {c, d, e}}; listOfRules = {a -> b, c -> d}; I can discern between listOfLists and listOfRules using MatchQ: ...
  • 973
4 votes
1 answer
94 views

What does Dash Question mark do? [closed]

This is an example I'm reading that picks out the Primes, but I don't quite see how the _? works. I haven't seen it before and can't find it in Help. ...
5 votes
1 answer
391 views

I understand b_ matches beta, but why does it also match alpha?

Why does this pick out everything instead of just beta? Cases[{alpha, beta, mxyzptlk}, b_] In a function you have to match the ...
0 votes
0 answers
45 views

Why /. pm->\[PlusMinus] does not work, if \[PlusMinus] does not have internal meaning?

I want to replace pm with PlusMinus character in the output, why this does not work? How can I treat it as a constant? Context. Here is my code for quaternion ...
  • 3,337
0 votes
1 answer
35 views

Parts of the expression are evaluated well, but not the whole expression together

I have the following code inended to define quaternions: ...
  • 3,337
0 votes
1 answer
38 views

How can I redefine multiplication this way?

I want the product of matrix objects to be evaluated to the matrix object of dot products of the arguments, but only if the matrices commute. Why this does not work? ...
  • 3,337
1 vote
3 answers
305 views

Why does this substitution not work?

Sqrt[Matrix[( { {0, 1}, {-1, 0} } )]] /. f_[Matrix[x__]] :> Matrix[MatrixFunction[f, x]] Matrix is an ...
  • 3,337
0 votes
0 answers
31 views

This code works for 1-argument functions, how can I make it work for multiple-argument functions?

...
  • 3,337
2 votes
3 answers
109 views

Why does replacement {0, 0} /. {a_, -a_} -> c does not work?

Why does replacement {0, 0} /. {a_, -a_} -> c not work? How to make {0,0} to match the pattern ...
  • 3,337
1 vote
1 answer
34 views

Why this replacement rule {{I, 0}, {0, -I}} /. {a_, b_ ?Conjugate[a]} -> c does not work?

I want to make a replamement in a $2\times2$ matrix only if the elements on the main diagonal are conjugates of each other. Why does not this work and how can I make it work?
  • 3,337
2 votes
1 answer
50 views

Using list of variables, explain behavoir

I have some basic lack of understanding how Mathematica works. Could you please explain to me the following behavior or point me to a resource that will let me understand. ...
  • 145
2 votes
1 answer
63 views

Pattern Match "Empty" Term in NonCommutativeMultiply

Not sure if this has been asked before, please point me to the post if it has. Is there a way to treat terms like (b+c), a**(b+c)...
  • 33
4 votes
0 answers
69 views

Condition-in-scope as a rule for SequenceReplace

Bug persisting through 13.1.0 [CASE:4841653] From ...
  • 3,630
3 votes
2 answers
128 views

How to define the pattern for the sum of some specified terms?

I want to define a function to check whether the expression is the sum of these terms a*x+b*y^n+c*Exp[k*z] (* n can be 1*) These ...
2 votes
2 answers
84 views

Pattern or ForAll statment in Assumptions

I would like to simplify an expression like $\sum _{i=1}^k \sqrt{a_i^2}$, with the condition $\forall _{i\in \mathbb{Z}_{>\, 0}}a_i\in \mathbb{R}_{>\, 0}$ and with $k$ being undefined, which ...
  • 471
1 vote
1 answer
130 views

MemberQ and FreeQ not working as expected

Could someone explain to me why MatchQ[L[1, {3}], L[s_, l___?(FreeQ[#, s_] &)]] returns False? I would have expected that ...
  • 33
3 votes
2 answers
91 views

Replacing only variables in specific locations with Replace All

Suppose I have an example expression defined like this: line = R0*f[R0,x] + R0^2*42*D[g[R0,x],x] I would like to replace R0 with ...
  • 133

1
2 3 4 5
29