# Questions tagged [pattern-matching]

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

1,297 questions
Filter by
Sorted by
Tagged with
45 views

### pattern test on position

At the moment I'm using something like this to get the positions of entries of a matrix satisfying a specific value: ...
152 views

### Permutation avoidance function

It is common for me to see if a permutation of 1,2,...,n, avoids some fixed permutation pattern. For example, the permutation [1,4,2,3,5] contains the pattern 1,3,2, as the elements [1,4,3] appear in ...
89 views

### Patterns are not Sequences are not Lists, or: Global Function Argument Reversal

H[z_] := z /. {F[p__] -> F[Reverse[p]]} F[{x, y}] + F[{x, z}] + F[{}] // H Almost. It gives an annoying "Nonatomic Reverse" error message at the ...
24 views

### N on equation with numbered variables [duplicate]

Say I have a system of polynomial equations in the variables x,...,x[n]. Sometimes want to make such equations numerical (as in: the coefficients become floats) ...
128 views

### Why the two outputs are different?

My first code is {a, b, b, a, b, a, b, a, a, b} /. {___, x : Longest[PatternSequence[a, b] ..], ___} :> {x} and its output is {a,b,a,b}. The second code is ...
81 views

### Equality of expressions with different variable names

I have some multivariate systems of expressions that I want to check for equality up to allowing relabelling of the variables. For example the two expressions below would be the same: ...
68 views

### why combining 2 patterns in Alternatives in Cases do not give same output as when each pattern is used separately?

Given expression expr = y'[x]^3 + y'[x]*Log[y[x]*y'[x]^2] + y'[x]^(1/2) + x/y'[x]^7 $$y'(x)^3+\sqrt{y'(x)}+\frac{x}{y'(x)^7}+y'(x) \log \left(y(x) y'(x)^2\right)$$ ...
38 views

### Formatting expressions with numeric exponents using pattern matching

Thank you all in advance. Question: I want to change the format of this expression (n + r)^3/n^3 into this (1+r/n)^3using rules ...
277 views

### How to extract email addresses from a text file using Mathematica

