Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
1 vote
2 answers
213 views

How to define replacement rule for Minkowski inner product

following question, I have an expression that consists of several Minkowski inner products between 2 four vectors written as in Mathematika: ...
5 votes
3 answers
133 views

$Assumptions = _ ∈ Reals results in incorrect simplification of ConjugateTranspose[..]

The linear combination $Q$ of two Hermitian matrices $A^\dagger = A, B^\dagger = B$ with real coefficients $m,n \in \mathbb{R}$, i.e. $Q = m A + n B$ is itself a Hermitian matrix $Q^\dagger = Q$. ...
  • 53
3 votes
1 answer
69 views

Deleting list of expression by rules

I have a list of expression: ...
3 votes
1 answer
77 views

Transforming A Complicated Expression

I am trying to transform an expression according to these rules I've defined: $$ L_1^m L_2^n L_3^p L_4^q \rightarrow \frac{m!n!p!q!}{(m+n+p+q+3)!}6V \\ \text{constant } k \neq 0 \rightarrow kV$$ Some ...
4 votes
1 answer
61 views

How to check if all elements of a list contain an expression, with 'MatchQ' or otherwise

Sorry, I am new user of patterns. I thought that the check: MatchQ[{-y \[Gamma], -y z }, {__?Times[y, __]}] would produce True, ...
  • 1,202
4 votes
2 answers
110 views

Replace vs. ReplaceAll involving Flat

I'm aware that Replace and ReplaceAll behave differently as discussed in this question, but still get confused with the ...
  • 667
3 votes
2 answers
173 views

Equivalent of Nothing for patterns

Let's say we want a pattern, that matches, when expression contains some terms but it can optionaly contain some other terms. For example: ...
0 votes
0 answers
33 views

How to group or factor out exponential terms under complex i paranthesis

I am not able to group the terms of the below two Gaussian exponentials according to e^(const*(r1^2+r2^2))+e^(const* I*(r1^2-r2^2))+e^(const*(r1-r2)^2). Note that (r1^2-r2^2) paranthesis has complex I ...
3 votes
2 answers
72 views

Extracting values from association via Cases

related: Extracting values from Associations I have an association of the form temp = <|"u" -> -5, "v" -> -5|> -> -30 and want to ...
  • 5,495
2 votes
1 answer
79 views

How to group or factor out exponential terms of an integral result

I would like to group the following integral result according to e^(r1^2+r2^2)+e^(r1^2-r2^2)+e^(r1-r2)^2. I've tried Collect, Factor, FactorTerms, doThat, Simplify...etc. but no luck. Here is the ...
5 votes
5 answers
208 views

Translate selection from one list to another

I wanted to know the most efficient and less resource consuming way to do the following: first, use the Select function to filter some values out of some list, e.g. ...
  • 209
1 vote
1 answer
61 views

Clarification of the $MemberQ$

There are a number of expressions: ...
  • 1,893
0 votes
1 answer
86 views

Replacing a term according to a given criterion in an expression with non-commutative multiplication (for any sign and location)

There is the following expression: ...
  • 1,893
2 votes
1 answer
77 views

Pattern not matching BoundaryMeshRegion

Mathematica 13.1 on Windows 10. Can someone explain why the following does not match: MatchQ[BoundaryMeshRegion[Cube[]], BoundaryMeshRegion[__]] (* False *)
  • 29.5k
2 votes
1 answer
111 views

Can you use superscripts as variable and function names?

I want to express the following function: $z^{(1)}=w^{(1)}a^{(0)}+b^{(1)}$ I have read in Mathematica documentation that Symbolize can be used to define variables ...
  • 233
1 vote
0 answers
27 views

How to best query if an expression contains Symbol/variable

Say I have this expression, among many: ...
5 votes
2 answers
104 views

StringCases with string replace and with escape characters

I want to extract url address of all css files inside HTML source. First output is as expected. But why second and third outputs are different? Notice that only "href" was removed from ...
  • 3,840
1 vote
3 answers
89 views

Extracting elements from a List in a specific way

I have a set of polynomial expressions in multiple variables: ...
  • 193
3 votes
1 answer
52 views

Is it possible to name a pattern within a repeating pattern during function construction?

Question I would like to know if it is possible to have a named pattern within a repeated pattern during function desctiption. For example: f[(a_)..]:= Why I am ...
5 votes
2 answers
245 views

What is a nice way to convince Mathematica to use $\tau$ instead of $\pi$ in evaluated formulas?

I have tried like Replace[something, {2*π -> HoldForm[τ]}] Although it works well where a lone 2 π symbol, it fails to ...
0 votes
0 answers
30 views

ToExpression in nested Set/SetDelayed

Suppose we have the following definitions. ...
3 votes
2 answers
142 views

Find the smallest subexpression containing all subexpressions matching a given pattern

