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

learn more… | top users | synonyms (1)

1
vote
3answers
64 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 ...
1
vote
3answers
55 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
81 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
103 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
71 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
184 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
41 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
162 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
149 views

Using ReplaceAll to replace a head

Why would this work to replace the head b with newHead: ...
8
votes
1answer
84 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
38 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
33 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 ...
3
votes
2answers
52 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
79 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: ...
13
votes
1answer
197 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
2
votes
2answers
121 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
157 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
57 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 ...
9
votes
1answer
133 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
165 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 ...
7
votes
1answer
233 views

Mma10 mystery: Failure of pure function substitution with Interpretation?

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 is now breaking an innocuous pure function ...
4
votes
2answers
134 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
81 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 ...
1
vote
2answers
85 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
81 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
309 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
155 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
845 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
221 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
180 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
100 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
79 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 ...
3
votes
1answer
88 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
65 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
105 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
118 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
49 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
43 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
88 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
82 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
114 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
39 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
414 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
48 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
121 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
102 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
151 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
86 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
87 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: ...