Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
7 votes
2 answers
327 views

Why does Blank sometimes not match the empty expression?

Consider these examples: ...
user7427029's user avatar
0 votes
1 answer
68 views

PiecewiseExpand fails to address assumptions

PiecewiseExpand seems to ignore more complex assumptions. Here's an example: ...
user46831's user avatar
  • 633
1 vote
0 answers
57 views

DigitQ, LetterQ, UpperCaseQ, LowerCaseQ on empty string

Is this a bug or I am missing something? #[""] & /@ {DigitQ, LetterQ, UpperCaseQ, LowerCaseQ} ...
azerbajdzan's user avatar
  • 9,568
3 votes
1 answer
110 views

Switch with string patterns

I'm trying to use Switch to process lines from a text file. I know that StringMatchQ["abc", "a"~~___] ...
Aufheben's user avatar
  • 133
3 votes
1 answer
125 views

Pattern matching vs. condition in a function

I'm trying to learn stuff from pattern matching but I found sometimes pattern matching does not work as I expected. Could anyone explain why the below outputs are not same? Thank you. And could anyone ...
Josh's user avatar
  • 31
0 votes
1 answer
40 views

Transform a list of delayed rules with conditions to a new list of delayed rules

I have a list of rules that implement some abstract algebras, for instance, that of the Pauli matrices, ...
Lelouch's user avatar
  • 513
2 votes
1 answer
51 views

ToString[n] in pattern replace does not take the value of n

I would like to convert expression to C++ code. Based on these demostrations. ...
eason's user avatar
  • 197
2 votes
1 answer
51 views

Is it possible to ask Mathematica to give/find this largest (absolute value) argument of $\cosh$ in the expression?

I have the expression exp in terms of Cosh functions: ...
MsMath's user avatar
  • 137
2 votes
1 answer
48 views

How to apply a transformation to a total derivative?

I want to transform total derivatives into normal derivatives: Dt[r, t] == -r*Dt[v, x] - v*Dt[r, x] /. Dt[f_, s_] :> D[f[x, t], s] To get: ...
user6552's user avatar
  • 363
5 votes
1 answer
201 views

How to Replace 'delta t' with 'a' in Mathematica Without Affecting Separate Instances of 'delta' or 't'?

In Mathematica, how can I replace instances of delta t in an expression with a, while keeping instances of ...
Tokubara's user avatar
  • 215
6 votes
1 answer
304 views

Why does this pattern not match?

Why does the following match? MatchQ[HoldPattern[f], HoldPattern[_]] True But the following does not: ...
Daniel Huber's user avatar
  • 47.1k
7 votes
6 answers
483 views

Apply function to all first arguments of a nested list of rules

