Questions tagged [pattern-matching]
Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
1,511
questions
0
votes
1
answer
106
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:
...
3
votes
1
answer
152
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 *)
2
votes
1
answer
194
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 ...
1
vote
0
answers
35
views
How to best query if an expression contains Symbol/variable
Say I have this expression, among many:
...
5
votes
2
answers
213
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 ...
1
vote
3
answers
104
views
Extracting elements from a List in a specific way
I have a set of polynomial expressions in multiple variables:
...
3
votes
1
answer
76
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
271
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
35
views
ToExpression in nested Set/SetDelayed
Suppose we have the following definitions.
...
3
votes
2
answers
157
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
171
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 ...
0
votes
1
answer
55
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 ...
5
votes
2
answers
179
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,...
1
vote
2
answers
107
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 ...
1
vote
2
answers
61
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, ...
2
votes
1
answer
125
views
How to give a property to a list of functions?
For example, I have defined a bunch of binary functions Sum1, Sum2, Prod1, ...
10
votes
3
answers
818
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 :
...
1
vote
1
answer
80
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 ...
2
votes
1
answer
43
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 ...
3
votes
3
answers
125
views
Parsing an expression for coefficients and more
I have many expressions of this form:
...
4
votes
2
answers
220
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
94
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 ...
3
votes
2
answers
954
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
157
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 ...
3
votes
2
answers
194
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
160
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:
...
4
votes
1
answer
91
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 :
...
2
votes
1
answer
131
views
Pattern match with "+" sign
Can you pattern match with a "+" sign?
Here is the recursion...(which does not work)
...
2
votes
0
answers
103
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 <...
1
vote
0
answers
68
views
Pattern matching and polluting the namespace
I am building a library. Inside of BeginPackage environment there is a function
...
1
vote
0
answers
28
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.
...
0
votes
0
answers
44
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
<...
3
votes
1
answer
182
views
ReplaceAll with Powers [duplicate]
If I run the following code:
a^2/b^2 /. {a/b -> c}
I'm getting output:
a^2/b^2
How can I obtain ...
6
votes
4
answers
798
views
Finding words that start and end with same letter
Why does this code run too slow?
...
9
votes
5
answers
327
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
...
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. ...
0
votes
1
answer
93
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....
12
votes
7
answers
576
views
Replace $n$-th occurrence with number $n$
How can I replace $n$-th occurrence with number n for string/for list?
For string :
...
1
vote
1
answer
91
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 (...
8
votes
5
answers
493
views
How can I change the expression x1 to x[1] in a simple way?
I have one expression,
exp0=x1+x9/8+ x4 x5
I want to get
exp1=x[1]+x[9]/8+x[4] x[5]
Is there a very simple way to do that?
5
votes
1
answer
65
views
Picking integer compositions with certain descent patterns
I am trying to find a nice way to pick out all the integer compositions (ordered partitions) of an integer $n$ that satisfy a given pattern of descents between some adjacent elements. Writing a ...
1
vote
0
answers
136
views
How to make a replacement rule matching exponents including 0 and 1
==============Edit for solution(s):
For anybody who does not need to worry about the structure of any coefficents and has non-negative powers this solution from @kirma works very well:
...
6
votes
4
answers
201
views
How do I locate non-numbers
If I have a list containing both numbers and non-numbers, such as
{1,2,3,4,5,6p6,7p7,8,9}
how do I locate the positions in the list which do not contain numbers. ...
4
votes
1
answer
130
views
Substitute function back into solution
I have a expression
G[x] := (1-x^2)(1+A*x)(1-a*x)
where $(a,A) \in \mathbb{R} $.
Now this heavily simplifies some other expression. But these expression are no ...
3
votes
2
answers
334
views
How to define a function in Mathematica without overriding the previous definition?
I'm a beginner at Mathematica and trying to define a function for the Collatz conjecture, and here is my code:
...
11
votes
5
answers
309
views
Select pattern at the list sub-level
I want to get a list from a parent list (containing sub-lists of unequal length) using Cases and a matching pattern. The parent list is
...
1
vote
1
answer
55
views
Why this pattern can't be matched? [closed]
My code is {x,y,z}/.PatternSequence[x,y]->t.I think the answer is {t,z},while the output is still ...
3
votes
1
answer
82
views
Is there any particularity of Sum/Product in replacement
Here is an simple replacement:
Log[x[k]] /. Log[a_] -> a*xbar
I get the answer in my mind:
xbar x[k]
Similarly, I use ...
0
votes
0
answers
48
views
Evaluate derivative after replacement?
I am trying to implement a simple integration by parts on functions with zero boundary conditions. For example, I currently have something like
...
3
votes
1
answer
230
views
Pattern matching with two variables - derivatives on 4-vectors
I am interested in pattern matching with two variables and not just one, and more specifically implementing derivatives on 4-vectors. In the case of interest the 4-vectors have two indices; one is in ...