Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
2
votes
1answer
23 views

How to substitute an expression in side a particular head or inside a particular pattern?

Let say I have any expression ex= f[h[r[x y]]] g[x z] and I want to replace the x with w, ...
10
votes
3answers
694 views

How to NOT a specific pattern?

been going around in cirlces on this: {1/3, 1, 0.22, 3, "v", 4} /. _?NumericQ -> "999" Now I'm trying to replace everything that is not a ...
10
votes
2answers
216 views

i_:0|1 varies in v12.1: incompatible change or bug?

Just check i_: 0 | 1 // FullForm In v12.0 or earlier the output is Alternatives[Optional[Pattern[i, Blank[]], 0], 1] But in ...
0
votes
0answers
30 views

Matching sum of negative terms with a pattern [closed]

Consider a really, really simple expression: test = (-x - y); The FullForm of test is ...
5
votes
2answers
272 views

Why association pattern unmatched when using variable

I have association and want to replace integer in specific element in this association. I'm trying to build pattern to be used by ReplaceAll command. When number is ...
4
votes
1answer
47 views

Name of patterns cast as expressions from strings

I'm trying to match a part of a string while imposing conditions on the patterns, e.g.: StringCases["223", a: DigitCharacter ~~ b: DigitCharacter /; Unequal[a, b]] ...
4
votes
1answer
240 views

What does Sequence[2,4]^2 mean?

I am learning pattern matching. An example that confuse me is as follows. Foo[x__?EvenQ] := x^2; Foo[2, 4] which is equal to ...
1
vote
1answer
38 views

ReplaceAll with pattern matching not working with list of lists

Abstract This is just a learning exercise for me. I am trying to create a function that will take a list of vectors, multiply them by a constant if constant is given, and then add all vectors. If we ...
2
votes
1answer
50 views

Write repeated expressions within a rule more simply

Is it possible to shorten the length of a (delayed) rule (i.e. lhs :> rhs) when there are expressions which repeat several times? Those expressions involve ...
2
votes
1answer
39 views

Replacement rule does not recognize parity?

All of the following ...
1
vote
1answer
58 views

Matching Polygons in a list of Polygons

In the process of creating a more complex algorithm, I've stumbled upon some strange behaviour I am trying to understand with my matching function. What I'm expecting to see on the right hand side is ...
3
votes
2answers
39 views

Replacement rules which for elements of a vector which meet conditions

I'm trying to create a function which will replace elements of a list with their values modulo 1, i.e. Mod[x,1], but only if ...
0
votes
1answer
31 views

using pattern matching to separate coefficients and functions

I want to find out the coefficients and functions from an expression and possibly save them in separate files with same order. I have the following toy example: ...
-1
votes
1answer
47 views

Function matching any head (Example: h_[_,_] := 5)

I want to define a function matching any expression with 2 parameters. For example: _[_, _] := 5 I get the following error: SetDelayed::nosym: "[,_] does not ...
6
votes
3answers
189 views

replacing a sub-partition (unordered list; multiset)

I am working on a bijection for integer partitions and would appreciate help with two steps. First, I want to replace each occurrence of (4, 2, 1, 1) with (3, 3, 2). One complication is that the 4, 2,...
4
votes
1answer
90 views

A pattern replacement to force Mathematica to replace inexact 0 multiplication by zero

Hi Consider the following expression which contains multiplication by inexact 0 ...
6
votes
2answers
115 views

How to extract all possible sub-products which match specific pattern?

I have an expression of the form expr = 3*(a.b)*(c.d)*f[x] + 5*(m.n)*(p.q)*(r.s)*f[y] and want to extract all possible subexpressions matching the pattern ...
0
votes
1answer
30 views

A problem with Cases and Pattern recognition: unexpected behavior

I need the following command (to isolate exponents and variables): F[X_] := Cases[ X , G[x_]^m0_. -> {x, m0}] This example gives the correct answer: ...
3
votes
1answer
53 views

Matching symbols in assignments using NotebookImport

How can symbols from assignments, ie left hand side in expressions Set and SetDelayed be matched after importing using ...
5
votes
3answers
233 views

Replacing all subsequences in a list

How do I replace all occurrences of -1,1 with 0,1 in a list example: list = {-1, 1, -1, 1, 1, 1, -1, -1, -1, 1} desired output: ...
10
votes
1answer
221 views

How to clear all definitions matching f[1, …] but not f[2, …]?

I would like to clear all definitions of the form f[1,...] in such a way that f[2,...] remain unaffected. The two methods I know ...
8
votes
1answer
550 views

Why does Mathematica ignore 1 as an exponent? Or as a coefficient?

Two different examples of same problem: In: Cases[{f^2, g^3, k^p, h}, x_^n_ -> n] Out: {2,3,p} # The result misses the ...
2
votes
1answer
44 views

No need to use Alternatives to construct alternative string patterns?

I am trying to match a list of strings with a pattern, in which there are several possibilities for the characters to be matched. To me, the following looks the most natural way: ...
0
votes
1answer
58 views

Strange behaviour of Cases and pattern matching [closed]

I have encountered a strange pattern matching behaviour which I cannot explain even after going through manual again. Why does the second line in the code yield different result? Is it a feature of ...
2
votes
1answer
37 views

Why does ReplaceRepeated work differently in numerator and denominator?

