Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
8
votes
2answers
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, ...
2
votes
2answers
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 ...
5
votes
1answer
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 ...
9
votes
2answers
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 ...
2
votes
1answer
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}$. ...
2
votes
0answers
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 ...
4
votes
1answer
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 ...
2
votes
1answer
41 views
4
votes
2answers
70 views

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

Here's an example input list: ...
5
votes
2answers
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 ...
6
votes
6answers
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 ...
2
votes
2answers
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 ...
18
votes
7answers
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 ...
6
votes
2answers
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 ...
2
votes
0answers
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 ...
2
votes
0answers
42 views

How to find consecutive things in nested expressions?

To find things that are consecutive you can use SequenceCases: ...
4
votes
1answer
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 ...
2
votes
1answer
76 views

How can Cases detect x as Times[1,x]

I'm trying to use Cases to extract the coefficients of a polynomial $$x^5+6x^4+2x^3-8x^2+x+10$$ I was hoping I can first find the coefficients of the $x,\,x^2,\,...,...
1
vote
1answer
59 views

Extract coefficients from ODE

I'm trying to extract the coefficients from an ODE $$y''''+y''+x^2 y'-6y+ 8\ cos(x)$$ and hoping the code will give a list with all the coefficients in, ranging from lower order to higher order, i.e. $...
4
votes
2answers
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 ...
-1
votes
2answers
106 views

How to take the negative sign in a string

What pattern should i give to the following expression ("146d0" A + "-594d0" B) so i would get ("146d0" A - "594d0" B)
0
votes
0answers
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 ...
0
votes
0answers
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 ...
3
votes
1answer
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][[1]] == 0) :> 1}, {4, 4}] This should give a $4\times 4$ matrix in which the ...
2
votes
1answer
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 ...
6
votes
1answer
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, ...
0
votes
0answers
35 views

making replacements when patterns don't match exactly

I want to simplified some mathematical expressions, and they are similar to ...
7
votes
1answer
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 >=...
1
vote
1answer
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. ...
8
votes
3answers
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 ...
2
votes
1answer
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: ...
2
votes
0answers
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 ...
2
votes
2answers
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 ...
2
votes
1answer
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, ...
8
votes
3answers
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 ...
13
votes
6answers
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 ...
6
votes
1answer
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 <...
0
votes
0answers
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 ...
1
vote
1answer
37 views

Compile string cases into list

I have a html formatted string: ...
1
vote
0answers
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 ...
1
vote
0answers
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 ...
8
votes
2answers
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: ...
0
votes
2answers
53 views

Monte Carlo Method [duplicate]

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

Creating rule-based sublists

I have a list each element of which consists of a list containing two strings and two integers: ...
2
votes
1answer
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-...
3
votes
3answers
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 ...
7
votes
0answers
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 ...
2
votes
2answers
90 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 ...
0
votes
0answers
26 views

Why are unrelated symbols localized in Module? [duplicate]

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