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
0answers
62 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
45 views

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

Consider the following Pattern expression: ...
9
votes
2answers
224 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
72 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 ...
3
votes
1answer
106 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
25 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
20 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
38 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
36 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
49 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
61 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
67 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
75 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
50 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
37 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
48 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 ...
2
votes
0answers
37 views

Pattern with alternative heads

This works: ...
1
vote
0answers
40 views

Behaviour of PatternTest [duplicate]

I was surprised by the following behavior of PatternTest and I think that this may be instructive for other people too. Consider ...
1
vote
1answer
24 views

Why can't I get the argument for my base function from the transformed function?

Consider the following transformed function as an example: ...
2
votes
1answer
90 views

Why does this DeleteCases not work? [duplicate]

I believe this is simple but I couldn't figure out why this doesn't work. Everything looks good to me. ...
1
vote
1answer
49 views

Comparing parts of patterns by name

Let's say I wanted to pick out all the rows of a table for which columns 3 and 5 are equal. The following doesn't work, as the second x is treated as an independent ...
3
votes
0answers
64 views

What's the time / space complexity of pattern matching?

For example, in ReplaceAll[expression, pattern], what are the main algorithms used internally? How does the asymptotic time / space complexity scale with the length ...
0
votes
2answers
53 views

Extract subscript of variable in product

My input is a product of the variable x with index: x[1]^2 x[5] x[7] How do I extract the indices of the above product to get ...
3
votes
0answers
32 views

Unexpected behaviour with Coefficient

If I run expr = (CF G[0, zb])/(1 - zb) + (CF zb^2 G[0, zb])/(1 - zb) + (2 CF G[1, zb])/(1 - zb) + (2 CF zb^2 G[1, zb])/(1 - zb); Coefficient[expr, zb, -1] in ...
2
votes
0answers
56 views

How to produce an error message for each argument?

I want to print a helpful message for every wrong input. How do you compactly implement a function with custom pattern constraints, default values and error messages for each argument separately? For ...
0
votes
1answer
69 views

How to convert decimals to integers and joint them to a string for exporting

There's a list such as {0.1,2,0.15,5}. I want to know how to convert the decimal to integer( but begin with zero 0), and don't ...
1
vote
1answer
50 views

Differential not returning the answer

I have the following set of rules for differentiation. ...
4
votes
1answer
78 views

Simplifying the Summation

I have to sum: $$\frac{1}{2} x_{i,k} x_{j,\nu } g_{i,j,k,\mu }+\frac{1}{2} x_{i,k} x_{j,\mu } g_{i,j,k,\nu }+x_{i,l} x_{j,\nu } g_{i,j,l,\mu }+x_{i,\mu } x_{k,l} g_{i,k,l,\nu }$$ g and x are real ...
0
votes
0answers
70 views

Commutation relation

I have defined: COM[a_, b_] := a ** b - b ** a for commutator bracket for the operators. We also have: ...
2
votes
1answer
125 views

Operator products summing

Is there a way to perform the below summation in Mathematica? $$\frac{\left(\hat{X}_{ij}+\mathbb i\delta_{ij}\right)}{\mathbb{i}}\frac{\left(\hat{X}_{kl}+\mathbb i\delta_{kl}\right)}{\mathbb{i}}$$ I ...
1
vote
1answer
96 views

Summation without writing term by term

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link ...
0
votes
2answers
54 views

What pattern will distinguish an element of a list from a list when used in a function?

In Mathematica several versions of a function can have the same name with a pattern distinguishing which version of the function should be used for which type of input. Here is a minimum working ...
2
votes
0answers
30 views

recursively find StringCases

Can I recursively extract StringCases[] that match a specific pattern? Here is a minimalist example (not my actual problem): input ...
5
votes
1answer
149 views

Detecting patterns in a graph network

If we create a random graph Chop[LowerTriangularize[RandomReal[{-1, 1}, {10, 10}], -1], 0.6] /.0 -> \[Infinity] // MatrixForm WeightedAdjacencyGraph[%] How can ...
0
votes
1answer
62 views

Get position while matching pattern

I want following imaginary feature : PositionOf Example1) ...
2
votes
0answers
25 views

VertexShapeFunction with string pattern matching [duplicate]

I am trying to visualize a heterogeneous graph which nodes are categorical. Here is a dummy graph for implementation: Input is ...
2
votes
2answers
64 views

Checking if a polynomial contains a certain monomial

I want to check if a given polynomial contains a certain monomial while ignoring ordering of the variables. For example: $$P(a,b,c)=abc + bca$$ And then I want to check if $P$ contains $bac$ (should ...
1
vote
2answers
60 views

WhenEvent inside Ndsolve how to evaluate actions numerically every time the event happens

I need to force a whenevent action to be evaluated numerically all the times the event happens. The fact is that I use very complicated matrices and I need to invert them so the system requires a lot ...
5
votes
4answers
239 views

Selecting rows from two multi-column lists, where values are the same in a chosen column

If I have two multi-column lists, for example: ...
2
votes
2answers
190 views

How do you get substitution to recognize a derivative?

I have a simple expression involving a derivative: x[t_] = Q/T[t]; Simplify[Derivative[1][x][t] /. Q -> x T[t]] That produces:$$-\frac{x T'[t]}{T[t]}$$I have ...
3
votes
2answers
89 views

Comparison of Coefficients involving complex exponentials

I'm trying to solve a couple (independently) of equations of the following type $ a_0 x + 3 a_1 y(1 + xe^{i3q} + e^{-iq + 2ip}) + ... = 0, \forall x, y, q, p$. Using SolveAlways doesn't work well. So ...
4
votes
3answers
94 views

Cases in list with varying length in sublists

Given t1 = {{14, {{1, 2, 3}}}, {15, {}}, {16, {{0, 0, 4}}}, {17, {{0, 1, 4}, {2, 2, 3}}}, {18, {{0, 3, 3}, {1, 1, 4}}}, {u, {{1, 0, 2}, {2, 3, 0}, {2, 3, 4}}}} I ...
2
votes
1answer
51 views

How to use IntegerQ on list?

t1 ={0, 1, 2, Log[6]/Log[2], 3, Log[12]/Log[2], 4, Log[24]/Log[2]} I want to get the Position of the Integers. I can do it by <...
2
votes
0answers
58 views

How does Mathematica choose which function definition to use?

I was wondering what the process of matching function arguments arguments against different function definitions actually looks like for Mathematica. (Just to be clear, I'm not simply looking for an ...
0
votes
1answer
45 views
1
vote
0answers
48 views

“Compile-time” patterns

If I define a function like f[x : {_, _Integer}] := (* function body *) then, as I understand it, every time f[expr] is ...
3
votes
1answer
49 views

warning in Position + IntersectingQ

Why there is a warning message (with correct output) for the following code ? ...
3
votes
1answer
88 views

BlankSequence with Plus

When I execute f[g[a, b]] /. f[g[l__]] :> f[l] I get f[a, b] as expected. When I execute ...
3
votes
1answer
40 views

Generate replacement rule from user-defined pattern

I want to generate a replacement rule from a user-defined pattern. For example, the user provides a list of patterns {A[i_][j_], BB[k_]} and a user-defined function ...
6
votes
0answers
94 views

Exceeding recursion limit with no obvious recursion: Optional pattern-matching causing nonlocal behavior

So, I've come across something strange, and wanted to get a deeper understanding of what was going on. Consider the following code: ...

1
2 3 4 5
26