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

learn more… | top users | synonyms (1)

21
votes
2answers
1k 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 ...
5
votes
3answers
332 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 ...
17
votes
6answers
740 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
348 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
225 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 ...
6
votes
4answers
289 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. ...
9
votes
1answer
194 views

Orderless pattern matching

The MMA docs state that "In matching patterns with Orderless functions, all possible orders of arguments are tried". Why then does the following not work? ...
6
votes
1answer
447 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 ...
5
votes
3answers
191 views

Converting expressions to “edges” for use in TreePlot, Graph

This question is similar to this: Nested list to graph. How to "flatten" an arbitrary expression, eg expr = a[b[c, d[e][f], g], h] to a list of key-value pairs ...
4
votes
4answers
396 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 ...
3
votes
1answer
135 views

Inheritance in Mathematica using pattern matching on UpValues

Edit: I think in my original post, I missed emphasizing the main point. This is mainly a pattern matching question. If you're interested in the 'why' of this question, please read the original post ...
10
votes
2answers
876 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 ...
9
votes
5answers
336 views

How can I remove matching sublists from my list?

I want to use DeleteCases (or any appropriate function) to remove elements of a list, which in turn are lists of fixed size. The rule I wish to apply is that any ...
7
votes
4answers
583 views

How to collect terms with positive powers in polynomial

I am trying to collect all terms with non-negative powers of $x$ in polynomials like $\frac{1}{x^2}\left(a x^2+x^{\pi }+x+z\right)^2$ First expand the polynomial ...
6
votes
1answer
92 views

Held keys in associations

Jacob Akkerboom commented: Note that PositionIndex does work correctly with held expressions, whereas this is a bit painful to implement using ...
4
votes
2answers
195 views

Alternatives ordering affects pattern matching in Cases?

Given expr = f[x, g[y], z] In the following query, the pattern h_[__, c_] appears as last slot in ...
3
votes
2answers
715 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 "...
9
votes
1answer
230 views

How to define function that can change patterns

Given time series data as follows: data = {{{2011, 1, 13}, 10}, {{2011, 2, 14}, 17}, {{2011, 3, 15}, 20}, {{2012, 1, 16}, 30}, {{2012, 2, 17}, 40}}; ...
5
votes
2answers
189 views

Why rule does not work if Flat attribute?

The following works: ...
5
votes
2answers
223 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
3answers
440 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
4
votes
5answers
1k views

Removing zero values from ordered pairs

I have some processed data, which are basically a collection of ordered pairs in the form: {{x1, y1}, {x2, y2}, ..., {xn, yn}} Ordered pairs which have either a ...
4
votes
1answer
315 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: ...
3
votes
4answers
522 views

More elegant way

I read ReplaceOrRemoveInvalidOrMissingData Mathematica documentation, and I tried to apply the same approach to my dataset. I've got two problems, but one is only a style problem! There is one more ...
3
votes
3answers
239 views

How to do Cases with multiple related patterns?

I would like to write a function similar to Cases, but can search for a group of related patterns together. For example, ...
2
votes
3answers
88 views

Testing for a vector of reals [on hold]

Suppose I define: RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)]; Then: ...
2
votes
2answers
147 views
2
votes
1answer
163 views

Simplify equations with pattern assumptions

When simplifying an expression using Simplify[expr, assumptions], the assumptions has to be explicit. Is it possible to simplify an expression with pattern ...
2
votes
1answer
210 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
338 views

Pattern test for variables during function definition

I am having a problem with pattern testing on variables during function definition ...
20
votes
1answer
743 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
3answers
176 views

What does `: Hold[$IterationLimit]` mean?

In this answer, what does the : Hold[$IterationLimit] part of the following construct do? ...
16
votes
1answer
489 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 ...
16
votes
1answer
244 views

Bug in RepeatedNull?

RepeatedNull appears to be buggy (versions 8.0, and 9.0). Define f as follows: ...
8
votes
1answer
126 views

CSS Selectors for Symbolic XML

Symbolic XML is a convenient way of managing XML in Mathematica, at least for casual use or small tasks. Here and elsewhere you can find examples of XML handling in Mathematica, including XSLT/XPath ...
11
votes
2answers
455 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, ...
10
votes
4answers
423 views

Building a tree

Given a list of word characters, such as this one, I'd like to build a tree, similar to this makeTree function, but with the tree in a different format. So, for an input such as ...
9
votes
4answers
647 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 ...
14
votes
2answers
320 views

Pattern to match L, but not L[_]

I have an expression where I want to replace L but not L[_] For example, the following doesn't work and ...
13
votes
3answers
443 views

How to match a cyclically repeating sequence?

I have run across several times recently where I must match a sequence that is cyclically repeating. For example, with the sequence, Sequence[a, b, c] how would ...
13
votes
3answers
298 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 ...
11
votes
4answers
511 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 ...
10
votes
0answers
82 views

Unexpected pattern matching behaviour: PatternSequence vs. Optional

Consider the following pattern-matching expressions: ...
10
votes
4answers
450 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
853 views

Passing down arguments

In R, one can use ... to pass arguments down to another function. For example ...
8
votes
2answers
216 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 ...
5
votes
2answers
198 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 ...
7
votes
5answers
366 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
7
votes
1answer
212 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 ...
4
votes
1answer
380 views

Extracting text from nested HTML lists <ul> <li>

We wish to analyze the tree structure of HTML documents that consist of nested lists, ie, using the HTML tags <ul> and ...