Tagged Questions

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

learn more… | top users | synonyms (1)

2
votes
5answers
113 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
101 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
56 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
41 views

Understanding the order of applying Rule to expression [on hold]

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
109 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
114 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
60 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
65 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
361 views

Repeating Digits

I have written ...
4
votes
4answers
134 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
116 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
66 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
91 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
224 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
97 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
118 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
66 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
87 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
41 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
26 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
343 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
385 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
43 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
296 views

Find patterns in a list

again, I have a list like this: ...
10
votes
4answers
322 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 , ...
1
vote
3answers
72 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
65 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
99 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
115 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
82 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
192 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
52 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
167 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
202 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
8
votes
1answer
97 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
37 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
230 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 ...
2
votes
1answer
81 views

How to efficiently process pasted content from PDF

I had selected a matrix in a PDF file, and paste it onto my MMA notebook. Below is the exact copy-pasted content: ...
14
votes
1answer
249 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
13
votes
1answer
90 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 ...
2
votes
2answers
132 views
0
votes
0answers
27 views

Problem with replacing a head with another head using pattern [duplicate]

Why would this work Times[a, b, c, d] /. Times :> List (* {a, b, c, d} *) but this doesn't work (the Times still remains ...
3
votes
2answers
160 views

Why wouldn't ReplaceAll (/.) work with this list of number? [closed]

The answer to this might be simple, but why would ReplaceAll return such a weird result when replacing a number in a list of numbers: ...
3
votes
1answer
58 views

Need help in building a pattern [duplicate]

Suppose I have the following list: list = {w, r, {f, first, t, y}, {a, y}, last, {k, r, first, e}, m, e, last, r, t} I am expecting this ...
10
votes
1answer
147 views

Why doesn't repeated PatternSequence work with explicit pattern names?

Why would this work: Clear[f] f[a : PatternSequence[b_, c_]] := {a}; f[1, 2] (* {1, 2} *) and this also works: ...
6
votes
3answers
174 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 ...
8
votes
1answer
271 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 ...