Currently, I have a nested list of rules of the form lst1={1->a,2->{3->c,4->d,5->{6->f,7->{8->h,...}}}} (where I don't know the length of ...
cstover's user avatar
  • 323
1 vote
0 answers
98 views

Optimize grouping of list of lists by elements

I have a sorted list of lists of size ~200,000 x 3. The data are such that the first element of each triplet loops through i = 1, 1.1, 1.2, ..., 199.9, 200, and for every value of i, the second ...
TradeMark's user avatar
1 vote
2 answers
88 views

How to break a term in its individual factors?

I have some algebraic term that looks like this, Conjugate[Subscript[u, 1, 1]] Conjugate[Subscript[u, 2, 1]] \!\(\*SubsuperscriptBox[\(u\), \(1, 1\), \(2\)]\) $$...
Endeavour 's user avatar
1 vote
1 answer
81 views

Applying matrix commutation rules

I want to symbolically replace occurences of matrix terms such as $zAB-zBA$ with $zC$, where $z$ is a number. This works as expected: ...
Yashman's user avatar
  • 13
2 votes
3 answers
136 views

Replace only some instances of a symbol in an expression depending on what it's multiplying it

I have an expression of the type expr = k*a + j*a + a*f + a*g + h And I would like to set a=0 but ONLY if it's NOT multiplied ...
Bibliotebarbarian's user avatar
0 votes
2 answers
126 views

Einstein summation pattern matching

I have an expression of the form expr = $(A\cdot B)\;(C\cdot D)\;(F\cdot G)$ where $A, B,C, D, F, G$ are all vectors. I would like to use Einstein summation convention for each dot product and then ...
Harken's user avatar
  • 95
5 votes
2 answers
207 views

Crafting a replacement rule to convert derivatives to finite differences

I need to construct a replacement rule to replace (first) derivatives with centered finite differences. What I've got so far is ...
Chris K's user avatar
  • 19.5k
2 votes
2 answers
60 views

MatchQ strange behavior

While checking for a pattern I came across this test: MatchQ[-A.Grad[Subscript[f, 1, 1],k], ___.Grad[Subscript[f, x_,x_],_]] which returns ...
Harken's user avatar
  • 95
5 votes
6 answers
160 views

Generate all possible lists of integers that match a pattern

I have a pattern defined using variables: {a,b,a,b} and I want to generate all possible instances of this list where each variable ranges from 1 to n, and each ...
David's user avatar
  • 139
0 votes
0 answers
36 views

Commutator for Solving Coefficients in a Hamiltonian

I am relatively new to Mathematica but I understand the basics. I want to solve for coefficients by commutator constraints and I have problems implementing the commutator. The setup is the following: ...
iron's user avatar
  • 1
1 vote
2 answers
83 views

Pattern matching for an algebraic equation

I have question related to pattern matching of a complex algebraic equations. If I am about to extract certain terms from an algebraic expressions that contains mixture of terms $\nabla_\gamma 𝑓_{\...
Harken's user avatar
  • 95
1 vote
1 answer
81 views

Simplifying algebraic expression using pattern

Given the following expression expr = x Cos[y] + x^(2 y) + x y + Sin[x^2 z Cos[y]]; I would like to achieve the following output: ...
Harken's user avatar
  • 95
0 votes
1 answer
42 views

Dealing with commutative, noncommutative variables and patterns with NC Algebra

I use NC ALgebra to deal with polynomials in non commutative variables ei and fj. The eis do not commute with themselves, the fjs do not commute with themselves, but the ei commute with the fjs. So e2 ...
Goupildz's user avatar
5 votes
2 answers
154 views

Element position and repetition syntax

Please give me some advice. There is box[x,y] and this is consisting of {{a1,{b1,c1},d,e},{a2,{b2,c2}}} elements. For example, box[x] is list like this.(and Sometimes there is empty set) ...
hare's user avatar
  • 294
0 votes
1 answer
50 views

Simplify expression with function with symmetric arguments

I have the following expression $$ \frac{1}{2}\partial_\nu\partial_\xi h^{\nu\xi}-\frac{1}{2}\partial_\nu\partial_\xi h^{\xi\nu}+ \partial_\xi\partial_\nu h^{\xi\nu}- \frac{1}{2}\partial^\nu\partial_\...
jojo123456's user avatar
1 vote
2 answers
100 views

Replacing Kronecker delta times expression with the expression with inserted index

Consider the following symbolic expression: expr = f[a,b]*g[c,d]KroneckerDelta[a,m]KroneckerDelta[d,k] + KroneckerDelta[a,p]f[x,a]+const Could you please tell me ...
John Taylor's user avatar
  • 4,632
3 votes
2 answers
187 views

Replace a column/row of a matrix under a condition by a random number

Lets say there is a matrix of the form: matrix = {{1,2,3},{4,5,6},{7,8,9}} I want to replace the elements of the last column, if they are larger then zero, by the ...
100xln2's user avatar
  • 427
1 vote
1 answer
64 views

Can not get OptionValue with SetDelayed variable

ClearAll[foo1]; foo1[OptionsPattern[]] := Hold[OptionValue[""]][[1, 1]] foo1[] gives function name foo1 But ...
matheorem's user avatar
  • 16.9k
1 vote
0 answers
49 views

Replacing a symbolic fraction in a matrix by pattern matching

I have the following 4x4 symbolic matrix ...
TDH's user avatar
  • 429
4 votes
2 answers
123 views

Selecting sublists from a ragged array of lists of strings

A ragged array of strings with a mix of sublists is processed to output some sublists and not others. The sublists to output have a date, data, and information; in no particular order. The information ...
ChuckD's user avatar
  • 51
1 vote
1 answer
69 views

How to collect products of DiracDelta functions?

After taking a FourierTransform, I have the following expression containing multidimensional products of delta functions (what I am actually working with is far longer than this excerpt): ...
nzh's user avatar
  • 113
4 votes
3 answers
211 views

Find shortest subsequence

We will deal with arrays of chars, but not strings! Let we have the next three arrays: ...
lesobrod's user avatar
  • 1,197
2 votes
1 answer
63 views

How to match all zero valued cashflows?

I have a function that needs to identify cashflows with zero value: MatchQ[Cashflow[{{0,0},{1/2,0.`},{1,0.`},{3/2,0.`}}],Cashflow[{{_,c_/;Chop[c]==0}...}]] The ...
user13892's user avatar
  • 9,113
0 votes
0 answers
40 views

Is this behaviour of pattern ordering of OptionsPattern stable and documented? [duplicate]

My version is 13.2. This behaviour of OptionsPattern together with `` is extracted from some complicated functions. Motivation <...
Lacia's user avatar
  • 2,208
5 votes
2 answers
89 views

Is there a difference between a (general) pattern and a string pattern?

I have a list myList that contains several strings. I wish to use Position to find the positions of strings in ...
Andrew's user avatar
  • 10.4k
1 vote
3 answers
67 views

Functions with a greek, subscripted argument [duplicate]

I am working on a problem with a lot of (greek) variables and it makes sense to sub-script them. Some of them must appear in functions and I am having trouble using greek, subscripted variables ...
villaa's user avatar
  • 187
0 votes
1 answer
69 views

Finding a pattern an exact number of times and excluding patterns

I have a list of expressions which are multiplications of different types of objects. The question is broadly about how I can look for a pattern an exact number of times and exclude patterns from the ...
michelangelov's user avatar
0 votes
0 answers
55 views

Using Count[] on a numerical list with approximate numbers

I have a matrix, SpinOp, for which I obtain the Eigensystem using {evalSpinOp,evecSpinOp} = Eigensystem[SpinOp]. I need to ...
Nitin's user avatar
  • 431
5 votes
0 answers
84 views

Using Cases to select expressions from a list

I am using v12.2.0 on Win7-x64. I want to use Cases to select the Times and Plus cases from ...
Syed's user avatar
  • 41.4k
0 votes
1 answer
26 views

Given input expressions, extract the base and make it as a list

Given input expressions, I want to extract the function and again extract its element. For example, given my input ...
phy_math's user avatar
  • 801
1 vote
2 answers
67 views

Assigning a value to a functions argument

I´m struggeling now a few hours on a very basic problem. I want to assign the value 0 to variable i, if i has another value, then it should be changed to 0 in the function. Function ...
Roberta's user avatar
  • 33
2 votes
0 answers
43 views

Assign a name for OrderlessPatternSequence

...
Crack-Hu's user avatar
2 votes
2 answers
80 views

Pattern match and then replace the argument

I've want to replace the pattern, say I the arbitrary function $o$ that takes one argument, now, if the two of these function meet in a expression, one with some arugment $B$ and the other with the ...
Nitaa a's user avatar
  • 698
1 vote
0 answers
72 views

DeleteCases/ReplaceAll and PatternSequence [closed]

I thought it must be ...
imida k's user avatar
  • 4,047
9 votes
6 answers
725 views

Can I tell DeleteCases not to delete function arguments?

Given list = {1, 2, f[3, 4], {5, {6, g[7, h[8, 9]]}}, {f[10], 11}}; I want to get {f[3, 4], {{g[7, h[8, 9]]}}, {f[10]}} just ...
expression's user avatar
  • 5,562
8 votes
1 answer
130 views

Replacing 2D Patterns in 2D Lists | Markov Programming Language

Recently I came across with a probabilistic programming language where programs are combinations of rewrite rules - Markov Junior I think it is quite interesting to try out implementing some features ...
Kirill Vasin's user avatar
  • 1,177
1 vote
1 answer
76 views

Reducing roots of power sums

Often I encounter expressions like for example Root[ 1 - # + #^2 - #^3 + #^4 - #^5 + #^6 &, 3 ] or ...
Gert's user avatar
  • 1,470
2 votes
2 answers
114 views

Remove sublists with duplicate elements within

I have this list DeleteDuplicates[Sort[#] & /@ Tuples[Range[4], 3]] which gives me {{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}, {1, 2, 2}, {1, 2, 3}, {1, 2, 4},...
Ruth Lazkoz's user avatar
2 votes
1 answer
77 views

Pull out numerical factors from the argument

I'm trying to define a function that is distributive over Plus and pulls out the numerical factors from the argument: ...
Albercoc's user avatar
  • 966

1
2 3 4 5
30