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

learn more… | top users | synonyms (1)

1
vote
2answers
118 views

Mathematica won't plot the a function that is solved from NDSolve

I have used NDSolve to solve for a function (an angle of a triple pendulum), and now I wish to plot the derivative of that function (the angular velocity). ...
31
votes
1answer
508 views

Why is StringExpression faster than RegularExpression?

Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar: ...
1
vote
1answer
103 views

Finding the index of (x,y) pairs in a list

I have a large list of $(x,y)$ pairs, and I want to find the index of a particular pair in order to extract some data from another array. By way of an example, ...
6
votes
3answers
121 views

Pattern matching: Times[a_] vs Times[a__]

I don't understand why MatchQ[ HoldComplete[Times[3, 2, 2]], HoldComplete[Times[a_]] ] is returning True. I thought that ...
1
vote
0answers
53 views

Crosscorrelation for nonperiodic signals

I want to detect patterns (of an absorption spectrum which is nonperiodic) with Mathematica automaticly. I have found CorrelationFuncion but as far as I understand, ...
3
votes
3answers
187 views

String pattern defined by number of characters

I want to extract non-overlapping substrings from strings with only four possible characters a,b,c,d. These strings will always have equally many "a" and "d" characters. The substrings x I want satisy ...
3
votes
1answer
37 views

How to define patterns to filter lists of replacement rules? [closed]

I have a list of replacement rules like list˘of˘replacement˘rules = {a -> 1, b -> 2, c -> 3} and I want to filter all the elements where ‘b’ is replaced ...
8
votes
1answer
205 views

Transformation rule on a list of non-negative integers

I am trying to create a transformation rule that takes a list of non-negative integer values of any length, finds a non-zero entry in the list, adds 1 to all preceding numbers, subtracts 1 from the ...
15
votes
3answers
460 views

Grabbing a number of characters around a string pattern

If I have some string data: text = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 400] "When in the Course of human events, it becomes ...
28
votes
4answers
3k views

Looking for “Longest Common Substring” solution

I'm looking for robust code to solve the "Longest Common Substring" problem: Find the longest string (or strings) that is a substring (or are substrings) of two or more strings. I can just code it ...
6
votes
3answers
171 views
2
votes
1answer
35 views

Deleting empty cells and selecting cells matching a pattern

Is there any way for me to automatically select all cells that are completely empty? You know, those cells that you create but end up cutting what's in them and paste it somewhere else, so after a ...
14
votes
5answers
663 views

Getting number of binary digits combinations without “forbidden” patterns

I need to get the number of all combinations of binary digits in an 8-digit binary number, but not including those that follow some "forbidden" patterns like these: ...
2
votes
1answer
31 views

Linear function on strings using UpValues

I have two functions Sup and Sdown that take a string of letters with allowed characters ...
4
votes
5answers
446 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 ...
14
votes
1answer
267 views

Why is ReplaceAll behaving like this?

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing. For Sqrt[f[x, y]] /. f[___] -> u Mathematica returns ...
10
votes
4answers
406 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 ...
1
vote
0answers
45 views

Symbolic and numeric calculations (and plots) simultanuosly

I use Mathematica to do a bunch of symbolic calculations (integrals, ...). This is good because I found that sometimes, if I plug in numeric values, Mathematica takes much longer. However, sometimes ...
4
votes
6answers
227 views

Command to take the first elements of a list that match a pattern

I want to find the number of elements in a list in a row that match a certain pattern. So for this list {1,1,1,1,0,0,0,1,1,1} It should return "4" because there ...
0
votes
2answers
66 views

How do I tell pattern searcher the order in which to search for patterns given in general form?

I have a really long sequence of Symbols inside a function, say nonCommute[a,b,c,e,e,f,g,b,i,j], and I am implementing a ...
3
votes
2answers
69 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 ...
1
vote
1answer
103 views

Making a list of rules form the list of elements

Starting from the list: {f[q1, q2], f[q3, q4], g[q1], g[q2, q3, q4], d[k1 - q1], d[k2 - q2 - q3 -q4]} I would like to get the result: ...
3
votes
1answer
62 views

FreeQ and arguments of Hypergeometric2F1

