Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
3 votes
1 answer
38 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 ...
user avatar
  • 1,626
3 votes
2 answers
810 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 <...
user avatar
3 votes
3 answers
137 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 ...
user avatar
  • 1,287
3 votes
2 answers
150 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 ...
user avatar
1 vote
1 answer
114 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: ...
user avatar
  • 8,105
4 votes
1 answer
68 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 : ...
user avatar
  • 2,451
2 votes
1 answer
117 views

Pattern match with "+" sign

Can you pattern match with a "+" sign? Here is the recursion...(which does not work) ...
user avatar
  • 341
1 vote
0 answers
71 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 <...
user avatar
  • 2,451
1 vote
0 answers
50 views

Pattern matching and polluting the namespace

I am building a library. Inside of BeginPackage environment there is a function ...
user avatar
  • 74
1 vote
0 answers
23 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. ...
user avatar
  • 3,720
0 votes
0 answers
36 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 <...
user avatar
  • 595
6 votes
4 answers
545 views

Finding words that start and end with same letter

Why does this code run too slow? ...
user avatar
7 votes
3 answers
252 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 ...
user avatar
  • 305
0 votes
1 answer
26 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. ...
user avatar
  • 289
0 votes
1 answer
77 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....
user avatar
  • 625
8 votes
3 answers
486 views

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

How can I replace n-th occurence with number n for string/for list? For string : ...
user avatar
  • 2,451
