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

learn more… | top users | synonyms (1)

6
votes
3answers
288 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 ...
6
votes
4answers
338 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: ...
6
votes
3answers
139 views

Using PatternTest and/or Condition to detect equal list lengths as well as list element formats

On searching Mathematica.SE for information about overloading functions, I came across several threads about PatternTest and ...
6
votes
2answers
269 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. ...
6
votes
4answers
333 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
181 views

Match sequence of zero or more characters except a given character

This looks like it should be simple: I want to match an "i" followed by an "o" with an arbitrary (possibly zero-length) sequence of characters in between, unless that sequence also contains an "i". ...
6
votes
2answers
183 views

Variable scoping problem when mapping over delayed replacement

This is something that got me curious while I was playing around with Mathematica. Consider the following (contrived) example: ...
6
votes
1answer
214 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. ...
6
votes
1answer
181 views

Pattern definition for replacing plus and subtract

Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$ I have a complicated expression, in ...
5
votes
5answers
344 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. ...
5
votes
3answers
382 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 ...
5
votes
4answers
262 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. ...
5
votes
2answers
152 views

Extract substring by repeated StringPattern

This code failed, but may exemplify my intention. StringCases["aA1bbbbbbbbbbaA2baA3b...aA4b",Repeated[Shortest["a"~~xx__~~"b"],{2,100}]] I want to get the ...
5
votes
2answers
185 views

How to get complement of a pattern

I have a list. I want to get all the elements in that list which do not match a given pattern. That is, I want the strings which do not match any of the following patterns: First and last characters ...
5
votes
4answers
160 views

Extracting sequences from a list?

Suppose I have a list of arbitrary length like {1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5} and I am trying to extract the sequences ...
5
votes
2answers
213 views

Keep Ordering of Bra Ket When Printing

I would like to keep the ordering of the bra ket-print but right now this doesn't happen. Any Idea? I tried playing with HoldForm but then i could evaluate the expression inside properly ...
5
votes
3answers
256 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 ...
5
votes
2answers
193 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 ...
5
votes
2answers
186 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 ...
5
votes
1answer
136 views

Why does this pattern for combining two lists require triple underscores (___) to work?

I understand that __ is a list of one or more elements, and that ___ is 0 or more elements, but when I try this rule with 2 ...
5
votes
2answers
160 views

Why rule does not work if Flat attribute?

The following works: ...
5
votes
1answer
194 views

Use Pattern with Vectors in Assumptions

I had a problem using Pattern with Vectors in Assumptions. Here's how I use ...
5
votes
0answers
164 views

Convert logical relational expression to / from disjunctive and conjunctive forms?

I would like to convert logical relational expressions in disjunctive form, e.g., $$(x \lt -1) \lor (-1 \lt x \lt +1) \lor (x \gt +1)$$ into conjunctive form, e.g., $$(x \ne -1) \land (x \ne +1)$$ ...
4
votes
7answers
321 views

Given a symbolic expression how to find if starts with a minus or not?

I am using a Mathematica function which returns some error term in symbolic form. I needed a way to determine if this term starts with a minus sign or not. There will be only one term This is to ...
4
votes
4answers
425 views

Creating lists from a string, separating words from numbers

Say I have a string that contains numbers and words, such as this one: string = "there are 1234 words and numbers 5678 in here $999"; How would I separate the ...
4
votes
2answers
283 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 ...
4
votes
5answers
587 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
2answers
203 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 ...
4
votes
2answers
165 views

How to use Optional as an omissible parameter in pattern-match?

Below is a function intended to test if an expression is a polynomial in which all the coefficients and exponents are integers: ...
4
votes
3answers
121 views

Simplifying expression with patterns (Replace)

Q1: I need to simplify some algebraic expression, according to particular pattern, namely: \begin{equation*} x^m = \left\{ \begin{array}{ll} 1 & \text{if } m \text{ is even} \\ x & \text{if ...
4
votes
2answers
219 views

Pattern matching repeated structures with named subexpression components

I am trying to write a function with a pattern that tests its arguments for a particular structure. But in that pattern I'd like to name a subpattern to make it easier to exract and operate on. The ...
4
votes
2answers
359 views

How can I name patterns in a Switch expression?

Suppose I have an expression and want to match it against successive patterns and return an expression corresponding to the first pattern that matched. In that case, I can use ...
4
votes
2answers
59 views

Pattern-rewriting Pattern triggers error?

I am trying to rewrite patterns. For example, I'd like to rewrite the pattern jFoo_Integer which has FullForm ...
4
votes
2answers
148 views

How can I replace the general element of a geometric series with the sum of the series?

Consider the list mylist = {b(a/bc t)^r, a/b(b t)^r, a (c/d t)^r, ...} where a, b, c, t, etc., are real numbers between 0 and 1. I want to sum each term in the ...
4
votes
2answers
144 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 ...
4
votes
2answers
189 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
4
votes
2answers
100 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 ...
4
votes
2answers
91 views

Pattern Matching with Indexed Object

If I have a list of rules associated with indexed variables like: solution = {a[1] -> 1, a[2] -> 10, a[3] -> 100} I would like to be able to extract all ...
4
votes
1answer
67 views

Dot when used in a pattern becomes Times… Why?

In[1] := Hold[a.b] // FullForm Out[1] := Hold[Dot[a,b]] So far so good. ...
4
votes
1answer
319 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 ...
4
votes
1answer
279 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
279 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 ...
4
votes
2answers
184 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 ...
4
votes
1answer
242 views

Pattern matching in subscripted variables

I am trying to use subscripted variables as symbols and provide assumptions elegantly for them, e.g. in order to simplify logs, I'd like to have ...
4
votes
1answer
98 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. ...
4
votes
1answer
216 views
4
votes
4answers
313 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
1answer
130 views

Selecting expressions from a sequence that are not subexpressions of earlier elements

I have a While loop that generates and prints an expression on each iteration (there can be thousands of iterations). UPDATE: Some expressions are generated in ...
4
votes
1answer
146 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 ...