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

learn more… | top users | synonyms (1)

8
votes
8answers
892 views

How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
9
votes
1answer
335 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: ...
6
votes
2answers
301 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. ...
4
votes
3answers
458 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 ...
23
votes
1answer
827 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 ...
21
votes
4answers
1k views

Detecting patterns of black and white stones on a 2D board

I'm writing a program to play a game of Pente, and I'm struggling with the following question: What's the best way to detect patterns on a two-dimensional board? For example, in Pente a pair of ...
9
votes
4answers
709 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 ...
17
votes
6answers
753 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
360 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 ...
10
votes
1answer
216 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? ...
7
votes
3answers
227 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
297 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. ...
8
votes
2answers
150 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 ...
6
votes
1answer
478 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
203 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
432 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
820 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 "...
3
votes
1answer
145 views

Custom operators; custom pattern matching with pure functions

What I'm trying to achieve in Mathematica is the creation of a binary operator whose operands are both pure functions over the natural numbers. The result of the operator should be another pure ...
3
votes
1answer
146 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
1k 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
356 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
657 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 ...
4
votes
3answers
79 views

Extracting subscripts and superscripts

Suppose I have several types of expressions (in some random order) of the form Power[Subscript[g, a], μ] which is $g_a{}^\mu$ ...
4
votes
2answers
199 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 ...
9
votes
1answer
239 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}}; ...
7
votes
2answers
193 views

Using Except with strings

Suppose that I have the following sample list, called list: list = {"bBb", "aAa", "aaa"}; Now suppose I want to select the ...
5
votes
2answers
192 views

Why rule does not work if Flat attribute?

The following works: ...
5
votes
2answers
226 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
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
321 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
538 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
1answer
187 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 ...
3
votes
3answers
276 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
94 views

Testing for a vector of reals [closed]

Suppose I define: RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)]; Then: ...
2
votes
2answers
173 views
2
votes
1answer
221 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
353 views

Pattern test for variables during function definition

I am having a problem with pattern testing on variables during function definition ...
14
votes
3answers
181 views

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

In this answer, what does the : Hold[$IterationLimit] part of the following construct do? ...
18
votes
1answer
519 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 ...
20
votes
7answers
1k views

Elegant and fast way to join all string sequences

I want to do what title says: {"a","b",1,2,3,4,"a",2,2,2,"edg","?"} => {"ab",1,2,3,4,"a",2,2,2,"edg?"} in the most elegant way. For example one replacement rule for ...
16
votes
1answer
250 views

Bug in RepeatedNull?

RepeatedNull appears to be buggy (versions 8.0, and 9.0). Define f as follows: ...
10
votes
4answers
435 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 ...
8
votes
1answer
140 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 ...
13
votes
1answer
112 views

Unexpected pattern matching behaviour: PatternSequence vs. Optional

fixed in 10.1 (windows) Consider the following pattern-matching expressions: ...
11
votes
2answers
495 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
5answers
531 views

Best way to modify values in a list of rules?

Recently I had to solve a problem similar to this: Let's say I have a list of list of rules ...
7
votes
2answers
379 views

Adapting “Find and Replace” functionality to work with Patterns

I was thinking if there is something like Find and Replace (Crtl+F) functionality adapted to work with patterns. Probably not, but it seems possible to create this with some effort and skills in ...
6
votes
5answers
368 views

Find patterns in a list

again, I have a list like this: ...
14
votes
2answers
323 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
479 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 ...