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
4 votes
3 answers
238 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,369
0 votes
2 answers
75 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
55 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
32 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
34 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,491
3 votes
1 answer
87 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
  • 52.6k
2 votes
1 answer
75 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
  • 145k
1 vote
0 answers
51 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,699
4 votes
1 answer
133 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
88 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
35 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
7 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
  • 66.8k
3 votes
2 answers
117 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
86 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
40 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
33 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
46 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
321 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
209 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
  • 18.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
88 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
100 views

Count consecutive repeated patterns in a list [closed]

Given a list such as: ...
wooohooo's user avatar
  • 319
5 votes
3 answers
270 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
6 votes
2 answers
248 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,369
1 vote
2 answers
82 views

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

Consider the following setup: ...
user13892's user avatar
  • 9,443
0 votes
1 answer
49 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,453
9 votes
2 answers
501 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
  • 981
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,369
2 votes
2 answers
121 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
  • 499
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,315
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,562
4 votes
3 answers
180 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
64 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
169 views

Find non-descending sequences of list pairs

...
eldo's user avatar
  • 74.4k
3 votes
1 answer
92 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
158 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
  • 201
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,097
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
137 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
4 votes
2 answers
153 views

Pattern matching based on parts of Head

I want to find a function (could be user-defined) from an expression based on pattern matching. However, I do not know the exact name (Head) of the function but know only the first few characters. Say ...
BabaYaga's user avatar
  • 1,888
1 vote
1 answer
58 views

Difficulty using StringCases to obtain desired substring

I have extracted and stored some plaintext strings (derived from a pdfs) from the web that I wish to search for various values following certain text substrings that are different for different ...
Stuart Poss's user avatar
  • 1,883
12 votes
5 answers
783 views

How can I remove empty lists { } from a nested list?

I have a list: list={{{1},{2},{1,2},{1,2,3},{},{}},{{2},{3},{}}} I would like to remove the empty lists {} to get: ...
Thuy Nguyen's user avatar
  • 1,097
4 votes
5 answers
230 views

Partitioning a list based on the count of a certain marker

My goal is to get lists containing k number of 0.1s. For example, in ...
Soon's user avatar
  • 648
2 votes
3 answers
164 views

Simplifying 'symbol-linear combinations' of nested monomials

I'm working with a custom binary 'operation' $F$ which takes $x, y$ and produces $F[x, y]$, where the possible arguments are already given by some list $\{ \text{one}, a, b, c, d, x, y, e, f\}$ where $...
VertexVexed's user avatar

1
2 3 4 5
31