Tagged Questions

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

learn more… | top users | synonyms (1)

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 ...
1
vote
0answers
42 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
186 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 ...
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 ...
2
votes
1answer
29 views

Linear function on strings using UpValues

I have two functions Sup and Sdown that take a string of letters with allowed characters ...
6
votes
3answers
120 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
44 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 ...
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 ...
4
votes
6answers
226 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 ...
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
44 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
132 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 ...
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 ...
9
votes
5answers
366 views

Repeating Digits

I have written ...
4
votes
4answers
141 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
4answers
126 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 ...
0
votes
2answers
69 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: ...
4
votes
3answers
94 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 ...
5
votes
4answers
233 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
119 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
124 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 ...
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 ...
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 ...
7
votes
5answers
351 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
386 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
46 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: ...
0
votes
0answers
60 views

Making a list of terms ordered by product order

I have some expressions f,g,h that are elements of a symmetric tensor algebra. The expressions are generated by a basis ...
6
votes
5answers
300 views

Find patterns in a list

again, I have a list like this: ...
10
votes
4answers
345 views

What is the correct way to use specific rule for specific pattern match when parsing an expression?

update: Thanks for the answers below. But I noticed they do not handle the following special cases: lis=1 and lis=z , ...
6
votes
3answers
171 views

Isolating string characters

I have the following string: ...
1
vote
3answers
74 views

Store matched variables

Edit: I am matching and replacing a particular expression. At the same time... I want to store matched variables for later use. I got it to work with messed up code involving ...
0
votes
3answers
67 views

Unexpected behavior of rule-replacing with string concatenation [closed]

Here's the code. Clear[x,y,z]; {x,y,z} /. x_ :> ("^_^" <> ToString[x]) I get the result ^_^{x, y, z}, which is not ...
2
votes
1answer
103 views

Extracting rational coefficients from an irrational sum [closed]

I have a function that returns results that always can be represented as $p+q\,\pi^2+r\ln2+s\ln^22+t\sqrt2$ where $p,q,r,s,t\in\mathbb Q$. But the actual Mathematica expression returned not always in ...
4
votes
1answer
119 views

What would be a pattern for a general row of an identity matrix?

I know this is a novice question but I would like a pattern for a list containing zero or more zeros followed by exactly one 1, followed by zero or more zeros. Something like: {0___,1,0___} But ...
2
votes
3answers
94 views

Finding the maximum order derivative of an expression

I have an expression that is the the sum and product of undefined derivatives and their derivatives. e.g. fg+ Dt[f,t]g +Dt[g,{t,2}]f+ What I would like to find ...
6
votes
3answers
196 views

Problem with WordBoundary in string patterns

I am trying to extract from a string all words that end with "-man", but the problem is that WordBoundary does not seem to work as it returns whole string ignoring ...
1
vote
2answers
55 views

How do I format expressions so that replacement rules correctly apply to exponents? [duplicate]

I have a series of rules that I would like to apply to a series of expressions with variables, but cannot get Mathematica to correctly recognize that these rules should apply when my variables have ...
1
vote
4answers
171 views

Need an efficient way to do this list manipulation

Lets say i have two lists with me as given below: l1 = {4, 6, 8, 9, 10, 12}; l2 = {2, 3, 4, 5, 6, 7}; Now i have a function S2P(details irrelevant) which takes ...
3
votes
2answers
213 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
8
votes
1answer
101 views

How to get a list of all expressions in a notebook matching a certain pattern?

Suppose I have a Mathematica notebook saved to a certain location on disk. Given a pattern I need to get the list of all expressions matching this pattern that appear in input, output or print cells ...
0
votes
0answers
39 views

How to correctly use PatternTest (?) [duplicate]

This works fine: {3, -5, 2, 7, -6, 3} /. x_ /; x < 0 :> 0 (*{3, 0, 2, 7, 0, 3}*) and this works fine: ...
0
votes
1answer
38 views

Replace all instances of Integrate by NIntegrate and add a Method option

I have an analytic expression which involves several integrals Integrate[f[x], {x, lb, ub}] where the functions f and bounds ...
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 ...