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
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: ...
dtn's user avatar
  • 2,394
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 *)
Daniel Huber's user avatar
  • 51.3k
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 ...
M.E.'s user avatar
  • 333
1 vote
0 answers
35 views

How to best query if an expression contains Symbol/variable

Say I have this expression, among many: ...
Tempus Nomen's user avatar
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 ...
azerbajdzan's user avatar
  • 15.1k
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: ...
abstract's user avatar
  • 203
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 ...
nathan bartholomew's user avatar
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 ...
George Rey's user avatar
0 votes
0 answers
35 views

ToExpression in nested Set/SetDelayed

Suppose we have the following definitions. ...
Math Gaudium's user avatar
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, <...
Vladimir Reshetnikov's user avatar
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 ...
userrandrand's user avatar
  • 5,817
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 ...
Gert's user avatar
  • 1,510
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,...
Void's user avatar
  • 405
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 ...
Anixx's user avatar
  • 3,585
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, ...
AlbertLew's user avatar
  • 473
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, ...
Yifeng Huang's user avatar
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 : ...
imida k's user avatar
  • 4,285
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 ...
Jules Manson's user avatar
  • 2,457
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 ...
FHD's user avatar
  • 21
3 votes
3 answers
125 views

Parsing an expression for coefficients and more

I have many expressions of this form: ...
Mondo Duke's user avatar
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 ...
Joshua Salazar's user avatar
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 ...
dtn's user avatar
  • 2,394
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 <...
Manu Chadha's user avatar
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 ...
Benighted's user avatar
  • 1,327
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 ...
Jim Marks's user avatar
  • 187
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: ...
user13892's user avatar
  • 9,363
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 : ...
imida k's user avatar
  • 4,285
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) ...
PiE's user avatar
  • 437
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 <...
imida k's user avatar
  • 4,285
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 ...
fqrt's user avatar
  • 84
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. ...
user5601's user avatar
  • 3,563
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 <...
miggle's user avatar
  • 657
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 ...
Mieczmik's user avatar
6 votes
4 answers
798 views

Finding words that start and end with same letter

Why does this code run too slow? ...
FelipeAmorim's user avatar
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 ...
Nitaa a's user avatar
  • 738
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. ...
AlbertLew's user avatar
  • 473
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....
Agnieszka's user avatar
  • 677
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 : ...
imida k's user avatar
  • 4,285
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 (...
Lelouch's user avatar
  • 523
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?
Orders's user avatar
  • 1,247
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 ...
Brian Hopkins's user avatar
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: ...
user14763624's user avatar
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. ...
Chris's user avatar
  • 1,033
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 ...
Nitaa a's user avatar
  • 738
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: ...
wang1zhen's user avatar
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 ...
BabaYaga's user avatar
  • 1,826
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 ...
helloworld's user avatar
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 ...
jerry's user avatar
  • 31
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 ...
pseudo-goldstone's user avatar
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 ...
user avatar

1 2 3
4
5
31