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

learn more… | top users | synonyms (1)

6
votes
4answers
817 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
214 views

Isolating string characters

I have the following string: ...
6
votes
3answers
217 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
315 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
3answers
145 views

Filtering lists by partial matches

Suppose I have the following data: ...
6
votes
4answers
351 views

Pattern matching - comparing two lists

I would like to compare two different lists to see if the shorter one is a member of the larger e.g. is the list {2,3}, with that sequence of elements, a member of {1,2,3,4,5}? I have tried using ...
6
votes
3answers
274 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 ...
6
votes
2answers
425 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 ...
6
votes
2answers
98 views

Pattern matching an expression involving Times

I'm trying to write a Mathematica function that will format expressions according to my preferences, for example displaying (5 c x)/Sqrt[2] as ...
6
votes
2answers
212 views

ReplaceAll problem

Bug introduced in 10.1 and persisting through 10.2 or later Confirmed, as bug, by WRI: [CASE:3402234] I am trying to combine terms of the type ...
6
votes
1answer
332 views

Sentiment analysis of tweets

I want to use Mathematica for a sentiment analysis on tweet-messages. For that purpose i selected several tweets. Each tweet is interpreted by myself as positive (pos) of negative (neg). For example: ...
6
votes
5answers
283 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 ...
6
votes
2answers
302 views

Apply Trig Identity to Transform Sum of Sinusoids into Phase-Shifted Cosine Function

I'm trying to solve for the output of a simple RC low-pass filter, with Cos[w t] input. The convolution of the input with the RC transfer function gives me the right output, but I'd like to be able to ...
6
votes
3answers
208 views

Strange behavior of MemberQ, Position

After importing an array of latitudes and longitudes and trying to find the position of some latitudes I was encountered with an unexpected problem since certain values could not be found for some ...
6
votes
2answers
517 views

How to test whether an expression is a valid variable?

I would like to test whether an argument is a valid variable for functions like Solve and DSolve. For instance several ...
6
votes
3answers
355 views

Match a sequence of integers

I would to match a pattern p1 = 0,0,1,0,0 to the elements in listA. The elements are a boolean sequence that may look like this ...
6
votes
3answers
462 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 ...
6
votes
2answers
340 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
6
votes
1answer
106 views

StringPattern to remove markers around text

I am not able to figure the StringPattern to use to remove markers in string. This is the input. ...
6
votes
1answer
309 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
232 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 ...
6
votes
2answers
103 views

Why does this recursive pattern guard not work?

I have the following list: ...
6
votes
3answers
187 views

Removing non-word characters in certain parts of a string

I would like to ask how I can remove non-word characters from a string, but only in certain cases. I have read this article, so I know how to get the words out of a string. My text is however a bit ...
6
votes
1answer
558 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 ...
6
votes
2answers
264 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
102 views

Find subsequence A in sequence B A007908 - OEIS

I have sequence A as A007908 - OEIS and pattern B which is a sub-sequence of that sequence A. I need to find index of first occurrence of the given pattern. For example pattern ..1010... I've looked ...
6
votes
1answer
360 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 ...
6
votes
1answer
69 views

Can't access `AnyOrder` named parameters

Straight from AnyOrder documentation ...
6
votes
1answer
114 views

Error when combining Except, “*” and an additional character

Bug introduced in 5 or earlier and persists through 10.4.1 Anyone knows why ...
6
votes
1answer
299 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 ...
6
votes
1answer
128 views

Is there an elegant way of delaying evaluation of `A==B` when calling a function that uses this pattern

Suppose I wanted to define a number of functions that accepts expressions of the form: ...
6
votes
0answers
268 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)$$ ...
5
votes
4answers
1k 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 ...
5
votes
3answers
1k 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 be ...
5
votes
5answers
854 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
2answers
241 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
284 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 ...
5
votes
2answers
404 views

Opposite of Alternatives

I was looking at this question (here) and I tried to find any thing gives the opposite of Alternatives. In other words, some pattern matching function that gives ...
5
votes
5answers
182 views

Isolating cross terms

I have a set of expressions that contains terms like $\frac{1}{(x + a + b)(c+d)}$. I would like to simplify the denominator so that products of $a,b,c,d$ are dropped, but $x c$ and $x d$ are kept. ...
5
votes
3answers
185 views

Delete all strings that are prefixes of another string

I have a long list of strings and I'm trying to find a more efficient way of removing those elements that are prefixes of another: ...
5
votes
2answers
270 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
3answers
201 views

How can I improve NumberString?

I would like to test strings to see if they represent numbers. This should include number representations with precision marks and scientific notation. NumberString ...
5
votes
4answers
240 views

Finding the position of a single array in a list of arrays that contains wildcard variables

Consider two lists: superList = {{wc,wc,1,wc,wc}, {wc, wc,2, wc, wc}, {wc, wc, 3, wc, wc}, {wc, wc, 4, wc, wc}}; littleList = {a,b,3,c,25}; In ...
5
votes
2answers
237 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 as ...
5
votes
2answers
78 views

Why does this pattern fail to match?

I'd like to replace fractions with Missing[] as the denominator with Missing[]. This pattern in ...
5
votes
3answers
189 views

Better output for NonlinearModelFit “ParameterTable”

The Standard table fo parameters of NonlinearModelFit is obtained from NonlinearModelFit[{{0, 1}, {1, 2}, {3, 3}}, a + b*x, {a, b}, x] %["ParameterTable"]] I ...
5
votes
3answers
131 views

Matching Symbol or Subscript, but not Symbol arguments of Subscript

I'm trying to get a list of all the variables in a symbolic expression. Some are named with Symbols, some are named with ...
5
votes
2answers
132 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 ...
5
votes
1answer
443 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
93 views

How do I construct a Pattern with Alternatives and PatternSequence without getting Pattern::patvar messages?

Bug introduced 8.0 or earlier and persisting through 10.4.1 [CASE:3631078] In Mathematica 8 and 9, defining this rule: ...