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

learn more… | top users | synonyms (1)

34
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]}] ...
41
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 ...
19
votes
1answer
396 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
530 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: ...
39
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 ...
16
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
465 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 ...
16
votes
2answers
788 views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
4
votes
2answers
213 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 ...
46
votes
4answers
1k views
34
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 ...
23
votes
2answers
423 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 (...
14
votes
4answers
699 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
641 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 ...
27
votes
0answers
406 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: ...
22
votes
5answers
1k 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 ...
9
votes
3answers
581 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: ...
8
votes
3answers
375 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 ...
12
votes
4answers
292 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
4answers
293 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 ...
4
votes
2answers
155 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 ...
27
votes
0answers
512 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, ...
19
votes
4answers
540 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. ...
15
votes
3answers
479 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 ...
15
votes
6answers
842 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 ...
6
votes
2answers
209 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 ...
13
votes
3answers
339 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 ...
11
votes
4answers
420 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
394 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
3answers
297 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
488 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
298 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: ...
7
votes
2answers
196 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 ...
2
votes
1answer
129 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 ...
17
votes
1answer
177 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 ...
10
votes
4answers
219 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: ...
5
votes
3answers
273 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
318 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 ...
7
votes
3answers
216 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 ...
5
votes
4answers
270 views

Is there a more Mathematica-y way to label these plots?

I am studying an infinite square well in the context of quantum mechanics. ...
16
votes
6answers
674 views

Split a string at specific positions

Given a string of alphanumerical characters, how to split it simply and quickly at the center of continuous letter-substrings? Is there an elegant and fast solutions out there in the "computational ...
6
votes
1answer
361 views

Count sublists which match pattern

I have a list which looks like this: data={{12,0},{0,0},{20,1},{0,0}, {0,-1}}. Now I would like to count the number of sublists which: have both slots zero the ...
4
votes
4answers
333 views

Listing matrices up to symmetry

I am interested in the equivalence relation on N x N binary matrices, in which two matrices are equivalent if one can be obtained by rotating/reflecting the other. I would like to obtain a list ...
4
votes
2answers
195 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
3
votes
2answers
556 views

Classifying poker hands by pattern matching

I am working on this question posed in a Mathematica course revised in 1998. Here are the instructions: Poker Hands You are to define and show working examples of a function "...
10
votes
3answers
160 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 ...
7
votes
2answers
611 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 ...
6
votes
2answers
277 views

Rule for orderless function Times

I have problems extending a replacement rule. Schematically, this is the situation: I have an expression and a rule, and they work perfectly together. ...
5
votes
2answers
208 views

Pattern matching deletion of list items [duplicate]

Possible Duplicate: How can I remove B -> A from a list if A -> B is in the list? I'm working toward building a graph and use some code to construct my rules which results in a form ...
4
votes
7answers
342 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 ...