We’re rewarding the question askers & reputations are being recalculated! Read more.

# Questions tagged [pattern-matching]

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

1,027 questions
Filter by
Sorted by
Tagged with
83 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, ...
215 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 <...
65 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 ...
243 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 ...
35 views

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

I want to simplified some mathematical expressions, and they are similar to ...
100 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 >=...
213 views

### Find prefix of list in Association

I am trying to write some kind of demonstration for coding algorithm (for prefix codes). I have an Association that contains lists of bits (0s and 1s): ...
46 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. ...
58 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: ...
25 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 ...
65 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 ...
255 views

### Bug: x === y but results of a /@ x and a /@ y differ?

Bug introduced in 10.2, and persisting through 11.1 Bug resolved in 12.0 This is strange: ...
32 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, ...
247 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 ...
76 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, ...
635 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 ...
407 views

### Confused by the opts : OptionsPattern[ ] pattern

I'm trying to make sense of the syntax in the clause opts:OptionsPattern[], as found in this standard "rule-based" (pseudo-)function definition pattern: ...
22 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 ...
35 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 ...
64 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 ...
432 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: ...
49 views

### Monte Carlo Method [duplicate]

How do you use a Monte Carlo method to approximate the area enclosed by the circle.
72 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: ...
38 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-...
90 views

### Replacing an element in table with indices other than i & j

I have a function F, of four other f functions, as shown below (1, 2, 3, 4 are indices here, while m is an integer) F = f[m, 1]*f[m, 2]*f[m, 3]*f[m, 4] I want to ...
472 views

### Make my data-structure atomic

Lately, and by lately I mean since version 7 or so, the number of atomic expressions in Mathematica constantly grew. In former times only the native types like integers and optimised arrays were ...
77 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 ...
67 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 ...
88 views

### DeleteCases pattern which applies Chop to all elements

In my current macro, I have: DeleteCases[Chop[FullSimplify[RowReduce[something]]], {0 ..}] This works fine for both, exact and numeric complex numbers (with ...
93 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 ...
94 views

### Pattern matcher behavior change in 11.3

When trying to switch from version 10.3 to 11.3 I noticed that some of my code do not work properly. After some investigation I found that the problem is with pattern matcher. Here is the example: <...
26 views

### Why are unrelated symbols localized in Module? [duplicate]

As I understand, an expression like Module[{var},expr[var]] localizes var and evaluates ...
117 views

### Using PatternSequence and __ / Repeated[]

I'm trying to scrape this Wikipedia page ('Deaths in 2019- July', I'm writing this on the 14th.). I am failing miserably and I'm starting to think that it is due to some misconception on my part on ...
42 views

### Respecting excluded index in sum

I'm using a function involving a sum where some indices are excluded: ...
59 views

### Pattern of nested functions

Suppose I have a list of elements list = {f[b[G]],f[b],k[k[k[G]]]} What is the pattern associated to an expression in which the function ...
22 views

### Count does not count NumberString directly [duplicate]

I want to count all NumberStrings in a list: In:= myList = {"123", "456", "789", "ab1", "cd2", "xyz"}; but, surprising (to ...
46 views

### Unconstrained pattern evaluated by a constrained replacement rule

I'm working with a notebook that defines NonCommutativeMultiply on a defined set of symbols, called operators. Semantically, ...
60 views

### Find and replace particular multiplication of factors in big multiplication

I have a sum of terms where each is a multiplication of several factors. For example my expression in FullForm is ...
4k views

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

Both of the following return True: ...
99 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 ...
70 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: ...
31 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?
74 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: ...
47 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: ...
64 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],...
113 views

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

I was trying to match some polynomials, but this is weird: ...
### 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 ...