I have a text file which I have uploaded here. I want to find all email addresses in this text file. I just know that some of them are after "Subscription Date" expression (I don't know if ...
82 views

### Mathematica does not recognize pattern when changing by numerical values

The following returns False: MatchQ[(-1 + (1 + E^-x)^0.1 )/(1 + E^-x)^0.2, ((1 + Exp[-x_])^v_. - 1)/(1 + Exp[-x_])^u_.] False And the following returns True: <...
46 views

### How to create a simple transformation function on lists using short form of ReplaceAll?

I can easily do this with Map so the real goal of this exercise is to get a better grip on using transformation rules. Please help with this. The outputs should be ...
36 views

### Converting parts of some string into another notation

I have a big string of data and most of them are in the form Name = number^{number}_{-number}, some are in the form Name = number \pm number. I what to convert the latter into the notation. For a ...
98 views

### Matching a Dynamic Argument Pattern

times is a list of times i.e. matching {{hour_, min_, sec_Real}..}. I want to call ...
36 views

### Can't match custom head in function signature [closed]

I have written a financial charting function which has the following signature; ...
52 views

### Pattern matching at multiple levels [closed]

Pattern matching is my favorite feature. Is it possible to destructure a symbol but still refer to the whole symbol? I mean something like (this won't work of course) ...
31 views

### Removing conditions on patterns from LHS of a rule

Let's consider a rule which takes conditions on the patterns specified on the LHS: In:= rule = j[n1_?NonPositive,n2_?Positive,n3_?Positive]:>AAA I would like ...
189 views

### How to set deeper patterns in arguments within function definitions. For example if an argument must be a list of 1 or more strings

Suppose I want to define a function whose argument must be a List of only Strings and List must have at least one member. I know how to require that the Head must be a List but how does one qualify ...
41 views

### Define multivariate even general function

I want to define a multivariate even function. This question shows how to do it for a single variable function Defining general odd function but I don't see how to generalize the syntax to multiple ...
154 views

### How not to Simplify square roots

Given t1 = {1/7 (4 + Sqrt), 1/15 (3 + 2 Sqrt), 1/2 (5 + 3 Sqrt)} I do not want to simplify the square roots. I want to get ...
43 views

### 'Cases' command does not recognize specified patterns

I am having a problem with the "Cases" command. If I execute the following code ...
50 views

### Derivative with respect to function of any argument

I have the expression L = v1[k1] * M[k1, k2] * v2[k2]. v1, M, and ...
71 views

### Association vs Pattern Matching: which one is better for data storage?

Say I have a few key-value pairs. I can store them with Association as ...
68 views

### Cutting of terms on denominators of giant expression

I have an immense expression of the form $$\sum_i \frac{f_i(X,Y,x,y)}{(a_iX^2+b_iY^2+c_ix^2+d_iy^2)(e_iX^2+g_iY^2+h_ix^2+k_iy^2)},$$ where at $a_i$ or $b_i$ AND $e_i$ or $g_i$ is different from zero....
135 views

...
67 views

### How to construct a pattern that match the multiple partial differential of any multi-variable functions?

For example,I want to match a multiple partial derivative of a function $f^{(1,0,1)}[x,y,z]$ which is the same as$\frac{\partial^2{f}}{\partial{x}\partial{z}}$.The only information given is the list ...
45 views

### How to make a vector of elements the pattern of a function?

if I have an expression that contains a lot of symbolic elements and I would want to make it a function of the vector containing all the elements I've found a solution based on rule delayed: ...
211 views

### using StringReplace and StringPattern to convert exp(...) to Exp[....]

I am looking at a file generated by another CAS. I'd like to convert it to Mathematica syntax. Only issue is how to convert exp(....) to ...
88 views

### How to make a function ignore an argument [closed]

Motivation I am trying to call a function f, that has either 1 or 2 (or multiple) parameters, from a function g that uses ...
138 views

### Fill in the StringBlank

I have a string that has blanks marked by a marker; (in this case, an underscore): "f_ath_r". I can execute the following command to get the word. ...
100 views

### Built-in functions on the LHS won't pattern-match

I have this pattern-matching problem: F[X_,myminus[X_]]:=0 F[1,myminus] Works. ...
54 views

### How to write a pattern that matches all linear combinations of functions involved except 0?

I am trying to write the simplest (and efficient) pattern that matches the linear combination of all functions except when the combination produces zero? Consider the following as an example: ...
45 views

26 views

### How to rearrange terms in an expression to a particular form (e.g. A/w-> A*(1/w)

I have a huge expression for which the numerators are a big mess, involving numbers, variables, formal sums and unevaluted function, let's call the numerators Ai, with i=1,2,...n This "beast"...
42 views

### Type formation rules for 1-position expressions

I made a summary of the rules for formation of types for simple 1-position expressions, as I understand them. Here it is in textual form: A symbol, say, α, is a discrete entity, represented by a ...
39 views

### What sorts of expressions can be Solve'd for?

We can Solve for expressions that match _Symbol, as in Solve[x^2+1 == 0, x]—but we can also ...
54 views

### Defining a two-variable function with a parity property

I would like to define a function that has the following property $f(-x,y)=f(x,-y)$. An example is $f(x,y)=x y$. For simple inputs, I can impose this easily enough as: ...
65 views

### How to Map over a BlankSequence? [closed]

I would like to replace any function f[x, y, ...] in a certain expression with the same function with all its arguments squared, i.e. ...
70 views

### How to do context-sensitive matching with the pattern system?

In Perl RegEx, we have the look-ahead/behind feature; it there a similar functionality available for Wolfram Language patterns? Re @mikado: Suppose we have a random expression in ...
77 views

### SequenceSplit around empty sequences is inconsistent

Bug introduced in 11.3 and persisting through 12.3 Only the first one recognizes a zero-length sequence between non-X's. ...
61 views

### Finding a mapping between two types of (generalized) hypergeometric series

I am given two functions, one is of the form $2F1(a,b,c;z)$, where $2F1$ is a hypergeometric series. The other one is a generalized hypergeometric series $3F2(d,e,f;g,h;w)$, where the characters are ...