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.

2
votes
1answer
30 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
63 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
51 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
89 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
84 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
62 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
32 views

KeySelect via Not[StringMatchQ[Pattern]] fails

I have an Association asc that has mixed type Keys: ...
1
vote
1answer
21 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
55 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
62 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
46 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
110 views

Cases, Pattern, Condition [duplicate]

I supposed Cases would do the list produced as example li = RandomSample[Range[100, 999], 30] ...
2
votes
1answer
51 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
60 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
33 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
743 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
148 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
38 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
365 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
141 views
0
votes
0answers
20 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
30 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
329 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
98 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
38 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
38 views

Simplify by defining new functions

if I have a formula like ...
4
votes
1answer
63 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
65 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
66 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
175 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
93 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
59 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
47 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
53 views

Construct a function like Coefficient

I'am trying to construct a function that will work like Coefficient as an exercise. ...
8
votes
1answer
134 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
66 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
412 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 \...
9
votes
2answers
157 views

Find pattern with Position

I have a list a = {L, S, L, L, S} and I can find the position of of either L or S by: ...
6
votes
2answers
410 views

What does _. mean in patterns? [closed]

Here is a quick one, hopefully. I searched through different tutorials and documentation articles but haven't been able to find anything yet. What does _. mean? As ...
1
vote
1answer
54 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 ...
2
votes
1answer
29 views

Why does N result in some inconsistency in this context?

In the following, why does N not produce the expected decimal output, while Round does? ...
8
votes
2answers
174 views

How can you Unset the following UpValues?

This expression is taken from a talk given by the late Robby Villegas. The UpValues to some symbol a is defined below as: ...
2
votes
1answer
35 views

Matching combinations of StringExpressions and general patterns i

Is there a clean way to match patterns that combine general patterns and ?StringExpression For example, Cases will not match ...
1
vote
1answer
50 views

Pattern match sum of terms

I am trying to define a replacement rule for an arbitrary sum of terms in an equation, that returns the x[i] terms along with their coefficients. For example, ...
2
votes
0answers
46 views

Why do I get this warning when replacing pattern with list element? [closed]

The input x^2 + x^3 /. x^i_ -> i yields: 5 The input u = {1, 2, 3} x^2 + x^3 /. x^i_ -> u[[i]] still yields the ...
10
votes
4answers
364 views

Making a list of patterns of arbitrary length

I would like to create a list of arbitrary length: {x1_, x2_, ...} Where each of the elements of the list has the full form: ...
1
vote
1answer
53 views

How to select terms which contain certain letters?

I would like to select the term Subscript[Cos\[CapitalDelta], Subscript[p, B]] of expression ...