Questions tagged [pattern-matching]

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

Filter by
Sorted by
Tagged with
2
votes
1answer
30 views

Removing all sublists containing any arbitrary string as one or more of its list component

so, basically I have a list of sublists of varying length. For example, ...
7
votes
3answers
236 views

How can I check if a function can be written as $a * \sin (b * x + c) + d$?

I want to write a function that takes another function, say $f(x)$ and a variable $x$ as input and return True it this function can be written in the form ...
13
votes
6answers
617 views

Replace zeros in a list with last nonzero value

This question is closely related to the problem of zero crossings. Suppose I have a list l = {-1, -1, 0, 1, 0, 0, -1, -1, 1, 1, 1, 0, 0, 0, -1} and I want to replace the zeros with the previous ...
0
votes
0answers
22 views

Check whether the argument of a function is a matrix with numerical values

I know that MatrixQ[expr,NumericQ] tells whether expr is a matrix with numerical values or not. How do I implement such a ...
1
vote
1answer
35 views

Compile string cases into list

I have a html formatted string: ...
1
vote
0answers
32 views

Replacing factors that are only dependent on a single specific variable

I have expressions with terms that are products of factors. Some of the factors are expressions that depend on a single specific variable. I would like to replace those factors with constants and ...
1
vote
0answers
64 views

Graph of a list of rules with coefficient

I am trying to transform an expression to a list of rules and after that plot its graph. The expression has the following form ...
8
votes
2answers
426 views

A simple problem about a Rule example

The documentation says that "Rule represents a rule that transforms lhs to rhs" and gives the following example: ...
0
votes
2answers
47 views

Monte Carlo Method [duplicate]

How do you use a Monte Carlo method to approximate the area enclosed by the circle.
2
votes
0answers
71 views

Lambda calculus reduction in Mathematica

Are there any built-in functions for reducing lambda terms in Mathematica? If not, how might one do it? I've heard of Function, which is good for $\beta$-reduction, ...
7
votes
3answers
150 views

Creating rule-based sublists

I have a list each element of which consists of a list containing two strings and two integers: ...
2
votes
1answer
36 views

Replace sub-expressions matching a pattern unless located under specific heads

Suppose I have some rule, e.g. Re[x_] :> (x + Conjugate[x])/2. I want to apply it to all matching sub-expressions in some expression, excluding any sub-...
3
votes
3answers
75 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
67 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
87 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
94 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
115 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
58 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
22 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
45 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
59 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
98 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
31 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
47 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
73 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
67 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
62 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
110 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 ...
4
votes
1answer
136 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
287 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
41 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
65 views

Pattern matching failed [closed]

I want to replace Θ[z + m τ] by a[m] Θ[z]. So I consider the pattern ...
1
vote
2answers
76 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
82 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
73 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
48 views

PatternTest vs Parallelization

Bug introduced in 10.0 or earlier and fixed in 11.2 Consider the following function ...
0
votes
1answer
35 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
74 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
203 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
62 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
75 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
41 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
74 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
132 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
106 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 ...