Tagged Questions

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

learn more… | top users | synonyms (1)

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
256 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
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 ...
2
votes
2answers
135 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
162 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
148 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
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 ...
4
votes
2answers
135 views

Get part of lousy data

I want to get part of large data. some part of this data is as follows: ...
-1
votes
3answers
87 views

How do i import a list of files, choosing the name

I have this list of files, i'm using FileNames[{"*.txt"}, NotebookDirectory[]] to import it, but i just want the ones without inelastic on the name. How can i select just by the name? Thanks ...
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
97 views

Why does pattern test not work in this case to restrict the function parameters?

I was looking at this Q&A about using pattern test (pattern_?test) vs pattern condition (pattern_/;cond) and came across ...
13
votes
1answer
337 views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
5
votes
3answers
178 views

Converting expressions to “edges” for use in TreePlot, Graph

This question is similar to this: Nested list to graph. How to "flatten" an arbitrary expression, eg expr = a[b[c, d[e][f], g], h] to a list of key-value pairs ...
8
votes
8answers
863 views

How can a power of 0 or 1 be replaced?

Consider this given example for ReplaceAll 1 + x^2 + x^4 /. x^p_ -> f[p] This returns ...
5
votes
1answer
225 views

More efficient Omnibus number determination?

Amusing myself with concepts from "Omnibus Sequences, Coupon Collection, and Missing Word Counts", in particular the sequences. To paraphrase the concept, a string is k-Omni over some alphabet a if ...
4
votes
2answers
193 views

Alternatives ordering affects pattern matching in Cases?

Given expr = f[x, g[y], z] In the following query, the pattern h_[__, c_] appears as last slot in ...
2
votes
2answers
107 views

how to take some parts of String with certain pattern

I have the large number of files in which the file's names follow certain pattern. for example here are names of two files ...
2
votes
2answers
83 views

Find names similar to a given name in a large data set

I'm trying to find a 'similar' name to every item in a list ...
4
votes
1answer
91 views

How can I extract all the non-number symbols from an expression [duplicate]

If I have an expression, let us say something like this: Cos[3 x] + π/cofe Exp[y^(c z)] how can I get all non-number symbols in the expression which are: ...
1
vote
2answers
85 views

Searching for phrases in a block of text

One could call this a pattern finding question with dictionary (or WolframAlpha) lookup, but let me give you some background first. I have a couple of friends who are trying to improve their English ...
8
votes
1answer
114 views

CSS Selectors for Symbolic XML

Symbolic XML is a convenient way of managing XML in Mathematica, at least for casual use or small tasks. Here and elsewhere you can find examples of XML handling in Mathematica, including XSLT/XPath ...
1
vote
2answers
132 views

dropping a row with at least one value larger than a particular value

So for instance I have M X N table. I want to drop any row which has an element larger than, say, one in the second or the third column. I guess I have to do some sort of pattern matching. But not ...
1
vote
2answers
61 views

Pattern matching to head with HoldFirst

I want to define a function that only operates on expressions with a head from a given list, such as f[expr:(_And|_Or)] := Print["LHS is ", expr[[1]]]; This of ...
1
vote
0answers
44 views

Placement of pattern matching rules in functions [duplicate]

Is there any difference at all between f[x_Integer /; x >= 0] := x + 1; f[x_Integer] := x - 1; f[x_] := Null; and ...
3
votes
1answer
114 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
88 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
122 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
41 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 ...
4
votes
3answers
428 views

Function with zero or one arguments

Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for ...
2
votes
1answer
55 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 ...
3
votes
1answer
127 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 ...
3
votes
2answers
118 views

What is the correct way to pattern match nested lists?

I have the following function: f[lst_List /; MatchQ[lst, SOME PATTERN HERE]] := ({#[[1]] + #[[2]], #[[1]] - #[[2]]} &) /@ lst; I want to match lists that ...
3
votes
3answers
159 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 ...
4
votes
1answer
95 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
88 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
1k 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
76 views

How can I represent this automorphism of Q(i,a)?

Let $a$ represent some real irrational number. I am trying to perform computations with an automorphism $f:\mathbb{Q}(i,a)\to\mathbb{Q}(i,a)$ which fixes $\mathbb{Q}(i)$ pointwise and maps $a$ to $i ...
0
votes
1answer
67 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 ...
3
votes
3answers
101 views

Name scope confusion

Sorry for the too ambiguous title. First consider a following code snippet. ...
4
votes
5answers
444 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 ...
1
vote
1answer
69 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: ...
2
votes
1answer
68 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. ...
3
votes
1answer
193 views

Covariant derivative for symbolic tensors

I want to define a "prefix" (D_i) covariant derivative operator CD[] for symbolic tensors in form of a function, i.e. for ...
0
votes
0answers
43 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
85 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
186 views

Finding similar expressions in list

I have a function that returns to me lists of expressions, like: ...