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

learn more… | top users | synonyms (1)

10
votes
9answers
971 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 ...
10
votes
3answers
358 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 ...
9
votes
1answer
349 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. ...
5
votes
3answers
159 views

Pattern match for nested Association

Suppose I have Association testAssoc=Association[a->Association[1->1,2->2],b->Association[1->1,2->2]] which ...
4
votes
3answers
472 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 ...
25
votes
1answer
898 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 ...
16
votes
4answers
728 views

How to define a recursive pattern?

I want to translate this recursive syntactic definition into a Mathematica pattern1: $$ \mathtt{x}: \begin{cases} \text{Null}\\ \{\textit{integer}, \mathtt{x}\} \end{cases} $$ In other ...
10
votes
4answers
760 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 ...
7
votes
2answers
401 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 ...
17
votes
6answers
765 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 ...
14
votes
2answers
329 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 ...
12
votes
2answers
366 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
234 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
306 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. ...
11
votes
4answers
276 views

Recognizing special cases of a defined function for permuted arguments

I have a number of ugly ways to implement the following pattern recognition task, but I'm looking for something elegant to keep my notebook tidy. I need to define a function of six variables ...
8
votes
2answers
181 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
490 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
220 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
461 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
868 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
169 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
153 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 ...
12
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
368 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 ...
8
votes
2answers
213 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 ...
7
votes
4answers
720 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 ...
5
votes
3answers
636 views

position of sequence of elements in list [duplicate]

Possible Duplicate: Finding a subsequence in a list Question The position of {3, 5} is the list {1, 3, 4, 3, 5, 5, 1} is 3. How can such a position ...
4
votes
3answers
86 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
205 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
247 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
209 views

Why rule does not work if Flat attribute?

The following works: ...
5
votes
2answers
230 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
6answers
2k 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
323 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
556 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
207 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
305 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, ...
3
votes
2answers
363 views

Pattern test for variables during function definition

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

Testing for a vector of reals [closed]

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

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

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

Bug in RepeatedNull?

RepeatedNull appears to be buggy (versions 8.0, and 9.0). Define f as follows: ...
10
votes
4answers
444 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
153 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 ...
14
votes
1answer
119 views

Unexpected pattern matching behaviour: PatternSequence vs. Optional

fixed in 10.1 (windows) Consider the following pattern-matching expressions: ...