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

learn more… | top users | synonyms (1)

38
votes
9answers
1k views

Injecting a sequence of expressions into a held expression

Consider the following toy example: Hold[{1, 2, x}] /. x -> Sequence[3, 4] It will give Hold[{1, 2, Sequence[3, 4]}] ...
46
votes
4answers
2k views

Using a PatternTest versus a Condition for pattern matching

My last question to the site resulted in several answers that involve using pattern matching in Mathematica, a feature I wasn't very familiar with at the time. I am currently reading Mathematica ...
28
votes
4answers
3k views

Looking for “Longest Common Substring” solution

I'm looking for robust code to solve the "Longest Common Substring" problem: Find the longest string (or strings) that is a substring (or are substrings) of two or more strings. I can just code it ...
23
votes
1answer
448 views

Why doesn't PatternTest work with Composition?

While playing around with the solutions to this question, I've found some very strange behaviour: ...
17
votes
2answers
605 views

How to inject an evaluated expression into a held expression? [duplicate]

I know that there are methods to structurally manipulate held expressions (discussed e.g. here), but I failed to apply those for this particular problem: ...
17
votes
5answers
2k views

The difference between 0. and 0

I have a function for which 0 is a special case: f[A___, 0, B___] := 0 But since I am doing numerics, sometimes in the course ...
14
votes
4answers
554 views

Returning an unevaluated expression with values substituted in

Let's say I have a function formula[x_List] := (x[[1]] - x[[2]]) + (x[[3]] - x[[4]]); and I want to pass it a variable and get back the unevaluated formula with ...
18
votes
2answers
1k views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
6
votes
2answers
231 views

Unexpected behavior of rule matching a pattern

I am a beginner exploring the world of Mathematica. I expected the following code T[6, 5, 4, 1, 2, 3] /. {T[a___, 1, b___] -> Length[List[b]]} should return ...
42
votes
1answer
1k views

How to generally match, unify and merge patterns?

This question was split from this one. While that question is now about how to match two particular patterns (mostly using Verbatim or ...
14
votes
1answer
286 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
48
votes
4answers
1k views
35
votes
6answers
2k views

Find zero crossing in a list

I'm looking for a function that finds the index of the zero-crossing points of a list. Before I go making my own subroutine to do this, I was wondering if anyone knows of any built-in Mathematica ...
35
votes
2answers
661 views

How is pattern specificity decided?

Mathematica has a notion of pattern specificity, which is a partial ordering on patterns. The rules (e.g. DownValues, ...
25
votes
2answers
469 views

How can I ensure that I am constructing patterns in the most efficient way possible?

Inspired by this answer, I am interested to know if there are best practices or rules of thumb for constructing patterns, for example for use in function definitions (...
16
votes
4answers
850 views

How to match a pattern with a pattern?

UPDATED This question is about: "What is the pattern to match a pattern-definition, exactly as it is written?" My original question is now split according to Mr.Wizards insightful answer below. This ...
15
votes
1answer
745 views

How to Combine Pattern Constraints and Default Values for Function Arguments

EDIT: As several respondents have noted in the answers and comments below, the original example had a default value that would never be used because of the way patterns and default values are ...
9
votes
3answers
726 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: ...
12
votes
4answers
336 views

The gap between MapAt and ReplacePart for 2D data tables with headers

Consider a relational table derived from survey data, where each column ("001-01" ...) represents a responder and each row (...
10
votes
3answers
188 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 ...
31
votes
1answer
526 views

Why is StringExpression faster than RegularExpression?

Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar: ...
23
votes
5answers
2k views

Fetching data from HTML source

I want to generate a couple of plots/graphs with Area51 statistics. Since Area51 doesn't work with the SE API, I'm forced to find another way to get the information I want. That other way is with ...
17
votes
2answers
368 views

Pattern that matches colors

Suppose I'm writing a function that takes a color as a parameter; for example: drawShape[color_] := Graphics[{Style[Disk[], color]}]; But if the caller inputs an ...
9
votes
3answers
490 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 ...
10
votes
4answers
316 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 ...
6
votes
2answers
249 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
7
votes
2answers
218 views

Using MatchQ (or other means) to parse an expression using sums of COS or SIN correctly

I am writing a function that will take an expression as input. I want to make sure the expression is valid first before processing it. Looking for a correct to use ...
4
votes
2answers
163 views

How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...
4
votes
7answers
381 views

Given a symbolic expression how to find if starts with a minus or not?

I am using a Mathematica function which returns some error term in symbolic form. I needed a way to determine if this term starts with a minus sign or not. There will be only one term This is to ...
3
votes
2answers
112 views

Pattern matching not going over a list when using default values

if I execute the following command in Mathematica: {a,a^2,a^3}/.a^(n_:1)->b^(2 n) Mathematica returns: {b^2,b^4,b^6} ...
2
votes
1answer
142 views

replacement rule to pull independent expression outside of Integrate

I'm trying to write a fairly general replacement rule that will pull expressions independent of the variable of integration outside of Integrate, since Simplify and ...
20
votes
4answers
569 views

How can a big table be treated as a database?

I often work with big tables that I want to treat kind of like a database. Here's an example table. ...
19
votes
1answer
193 views

How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take ...
16
votes
3answers
580 views

Position of a pattern-matched part of an expression

What you have A pattern, with certain part of it labeled As an example — Label: wally ...
11
votes
1answer
277 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. ...
15
votes
6answers
937 views

Using patterns in pure functions

Pure functions may be handy if you don't want to assign your function a name. For example I would calculate $x(x-1)$ for some numbers $x$ by ...
10
votes
4answers
231 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: ...
6
votes
2answers
218 views

Is it possible to use PatternTest and Optional value on one Pattern simultaneously? [duplicate]

Possible Duplicate: How to Combine Pattern Constraints and Default Values for Function Arguments First a simple example: define a function "add" with two arguments, and its second argument ...
14
votes
3answers
348 views

Make mathematica treat $e_i^2$ as numeric

With NumericQ[symbol] = True, I can declare that a symbol is numeric. I want the expressions matching: $$e_{\text{i$\_$}?\text{IntegerQ}}^2$$ to be treated as ...
12
votes
4answers
527 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 ...
6
votes
4answers
509 views

Function argument to default under certain condition

Inspired by this and this question (and how I handle this in practice), what is the best way to default a function value when a certain condition is met? For example, if a function is defined as: ...
8
votes
8answers
868 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
322 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 ...
11
votes
3answers
543 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: ...
9
votes
1answer
315 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: ...
14
votes
1answer
271 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 ...
3
votes
2answers
219 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
20
votes
4answers
1k views

Detecting patterns of black and white stones on a 2D board

I'm writing a program to play a game of Pente, and I'm struggling with the following question: What's the best way to detect patterns on a two-dimensional board? For example, in Pente a pair of ...
5
votes
3answers
326 views

Highlighting text with StringReplacePart but also using Style, Subscript

For a Natural Language Processing analysis task it's desirable to highlight a text string programmatically according to XML tags that reference specific words or phrases. The XML tags specify the ...
12
votes
2answers
347 views

Split a Unicode string maintaining uppercase characters

I want to split a string according to a predefined set of substrings (lowercase), though the actual text can contain uppercase characters anyplace. The task is to find the matches, longer preferred ...