Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
22
votes
9answers
743 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]}]
...
29
votes
4answers
778 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 ...
17
votes
1answer
253 views
Why doesn't PatternTest work with Composition?
While playing around with the solutions to this question, I've found some very strange behaviour:
...
13
votes
2answers
290 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:
...
44
votes
4answers
740 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
893 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
2answers
334 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
5answers
761 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 ...
19
votes
2answers
299 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 (...
13
votes
4answers
490 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 ...
26
votes
6answers
891 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 ...
9
votes
1answer
333 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 ...
13
votes
3answers
296 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 ...
3
votes
2answers
144 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 ...
18
votes
4answers
429 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.
...
21
votes
5answers
917 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 ...
14
votes
3answers
254 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
3answers
237 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 ...
12
votes
4answers
234 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
202 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 ...
9
votes
1answer
150 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
4answers
227 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
...
7
votes
3answers
209 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 ...
7
votes
2answers
225 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 ...
5
votes
2answers
148 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
1answer
250 views
Path queries for tree-structured data
Can anyone suggest documentation or tutorials for developing path queries and indices for (XML-like) tree-structured data?
Suppose data is organized hierarchically in key->value pairs, eg:
...
18
votes
1answer
211 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 ...
14
votes
6answers
562 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
...
7
votes
3answers
302 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:
...
6
votes
2answers
125 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 ...
10
votes
4answers
247 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
463 views
Passing down arguments
In R, one can use ... to pass arguments down to another function. For example
...
8
votes
3answers
104 views
What does `: Hold[$IterationLimit]` mean?
In this answer, what does the : Hold[$IterationLimit] part of the following construct do?
...
8
votes
2answers
206 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,
...
7
votes
2answers
189 views
Why does the name of a pattern affect the result of a transformation rule?
I was playing a little dirty, trying to get a template for defining a Curl[] operator.
I wrote the following rule, which I knew was going to spit out a few ...
11
votes
4answers
283 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 ...
7
votes
1answer
147 views
Built in way to de-list singletons?
Is there a built in method to de-list singleton expressions, like this rule-based solution:
expr_ :> If[Length[expr] == 1, First@expr, expr]
In data ...
5
votes
2answers
166 views
Delete contexts from a string representation of a symbol
I want to delete the string representing the context of a symbol.
For info I need such a functionality in my ShowIt function defined here
I'd like to know how I ...
16
votes
6answers
523 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 ...
12
votes
2answers
181 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
191 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 ...
6
votes
4answers
170 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:
...
3
votes
2answers
165 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 ...
2
votes
2answers
111 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 ...
8
votes
1answer
282 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
449 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 ...
7
votes
1answer
159 views
Is it possible to specify a context-sensitive, “depth-agnostic” rewrite rule?
I'm using the term "depth-agnostic" in this post to describe structural patterns featuring a "target" sub-pattern that can occur "at any depth" relative to one or more "context" sub-patterns.
As a ...
2
votes
0answers
94 views
I am missing a function like HoldPattern
Temporary message: I am planning to edit this question, making the examples less confusing and by moving the things that are confusing to a separate question
For any confusion about expressions that ...
2
votes
1answer
88 views
Convert boolean test function to pattern?
What is a "reverse" to MatchQ? MatchQ converts pattern into boolean function. So how to convert back?
Suppose I want to use ...
2
votes
2answers
175 views
Pattern test for variables during function definition
I am having a problem with pattern testing on variables during function definition
...


