# Tagged Questions

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

330 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 ...
132 views

### How can I know if a pattern corresponds to a string pattern?

What would be the best way to detect if a pattern corresponds to a string pattern ? An invalid string pattern p would lead to an error in ...
81 views

### Specifying string patterns in a plain text

I need to extract three data, like that: (1.05, 1.37,A) from an imported list of plain texts: "... Syntax of parallax is : 'value quality [error] bibcode' value : parallax value quality : flag ...
33 views

### Please explain this pattern/Message related issue and help with finding workaround

I attempted to clean up my code and also add messages to functions if they are called with wrong arguments. This introduced some error which I tracked down to the following: ...
51 views

### Count and NMinimize don't seem to work together

Initially I write down a 2x2 unitary (Uni) , make a tensor product 4 times (LocalUni) and apply it to an initial numerical matrix Rho, to obtain RhoLU, that depends on 12 parameters. This is my code: ...
54 views

### Good coding for integral substituions

This question is aimed at improving both my coding and my understanding of MMa's capabilities. A lot of my code is still hacks and workarounds for things I don't know how to do properly. I want to ...
153 views

### Using Mathematica's Position Function

I have a list a = {-1, 2/3, -2 - 4 I, -2 + 7 I, 2 I} and when I use the Position[a, I] it returns ...
57 views

### How to use patterns to return non-overlapping sequences of consecutive elements from an array

Let me first describe the data structure I'm looking at before describing my question. I have an array of nx3 elements with a data-generating process such that each row has the format: {previous ...
222 views

### Command like “find” in Matlab [closed]

Is there a command working exactly like Matlab's find? I know that Pick might work, but I just want to give a logic condition ...
366 views

### Highlighting pattern strings in a large text

I would like to apply Style to strings that are matched, for example, with one or more specified patterns or regular expressions, a string or a list of both, ...
147 views

### How to write this pattern in Pick function

Suppose I have a list called mask composed of 1,2,3,...n. n is different in different situation. Let me takes n=3 for demonstration ...
216 views

### Question about Rules and Pattern (The operator patt/;test)

Maybe this question is so trivial but it has confused me. I'm studying the What the @#%^&*?! do all those funny signs mean? and in the Rules and patterns under ...
68 views

### Why does this pattern fail to match?

I'd like to replace fractions with Missing[] as the denominator with Missing[]. This pattern in ...
46 views

### How to use pattern matching to apply a function to specific elements of a matrix

I have a 3x3 matrix testweightedgraph = {{{}, {p, q, pq, pq}, {}}, {{p, q, pq, pq}, {q}, {}}, {{}, {}, {}}}; whose elements are (possibly empty) lists. I would ...
97 views

### How to write or extend operators like $*\!\!*$

I'm stuck on trying to define simplification rules for NonCommutativeMultiply, much like ExpandNCM found in Documentation Center, under Applications. For example, I'd like ...
64 views

### Unexpected result when trying to match against the arguments of Times

Consider the following question: ...
103 views

### Using Select with Alternatives

I want to select the elements of a list like: ...
161 views

### Given an expression using kets, such as '|2> + 4|3>+…,' find the largest ket index in a ket?

I have some expressions containing Kets, like |1> + Sqrt[6] |3> where the kets are created using [esc]ket[esc]. How can I find the largest number inside a ...
65 views

### How to bound a recursive Derivative definition?

I need to convert derivatives of g[x,v] wrt v into derivatives wrt x using this relation: $$\frac{dg[x,v]}{dv}=\frac{1}{2}\frac{d^{2}g}{dx^{2}}+\frac{1}{2}\left(\frac{dg}{dx}\right)^{2}$$ This code ...
84 views

### Strange failure of Series and Derivative

I just spend three hours and posted two Questions trying to figure something out, and it turned out all the confusion was caused by this mysterious quirk. I want to expand g[x,v] in v at v=0, using ...
60 views

172 views

### Consecutive identical integer members of a list

Can you help me to find and count sub-sequences of consecutive, identical integer members of a long list (at least 1000 members)? By sub-sequences I mean runs like ...
76 views

### Can I map a function over a sequence of arguments without making the arguments into a list first?

