Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
19 views

Solve 16x16 matrix equality with non linear combination of variables on one side and reals on the other

I am trying to solve the following problem: $A = B$ where $A_{bis}$ is a symmetric 16x16 matrix of numbers and $B$ is a symmetric matrix of variables. $B$ definition To define $B$, I first define $Y$...
3
votes
3answers
71 views

Identify complex expression and replace

I'm having trouble finding a way (if there is) to analyse an expression, identify a complex pattern in it and then replace it with another thing. I think it will be easier to explain my problem with ...
6
votes
0answers
65 views

Repeated string pattern: difference between the FrontEnd and WolframScript?

This code works fine in a string based interface like WolframScript or Mathematica Kernel. Run this through a string based interface ...
2
votes
2answers
84 views

DeleteCases pattern which applies Chop to all elements

In my current macro, I have: DeleteCases[Chop[FullSimplify[RowReduce[something]]], {0 ..}] This works fine for both, exact and numeric complex numbers (with ...
0
votes
0answers
26 views

Why are unrelated symbols localized in Module? [duplicate]

As I understand, an expression like Module[{var},expr[var]] localizes var and evaluates ...
9
votes
1answer
90 views

Pattern matcher behavior change in 11.3

When trying to switch from version 10.3 to 11.3 I noticed that some of my code do not work properly. After some investigation I found that the problem is with pattern matcher. Here is the example: <...
7
votes
1answer
108 views

Using PatternSequence and __ / Repeated[]

I'm trying to scrape this Wikipedia page ('Deaths in 2019- July', I'm writing this on the 14th.). I am failing miserably and I'm starting to think that it is due to some misconception on my part on ...
1
vote
1answer
41 views

Respecting excluded index in sum

I'm using a function involving a sum where some indices are excluded: ...
4
votes
1answer
52 views

Pattern of nested functions

Suppose I have a list of elements list = {f[b[G[1]]],f[b[1]],k[k[k[G[1]]]]} What is the pattern associated to an expression in which the function ...
1
vote
0answers
21 views

Count does not count NumberString directly [duplicate]

I want to count all NumberStrings in a list: In[1]:= myList = {"123", "456", "789", "ab1", "cd2", "xyz"}; but, surprising (to ...
2
votes
1answer
44 views

Unconstrained pattern evaluated by a constrained replacement rule

I'm working with a notebook that defines NonCommutativeMultiply on a defined set of symbols, called operators. Semantically, ...
1
vote
2answers
57 views

Find and replace particular multiplication of factors in big multiplication

I have a sum of terms where each is a multiplication of several factors. For example my expression in FullForm is ...
3
votes
3answers
96 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
30 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
44 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
70 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
64 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
58 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
108 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
131 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
274 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
40 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
77 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
72 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
47 views

PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...
0
votes
1answer
31 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
72 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
197 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
60 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 ...
3
votes
1answer
73 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
38 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
127 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
71 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
130 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
102 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
81 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
290 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
69 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
147 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
93 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
32 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
47 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
42 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
51 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: ...