Stack Exchange Network

Stack Exchange network consists of 174 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.

5
votes
3answers
255 views

How to remove the item that is repeated?

If i have a list={m,n,p,q,q,r,l,l} how can i remove the repeated item with pattern matching so i would have ...
3
votes
2answers
70 views

Change arguments of a product of functions

I am trying to do the following with rule-based pattern matching (f1[t] f2[t] f3[t]) /. {x__ -> n[ Product[x[[i, 0]][om[i]], {i, 3}]]} // Quiet which ...
1
vote
1answer
31 views

Extract powers of exponential into a list

I have a very complicated, long (thousands of terms) expression that boils down to a form of expr = a1 Exp[b1] + a2 Exp[b2] + a3 Exp[b3] Note that all a and b ...
0
votes
0answers
38 views

Pattern to match expression with head containing a pattern object

In the documentation for the Blank[h] pattern object which matches any expression with head h (AKA ...
4
votes
1answer
99 views

Why Cases and Select treat the List differently?

Look at these two examples: In[1]:=Select[f[i], MatchQ[_f]] Out[1]:=f[] In[2]:=Cases[f[i],_f] Out[2]:={} It seems that the Select take $f[i]$ as List, but Cases ...
4
votes
2answers
82 views

Select certain terms in an expression

I would like to have a function to select terms containing q[i] terms in expressions, for example, for an expression a q[i]+b +c q[j]+d I would like the function ...
0
votes
1answer
44 views

Replacement of the patterns in the replacement

I want to make such changes: $(A\, p + B\, q)[i] \to A\, p[i]+B\, q[i]$ But the replacement below can not do this ...
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
1answer
39 views

Replace an If by a combination using replacement rules

Consider the following code : tttest = a^2 + If[a > 0, a, -a] a^2 + If[a > 0, a, -a] I would like to replace my If function by something like fonction @ ...
2
votes
0answers
44 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
1answer
39 views

OptionValue and OptionPattern not Changing Values

I am trying to produce a function with a few sets of arguments, one of which can accept options. However it doesn't seem to be responding to the changes that I accept. ...
2
votes
3answers
70 views

How to check expression depends on symbol in a particular way

Consider these two expressions: expr1 = a d g[X, c]^2 + e g[X, f] + 3 expr2 = a d g[X, c]^2 + e X + 3 How do I check whether these expressions depend on ...
0
votes
1answer
61 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: ...
4
votes
1answer
92 views

How to bring out all factors recursively except one particular term?

I want to write a function keepOnly[expr_, keep_] Such that keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep] becomes ...
3
votes
3answers
87 views

How to use “Cases” for isolating terms of a sum in a nice way?

I'd like to isolate some terms with specific patterns within a sum that I called chunk. ...
3
votes
2answers
63 views

Dropping lists using pattern [duplicate]

I have the following list of lists. data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}} I want to drop all the lists with ...
3
votes
1answer
38 views

KeySelect via Not[StringMatchQ[Pattern]] fails

I have an Association asc that has mixed type Keys: ...
1
vote
1answer
24 views

Splitting a list of letters into subsequences starting with uppercase letters [duplicate]

Given a list of upper/lowercase characters, I'm trying to split it into subsequences that start with an uppercase and contain the next few (not necessarily positive number of) elements up until the ...
3
votes
1answer
60 views

How to change node color in .dot file using pattern matching?

For example I have a dot file that describe a decision tree or binary tree In the file, the color of node is always gold, so I try to change it according the number in ...
0
votes
2answers
63 views

Splits an expression into two parts if possible

The series The series is: $\text{expr}_{1} \pm \text{expr}_{2} ...$ $\pm$ represents plus or minus sign. The number of terms in the series can be 1 or more than 1. Each term $\text{expr}_{n}$ is an ...
2
votes
2answers
47 views

Substituting main diagonal elements when a condition is satisfied

I'm trying to substitute the main diagonal elements when their value is less or equal to zero. I've tried the following: ...
6
votes
1answer
114 views

Cases, Pattern, Condition [duplicate]

I supposed Cases would do the list produced as example li = RandomSample[Range[100, 999], 30] ...
2
votes
1answer
53 views

How to refer to a character group without ambiguity in a regex?

Suppose I want to realize a replacement in a string: "aabbcc11dd ee" -> "aabbcc112dd ee", I can use a regex like this within ...
2
votes
1answer
70 views

How do you find a sub-graph and replace it with another subgraph?

I saw a video where SW shows how he has a large graph and some rules for finding a given subgraph in a graph and replacing it with another subgraph. How would one go about doing this in Mathematica? ...
2
votes
1answer
35 views

Reformatting the pattern inside a list

So, in principle, I have a simple problem which goes like this: I have vectors that should consist of lists of 2 numbers, see here: ...
7
votes
4answers
745 views

Add a space after a ending dot of a sentence

