Questions on expression testing and manipulation through pattern matching and constructing efficient patterns.
2
votes
2answers
143 views
Formatting text through pattern matching
I am trying to format some text based on some patterns.
...
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 ...
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
...
5
votes
5answers
227 views
How can I use nested conditionals inside my functions?
I'm a big fan of using conditionals inside my functions to deal with cases, that is, to pattern match in stages and possibly leave a function unevaluated.
...
15
votes
4answers
341 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:
...
7
votes
3answers
169 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 ...
3
votes
1answer
108 views
Set promotes Rule to RuleDelayed?
In mentally building a model of how Mathematica works, I naively expected Set to create a Rule in a symbol's ...
6
votes
1answer
184 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 ...
4
votes
1answer
131 views
Searching Notebooks for String Patterns using Workbench v2
The Workbench webpage describing new features in version 2 lists the ability to "Search notebook documents with the Mathematica pattern search utility" under the heading Mathematica Front End ...
14
votes
2answers
408 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
...
14
votes
3answers
212 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
...
11
votes
3answers
270 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:
...
2
votes
2answers
76 views
Using FreeQ with implicit functions
In Mathematica 7, I want to determine whether a list of free of numbers greater than 1 or not. It seems like using FreeQ would be a concise way to do this:
...
15
votes
1answer
328 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 ...
0
votes
1answer
222 views
What does the slash-colon symbol do?
I came across a bit of code that uses the syntax /: and I don't know what it does. I can't find its documentation, or maybe I'm just not looking properly. The code ...
14
votes
2answers
286 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 ...
7
votes
2answers
316 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 ...
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 ...
16
votes
1answer
262 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. ...
8
votes
1answer
216 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
154 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
1answer
283 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:
...
9
votes
6answers
231 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, ...
9
votes
3answers
192 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
186 views
How to match a sequence of complex patterns?
I have list with the form {{a1,b1}->{x1,y1},{a2,b2}->{x1,y2},...} where the {ai,bi}'s can be ...
8
votes
2answers
159 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?
...
4
votes
1answer
182 views
12
votes
2answers
226 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 ...
-2
votes
1answer
173 views
How to set a rule in pattern matching to change a string from one form to another
I want to define a rule that will change a particular type of string to another form.
For example:-
a string having ...
4
votes
2answers
248 views
Why can't NumberQ be used as the head of a pattern?
Consider the following:
list={1/First[{}], 1, 2, 1/First[{}], 3};
DeleteCases[list,_NumberQ]
I wanted to remove all cases, which did not match ...
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 ...
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:
...
4
votes
1answer
75 views
How do you set an Optional parameter with a global variable on a Function defined in a Package
In a Package I am writing, I'm trying to define a function with an Optional parameter in it that is set to a global variable.
...
14
votes
6answers
563 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
...
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 ...
36
votes
1answer
899 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 ...
13
votes
2answers
380 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_] := . . ...
13
votes
4answers
493 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
291 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:
...
0
votes
1answer
140 views
How to use Function[] in MatchQ[] and control argument's evaluation? [duplicate]
Possible Duplicate:
Why doesn't PatternTest work with Composition?
...
6
votes
1answer
156 views
How to set up new types for pattern matching strings?
Consider the following toy example: I have a set of language sounds, which I partition into two exclusive subsets, consonants and vowels. I want to set up string patterns for e.g. ...
8
votes
2answers
450 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 ...
6
votes
1answer
464 views
Extracting coefficients from a partial differential equation
Frequently, I come across the following problem:
How to rewrite a complicated partial differential equation in a more clear way? I would like to create some order by collecting terms that are equal.
...
23
votes
9answers
747 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]}]
...
10
votes
3answers
463 views
Passing down arguments
In R, one can use ... to pass arguments down to another function. For example
...
29
votes
4answers
779 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:
...
6
votes
3answers
232 views
Replacement rule only matches part of expression
I have the result of a calculation (which is too long to mention here) and it gives
...
8
votes
4answers
218 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 ...


