Skip to main content

Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
9 votes
7 answers
348 views

Create sublists whose totals exceed a certain threshold and that are as short as possible

list = {2, 1, 8, 10, 1, 4, 4, 5, 3, 4, 4, 11, 12}; th = 10; It is easy to create sublists whose totals don't exceed th: ...
eldo's user avatar
  • 78.8k
4 votes
1 answer
126 views

How to write a rule that matches alternatives but return one of them always?

Consider the following rule: $rule={"Period ID"|p:"Period":>p}; This rule will match ...
user13892's user avatar
  • 9,483
0 votes
0 answers
37 views

Replacement not working as desired [duplicate]

The following replacement works fine ReplaceAll[1/F[x],F[x]->y] (*1/y*) but this doesn't ...
qreus's user avatar
  • 343
0 votes
0 answers
52 views

Pattern matching for a repeated pattern (but different matching elements)

Is there a nice syntax for a pattern that is repeated (but it should match when the matching elements are different). I mean something like ...
Kvothe's user avatar
  • 4,591
1 vote
2 answers
152 views

How to make valid expressions with arbitrary number of named Blanks?

I want to make the rule for a generalized distributivity. In what I'm doing, I'll have expressions such as: $$(a_1 + ...+ a_n)**a_{n+1}$$ And I want to transform them to: $$(a_1 ** a_{n+1} + ...+ a_n *...
Red Banana's user avatar
  • 5,411
4 votes
3 answers
360 views

How to express the notion of "from "a" to the nearest "z"" with patterns?

If I compute this: StringCases["abczabvncz", "a" ~~ __ ~~ "z"] It yields: ...
Red Banana's user avatar
  • 5,411
1 vote
2 answers
99 views

Differential behaviour of FreeQ function depending on the number of terms in a sum

I have an expression consisting of a sum of terms. It looks like this in simplified form: $-\frac{e^{-t/t_1} \,a \, b \, c}{d} \, U_1 - \frac{e^{-t/t_2} \,a \, e \, c}{d} \, U_2 -\frac{e^{-t/t_2} \,a \...
CountBackwardsFromInfinity's user avatar
3 votes
1 answer
58 views

Function with complicated argument constraint with default value

I need to define a function, say f with an argument which is integer, positive and with a default value say 2. I tried: ...
Giovanni Russo's user avatar
1 vote
1 answer
38 views

How to do this replacement? Match the variable, but not the function [duplicate]

I have the following expression: $$\frac{\left(-2 \lambda \lambda [\theta ]+\lambda [\theta ]^2+\lambda ^2+\lambda \right) \lambda '[\theta ]^2}{\lambda [\theta ]^2}$$ I want to replace every ...
AccidentalTaylorExpansion's user avatar
1 vote
0 answers
39 views

Difference in pattern matching between Cases and ReplaceAll (Already searching at all levels and heads)

Why is there a difference between the pattern matching between Cases and ReplaceAll in the following: ...
Kvothe's user avatar
  • 4,591
3 votes
1 answer
91 views

Unexpected result from MemberQ [closed]

MMA 14.0 Can somebody explain why the following evaluates to False: MemberQ[1/y[x], y, Infinity] False
Daniel Huber's user avatar
  • 53.2k
2 votes
1 answer
77 views

Moving all terms with dependents variable to one side of differential equation

This question came up in the Maple forum: Given a differential equation, how do we move all terms with the dependent variable and all its derivative to the left side of the equation and all terms ...
Nasser's user avatar
  • 146k
1 vote
0 answers
55 views

Random replacement in array

In the spirit of this interesting article of Stephen Wolfram I also decided to study some issues related to mutations. I’m working with simple {0,1} arrays and I’ve ...
lesobrod's user avatar
  • 1,789
4 votes
1 answer
141 views

Pattern matching when decode Bencode. Problems with pattern name and nested pattern

I am writing a Mathematica function to import *.torrent files as Expression. The encoding used in *.torrent files are called Bencode. You can get more information on Wikipedia. Here are some examples: ...
李子涵's user avatar
  • 368
4 votes
4 answers
92 views

$n$-letter words with restriction

I would like to write a function or module to generate $n$-letter words, where $n$ is some positive integer, with the restriction that certain letters in the alphabet under consideration should not ...
abstract's user avatar
  • 245
1 vote
2 answers
36 views

Using patterns to replace the head of certain expressions [duplicate]

I would like to match things like Times[Cycles[_],Cycles[_],..] and replace them with ...
diracdeltafunk's user avatar
8 votes
3 answers
1k views

Why doesn't b = a; f[a_] := b; f[2] return 2?

As the title asks, why does b = a; f[a_] := b; f[2] evaluate to a rather than 2?
xzczd's user avatar
  • 67k
3 votes
2 answers
118 views

Plotting a function containing a $\pm$ sign

Is there any command in Mathematica to plot an expression: $f(x,y)=\exp^{x\pm y}$. As far as I know, it is not possible as the expression will vary differently with the sign. I have seen such plots in ...
Rag's user avatar
  • 45
2 votes
1 answer
91 views

Pattern Matching and Permutations

I am working with some math students on some problems involving permutations. My Mathematica knowledge is limited and I'm hoping to get some help. Two sample problems: a)how many ways can the ...
Tom De Vries's user avatar
  • 3,780
2 votes
1 answer
41 views

Position outputting lists which seemingly have the wrong dimension for the input array

I am writing some straightforward code to find the structure constants of $\mathfrak{su}(3)$ and experienced a weird result when using the position function to ask which positions had specific values. ...
tadpolio's user avatar
0 votes
0 answers
35 views

Patterns with Dot [duplicate]

I would like to understand the behavior of Dot to build patterns with it. Could someone tell me what is going on in these examples? ...
Albercoc's user avatar
  • 998
2 votes
1 answer
48 views

Intra-Symbolic Wild Card Pattern Matching

I have the following custom expression structure I am parsing: Theorema`Knowledge`Q$TM[ Theorema`Language`VAR$[Theorema`Knowledge`VAR$x$TM]] I already was able to ...
marie_antoinette's user avatar
5 votes
3 answers
334 views

Reversing the order of function arguments by a replacement rule

I am facing an issue when I try to reverse the arguments of a function in a replacement rule. Let me give an example : ...
Adrien Martina's user avatar
4 votes
5 answers
210 views

How to make expression-dependent replacements?

Consider as a toy example that I want a replacement which acts on {x,1,2,3} by replacing $x$ with the length of the list itself. In this example this should output ...
Adrien Martina's user avatar
4 votes
1 answer
79 views

PatternTest in Function

This is probably trivial and already asked but I did not find it. How to use PatternTest inside Function (...
azerbajdzan's user avatar
  • 19.7k
3 votes
1 answer
71 views

Troubles with BlankNullSequence in the case of multiplication

I have some trouble to use the triple underscore notation to use definitions with or without a term. Consider the following toy example f[a___ x]:=1 {f[x],f[2x]} ...
Adrien Martina's user avatar
1 vote
1 answer
89 views

Parse, pattern match and extract functions of variables

From expressions like i σ[1, 1] + p ** x σ[1, 1], I want to be able to extract and separate any functions of p and ...
Luca's user avatar
  • 93
2 votes
3 answers
102 views

Count consecutive repeated patterns in a list [closed]

Given a list such as: ...
wooohooo's user avatar
  • 319
5 votes
3 answers
272 views

Searching for a list in a list

I want to see if a specific list is part of another list and get either True or False. As an example: ...
Cptn Bluebear's user avatar
5 votes
2 answers
188 views

Pattern recognition for products of variables

Pattern recognition doesn't work as I would expect when trying to match products, for example Cases[a[1] b[1] a[2] b[2], x___ a[k_] b[k_] -> k, All] {1} ...
Adrien Martina's user avatar
3 votes
2 answers
44 views

Trouble with replacement rules for squared variables [duplicate]

I want to do replacement of products (because I am dealing with abstract projectors), but Mathematica seems to fail understanding this toy example replacement : ...
Adrien Martina's user avatar
7 votes
3 answers
265 views

Replace number in string with ? and /;

I am trying to make the following: StringReplace[{"A4", "A#3"}, {"A" ~~ x_ -> "B" ~~ x}] This should replace ...
Red Banana's user avatar
  • 5,411
1 vote
2 answers
84 views

How to pass arguments of outer function into inner function implicitly?

Consider the following setup: ...
user13892's user avatar
  • 9,483
0 votes
1 answer
50 views

Pattern matching with DeleteCases

I'm using Mathematica 14.0 and I'm having a pattern match problem with DeleteCases. The example below illustrates the problem. I define a test string followed by two patterns that both nominally match ...
jparm's user avatar
  • 1
12 votes
4 answers
605 views

How to match a pattern only when it is not used as a head?

I am working on modifying a mathematical expression and need some assistance. I have the following equation: -27 f + 27 f[1 + n] - 9 f[2 + n] + f[3 + n] == 1 + 3 n ...
LeafGlowPath's user avatar
  • 1,473
9 votes
2 answers
503 views

What's the difference between With and ReplaceAll?

A wolfram Tutorial mentions that ReplaceAll and With are similar: The way ...
ions me's user avatar
  • 991
0 votes
2 answers
67 views

Trouble with expressing "anything" using patterns?

Suppose I have a list: L = {p, p \[Implies] q} I want to create a function that I can apply to L (f/@L) such that whenever x is ...
Red Banana's user avatar
  • 5,411
2 votes
2 answers
122 views

How can I enforce the use of Subtract and Divide?

It has been noted before, here and here, that when you enter a/b or a-b, Mathematica does not convert these to ...
Felipe's user avatar
  • 599
1 vote
1 answer
114 views

Matrix Substitution & Operator Adjustment

How can I effectively substitute some variables in an expression with matrixes, while adapting the operators accordingly, to maintain the intended meaning and operations? For example, given a list ...
ssskkkky's user avatar
5 votes
2 answers
488 views

How to convert this logarithm into this form? [duplicate]

How to convert this logarithm: Log[Log[2]]/Log[2] into this form: Log[2, Log[2]]
csn899's user avatar
  • 4,351
2 votes
3 answers
227 views

The problem of matching consecutive cross-line strings

I have a file named "s0.log",There's a lot of this structure in there: You can see that the strings "Alpha occ.eigenvalues --" and "Alpha virt.eigenvalues --" will be ...
我心永恒's user avatar
  • 1,572
4 votes
3 answers
181 views

How do I get Replace to work with a complex repeating pattern?

I'm new to Mathematica, but with extensive searching and a lot of experimenting I can't seem to get this to work. I have a list of strings of the form: ...
p quint's user avatar
  • 43
1 vote
0 answers
65 views

Why is SequencePosition or SequenceCases so slow on large lists?

I observed that SequencePosition or Sequence Cases on a list of around 60'000 digits is so slow that I had to sort out to string pattern matching. Is this normal behavior or should Wolfram have a look ...
markilux's user avatar
  • 121
3 votes
2 answers
170 views

Find non-descending sequences of list pairs

...
eldo's user avatar
  • 78.8k
3 votes
1 answer
96 views

BlankNullSequence (___) when input variable is Null

f[x___] := Plus[x, -x] f[Null] gives output as 0 f[] ...
metroidman's user avatar
6 votes
3 answers
162 views

Cancelling factor in fractions with factorial

My goal is to make a simplification: $$\frac{k}{k!} \to \frac{1}{(k-1)!}.$$ However, $k$ in the denominator and numerator is not automatically cancelled by Mathematica. What is the best known method ...
Soon's user avatar
  • 648
5 votes
1 answer
120 views

More concise forms generate list with elements 0 and 1

I need to generate all such lists, it consists of 14 1's and 4 0's, there should be at least 3 1's between two adjacent 0's. I came up with a pattern matching approach, it's a bit long. ...
vector's user avatar
  • 243
7 votes
1 answer
449 views

How can I write Log[a]/Log[b] in the form Log[b,a]?

I solve the equation $2^x + 2^{x + 4} + 2^{x + 3} == 3^x + 3^{x + 4} + 3^{x + 3}$. I tried ...
Thuy Nguyen's user avatar
  • 1,129
2 votes
3 answers
102 views

I'm tired trying to get subsets

t={{a,b},{},{},{},{a},{a,b},{},{a,b,c},{},{b}} s={{},{a,b,c},{a,b}} Thread[Subset[t,s]] I'm tired of trying to calculate: {a,b} [which is the intersection [{a,b}, {...
Alyasaa Jasim's user avatar
2 votes
3 answers
140 views

Using patterns in ReplaceAll to match different symbols

I have an expression expr = kb.kpb+kb.kb+knb.knb+kpb.knb+kd.kpd+kd.kd+knd.knd+kpd.knd; and I want to make replacement rules as: ...
hossein mohseni's user avatar

1
2 3 4 5
31