I would like to set evenFunction[f_][a_, b_, c_, ...] = f[Abs[a], Abs[b], Abs[c], ...] I have come up with two ways to do this so far. Use pure functions ...
92 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 ...
206 views

### How to make a ReplaceAll Rule for adjacent items in a list?

I would like to make only one pass of a list of 2D items and replace adjacent items that have matching $x$ or $y$ coordinates with a series of linearly interpolated points between the two points. My ...
79 views

### How can I filter a permutation list depending of a pattern? [closed]

I've created a permutation list Grid[Permutations[{a, e, q, r, t, u}], Frame -> All] and only want to see rows where the 5th element is a "t". I tried to do ...
161 views

### How do Reap and Sow work in this context

I have a list of points in 3D and I need to count how many times each point is repeated. For a similar problem with a list of characters many different solutions have been proposed in this post: ...
35 views

### ReplaceAll - Position of list arguments not displayed properly [closed]

This code A[1, 2, 4, 5] /. A[a___] -> Position[{a}, 4] return {} Why this code not return 3 (a position of number 4) ? I ...
59 views

### What would be a pattern matching only numbers that are not real?

I wanted to delete all complex numbers out of a list, and I tried the following, but it didn't work. Suggestions? Im[x] != 0
57 views

### How do I let Fourier Transform know that it is a linear operator? [duplicate]

I have the following issue with FourierTransform, Fourier transform doesn't seem to know it is a linear operator. ...
29 views

### “Nonatomic expression” error when identifying certain positions in a list [duplicate]

Please can anyone help ? I have a list of sets, and I am trying to find the positions in that list of those sets which are subsets of a given set. (Here I am ignoring the distinction between lists and ...
56 views

### Braces in pattern with Cases function

Here's my problem: I've got a JSON-formatted response and want to match some variables over the response. I'm doing it using Cases: ...
28 views

### Implement unique occurrence against each other

I am trying to develop a card game, in which I have a situation like I need to present cards against each other in such a way so that they have never played with each other. So in general, I will have ...
261 views

### Is it possible to change the order that symbol definitions are checked? (DownValues vs UpValues)

Suppose I have the function f with the following definition assigned to it: ...
134 views

### Filtering lists by partial matches

Suppose I have the following data: ...
203 views

### Pattern does not match with Orderless head

Why does the following pattern matching not succeed? ...
68 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 ...
129 views

### Why does pattern test work only with pure functions?

Why is it that pattern test works only with pure functions? Cases[Range[0, 70], _?(Divisible[#, 7] &)] Cases[Range[0, 70], x_?(Divisible[x, 7])] I ...
63 views

### Transformation rules to distinguish common variable with Subscript and OverBar $x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p, \bar{x}^2_p$

I am looking for help in writing transformation rules in which I wish to distinguish between eight variables $x,x^2,\bar{x},\bar{x}^2, x_p,x_p^2, \bar{x}_p,\bar{x}^2_p$. For each of these eight ...
114 views

### Making a replacement on the numerator and denominator of a rational number

It's isn't a big deal, but when I run the following I don't get the output I expect. After discussing this in the comments it appears it has to do with being an atomic expression. ...
50 views

### Avoiding misconversion of data with ToExpression function [closed]

I've got a list of data with the following structure which can be easily converted To expresion by some manipulation such as ...
327 views

### How can I mix the behaviors of StringReplace and ReplaceAll in a single rule?

I'm trying to take a list of the form { {"65 + 3 months", 75}, {"65 + 4 months", 75.1} }, and transform the string part to a number such as 65.25 or 65.333, ...
586 views

### How to detect loops in a vector field?

Given a vector field and I'd like to detect if there are streamlines in closed patterns. For example, if the input is a vector field like this: ...
229 views

### Pattern and Orderless function

I'm a Mathematica beginner. I am exploring the use of patterns and (only for fun) am trying to implement a permutations function. My (dirty) code is: ...
53 views

### Substituting a list of values into a function [closed]

g[p_] := Mod[19^(-1 + p), p^2] This is my function, and the set of values I have is the first 999 prime numbers. I made a table listing the first 999 prime ...
205 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 ...