Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
48 views

Integration by parts

I know this question is already present in many variations, and it seems that for each one you have to define your own rules, and I am struggling to invent them in this case. I want to integrate a big ...
2
votes
0answers
28 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
93 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
56 views

Get position while matching pattern

I want following imaginary feature : PositionOf Example1) ...
1
vote
0answers
22 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
52 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
55 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
233 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
179 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
86 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
88 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
47 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
54 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
43 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
46 views

warning in Position + IntersectingQ

Why there is a warning message (with correct output) for the following code ? ...
3
votes
1answer
87 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
39 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 ...
5
votes
0answers
73 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
vote
1answer
45 views

Use of v_ in SetProperty for. graphs [closed]

I found the following code (I forgot to mark the link--it was on the topic of displaying two values at a graph vertex): ...
2
votes
1answer
41 views

Replace a Condition within an expression

I have a simple expression containing a Condition(s) like a /; a > 1 I wish to separate the expression from the condition, ...
5
votes
2answers
118 views

OptionsPattern does not match rule with compound left hand side

OptionsPattern was introduced in Mathematica v6 so I have finally decided to give a try. Look at this program: ...
9
votes
3answers
216 views

Is there any other way to access the n-th element of a BlankSequence x__?

Consider a named BlankSequence x__. How can I access the n-th element? Is {x}[[n]] the only ...
4
votes
1answer
181 views

Is it possible to combine the following pattern?

I want to simplify the following pattern Cases[Permutations[Range@3], {___, 2, 3, ___} | {___, 3, 2, ___}] into something like (but it does not compile). ...
11
votes
5answers
459 views

Pattern matching for repeated elements

Is there a pattern that can match repeated elements that appear more than twice in a row? For the sake of example let's say I want to use pattern matching to delete subsequent duplicate: ...
0
votes
1answer
56 views

Rename a variable in a pattern by an evaluation

I wish to take some expression containing instances of Pattern[], and rename the variables of those patterns based on evaluation featuring the variable. Let's say ...
5
votes
3answers
183 views

Finding beginning of end of sequences in a list

I have some data in a list that are non-numeric. I want to replace the bad data with a linear interpolation from the surrounding data. The non-numeric data can be easily identified. There may be more ...
4
votes
3answers
161 views

Identifying rows and columns of a matrix that satisfy a specific feature

Originally, I have a matrix of dimension (1040, 1040). This a very large matrix to visually inspect some features. I mimic the matrix of concern as: ...
0
votes
1answer
42 views
0
votes
1answer
38 views

Using ReplaceAll to change terms that match a pattern

Suppose I have a list like {a,b [Subscript[e, 1],c [Subscript[e, 1,2]. (1) Sometimes I need to replace the e-subscripted terms, (2) sometimes I need to replace ...
0
votes
1answer
54 views

Pattern matching tuples as inputs to pure function

I've been having an issue with functions which are supposed to receive as input a list of tuples. My troubles are two-fold: The function doesn't properly identify the pattern test I laid out in the ...
3
votes
3answers
44 views

Find all instances of a function with a given head and replace their arguments

I'm trying to pattern-match all the functions with head Sin in list and replace their single input with a value. ...
3
votes
2answers
120 views

Single expression containing the same pattern multiple times

I have an expression like this Exp[-x^2]^(Log[a+3])*Sin[x]*y^(x)*Sin[Cos[b]], and I want to match the pattern ...
1
vote
0answers
60 views

Alternatives not working with Sequence[]?

In the following piece of code I've specified two possibilities to match via Alternatives, but only one of them seems to be considered: ...
7
votes
1answer
105 views

StringMatchQ fails using Alternatives with complex pattern

Alternatives seems to work fine with these simple patterns ...
5
votes
6answers
132 views

Highlight elements in the list using pattern matching

Starting from the following list: list = {{a, b, c}, {d, e, f}, {g, h, i}, {b, c, d}, {c, a, m}, {c, d, n}}; I want to ...
3
votes
1answer
47 views

Group pattern using parenthesis

I have a very long list of data. Below is a simplified version data = {junk1, textHeader, moveTo, text, moveTo, text, junk2, textHeader, moveTo, text, junk3} I ...
0
votes
1answer
37 views

How to transform a rational function to get desired factor

I have an expression like this -t^-b + (t - t^(1/2 + 1/(8 k)))^-b I would like to transform it to get ...
6
votes
0answers
101 views

Difference between the infinite rewriting behavior of evaluation and ReplaceRepeated

In the documentation tutorial/Evaluation it's said that: Every time the expression changes, the Wolfram Language effectively starts the evaluation sequence over ...
2
votes
2answers
77 views

Matching several indexes

I have a large set of data in which I need to compare several samples in different tests and under varying conditions. I am looking for a way to pair and analyze these easily. As an example, lets say ...
1
vote
0answers
75 views

The danger of pattern matching?

I'm reading the book Mathematica® programming: an advanced introduction by Leonid Shifrin and there is a very nice evaluation. Here is: This, plus a large number of quite generic and efficient ...
6
votes
4answers
289 views

Converting an equation with units to one without units

I may have a solution but it is not slick! See below Doing numerical calculations with functions that use units can be very slow. I would like to have a function or operator that can turn my functions ...
7
votes
2answers
255 views

How to select the fastest approach for large numerical data computations?

I really love the flexibility of Mathematica: there are several ways to perform one task. However, to get the performance of the intense numeric calculation, it can ...
3
votes
1answer
67 views

Why `CenterDot[var_] := var` with `Flat` attribute induces infinite recursion? [duplicate]

I am trying to define some rules for CenterDot. One thing I don't understand is that if I define ...
3
votes
2answers
81 views

using DeleteCases with CoprimeQ

first let me show what I have working correctly f = Permutations[Range[5], {3}] Riffle[f, Apply[CoprimeQ, f, {1}]] now I would like to automate the deletion of a ...
6
votes
4answers
507 views

Replace sum x+y+z in expressions like 2x+3y+z

Related threads replacing-a-sum-of-expressions and Replace a sum of squared variables by a new squared variable Given the identity $x+y+z=p$ I'd like to simplify the generic expression $$kx+ly+mz$$ ...
0
votes
1answer
55 views

Pattern Matching BlankSequence

Consider the definition: Clear[fun] fun[a_ + b_] := fun[a] + fun[b]; fun[a+b+c] (*fun[a] + fun[b] + fun[c]*) This works as expected. However, if we use a ...
0
votes
0answers
36 views

Are not rules applied in succession?

I am trying this: ...
1
vote
1answer
70 views

Quantum Mechanics operators

$$\sigma_{z}^{A}|\downarrow\rangle_{A}=-|\downarrow\rangle_{A}$$ $$\sigma_{z}^{A}|\uparrow\rangle_{A}=|\uparrow\rangle_{A}$$ We have the above relations in Quantum mechanics. Is there a way to ...
3
votes
1answer
155 views

Replace in Mathematica [closed]

I have: $$X=Z+22*Y$$ $$Y=P$$ Edited: Is there a way to replace $Y$ in the first expression using Mathematica? I tried Replace and it's not giving the desired result. Can I replace both Z and Y at the ...

1
2 3 4 5
25