I'm confused about the behavior of ReplaceRepeated. ReplaceRepeated[a Sqrt[b], Sqrt[b] -> c] gives the expected (* a c *), ...
2
votes
1answer
35 views

Remove parentheses in a List

I want the output to be: a,b when the input is {a,b} . How do I do this please? Flatten command does not work.
1
vote
1answer
50 views

Attributes for private functions

I am not sure if what I want to do can be done, but I thought to ask anyway. Note: I already have in hand a workaround, I was just wondering for the power/simplicity that Mathematica may provide. ...
2
votes
1answer
22 views

Is there a better way to “ Select non self mapping elements from a list of rules”?

Is there a better way to get the non self mapping list of rules from the list of rules abc = {a -> a, b -> c, d -> d, e -> f}; To ...
0
votes
0answers
16 views

Do named patterns inside Except work inside OrderlessPatternSequence?

This works: f[{x, a, b}, {y, c, d}] /. f[{sgn1_, _, _}, {Except[sgn1_, sgn2_], _, _}] :> matched[sgn1, sgn2] (*matched[x,y]*) This doesn't match: ...
6
votes
1answer
72 views

How to use match and blank

This very basic problem baffles me. From the documentation I do not understand the results of ...
0
votes
0answers
26 views

Construct string pattern for two different types of lines

I need to increase my understanding of string patterns. My specific problem is that I have a definition of a type in SystemVerilog that has lines that are either single-bit or multi-bit. I want the ...
2
votes
1answer
42 views

Pattern to match any differential of a certain variable

I'm trying to come up with a pattern or just implement a function DifferentialEquationQ so I can have another function assume different behaviour when it's input is ...
0
votes
0answers
13 views

How to insert partitions into a list to make it match the partitioning structure of another list [duplicate]

I have two lists with the same number of elements at the bottom level: list1 = {a, b, c, d, e, f, g, h, i, j}; list2 = {{1, 2, 3, 4}, {5, 6}, {7}, {8, 9, 10}}. I need to insert partitions into list1 ...
0
votes
2answers
53 views

Pattern matching for products of variables [closed]

What I am trying to do is very simple: In an expression that is polynomial in x,y, I want to check if there are products of the form ...
0
votes
4answers
58 views

Match & replace the elements in a dataset

somewhat similarly to Replace Elements in a Dataset by rules, I'd like to replace the components in all columns named "microseconds" in a hierarchical dataset, with their respective quantity. ...
2
votes
1answer
38 views

Find minimal recoding of the (symbolic) entries of an $8 \times 8$ matrix

I have the following $8 \times 8$ matrix ...
0
votes
0answers
46 views

How to use DeleteCases to delete table entries with lists of strings longer than one character?

I'm currently looking at using Mathematica to perform sequence alignment on around 4000 sets of genomic data. I've been going about this by first isolating nucleotide sequences for the protein I need, ...
2
votes
1answer
47 views

How to match a pattern with exact occurence?

Maybe the way I expressed is not very accurate, but a very simple example will help to understand the question: For Subscript[a,i]+Subscript[a,j]**Subscript[a,k] ...
0
votes
0answers
25 views

Pattern matching on derivatives

I want to manipulate and do pattern matching on sequences of functions and their derivatives, but I'm struggling to work with the multiple indexing required. Here is an example. Let $$ y(z) = \sum_{j=...
5
votes
0answers
53 views

Can't seem to match multiple KeyValuePatterns in one pattern

Let's define a couple patterns that we can use to match Associations. ...
1
vote
1answer
26 views

Multiplying two expressions with a specific function, efficiently

Paraphrasing, the problem I am trying to solve is the following. I am given two "matrix valued" sums ...
4
votes
2answers
35 views

RandomVariate on multidimensional Bernoulli array

Executing... Map[RandomVariate, Map[BernoulliDistribution, RandomReal[1, 5]]] yields the expected result, e.g. ...
3
votes
1answer
103 views

How to use patterns to specify variable type as Boolean (True, False) in analogy to x_Integer, x_Real, x_Complex, x_List, etc

Say I define a function f[x_, y_]:= ..., the concrete definition of which is not important now. What is important though is, that I want ...
3
votes
1answer
90 views

DeleteCases with a case as float?

Might the DeleteCases have issues in matching when the list is of floats (machine precision numbers)? E.g. in this code it might fail to match correctly because ...
1
vote
0answers
36 views

Apply SequenceReplace with a fixed shift

How can I apply SequenceReplace with a fixed prespecified shift instead of a shift equal to the length of the pattern, on a given list? E.g. ...
6
votes
5answers
189 views

Taking some terms from a polynomial

I have a polynomial like this: ...
3
votes
1answer
52 views

Naming repeated patterns

How can I name a repeated pattern to apply a rule on it? In the example below I have two element sequence. There is no problem in naming them and then applying some function on it: ...
7
votes
2answers
90 views

How to use ExtractArchive[] with a pattern that does NOT match

ExtractArchive has the ability to Extract all files that match a pattern. For example this will extract all the JPEG files from the archive: ...
3
votes
2answers
62 views

Setting nested optional argument with a default when unpacking from a given Head

I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example: ...
4
votes
2answers
125 views

How to apply Cases on multiple parts of sublists?

I have the following data sol={{2063, 853}, {2066, 855}, {2069, 857}, {2072, 859}} I can find prime-pairs by ...

1
2 3 4 5
23