Questions tagged [pattern-matching]

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

3
votes
3answers
89 views

Find element in a list matching a pattern (using wildcards)

Somehow, I did not manage to find anything related to that. Suppose, in my Global list of variables, I would like to make a search such that I obtain all the ...
2
votes
1answer
29 views

Setting default function values for particular option values

I have a function with some optional arguments. How do I assign a value for the function when one of the optional arguments takes a particular value, regardless of any other argument values?
0
votes
1answer
42 views

Pattern ReplaceAll warning

Why does the following code give me a warning, but then still work? a1={{1,2},{3,1}} a2={"foo","bar"} a1/.{x_Integer,y_Integer}->{x,a2[[y]]} After a "Part: ...
1
vote
2answers
61 views

How to use KeyValuePattern with individual replacement rules?

What's the syntax to use individual replacement rules for key-value patterns? This returns only the first match: ...
4
votes
0answers
61 views

Why does Shortest not work inside SequencePosition?

Why does Shortest not work in a SequencePosition call? The following two commands return the same answer: ...
4
votes
1answer
49 views

Pattern matching repeated arguments of Times

I am trying to match against products of elements with a specific pattern. Since a product of symbols A B C is just Times[A,B,C],...
5
votes
1answer
105 views

DeleteCases using two lists but with partial match?

I searched and tried different solutions on SE but nothing worked. I have two lists and would like to remove items from list1 that have a PARTIAL match with items ...
3
votes
1answer
128 views

Pattern test: _Integer vs _?IntegerQ [closed]

While checking out the different pattern replacement methods in this question, I have a query regarding the basic difference between the following two replacements: ...
1
vote
1answer
265 views

Replacement using pattern matching?

Starting with the expression eq1 = 5 Exp[1 + 2 x + 3 x^2 + 4 x^3]; I want to implement the following transformation: ...
0
votes
3answers
36 views

Select elements from nested list where one element is largest

I'm trying to speed up the following, but implementing efficient pattern matching isn't intuitive to me yet, even with lots of examples. I have a list of ((x1,y1,z1), (x2,y2,z2)) pairs, and I want to ...
2
votes
1answer
64 views

Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...
1
vote
1answer
54 views

Function returning a pattern with condition - how to prevent condition to evaluate prematurely?

I figured it would be convenient to have a function that returns a pattern, that match equally nested brackets, of the specified type. Such a pattern is easy to create, but I get some error messages, ...
3
votes
2answers
69 views

Using DeleteCases with a defined function with two arguments as a pattern

I am trying to manipulate some large datasets in Mathematica. I have plotted the data, and I would like to be able to easily remove points from the dataset, and it's fairly easy to identify the x-...
3
votes
3answers
68 views

Composition of polymorphic functions

Using pattern matching one can easily create polymorphic functions in Mathematica f[{a_, b_}] := {a^2, b^2}; f[c_Integer] := c^4; Now I want to create another ...
3
votes
1answer
46 views

PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...
0
votes
1answer
30 views

Notation package: StringMemberQ in conditional notation definition

Suppose that I have a list, allSpecies, that contain a set of symbols, each of which has been assigned a new notation using Notation[...]. As an example, the internal representation is C60s, and the ...
2
votes
2answers
70 views

Replacing with pattern on built-in symbols?

I have: f[a, b] /. f[x__] -> g[x, 0] // InputForm (* Out: g[a, b, 0] *) Which is what I expect, but when I use a built in symbol instead of ...
0
votes
2answers
194 views

Construct a pattern with an optional head

How can I construct a pattern that matches both f and h[f] without using the symbol f more ...
0
votes
1answer
59 views

Parsing output of a reduce operation

This question is a broader question than this. The output of Reduce can be of different forms. The solutions proposed for that question works well when the output of Reduce is as provided in that ...
2
votes
1answer
70 views

How to trace value assigned to local variable(variables in Module)?

Bug introduced in v10.1 or earlier and persists through v11.3 CASE:4240757 Here is my code: ...
0
votes
1answer
36 views

How to get all the expression with pattern match? [closed]

The FullForm of my equation final(obtained from System Modeler) is: ...
12
votes
0answers
126 views

Orderless breaks pattern match

Bug introduced in 10.3 or earlier and persisting through 12.0.0 10.0.2 is not affected. I am perplexed by the effect of Orderless on pattern matching. Starting ...
4
votes
2answers
62 views

Identifying total derivatives

I would like to identify which terms in my expression are a total derivative and which ones are not. For expressions in which all terms are a total derivative, one can simply use integrate to find ...
3
votes
2answers
128 views

Can pattern assumptions be made reliable?

It is possible to use patterns in assumptions such as f[_]>0. This does not seem to be explained in the documentation but is for instance pointed out in Is it ...
4
votes
1answer
101 views

A single argument pattern definition applies to multiple-argument patterns?

Consider defining a pattern rule, such as myFun[x_]:=x As far as I understand Mathematica syntax, this rule means ...
1
vote
0answers
90 views

Design completion idea for Position [closed]

