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
0answers
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: ...
4
votes
3answers
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 ...
5
votes
1answer
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 ...
1
vote
0answers
24 views

N on equation with numbered variables [duplicate]

Say I have a system of polynomial equations in the variables x[1],...,x[n]. Sometimes want to make such equations numerical (as in: the coefficients become floats) ...
3
votes
1answer
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 ...
2
votes
2answers
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: ...
1
vote
1answer
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)$$ ...
0
votes
1answer
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 ...
3
votes
2answers
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 ...
2
votes
1answer
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: <...
1
vote
1answer
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 ...
0
votes
1answer
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 ...
0
votes
1answer
98 views

Matching a Dynamic Argument Pattern

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

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

I have written a financial charting function which has the following signature; ...
4
votes
0answers
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) ...
3
votes
1answer
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[1]:= rule = j[n1_?NonPositive,n2_?Positive,n3_?Positive]:>AAA I would like ...
7
votes
1answer
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 ...
0
votes
0answers
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 ...
3
votes
2answers
154 views

How not to Simplify square roots

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

'Cases' command does not recognize specified patterns

I am having a problem with the "Cases" command. If I execute the following code ...
0
votes
0answers
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 ...
3
votes
0answers
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 ...
2
votes
2answers
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....
1
vote
2answers
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 ...
1
vote
1answer
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: ...
5
votes
2answers
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 ...
1
vote
0answers
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 ...
7
votes
1answer
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. ...
3
votes
2answers
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[1]] Works. ...
0
votes
1answer
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: ...
3
votes
0answers
45 views

Preventing $-renaming of pattern names in lexically-scoped constructs

When you use a lexically-scoped construct like With or Function to perform replacements in an expression, Mathematica might ...
1
vote
1answer
37 views

user-defined functions and "pattern" being returned [closed]

I define a function as f[J_] := Exp[J^2/2\[Mu]] but when I try to taylor expand as k[J_] = Series[f[J] ,{J,0,2}] I'm getting ...
2
votes
0answers
50 views

Simple questions about Pattern and MatchQ

1st Question : Note that ...
7
votes
0answers
65 views

Why does Longest not work on OptionsPattern?

When having a function with optional arguments and options, I frequently find myself writing something like this: ...
2
votes
1answer
46 views

Is there a way to extract a sub pattern from a repeated PatternSequence?

Consider the following Pattern expression: ...
9
votes
2answers
239 views

Pattern matching product with arbitrary number of terms

I am trying to write a function to convert an expression of the form $$\alpha x_1^{n_1} x_2^{n_2} ... x_N^{n_N}$$ (with some coefficient $\alpha\in\mathbb{R}$) into a list of indices and powers $$\{\{...
3
votes
1answer
77 views

Pattern matching problems involving FullForm and Plus and Times

I am trying to match an expression that will be a sum of more than one terms, with the last one being a product. For example, the objecta+b+x*y. I am using the ...
4
votes
1answer
116 views

Alternatives in pure function construction

When I declare a function f in the following form: f[a|b] := 1 then I can use it like: ...
0
votes
0answers
32 views

Issues in expanding expressions with formal sums

I have a very long expression involving formal sums to infinity, unfixed functions and several variables. In this very long expression, I need a typical term, like $\frac{6 A \sigma \sum _{n=0}^{\...
0
votes
0answers
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"...
3
votes
1answer
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 ...
2
votes
0answers
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 ...
1
vote
1answer
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: ...
3
votes
1answer
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. ...
3
votes
1answer
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 ...
4
votes
1answer
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. ...
0
votes
1answer
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 ...
1
vote
3answers
38 views

How can I let StringPosition consolidate overlapping matches?

Consider an example like this one: StringPosition[" -aa.ef -ag.hi -aa.ei", RegularExpression["\\s+-(?=a+\\.)"]] which returns ...
3
votes
1answer
63 views

How can I let RegularExpression remove all colors from its regex-input string before processing it?

Regular expressions are a very powerful tool, but such expressions are often very cryptic. If you want to understand them, it is best to have colored pencils at hand to color-code the parts that ...

1
2 3 4 5
26