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
61 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 ...
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, ...
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 >=...
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. ...
242 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 ...
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 ...
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 ...
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 ...
214 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 <...
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: ...
33 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 ...
431 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: ...
47 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-...
76 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 ...
26 views

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

As I understand, an expression like Module[{var},expr[var]] localizes var and evaluates ...
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: <...
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 ...
45 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 ...
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 ...
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?
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: ...
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: ...
69 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: ...
63 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],...
111 views

### DeleteCases using two lists but with partial match?

I searched and tried different solutions on SE but nothing worked. I have two lists and would like to remove items from list1 that have a PARTIAL match with items ...
141 views

### Pattern test: _Integer vs _?IntegerQ [closed]

While checking out the different pattern replacement methods in this question, I have a query regarding the basic difference between the following two replacements: ...
292 views

### Replacement using pattern matching?

Starting with the expression eq1 = 5 Exp[1 + 2 x + 3 x^2 + 4 x^3]; I want to implement the following transformation: ...
42 views

### Select elements from nested list where one element is largest

I'm trying to speed up the following, but implementing efficient pattern matching isn't intuitive to me yet, even with lots of examples. I have a list of ((x1,y1,z1), (x2,y2,z2)) pairs, and I want to ...
66 views

### Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...
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, ...
83 views

### Using DeleteCases with a defined function with two arguments as a pattern

I am trying to manipulate some large datasets in Mathematica. I have plotted the data, and I would like to be able to easily remove points from the dataset, and it's fairly easy to identify the x-...
86 views

### Composition of polymorphic functions

Using pattern matching one can easily create polymorphic functions in Mathematica f[{a_, b_}] := {a^2, b^2}; f[c_Integer] := c^4; Now I want to create another ...
49 views

### PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...