Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [pattern-matching]

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

3
votes
3answers
59 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
39 views

PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...
0
votes
1answer
25 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
61 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
188 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
61 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: ...
11
votes
0answers
111 views

Orderless breaks pattern match

I am perplexed by the effect of Orderless on pattern matching. Starting from a clean Kernel (i.e. after using Quit), I find the following expected match for ...
4
votes
2answers
59 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
125 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
96 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
83 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
39 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
219 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
73 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
284 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
65 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
138 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
91 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
27 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
33 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
48 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
77 views
3
votes
0answers
81 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
87 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
118 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
338 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
79 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
129 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
112 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
48 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
27 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
42 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 @ ...
2
votes
0answers
47 views

Fast way to check if an expression matches any of the rules in a list?

I have a function defined roughly like so. This is a minimum recreation. foo[expr_, rules: {__Rule}] := "some fancy result" /; AnyRuleMatchesQ[expr, rules]; What ...
2
votes
1answer
44 views

OptionValue and OptionPattern not Changing Values

I am trying to produce a function with a few sets of arguments, one of which can accept options. However it doesn't seem to be responding to the changes that I accept. ...
3
votes
3answers
76 views

How to check expression depends on symbol in a particular way

Consider these two expressions: expr1 = a d g[X, c]^2 + e g[X, f] + 3 expr2 = a d g[X, c]^2 + e X + 3 How do I check whether these expressions depend on ...
1
vote
1answer
72 views

Pattern matching on Orderless functions

I'm trying the following pattern matching command: ReplaceList[Hold[b + a], Hold[x___ + y___] -> {{x}, {y}}] And Mathematica returns: ...
4
votes
1answer
95 views

How to bring out all factors recursively except one particular term?

I want to write a function keepOnly[expr_, keep_] Such that keepOnly[f[f2*g[g2*h[h2*keep, h1], g1], f1], keep] becomes ...
3
votes
3answers
104 views

How to use “Cases” for isolating terms of a sum in a nice way?

I'd like to isolate some terms with specific patterns within a sum that I called chunk. ...
3
votes
2answers
64 views

Dropping lists using pattern [duplicate]

I have the following list of lists. data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}} I want to drop all the lists with ...
3
votes
1answer
44 views

KeySelect via Not[StringMatchQ[Pattern]] fails

I have an Association asc that has mixed type Keys: ...
1
vote
1answer
28 views

Splitting a list of letters into subsequences starting with uppercase letters [duplicate]

Given a list of upper/lowercase characters, I'm trying to split it into subsequences that start with an uppercase and contain the next few (not necessarily positive number of) elements up until the ...
3
votes
1answer
69 views

How to change node color in .dot file using pattern matching?

For example I have a dot file that describe a decision tree or binary tree In the file, the color of node is always gold, so I try to change it according the number in ...
0
votes
2answers
66 views

Splits an expression into two parts if possible

The series The series is: $\text{expr}_{1} \pm \text{expr}_{2} ...$ $\pm$ represents plus or minus sign. The number of terms in the series can be 1 or more than 1. Each term $\text{expr}_{n}$ is an ...
2
votes
2answers
50 views

Substituting main diagonal elements when a condition is satisfied

I'm trying to substitute the main diagonal elements when their value is less or equal to zero. I've tried the following: ...
6
votes
1answer
121 views

Cases, Pattern, Condition [duplicate]

I supposed Cases would do the list produced as example li = RandomSample[Range[100, 999], 30] ...