I was trying to use FreeQ to test for the presence of Hypergeometric2F1 functions in my expressions. I encountered the following ...
1
vote
0answers
45 views

Understanding the order of applying Rule to expression [closed]

I am a little confused about: In what order is a Rule tried to apply to an expression? In a depth-first order or otherwise? For example, ...
2
votes
2answers
111 views

Matching a case when one element in a pair is Null

I've got a dataset of laitutdes and longitudes. Unfortunately, some of the dataset was from when a time when lat/longs weren't constantly recorded. So I need to remove the invalid data before working ...
5
votes
2answers
134 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 ...
2
votes
3answers
115 views

Cases do not return all expected result

I have the following line code: Cases[g[h, r, t, b, m], x_[__, y_, __] -> y, {0, -1}] I am expecting the results to be ...
3
votes
1answer
75 views

Recognizing Matching Colors in a Game Grid from a Camera Image

I am relatively new to Mathematica, having taken my first serious dive into it this week. As a fun project to teach myself some of the basics, I've come up with a goal of optimally solving a game ...
3
votes
4answers
68 views

Pattern for finding matches that are different [duplicate]

I am trying to solve the following problem. I have tuples of size 3 {{a, b, c}, {a, b, c}, {a, b, c}} I would like to find cases where all the elements in a ...
4
votes
4answers
127 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 ...
9
votes
5answers
367 views
4
votes
4answers
143 views

Sublist pattern matching

Is there any way to simplify the following pattern: {p___, {a___, x_, x_, b___}, q___} :> {p, {a, x, b}, q} Ie. where I remove duplicated elements within ...
4
votes
3answers
95 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 ...
0
votes
2answers
71 views

text analysis: split document in seperated lines

For a text analysis I want to split a large document in seperated lines. Then I want to calculated for example the average number of words. A example of the document is the following: ...
5
votes
4answers
236 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 ...
5
votes
1answer
122 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: ...
3
votes
2answers
125 views

Convert matrix rows into variables

Can anyone show me how to set all matrix rows to variables. For example, I have matrix w={{1,2,3},{3,2,1},{0,0,0}} and I need to set each matrix row to be separate ...
6
votes
1answer
72 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 ...
8
votes
1answer
272 views

Mma10 mystery: Failure of pure function substitution with Interpretation?

Bug introduced in 10.0.0 and fixed in 10.0.1 I had code to display with subscripts from Displaying index as subscript on output: e.g. C[i] -> C_i with Notation[...] or Interpretation[..]? which ...
13
votes
1answer
92 views

Report: IgnoringInactive: a bug?

Bug introduced in 10.0.0 and fixed in 10.0.1 Just to report. IgnoringInactiveshould work with Cases. In agreement with the ...
1
vote
1answer
89 views

Function returned an unexpected list

I was trying out a question in Wellin's Programming with Mathematica that says the following. Question: Given a set of points in the plane (or 3-space), find the maximum distance between any pair of ...
11
votes
4answers
500 views

Calling Table with custom iterator

I often find myself in situations where I, for example, need to build a table for some expression, but want to set the number of points rather then the step size, so the code ends up looking like this ...
1
vote
0answers
42 views

How to pattern match an association? [duplicate]

I have the following: Clear[a, b]; a = {{1, 2, 3}, {4, 2, 5}, {6, 7, 8}}; b = GroupBy[a, #[[2]] &]; b whose output is: ...
0
votes
2answers
77 views

Pick bug: level 0 matching

A previous discussion of this issue (Is there a bug in Pick?) accepted that matching at level 0 is not a bug in Pick. Was that correct? I am not yet persuaded. ...
2
votes
0answers
30 views

How to syntax check that all instance of symbol must be contained in Subscript[]

I have a function f[expr] that is only allowed to take an expression expr that is a polynomial in ...
10
votes
4answers
236 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 ...
7
votes
5answers
354 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
6
votes
5answers
387 views

The correct pattern for Cases

I have a problem that, admittedly, I have already solved using Select instead, but it is irking me that I cannot seem to construct the right pattern to solve it ...
0
votes
0answers
47 views

On the order of rules specification [duplicate]

I wanted to understand how Mathematica parses function definitions in rules-based programming, so I started with the simple code: ...
6
votes
5answers
302 views

Find patterns in a list

again, I have a list like this: ...