Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
6
votes
5answers
492 views

Sum all the adjacent values in an array, with some conditions

I would like to sum all the adjacent values in an array that are different from 0, then replace those values with zero, apart from the first value which should be the sum. For example having an array ...
1
vote
0answers
51 views

Importing multiple files setting the name with tostring

I'm trying to import several matrices, which contain a pattern in the name. I create first their names in a table ...
1
vote
0answers
54 views

Simplification and patterns

I have a very simple simplification to make, where I need to do a replacement but the pattern matching seems slightly awkward. I would like to simplify: ...
4
votes
1answer
89 views

KenKen Puzzle Helper - Dropping order-less sequences

In the following example, I am generating all the variants of a $9 \times 9$ KenKen puzzle that come in groups of three using addition that result in $18$. ...
1
vote
0answers
52 views

ReplaceAll with named pattern [closed]

If k is unassigned, this yields {a, b, c}: ...
1
vote
0answers
30 views

Difference in pattern matching between Cases and ReplaceAll

I don't understand why there is a difference in the pattern matching between x a /. coef_*x^pow_. :> {pow, coef} and ...
4
votes
2answers
175 views

How to divide a list into several single loops

Because of some problems, I need to divide the permutation represented by a list into several single loop lists. For example, for list {4, 3, 2, 1, 7, 6, 5}, it can ...
2
votes
1answer
74 views

matching Inequalities

Consider the following list list={0 < x < 1/12, 0 < x < 1/4} I wish to find the intersection of the two ranges of x ...
0
votes
1answer
64 views

Solving indexed ODE in Mathematica

I would like to solve an ODE with indexed variables symbolically. Is there a way to do that? $$\frac{dp_{\beta\alpha}}{dt}=m\left(p_{1\alpha}p_{\beta2}+p_{2\alpha}p_{\beta1}\right)$$ m is called mass ...
3
votes
2answers
183 views

How to set a rule that's too deep for UpValue?

I want to automatically transform Conjugate[a CenterDot[x, y, z]] to Conjugate[a] Conjugate[CenterDot[x, y, z]]. However, the ...
1
vote
1answer
97 views

Dummy index automatic summing

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out? ...
4
votes
1answer
256 views

Symbolic second order derivative of real antisymmetric matrix in Mathematica

I would like to calculate the second order derivative of a function with respect to a matrix coefficient symbolically. Below is the first order derivative of S with respect to the coefficient of x. ...
1
vote
0answers
51 views

What is the difference between form and pattern in the documentation?

In the documentation, I see the term form and the term pattern. For example, see Cases and MatchQ for both usages. What is the ...
4
votes
1answer
165 views

Efficiently removing elements from list of permutations

I'm looking for an efficient way to remove entries from a list of permutations. I have a list of variables from which I calculate all possible permutations. I then want to remove those permutations ...
1
vote
0answers
27 views

Pattern match SubValues with unspecified number of arguments

Does there exist a single pattern that would match SubValues of arbitrary "length" with a given head? For example, I would like to match all of the ...
4
votes
1answer
50 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
713 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 ...
11
votes
2answers
251 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 ...
5
votes
2answers
277 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
53 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
250 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
44 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
73 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
45 views

Replacement rule does not recognize parity?

All of the following ...
1
vote
1answer
72 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
75 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
47 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
53 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
210 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
104 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
125 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
32 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
58 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
247 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
240 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
568 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
58 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
80 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
48 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
38 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
53 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
23 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 ...
6
votes
1answer
81 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
31 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
50 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 ...
5
votes
1answer
85 views

Delete parts of a linear array

I think this question has not been answered. Suppose I generate a random linear array with only two entries -1 or +1. From this array I want to delete all sequences that alternate such as {-1,1,-1,1,-...
0
votes
2answers
85 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
72 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
42 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
51 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, ...

1 2
3
4 5
26