This is not exactly a question but rather a suggestion for a design which I think would greatly improve Mathematica. Before I put this to Wolfram Research I would like to see what other people think ...
2
votes
0answers
40 views

Match an expression that has a descendant pattern

I have a complicated expression in which I have another expression p which, when it’s present, I want to match the outer expression and all of its children. I was hoping for something like ...
5
votes
2answers
221 views

Manipulating a general length function

As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like ...
4
votes
2answers
79 views

Partitioning string into longest substrings of given characters

I want to partition string into longest substrings that each contain only specific characters, beginning from left to right with no overlaps, always choosing the longest one possible at current ...
8
votes
1answer
287 views

Is there a defined priority for pattern matching?

If you define a function g, like so: g[x_?EvenQ] := "even" g[x_?NumberQ] := "number" why will Mathematica always return ...
0
votes
2answers
67 views

How can I extract the parameters from my expression automatically?

I have an expression of this form $\qquad e^{-x^2b_0+xb_1+b_2}(c_0+c_1x+c_2x^3+c_3x^3+c_4x^4)$ However, it has complicated parameters. ...
5
votes
1answer
146 views

Can someone please explain the code from another question

Source: Position of a pattern-matched part of an expression Can someone please explain the following code by Leonid Shifrin. ...
5
votes
4answers
92 views

Finding where a specific number appears in an expression

I would like to see how many times (and where) the number "91" appear in a set of formulae. I thought of using Position[expr , 91 , Infinity], but it seems not to ...
0
votes
2answers
88 views

Learning to work with core pattern only [closed]

I am trying to learn "core" patterns so I can start using them more often. Consider I am trying to solve the following problem. data = {2,{4,2,3},5,{7,3},{},4,1} (...
2
votes
0answers
29 views

Using Min & Max of an Interval in a replacement [closed]

I want to use a replacement rule to transform a list of equations. The following works as expected: ...
2
votes
1answer
46 views

FreeQ checking for several variables [closed]

I am using FreeQ to define a replacement rule. However, it doesn't seem to be possible to specify a list of variables as the second argument, which is why I came up ...
1
vote
1answer
40 views

Commutativity of Times and pattern matching

I understand that Attributes of Times make a bit non intuitive the Pattern Matching of its arguments. However I do not understand why this gives False: ...
4
votes
1answer
50 views

String Pattern to match “x by y”

I'm trying to create a string pattern which will match either (with quotes included): "x by y" x by y 'x by y' I've tried to use the following pattern: ...
5
votes
1answer
78 views

Why is { } interpreted as an option in my function? [closed]

I don't understand the output from: ...
3
votes
0answers
83 views

Select certain term containing some patterns and confusing pattern Alternatives in FreeQ

I am considering a function termsContain[func_,list_] which select in an expression func the terms containing the ones in the ...
1
vote
3answers
88 views

Is there a possibility of adding a second condition, or an else clause to $/;$?

Let tally=RandomInteger[{0, 1}, 10]; In the code below, I'm trying to output a list where each component $C_n=\sum^n_{i=1} \text{tally}_i / n$ The problem with my ...
2
votes
1answer
140 views

How to preserve the order of multiplication with Cases?

i have following expression: Cases[cx[i, k] cx[k, q] a[z, x], _Symbol, 2] // DeleteDuplicates what i expect is to get: ...
5
votes
2answers
346 views

How to remove the item that is repeated?

If i have a list={m,n,p,q,q,r,l,l} how can i remove the repeated item with pattern matching so i would have ...
3
votes
2answers
80 views

Change arguments of a product of functions

I am trying to do the following with rule-based pattern matching (f1[t] f2[t] f3[t]) /. {x__ -> n[ Product[x[[i, 0]][om[i]], {i, 3}]]} // Quiet which ...
1
vote
1answer
33 views

Extract powers of exponential into a list

I have a very complicated, long (thousands of terms) expression that boils down to a form of expr = a1 Exp[b1] + a2 Exp[b2] + a3 Exp[b3] Note that all a and b ...
5
votes
1answer
139 views

Why Cases and Select treat the List differently?

Look at these two examples: In[1]:=Select[f[i], MatchQ[_f]] Out[1]:=f[] In[2]:=Cases[f[i],_f] Out[2]:={} It seems that the Select take $f[i]$ as List, but Cases ...
4
votes
2answers
125 views

Select certain terms in an expression

I would like to have a function to select terms containing q[i] terms in expressions, for example, for an expression a q[i]+b +c q[j]+d I would like the function ...
0
votes
1answer
49 views

Replacement of the patterns in the replacement

I want to make such changes: $(A\, p + B\, q)[i] \to A\, p[i]+B\, q[i]$ But the replacement below can not do this ...
1
vote
0answers
29 views

Remove expression with matched braces from string

I am writing a LaTeX document where the custom command \chg{...} highlights changes in some color. Here I am, with a document of about 50 of those. Sure, one could ...
1
vote
1answer
45 views

Replace an If by a combination using replacement rules

Consider the following code : tttest = a^2 + If[a > 0, a, -a] a^2 + If[a > 0, a, -a] I would like to replace my If function by something like fonction @ ...