1 vote
1 answer
66 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 (...
user avatar
  • 381
6 votes
3 answers
399 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?
user avatar
  • 1,217
5 votes
1 answer
54 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 ...
user avatar
1 vote
0 answers
56 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: ...
user avatar
2 votes
1 answer
104 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. ...
user avatar
  • 755
4 votes
1 answer
74 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 ...
user avatar
  • 305
3 votes
2 answers
291 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: ...
user avatar
7 votes
3 answers
199 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 ...
user avatar
  • 973
1 vote
1 answer
51 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 ...
user avatar
3 votes
1 answer
73 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 ...
user avatar
  • 31
0 votes
0 answers
36 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 ...
user avatar
3 votes
1 answer
213 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
  • 4,113
7 votes
2 answers
195 views

Dividing a list into $n$ non-overlapping pieces?

Is there a function that gives all the ways a list can be divided into a specified number of non-overlapping pieces, where the order is maintained? I can do this with ...
user avatar
  • 3,446
3 votes
3 answers
89 views

Regular Expressions in FileNameJoin[]

I'm attempting to create a list of folders that match a particular pattern. For example, consider I have the following files in a directory structure: ...
user avatar
  • 177
2 votes
3 answers
104 views

Selecting patterns

I have data as a list of four element lists, some with missing data, thus ...
user avatar
  • 579
0 votes
1 answer
43 views

weird behavior in pattern-matching and condition

I'm confused by the following three different results involving pattern matching and condition. ...
user avatar
  • 381
7 votes
1 answer
160 views

A uniform rule for matching numeric and symbolic expressions for inverse of complex numbers?

I am trying to do some substitution on the basis of pattern matching. I am trying to replace $\frac{1}{a+i b}\to\frac{a-ib}{a^2+b^2}$ in the following way, ...
user avatar
  • 419
0 votes
0 answers
43 views

Convert complex exponential to real exponentials, sines and cosines

While taking the inverse Laplace transform of certain expressions, Mathematica yields complex exponentials. For example, using the following code: ...
user avatar
  • 433
5 votes
1 answer
120 views

I understand mol but meet difficulty in understanding how `pdetoode` atumatically generate pde-to-ode-rules by using this strange pattern and rule?

I often solve pdes for my research, and years ago I found pdetoode in this forum is very handy. Although it is a small piece of code, it solves several interesting ...
user avatar
3 votes
3 answers
152 views

Finding the index of certain strings in a list

I'm actually using a dataset of much greater dimensions and length but I simplified my problem of string patterns in order to ask this question. ...
user avatar
  • 1,024
4 votes
1 answer
98 views

Search speed of downvalues when indexing with strings

I'm writing a lot of print functions for different parts in a package and at the moment am using something like myprint[ identifierString_String ][ data_ ] := ... ...
user avatar
  • 928
1 vote
0 answers
75 views

How to test for arguments for a pattern that is of a positive Integer OR Infinity so that both filter through to the definition on RHS?

How can I filter an argument of a function so that definition works on positive Integers (y >= 0) or Infinity? It will ...
user avatar
  • 1,787
2 votes
0 answers
54 views

How to specify complicated patterns in function arguments?

Unfortunately, I have not found in documentation how to create complicated patterns in arguments of a function Consider a toy example. Say, I would like to have a simple function ...
user avatar
2 votes
0 answers
55 views

Store pattern names in variables, and use them in Condition (/;)

I am trying to create patterns whose form is not hard-coded, and which are not explicitly directly written. For that I want to use one list of pattern names to name some parts of a ...
user avatar
  • 483
0 votes
0 answers
33 views

How do I modify all the list elements as patterns

Is there any easier way to change all the list elements as elements with patterns? Here is an example: list={a,b,c,d} I want to transform it as a list of patterns ...
user avatar
  • 973
3 votes
3 answers
178 views

Encode expression into a list

I have an expression of the form: $$c {\partial^{k+l} \Phi(\theta,\phi,n) \over \partial \theta^k \partial \phi^l}$$ $n,k,l$ are (positive) integers. $c$ is some constant. It is also possible that $c$ ...
user avatar
  • 951
2 votes
0 answers
49 views

pattern test on position

At the moment I'm using something like this to get the positions of entries of a matrix satisfying a specific value: ...
user avatar
5 votes
3 answers
169 views

Permutation avoidance function

It is common for me to see if a permutation of 1,2,...,n, avoids some fixed permutation pattern. For example, the permutation [1,4,2,3,5] contains the pattern 1,3,2, as the elements [1,4,3] appear in ...
user avatar
5 votes
1 answer
98 views

Patterns are not Sequences are not Lists, or: Global Function Argument Reversal

H[z_] := z /. {F[p__] -> F[Reverse[p]]} F[{x, y}] + F[{x, z}] + F[{}] // H Almost. It gives an annoying "Nonatomic Reverse" error message at the ...
user avatar
1 vote
0 answers
26 views

N on equation with numbered variables [duplicate]

Say I have a system of polynomial equations in the variables x[1],...,x[n]. Sometimes want to make such equations numerical (as in: the coefficients become floats) ...
user avatar
  • 928
3 votes
1 answer
134 views

Why the two outputs are different?

My first code is {a, b, b, a, b, a, b, a, a, b} /. {___, x : Longest[PatternSequence[a, b] ..], ___} :> {x} and its output is {a,b,a,b}. The second code is ...
user avatar
2 votes
2 answers
88 views

Equality of expressions with different variable names

I have some multivariate systems of expressions that I want to check for equality up to allowing relabelling of the variables. For example the two expressions below would be the same: ...
user avatar
  • 55
1 vote
1 answer
77 views

why combining 2 patterns in Alternatives in Cases do not give same output as when each pattern is used separately?

Given expression expr = y'[x]^3 + y'[x]*Log[y[x]*y'[x]^2] + y'[x]^(1/2) + x/y'[x]^7 $$y'(x)^3+\sqrt{y'(x)}+\frac{x}{y'(x)^7}+y'(x) \log \left(y(x) y'(x)^2\right)$$ ...
user avatar
  • 108k
0 votes
1 answer
44 views

Formatting expressions with numeric exponents using pattern matching

Thank you all in advance. Question: I want to change the format of this expression (n + r)^3/n^3 into this (1+r/n)^3using rules ...
user avatar

1
2 3 4 5
27