# Questions tagged [pattern-matching]

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

1,051 questions
Filter by
Sorted by
Tagged with
282 views

### StringReplace with multiple patterns

I am experiencing what seems to be a strange behaviour with StringReplace while trying to add an extra pattern to an existing list of patterns. The problem is to translate a string into Morse code, ...
238 views

### Using regular expressions to replace constants by functions

I have an expression of the form: expression = A1*x + A2*x^2 + ... and I would like to replace A1 by ...
88 views

### How to apply assumptions to pattern matching?

I'm trying to remove terms whose exponents are positive in a polynomial. I tried this one; Assuming[t > 0, x^t /. {x^a_ /; Positive@a -> 0}] but the result ...
315 views

### Partial monomial pattern matching

Is there an elegant way to "partially" match monomials in a polynomial expression? Let me explain what I mean by "partially" with an example. Suppose I have ...
82 views

### Prevent an expression from evaluating a certain way (pattern-matching?)

I've been trying to write some simple code to let me use physics vector notation: $\hat{x}$, $\hat{y}$, $\hat{r}$, and $\hat{\theta}$. ...
40 views

### KeyUnion by equivalent patterns

I have associations whose keys are patterns. For example, ass1 = <|a_ -> 1, f[] -> 3|>; ass2 = <|b_ -> 2|>; I would like to construct the ...
51 views

### FindMoleculeSubstructure fails on SMARTS MoleculePattern, but MoleculePlot is OK?

Background Info: SMARTS is a convention for specifying molecular patterns. It is supported by Mathematica 12's MoleculePattern function. Problem Setup: Consider ...
41 views

### How to compair pairs in to arrays and select only those that satisfy a condition?

There are two lists: ...
70 views

### Extracting values from function/list containing arguments/elements (using patterns)

Here's an example input list: ...
234 views

### How to insert a character between any several characters without knowing indexes?

There is a list of strings: {"x1x2", "x1", "x1x2x3", "x3", "x4", "x1x2x3x4"} The goal is to insert "&&" between every two elements in a list. I need to ...
355 views

### How to replace numbers by letters in a special way?

There are few sublists: lists = {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}} The goal is to put A if the first element of a sublist ...
83 views

### Replace elements of a list if the conditional is false

There is a list: {1, {a, b, c}, 1, {1, 2, 3, 4, 5, 6, 7, 8}, {A, B, D, Z}} It contains sublists of different elements and several $1$s. The goal is to replace ...
616 views

### How to replace all elements of a list by a rule “element” -> “element_”

I have a list: {a, b, c, d, e, f, g, h, i, j} The goal is to get: {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} I can replace ...
170 views

### How to divide list according to the pattern?

There is a list of lists: {{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}} I need to regrup it's elements according to a pattern: "if the ...
41 views

### Does passing patterns between packages alter their name unbeknownst to the user? [duplicate]

This question is a partial followup to my previous question. Mainly, after using With to inject the proper argument in the function at the rhs of ...
42 views

### How to find consecutive things in nested expressions?

To find things that are consecutive you can use SequenceCases: ...
143 views

### Pattern matching expressions that have been simplified

I run into this type of issue frequently when trying to apply known identities to complicated expressions: some of my expressions will have their forms simplified by mathematica in such a way that ...
76 views

96 views

### How to extract parts of an expression using Cases

Let the following exprs stand for a list of complicated expressions which we are interested in breaking down into parts by extracting a number of individual ...
106 views

What pattern should i give to the following expression ("146d0" A + "-594d0" B) so i would get ("146d0" A - "594d0" B)
60 views

### Identifying repeating patterns in a list of numbers

I have some generated lists of natural numbers which have a small number of distinct values, ie. referencing the variable "rowToCheck" for each list: list1: rowToCheck = 3: length=7, distinctvalues=3 ...
43 views

### Delayed Rule right-hand-side mis-evaluation

Outline I am trying to create a rule that will give me a minus sign if the number of times A is applied on X is odd where A is some οperator and X a random quantity. Some of the configurations of ...
27 views

### Condition in SparseArray does not check the condition for some matrix elements

Consider the following MWE: SparseArray[{{i_, j_} /; (IntegerDigits[i - 1, 2, 2][] == 0) :> 1}, {4, 4}] This should give a $4\times 4$ matrix in which the ...
69 views

### Pattern Matching, FullForm and DisplayForm

Has anybody an explanation for the following or is it a bug? The display form and the input form do not give the same result. I include the raw display form here. Simply copy it to Mathematica to ...
89 views

### ReplaceList Except Alternatives bug

Bug introduced in 11.3 or earlier and persisting through 12.0 or later [CASE:4352435] and [CASE:4354378] In Mathematica 11.3 and 12.0, ...
35 views

### making replacements when patterns don't match exactly

I want to simplified some mathematical expressions, and they are similar to ...
105 views

### Partitioning a list by cumulated sum when the accumulate reaches a fixed value

I have a list dat = {10, 10, 10, 10, 10, 40} . When the cumulative sum of dat reaches >=...
47 views

### TagSetDelayed for spotting zero derivatives

I am trying to implement my own differential operator, d[arg_, var_], that computes the derivative of arg w.r.t. ...
257 views

### Faster way to solve android lock valid patterns

With the password grid shown in the Android home screen, how many valid passwords are possible? The rules for creating a pattern is as follows. We must use four nodes or more to make a pattern at ...
60 views

### Pattern matches Sin but not Exp? [closed]

I want to write a pattern that matches unary functions applied to a certain head named dual, used in replacement rules like this: ...
28 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 ...
67 views

### Triangular matrices with conditions

I would like to generate a lower triangular matrix that has checkmarks in the non-zero entries. The way that I created this matrix is the following ...
34 views

### Removing all sublists containing any arbitrary string as one or more of its list component

so, basically I have a list of sublists of varying length. For example, ...
251 views

### How can I check if a function can be written as $a * \sin (b * x + c) + d$?

I want to write a function that takes another function, say $f(x)$ and a variable $x$ as input and return True it this function can be written in the form ...
642 views

### Replace zeros in a list with last nonzero value

This question is closely related to the problem of zero crossings. Suppose I have a list l = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1} and I want to replace the zeros with the previous ...
222 views

### The differences amongst f[x_], f[x__], and f[x___] [closed]

This is probably very elementary but I have not used the following and I cannot find anything online, and I was unable to find something here. Can someone explain to me the differences amongst <...
24 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 ...
37 views

### Compile string cases into list

I have a html formatted string: ...
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 ...
66 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 ...
439 views

### A simple problem about a Rule example

The documentation says that "Rule represents a rule that transforms lhs to rhs" and gives the following example: ...
53 views

### Monte Carlo Method [duplicate]

How do you use a Monte Carlo method to approximate the area enclosed by the circle.
73 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, ...
151 views

### Creating rule-based sublists

I have a list each element of which consists of a list containing two strings and two integers: ...
40 views

### Replace sub-expressions matching a pattern unless located under specific heads

Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2. I want to apply it to all matching sub-expressions in some expression, excluding any sub-...
81 views

### Identify complex expression and replace

I'm having trouble finding a way (if there is) to analyse an expression, identify a complex pattern in it and then replace it with another thing. I think it will be easier to explain my problem with ...
72 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 ...