How can I add a space after the ending dot of a sentence? So dots like in "1.2" should be unaffected. Also if there is already a space, then there should not be a ...
5
votes
2answers
152 views

Using DeleteCases to match a string pattern

I am trying to remove all of the elements from a list that contain the characters "err" in that order. I expect the following code to return an empty list: ...
1
vote
0answers
31 views

Holding evaluation of flat, orderless, function in up value [closed]

I have an operator that is flat and orderless that I want to evaluate only after the expression has been entirely flattened. Essentially, I want an expression of the form ...
4
votes
1answer
40 views

Pattern of list contains two types of variables: What is the correct definition?

What is the correct form of the pattern "list of variables of two (or more) types" These examples do not work MatchQ[{1., "a", 2.3, 7.}, _ {Real | String...}] <...
6
votes
4answers
386 views

partial numerical evaluation

Context The following is an issue I have come across many times and still do not have a satisfactory solution for. The context of it is this. Let's say we have an equation, a linear ODE for the ...
6
votes
3answers
142 views
0
votes
0answers
21 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 ...
1
vote
0answers
31 views

ReplaceAll does nothing with the rgb values of a picture [closed]

In the book "Programming with Mathematica: An Introduction" at the beginning of the chapter on Patterns and rules an example is given where the colors of an image are transformed according to ...
11
votes
2answers
337 views

About shorthand notation for conditional pattern (Paul Wellin “Essentials of Programming in Mathematica”)

I am reading Paul Wellin's "Essentials of Programming in Mathematica". Mr. Wellin wrote the following in his book: There is a convenient shorthand notation for conditional patterns that is ...
6
votes
2answers
101 views

Identifying Infinity, Indeterminate, etc

I am doing a calculation and sometimes in the middle of the calculation a parameter evaluates to the following ...
1
vote
1answer
41 views

Why does ReplaceAll match pattern while MatchQ fails

Why does the following pattern with MatchQ fail MatchQ[1+Sin[1] I, a_Complex /; Im[a] > 0] (* False *) Whereas, the same ...
0
votes
0answers
40 views

Simplify by defining new functions

if I have a formula like ...
4
votes
1answer
65 views

Subtle order-of-evaluation issues when pattern-matching with attributes

Related question concerning unrolling the tests that are shown below: Big and Little surprises when unrolling tests of pattern-matching and attributes Questions researched before posting this one: ...
3
votes
1answer
68 views

Big and Little surprises when unrolling tests of pattern-matching and attributes

Related question concerning interpretation of the substitutions: Subtle order-of-evaluation issues when pattern-matching with attributes Questions researched before posting this one: Orderless ...
1
vote
2answers
82 views

Pattern transformation to implement “factoring out”

I'm attempting to put together some code that would do pattern based expression manipulation (I know it's already implemented in mathematica), but I'm getting stuck on a pattern to deal with "...
3
votes
0answers
67 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 ...
7
votes
3answers
180 views

Finding items satisfying a condition in a list using patterns

Why the following command Cases[{1, 2, 6, 3, 4, 8, 5}, {x_, y_, z_} /; (y > Max[x, z]):>y] doesn't return the y values that are greater than their ...
4
votes
3answers
98 views

Repeated replacing elements in the list

I have a large list of the following form {{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1} and I want to combine all of the pairs ...
1
vote
1answer
60 views

Create mirror function using pattern matching

I want to implement pattern matching for a few simple types. In particular, I want to define a function mirror that mirrors circles and rectangles. That is, ...
0
votes
1answer
71 views

`:` versus `:=`

I am going through the Fast introduction for programmers tutorial on Mathematica. The page on patterns has the below line: x_ (short for x:_) stands for a pattern whose value will be named x ...
3
votes
1answer
48 views

How can I replace non-reals in a multidimensional list?

Ok. I'm obviously not understanding something about pattern matching. I have a list called t like the following: ...
2
votes
2answers
54 views

Construct a function like Coefficient

I'am trying to construct a function that will work like Coefficient as an exercise. ...
8
votes
1answer
135 views

Why does MatchQ[a, r_ /; Head[r] != Plus] evaluate to False?

I'm trying to understand. Why is it that: MatchQ[a, r_ /; Head[r] != Plus] Evaluates to: False ? For me, I would think ...
1
vote
1answer
71 views

Working of MatchQ[] with Blank, BlankSequence, BlankNullSequence [closed]

This code: In[76]:= Trace[MatchQ[{1,2,3}, {_?NumberQ}]] gives: Out[76]= {MatchQ[{1,2,3},{_?NumberQ}],False} This code: ...
13
votes
3answers
418 views

Generating a pattern from user-defined rules

I want to generate a list of $L$ and $S$ according to the following rule: upon each iteration, replace $S \rightarrow L$ $L \rightarrow LS$ Such that, starting with $L$: $ L \rightarrow LS \...