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

learn more… | top users | synonyms (1)

11
votes
2answers
393 views

Soft-Match String Comparison

I have a list of names in which I would like to check for duplicates. However, within the list, the duplicates may not show up as exact duplicates of each other - for instance, ...
11
votes
4answers
468 views

Calling Table with custom iterator

I often find myself in situations where I, for example, need to build a table for some expression, but want to set the number of points rather then the step size, so the code ends up looking like this ...
11
votes
3answers
514 views

Delete duplicates in a list, depending on the sequence of numbers

Below, list is a representative sample of my list, which contains lists of integers. I would like to be able to input: ...
11
votes
1answer
257 views

I can't use PatternSequence with Cases

I have a list of integers: s = {4, 5, 5, 6, 2, 5, 3, 2, 4, 2, 1, 1, 4, 3, 4, 3, 1, 6, 3, 4}; And if I do this, both occurrences of 4 after 3 are replaced. ...
11
votes
2answers
320 views

Should I use \\\“ or \” to match " in regex?

Both of the following return True: ...
10
votes
3answers
779 views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
10
votes
4answers
305 views

How to match expressions with a repeating pattern

The expression I want to match obey a simple pattern that repeats it self a number of times. f[a]@f[b]@f[c]@...@f[X] How would you match all expressions of this ...
10
votes
3answers
442 views
10
votes
4answers
226 views

Is it possible to separately call the default value of a variable which isn't at the end of the sequence?

After reading this post I recalled an old question I once met. We know that we can set default values for variables of a function: ...
10
votes
4answers
294 views

What is the correct way to use specific rule for specific pattern match when parsing an expression?

update: Thanks for the answers below. But I noticed they do not handle the following special cases: lis=1 and lis=z , ...
10
votes
4answers
410 views

How to use pattern matching to assign values to Subscript[f,x_]?

I want to define two subscripted functions Subscript[f,1] and Subscript[f,2]. To keep the assignments local, I would like to ...
10
votes
3answers
174 views

Pattern matching on Orderless functions inside Hold

MatchQ[Hold[1 + 2], Hold[2 + 1]] returns False. But in the presence of a Blank[] in the ...
10
votes
6answers
296 views

How can I mix the behaviors of StringReplace and ReplaceAll in a single rule?

I'm trying to take a list of the form { {"65 + 3 months", 75}, {"65 + 4 months", 75.1} }, and transform the string part to a number such as 65.25 or 65.333, ...
10
votes
1answer
526 views

Mathematica equivalent of regex '\w{2,3}'

It beats me to write the Mathematica pattern equivalent to the regex: \w{2,3} Any ideas?
10
votes
1answer
233 views

Why is ReplaceAll behaving like this?

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing. For Sqrt[f[x, y]] /. f[___] -> u Mathematica returns ...
10
votes
4answers
227 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
10
votes
1answer
143 views

Why doesn't repeated PatternSequence work with explicit pattern names?

Why would this work: Clear[f] f[a : PatternSequence[b_, c_]] := {a}; f[1, 2] (* {1, 2} *) and this also works: ...
10
votes
1answer
306 views

Mixing patterns with string patterns

I have imported an XML document into Mathematica. It is now represented as Symbolic XML expression in the variable xml: ...
9
votes
5answers
300 views

How can I remove matching sublists from my list?

I want to use DeleteCases (or any appropriate function) to remove elements of a list, which in turn are lists of fixed size. The rule I wish to apply is that any ...
9
votes
3answers
246 views

Splitting a list in which figures vary from negative to positive

Consider the following list: data={4078, 256, -1266, -388, 1290, 1368, 1446, 1524, 2}; I would like to split any kind of such list always, when figures turn form ...
9
votes
4answers
553 views

Using Position (which requires a pattern) in the same manner as Select (which requires a criterion)

I am using Mathematica7. I have a list of lists (creatively named list in the following sample code) containing data. I would like to find the positions of the ...
9
votes
2answers
233 views

Why doesn't Cases match all instances of XMLElement, given Infinity levelspec?

Why doesn't Cases match all instances of XMLElement, given Infinity levelspec? ...
9
votes
1answer
101 views

Apply N only outside a certain function

1 + f[1] // N gives 1. + f[1.] I don't want the argument of f evaluated by N; I ...
9
votes
1answer
109 views

Why can't I remove the {0, 0} in Circle[{0, 0}] using pattern matching?

Why does Circle[{0, 0}] /. List[_?NumericQ ..] :> Sequence[] return Circle[{0, 0}] yet ...
9
votes
1answer
306 views

Selectively clear DownValues

How can I clear a subset of a symbol's DownValues ? For example, suppose I have created some DownValues for $f$ like this: ...
9
votes
3answers
648 views

Pattern matching a pattern with patterns

