Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
44
votes
4answers
765 views
What best practices or performance considerations are there for choosing between Cases, Position, Pick and Select?
Cases, Select,Pick and Position each have different ...
36
votes
1answer
921 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 ...
31
votes
4answers
831 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 ...
26
votes
6answers
943 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 ...
24
votes
9answers
780 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]}]
...
22
votes
5answers
951 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 ...
22
votes
2answers
347 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 (...
22
votes
0answers
313 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, ...
21
votes
0answers
157 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:
...
18
votes
2answers
625 views
Pattern to match a non-empty list of non-empty lists
Is there a pattern to match a list of lists, containing at least one list, with each list containing at least one element?
In the Mathematica documentation it says that the pattern
...
18
votes
1answer
332 views
Why doesn't PatternTest work with Composition?
While playing around with the solutions to this question, I've found some very strange behaviour:
...
18
votes
4answers
431 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.
...
18
votes
1answer
222 views
replacement rules from a pattern and a matching expression
(This seems to be a very basic necessity in a language having pattern-matching capabilities such as Mathematica, yet I struggled for many hours to find a common solution to this problem.)
Suppose ...
16
votes
6answers
534 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 ...
16
votes
1answer
267 views
Is pure pattern matching without PatternTest and Condition Turing-complete?
Note that this question is not a duplicate of that one. It is specifically about the pattern-matching engine, and explicitly excluding those patterns which call back into the full evaluation (i.e. ...
16
votes
1answer
335 views
NotebookFind and String Pattern Expressions
Is there a way that NotebookFind can be used to match string pattern expressions rather than just strings?
The documentation for ...
15
votes
4answers
345 views
Splitting a list
Please consider the following list:
data={1, 0, 0, 0, 2, 5, 2, 3, 0, 0, 3};
Now I would like to split the list into 0-sequences and Not-0-sequences as following:
...
15
votes
3answers
298 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
...
14
votes
5answers
783 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
6answers
586 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
...
14
votes
3answers
213 views
What's wrong with this pattern of the form Except[Repeated[…]]?
I'm trying to build a head that recognizes molecules. Here's the code
...
14
votes
2answers
354 views
How do I perform string matching and replacements?
What are, and how do I use Mathematica's string matching and replacement tools?
14
votes
2answers
290 views
Successful match in Replace but not in Cases?
I have a little example that exhibits a successful pattern match in ReplaceAll that Cases misses and I wonder if the assembled ...
14
votes
2answers
410 views
Pattern issues weird warning
Take for example
head[b_[PatternSequence[_, _]]] := 9
I know this example serves no purpose as is. It also happens in useful examples such as
...
13
votes
3answers
300 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 ...
13
votes
4answers
534 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 ...
13
votes
2answers
302 views
How to inject an evaluated expression into a held expression?
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:
...
13
votes
3answers
167 views
Question about using a pattern in ReplacePart
I noticed that "ReplacePart" can use pattern. For example:
ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} -> x]
{{x, 0, 0}, {0, x, 0}, {0, 0, x}}
So ...
13
votes
2answers
388 views
How to use subscript in pattern names?
For example I want to define a function with parameters σx and σy, that is, the function will be declared as: f[σx_,σy_] := . . ...
12
votes
4answers
238 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 (...
12
votes
2answers
196 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 ...
12
votes
2answers
229 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 ...
12
votes
2answers
246 views
How can I separate a separable function
I have a separable function $f[x,y]$, and I would like to find two functions $g[x]$ and $h[y]$ with
$f[x,y]=g[x] h[y]$
where $g[x]$ doesn't depend on $y$ and $h[y]$ doesn't depend on $x$. Ideally, ...
11
votes
4answers
297 views
Assessing argument type in set delayed function definitions
I'm wondering how to properly assess the type of arguments passed to functions defined with :=. I want my functions to be the most efficient, unambiguous and clear ...
11
votes
3answers
240 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 ...
11
votes
3answers
287 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
2answers
180 views
10
votes
5answers
708 views
Counting negative values in list
I would like to count the negative values of a list.
My approach was Count[data, -_] which doesn't work.
How can I tell Mathematica to count all numbers with ...
10
votes
3answers
482 views
Passing down arguments
In R, one can use ... to pass arguments down to another function. For example
...
10
votes
4answers
208 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
4answers
256 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
1answer
174 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
89 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.
...
9
votes
3answers
195 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
295 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
216 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,
...
9
votes
4answers
240 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
...
9
votes
3answers
115 views
What does `: Hold[$IterationLimit]` mean?
In this answer, what does the : Hold[$IterationLimit] part of the following construct do?
...
9
votes
1answer
151 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
...
9
votes
6answers
235 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, ...



