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

learn more… | top users | synonyms (1)

3
votes
1answer
90 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
82 views

How to localize symbols on the right-hand side of Rule when using ReplaceAll?

I am frustrated by the following: {1, 2, 3.0} /. a_Integer -> a + 1 The a on the left side of the arrow is localized and ...
0
votes
1answer
114 views

Extracting graphics primitives from a Graphics expression with ReplaceAll

If we have a graphics object, plt = Plot[Sin[x], {x, 0, Pi}] and we need to retrieve the discrete point data from it. This can be easily done by using ...
0
votes
0answers
39 views

Hashing by Equals instead of SameQ

I have some numerical code involving complex numbers representing points on a plane. Some of these are equal but not identical, ie ...
3
votes
1answer
121 views

Why even try the semantic constraints, if expression can't match the pattern structure?

This result is unexpected: Trace[MatchQ[{7}, {_?NumberQ, __}], NumberQ] (* -> {{NumberQ[7], True}} *) Why evaluate ...
2
votes
1answer
48 views

StringExpression: pattern reapeted one or more times matches pattern repeated zero times

I try to construct patterns using Riffle and Except StringExpression@Riffle[{"a", "b", "c"}, Except[{"=", ","}] ..] and expect it to match "a_b_c", but not ...
1
vote
1answer
67 views

How can I get the functionality of a command ReplaceAllList?

I'm trying to use ReplaceList to match subexpressions. I expected the command: ReplaceList[{p, {p, p}}, {x_, y_} -> x y] to output two results, equivalent to: ...
3
votes
3answers
151 views

StringCases using RegularExpression fails when there is missing data

I got some text scraps with this structure "Focal Plane: 198' Active Aid to Navigation: Yes *Latitude: 35.250 N *Longitude: -75.529 W" But some of them lack ...
34
votes
6answers
2k views

Find zero crossing in a list

I'm looking for a function that finds the index of the zero-crossing points of a list. Before I go making my own subroutine to do this, I was wondering if anyone knows of any built-in Mathematica ...
4
votes
1answer
86 views

Pattern matching and HoldForm

Create rules based on data. I have a system that picks out on date,based on some rules. I then want to generate a new rule based on this date and apply the rule to a new dataset. ...
2
votes
1answer
87 views

What is the proper way to the pattern form Repeated in my code?

I'm matching a pattern with Repeated (..) How can I use the repeat count in the replacement rule? Example: ...
0
votes
1answer
71 views

Sum similar elements [closed]

I am interested in summing elements in a list or sublist that are similar. I want the sublists of my second elements to be added cumulatively only if their first elements are the same. This is my ...
20
votes
4answers
945 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 ...
0
votes
1answer
62 views

Pattern matching with PDF, Distributed and multivariate distributions

I have defined a function to compute the Shannon entropy of a probability distribution: ent[p_] := Expectation[-Log[PDF[p, x]], x \[Distributed] p] This works ...
4
votes
2answers
196 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
3
votes
3answers
98 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
13
votes
3answers
366 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 ...
2
votes
1answer
56 views

Ordering for Pattern matching in Subscripts

I have some transformation rules acting on subscripted variables. When I match the a product the order matters. ...
0
votes
0answers
42 views

Can Mathematica find the discriminant formula? [duplicate]

This question is not really practical, but rather a curiosity question. Can Mathematica solve a quadratic equation without its discriminant? I mean, whenever it see a quadratic equation it tries to ...
1
vote
1answer
84 views

Matching variables with Subscripts yielding True and False

I am trying match variables with subscripts just returning true or false depending if pattern is matched. My try is to use Cases. However the following does not ...
3
votes
2answers
174 views

Finding similar expressions in list

I have a function that returns to me lists of expressions, like: ...
2
votes
1answer
49 views

Matching a list of sequences with common form

I am trying to find a pattern that matches a list of sequences that share a common form. The sequence form is: 1 followed by zero or more nonzero integers, followed by 0. An example list is: ...
1
vote
0answers
55 views

Function behavior based on Head

I'm figuring how to implement a function behavior based on the Head of its argument. I mean ...
2
votes
2answers
167 views

Removing trailing zeros from a list

If I have a list like list1={2334.,342.,0.,0.,2322.,0.,0.,0.,0.,,,,,,}; it's easy to remove to trailing blanks with ...
2
votes
4answers
494 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
108 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 ...
2
votes
1answer
73 views

Applying rules to rewrite expressions that contain patterns

I'm trying to write a rule that can rewrite a pattern that exists in another expression. For some reason, this is proving to be more challenging than I expected ... most likely because I'm not totally ...
4
votes
1answer
134 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 ...
0
votes
2answers
109 views

Replace matrix with conditions

I want to replace the values of 1. in slot under the condition that the year and month in basevalue and ...
1
vote
1answer
62 views

blank null sequence acts weirdly [closed]

Could someone please explain why the ___ pattern matches the first but not the second case? ...
2
votes
1answer
115 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
185 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, ...
12
votes
10answers
658 views

Counting sublists containing 0

Given a list: data = {{0, 2}, {0, 0}, {1, 0}, {1, 2}, {2, 3}}. I would like to count the number of sublists which contain zero. ...
0
votes
0answers
19 views

Manipulating named patterns inside replacement rules [duplicate]

When I evaluate the following expression, {a -> "value1", b -> "value22"} /. {Rule[x_, y_] -> Rule[x, y]} I get ...
9
votes
1answer
214 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}}; ...
1
vote
3answers
81 views
7
votes
3answers
153 views

Checking through an expression

I am writing a code that manipulates expressions that contains functions with an arbitrary number of arguments. An example expression is ...
3
votes
1answer
97 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
3
votes
1answer
75 views

Elegant way to match a list with only certain types of elements

I'm working on learning Mathematica programming for personal edification and am trying to find a more elegant way to do a matching expression which will match only a list containing elements that are ...
3
votes
2answers
147 views

Extracting argument sequences from function expressions

Why is it that fun[x, y] /. fun[z__] :> z leads to Sequence[x,y] but ...
3
votes
2answers
116 views

truncating terms in symbolic expression basing on their values

I need to simplify big symbolic solution of equations system by truncating terms that are orders of magnitude less then others in subexpressions like a + b. The solution is big fraction with sum of ...
0
votes
2answers
125 views

Pattern matching

This is a very simple question I hope you can help me with. I am experimenting with pattern matching. I want to write a pattern for a difference between two symbols, ...
4
votes
1answer
68 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. ...
6
votes
2answers
124 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
68 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 ...
8
votes
2answers
179 views

Orderless pattern matching of rule lists

I am looking for a compact way to specify a pattern to match a rule list with MatchQ that allows for enumerating the rules in the rule list in an arbitrary order. ...
12
votes
4answers
338 views

How to enumerate all possible binary associations?

Suppose I have a list of symbols like: {a,b,c,d} I would like to enumerate all possible binary associations (combining symbols and/or sublists pairwise): ...
16
votes
1answer
235 views

Bug in RepeatedNull?

RepeatedNull appears to be buggy (versions 8.0, and 9.0). Define f as follows: ...
3
votes
2answers
142 views

String pattern matching & replacement

I would like to find all occurrences of a full stop "." followed by a capital letter in a string and have these replaced with a "; ". How do I do this in Mathematica? e.g. ...
3
votes
4answers
185 views

Pattern matching in nested lists

I'm sure this has been asked before, but I can't find the right combination of search keywords to find it. Apologies. I want to do a ReplaceAll on a list of lists, ...