Confusing title, I know. But the question is, if we have two patterns which have the same general structure but different names used in the patterns and different names: ...
9
votes
1answer
217 views

How to define function that can change patterns

Given time series data as follows: data = {{{2011, 1, 13}, 10}, {{2011, 2, 14}, 17}, {{2011, 3, 15}, 20}, {{2012, 1, 16}, 30}, {{2012, 2, 17}, 40}}; ...
8
votes
8answers
852 views

How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
8
votes
3answers
191 views

Select and Blank

I try to filter sublists of a list which match a pattern. test = {{"String1", "a"}, {"String2", "b"}, {"String3", "a"}, {"String4", "a"}}; The result should ...
8
votes
4answers
271 views

Check in a series if there exists adjacent values with less than a certain number of missing values

I'm working with a large number of series of different experiments, where there exist a lot of experiments where there are no entries. I now want to draw a sample where there should exist at least ...
8
votes
3answers
496 views

How can I convert x^2 to x*x?

When I try the following code: a b^2 c /. b c -> e Mathematica gives me: a b^2 c but what I want is: ...
8
votes
2answers
714 views

Determine whether some expression contains a given symbol

Given a symbol t and an expression expr, how can I determine whether or not the symbol t ...
8
votes
1answer
175 views

How to tell when a job is too big for the pattern matcher?

Pattern matching is great but it has its limitations. Consider sorting a list of numbers from smallest to largest: ...
8
votes
1answer
107 views

CSS Selectors for Symbolic XML

Symbolic XML is a convenient way of managing XML in Mathematica, at least for casual use or small tasks. Here and elsewhere you can find examples of XML handling in Mathematica, including XSLT/XPath ...
8
votes
2answers
188 views

Orderless pattern matching of rule lists

I am looking for a compact way to specify a pattern to match a rule list with MatchQ that allows for enumerating the rules in the rule list in an arbitrary order. ...
8
votes
3answers
304 views

Patternmatching sets

How to match set-patterns against sets? A set (in the mathematical sense) is a list of elements without repetition and order of elements does not matter. For example, we have a pattern set ...
8
votes
2answers
187 views

Replacing product expressions - named vs. unnamed pattern?

Suppose I have an expression of the form a b c d + x y z The FullForm of this is ...
8
votes
3answers
435 views

Find subexpression to minimize leafcount after replacment with temporary variable

Given an expression expr, is there an automated way to find a subexpression subexpr such that replacing ...
8
votes
4answers
408 views

Best way to modify values in a list of rules?

Recently I had to solve a problem similar to this: Let's say I have a list of list of rules ...
8
votes
1answer
266 views

Does Mathematica use first order or second order order unification?

When Mathematica is pattern-matching expressions, does it use first order or second order unification. Just to clarify the difference between first order and second order unification: In Second order ...
8
votes
1answer
201 views

MatchQ and patterns for similar lists

I can't seem to find the right set of patterns to independently match two different lists. Simplified versions of the lists are as follows: ...
8
votes
2answers
680 views

Mathematica rule substitution memory

First I present the specific programming question, and then I provide further background details. Given an expression in Mathematica, say f=3x+7y, there are two ...
8
votes
1answer
92 views

How to get a list of all expressions in a notebook matching a certain pattern?

Suppose I have a Mathematica notebook saved to a certain location on disk. Given a pattern I need to get the list of all expressions matching this pattern that appear in input, output or print cells ...
8
votes
1answer
184 views

Transformation rule on a list of non-negative integers

I am trying to create a transformation rule that takes a list of non-negative integer values of any length, finds a non-zero entry in the list, adds 1 to all preceding numbers, subtracts 1 from the ...
8
votes
1answer
304 views

Why does Mathematica choose the second function definition?

While working on a solution to this question I've come across a case where I simply don't understand Mathematica's behaviour. I've got the following definitions: ...
8
votes
2answers
366 views

How to Enable Syntax Coloring of Pattern Match Variable Only (i.e. Without Coloring any Associated Pattern)?

Syntax coloring for the following function definition foo[x_List] := ... will color green (and italicize by default) the argument name ...
8
votes
1answer
268 views

Mma10 mystery: Failure of pure function substitution with Interpretation?

Bug introduced in 10.0.0 and fixed in 10.0.1 I had code to display with subscripts from Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? which ...
8
votes
1answer
238 views

Creating a version of SequenceAlignment that accepts patterns

SequenceAlignment seems like a function that is not (yet?) fully integrated into Mathematica. I want a function that accepts general patterns instead. I ...
7
votes
5answers
336 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
7
votes
3answers
221 views

Pattern matching Q for rectangular data matrices

Given a List of rectangular nx3 arrays of data, each of the form {...,{x,y,z},...}, (the ellipses are not Mathematica pattern syntax, just for illustration). Some members of this list may be - by ...