I need to write a function that, given an expression like 1/x + 1/(Abs[x] + 1/(1 + Abs[y - Abs[x]])) finds all its subexpressions matching a certain pattern (say, <...
4 votes
2 answers
139 views

How to define a function to be numeric when the input is purely symbolic

My goal is to have NumericQ[h[j]]=True for any j regardless of whether j may be symbolic with no defined value. Setting NumericQ[h[j_]]=True does not work and as I understand the NumericFunction ...
  • 1,173
0 votes
1 answer
49 views

Converting a list of values to a criterion

Say I have a list of 4 integers valList = { v[1], v[2], v[3], v[4] } whose values may or may not be equal to one another. Now I would like to extract all rows from ...
  • 1,264
5 votes
2 answers
101 views

Simplifying expressions with nested square roots

When I run FullSimplify[Sqrt[(1 - Sqrt[1 - 2 x^2 y^2])^2], {1 > 2 x^2 y^2 > 0}] I obtain 1 - Sqrt[1 - 2 x^2 y^2]. However,...
  • 385
1 vote
2 answers
92 views

Apply function to the second level if the first level is integral from zero to infinity, otherwise, not evaluate

How can I make a function, say, R[expression] such that it works on arguments like this Integrate[f[x],{x,0,Infinity}], applying ...
  • 2,940
1 vote
2 answers
51 views

How can I collect the sum of any number of gradients without using ReplaceRepeated[]?

Recently, I have run into the problem where I need to collect the the sum of any number of gradients. I have defined a function grad[] to represent the gradient, ...
  • 443
2 votes
1 answer
122 views

How to give a property to a list of functions?

For example, I have defined a bunch of binary functions Sum1, Sum2, Prod1, ...
9 votes
3 answers
657 views

What is the best method to search pattern for 2D list?

What is the best method to search patterns for 2D list ? Let L be following 2D-list : ...
  • 3,221
1 vote
1 answer
66 views

Why does my simple string function fail to grab the only element in a List?

The idea is to normalize a string before processing it to extract information from it. Strings may come with embedded outer brackets which are easily stripped or as elements of a true list. This is ...
  • 1,987
2 votes
1 answer
28 views

MatchQ returns False for a match for e1 ** e2 while matching both e1 and e2

I used a simple predicate realQ[x_] := MemberQ[{Real, Integer, Rational}, Head[x]]. With that predicate both ...
  • 21
3 votes
3 answers
108 views

Parsing an expression for coefficients and more

I have many expressions of this form: ...
4 votes
2 answers
167 views

How to create patterns for DeleteCases / Cases / Select: deleting elements of a list, from the condition of a negative element in a column

I've been trying to create a simple and "human readable" instruction for deleting elements from a list with an specific pattern. I have the following list and I would like to delete the ...
3 votes
1 answer
75 views

Terms of expressions with non-commutative multiplication that meet the given criteria

There is the following expression: expr=a^3**x**Transpose[x]**y+x+y**Transpose[y]**x**a^3+x**Transpose[y] It is necessary to apply the ...
  • 1,893
3 votes
2 answers
849 views

How can a verbal reasoning question be solved with Mathematica?

Could a "verbal reasoning" problem like the following be solved with Mathematica? What comes next in the series. KD XP NG VN QJ TL TM Correct answer is <...
3 votes
3 answers
147 views

Deleting sublists where an element repeats beyond a repetition threshold

I am looking for some help constructing a function Part[M_,N_] which outputs the list of all integer partitions of positive integers up to $M$ such that in each ...
  • 1,307
3 votes
2 answers
153 views

StringReplace question from "An Elementary Introduction to the Wolfram Language"

I'm unable to come up with a solution to the following question in the "Introduction" online book, Chapter 42: "42.6 Make any 2-letter word uppercase in the first sentence from the ...
1 vote
1 answer
128 views

How to make a pattern that matches only when one of the argument's Head is a Plus?

I am trying to set up rules as follows: ClearAll[CircleTimes]; If the arguments are the same the output is zero: ...
  • 8,417
4 votes
1 answer
75 views

Replacement, PatternTest, Module related Question

Let's see how MMA handles following type of expression : Replace[L, {pattTest1->rhs1, pattTest2->rhs2, pattTest3->rhs3}, {1}] Specific example : ...
  • 3,221
2 votes
1 answer
121 views

Pattern match with "+" sign

Can you pattern match with a "+" sign? Here is the recursion...(which does not work) ...
  • 415
2 votes
0 answers
78 views

sometimes Select / sometimes Position is faster

As you see, For list L, L1(Select method) is slower than L2(Position method). But for list <...
  • 3,221
1 vote
0 answers
55 views

Pattern matching and polluting the namespace

I am building a library. Inside of BeginPackage environment there is a function ...
  • 74
1 vote
0 answers
24 views

TreePosition with multiple (not just node data) conditions

Given a Tree object, TreePosition takes patterns only on the "data" of the node plus a level spec. ...
  • 3,621
0 votes
0 answers
40 views

Extracting function domain from a finite set of assignments

Suppose we define a function through a finite collection of equalities, e.g., f[1] = 1; f[26] = 321 and so on. We can extract the set of output values by running <...
  • 605
6 votes
4 answers
596 views

Finding words that start and end with same letter

Why does this code run too slow? ...
7 votes
3 answers
261 views

Removed all undefined from a list of rules

My output from NSolve is a list of rules, most of these are undefined, I want to remove those rules. More specifically I have a list ...
  • 359
0 votes
1 answer
30 views

Confusing abnormalities observed when counting a specific operator in an expression [duplicate]

Recently I have met with some confusing abnormalities when trying to determine if there is a specific operator (usually Plus[] ) in an expression by programming. ...
  • 443
0 votes
1 answer
81 views

Symbol for "anything" [closed]

Is there a way to use in Mathematica something like "anything" symbol to simplify symbolic expressions? Exemplary, I would like to denote with "B[*]" B[t] for arbitrary parameter t....
  • 625
8 votes
3 answers
496 views

Replace 'n'-th occurence with number 'n'

How can I replace n-th occurence with number n for string/for list? For string : ...
  • 3,221
1 vote
1 answer
70 views

The replacement operator -> and :> and pattern

I'm confused by the difference between two replacement operators -> and :>, and their relation with the dummy variable (...
  • 391

1
2 3 4 5
28