Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
5
votes
3answers
190 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
171 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
44 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
89 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
52 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
133 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
72 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
109 views

StringMatchQ fails using Alternatives with complex pattern

Alternatives seems to work fine with these simple patterns ...
5
votes
6answers
136 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
51 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
39 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
110 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
83 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
86 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
347 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
270 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
70 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
94 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
515 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
60 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
37 views

Are not rules applied in succession?

I am trying this: ...
1
vote
1answer
103 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
210 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
vote
3answers
76 views

My rule does not do anything (understandably)

I want a rule that would transform, for example, And[i,F[j,k],l,G[m,F[j,k],n],p] into And[i,F[j,k],l,G[m,T,n],p], where ...
7
votes
1answer
210 views

How to replace only the isolated variable x

I want to replace the variable x in the result, not x in unrelated x[i]. ...
11
votes
2answers
382 views

Making a faster alternative for {PatternSequence[1, PatternSequence[2, 3 ..] ..] ..}

I need to improve a pattern or switch an approach. It is best described by an example For a hierarchy/order given by a list e.g.: order = {1, 2, 3} and a list: <...
0
votes
3answers
110 views

Pattern matching involving a sum of wildcards

I am trying to implement the following substitution to an expression in Mathematica: ti den[a-ti] -> -1 + a den[a-ti] where ...
2
votes
2answers
421 views

How can I fix my Simpson's rule integration code?

I was tasked with writing a function on Mathematica similar to NIntegrate, but that uses the Composite Simpson's Rule as its method for doing the calculation. The ...
3
votes
2answers
100 views

Vector entry matching problem

I have a list composed by vectors. Let's say with five entries in each one. The list can have any dimension. ...
3
votes
1answer
92 views

Replace Condition in Replacement Rules

In finding an answer to my other question, I'm finding myself needing to manipulate the conditions imposed on replacement rules, but this is proving to be a little difficult. In particular, how ...
0
votes
0answers
32 views

Except not working in Function [duplicate]

I have defined the CirclePlus operator as follows: ...
7
votes
1answer
195 views

Pattern-matching complex numbers as a + bi

I just started working with Mathematica and am toying with pattern matching. There may be something obvious I'm missing in this, but I can't figure it out by myself. I want to write down a function ...
5
votes
1answer
77 views

How to define a parameter with both Optional and PatternTest

Why this doesn't work? k[x_Integer:2?(#>0&)]:=x^2 k[] (*should be 4*) k[2] (*should be 4*) These following are all working as expected: ...
6
votes
5answers
497 views

Sum all the adjacent values in an array, with some conditions

I would like to sum all the adjacent values in an array that are different from 0, then replace those values with zero, apart from the first value which should be the sum. For example having an array ...
1
vote
0answers
55 views

Importing multiple files setting the name with tostring

I'm trying to import several matrices, which contain a pattern in the name. I create first their names in a table ...
1
vote
0answers
55 views

Simplification and patterns

I have a very simple simplification to make, where I need to do a replacement but the pattern matching seems slightly awkward. I would like to simplify: ...
4
votes
1answer
96 views

KenKen Puzzle Helper - Dropping order-less sequences

In the following example, I am generating all the variants of a $9 \times 9$ KenKen puzzle that come in groups of three using addition that result in $18$. ...
1
vote
0answers
53 views

ReplaceAll with named pattern [closed]

If k is unassigned, this yields {a, b, c}: ...
1
vote
0answers
35 views

Difference in pattern matching between Cases and ReplaceAll

I don't understand why there is a difference in the pattern matching between x a /. coef_*x^pow_. :> {pow, coef} and ...
4
votes
2answers
192 views

How to divide a list into several single loops

Because of some problems, I need to divide the permutation represented by a list into several single loop lists. For example, for list {4, 3, 2, 1, 7, 6, 5}, it can ...
2
votes
1answer
81 views

matching Inequalities

Consider the following list list={0 < x < 1/12, 0 < x < 1/4} I wish to find the intersection of the two ranges of x ...
0
votes
1answer
64 views

Solving indexed ODE in Mathematica

I would like to solve an ODE with indexed variables symbolically. Is there a way to do that? $$\frac{dp_{\beta\alpha}}{dt}=m\left(p_{1\alpha}p_{\beta2}+p_{2\alpha}p_{\beta1}\right)$$ m is called mass ...
3
votes
2answers
201 views

How to set a rule that's too deep for UpValue?

I want to automatically transform Conjugate[a CenterDot[x, y, z]] to Conjugate[a] Conjugate[CenterDot[x, y, z]]. However, the ...
1
vote
1answer
105 views

Dummy index automatic summing

https://mathematica.stackexchange.com/a/222410/73364 I have obtained the following code from the above link. But it is not working well with my situation. Can anyone help me out? ...
4
votes
1answer
288 views

Symbolic second order derivative of real antisymmetric matrix in Mathematica

I would like to calculate the second order derivative of a function with respect to a matrix coefficient symbolically. Below is the first order derivative of S with respect to the coefficient of x. ...
1
vote
0answers
53 views

What is the difference between form and pattern in the documentation?

In the documentation, I see the term form and the term pattern. For example, see Cases and MatchQ for both usages. What is the ...
4
votes
1answer
195 views

Efficiently removing elements from list of permutations

I'm looking for an efficient way to remove entries from a list of permutations. I have a list of variables from which I calculate all possible permutations. I then want to remove those permutations ...
1
vote
0answers
27 views

Pattern match SubValues with unspecified number of arguments

Does there exist a single pattern that would match SubValues of arbitrary "length" with a given head? For example, I would like to match all of the ...
4
votes
1answer
50 views

How to substitute an expression in side a particular head or inside a particular pattern?

Let say I have any expression ex= f[h[r[x y]]] g[x z] and I want to replace the x with w, ...

1 2
3
4 5
27