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

learn more… | top users | synonyms (1)

2
votes
3answers
283 views

Select sublists with second element nearest to zero

I've got some big lists from which i need some data (one of the big lists). To be specific, I need to divide this biglists in sublists of the same length (10000), and form a new list with the mean of ...
4
votes
2answers
99 views

Pattern Matching with Indexed Object

If I have a list of rules associated with indexed variables like: solution = {a[1] -> 1, a[2] -> 10, a[3] -> 100} I would like to be able to extract all ...
5
votes
3answers
273 views

Highlighting text with StringReplacePart but also using Style, Subscript

For a Natural Language Processing analysis task it's desirable to highlight a text string programmatically according to XML tags that reference specific words or phrases. The XML tags specify the ...
8
votes
3answers
186 views

Select and Blank

I try to filter sublists of a list which match a pattern. test = {{"String1", "a"}, {"String2", "b"}, {"String3", "a"}, {"String4", "a"}}; The result should ...
4
votes
7answers
342 views

Given a symbolic expression how to find if starts with a minus or not?

I am using a Mathematica function which returns some error term in symbolic form. I needed a way to determine if this term starts with a minus sign or not. There will be only one term This is to ...
20
votes
1answer
558 views

replacement rules from a pattern and a matching expression

(This seems to be a very basic necessity in a language having pattern-matching capabilities such as Mathematica, yet I struggled for many hours to find a common solution to this problem.) Suppose ...
2
votes
1answer
149 views

Add Pattern to List Removing Duplicates

Lets say I have a list of patterns ...
7
votes
2answers
196 views

Using MatchQ (or other means) to parse an expression using sums of COS or SIN correctly

I am writing a function that will take an expression as input. I want to make sure the expression is valid first before processing it. Looking for a correct to use ...
2
votes
1answer
123 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 ...
10
votes
3answers
160 views

Pattern matching on Orderless functions inside Hold

MatchQ[Hold[1 + 2], Hold[2 + 1]] returns False. But in the presence of a Blank[] in the ...
4
votes
4answers
333 views

Listing matrices up to symmetry

I am interested in the equivalence relation on N x N binary matrices, in which two matrices are equivalent if one can be obtained by rotating/reflecting the other. I would like to obtain a list ...
1
vote
2answers
170 views

Collect pattern (Log's)

Is it possible to apply "Collect" command (to put Log's in particular combination) to factor out (pattern) a particular form from the full expression. For example, consider the following expression: ...
2
votes
0answers
31 views

Detecting a more general pattern [duplicate]

I need a function that, given 2 patterns, determines if the first pattern is more general than the second (i.e. that any expression successfully matched by the second, will be successfully matched by ...
2
votes
3answers
211 views

Isolating a prefactor in an analytical expression

I have a large mathematica expression that looks roughly like f = A1 Exp[a1 x] + A2 Exp[a2 x] + b1/x + b2/x^2 + c0 + c1 x + c2 x^2 where all the ...
9
votes
1answer
101 views

Apply N only outside a certain function

1 + f[1] // N gives 1. + f[1.] I don't want the argument of f evaluated by N; I ...
19
votes
7answers
951 views

Elegant and fast way to join all string sequences

I want to do what title says: {"a","b",1,2,3,4,"a",2,2,2,"edg","?"} => {"ab",1,2,3,4,"a",2,2,2,"edg?"} in the most elegant way. For example one replacement rule for ...
1
vote
1answer
89 views

How to define Similarity Rules?

I have a question about Mathematica's SimilarityRules defining (for e.g. SequenceAlignment function). So help centre clearly ...
10
votes
4answers
219 views

Is it possible to separately call the default value of a variable which isn't at the end of the sequence?

After reading this post I recalled an old question I once met. We know that we can set default values for variables of a function: ...
5
votes
2answers
160 views

Extract substring by repeated StringPattern

This code failed, but may exemplify my intention. StringCases["aA1bbbbbbbbbbaA2baA3b...aA4b",Repeated[Shortest["a"~~xx__~~"b"],{2,100}]] I want to get the ...
4
votes
2answers
160 views

How can I replace the general element of a geometric series with the sum of the series?

Consider the list mylist = {b(a/bc t)^r, a/b(b t)^r, a (c/d t)^r, ...} where a, b, c, t, etc., are real numbers between 0 and 1. I want to sum each term in the ...
14
votes
4answers
465 views

Returning an unevaluated expression with values substituted in

Let's say I have a function formula[x_List] := (x[[1]] - x[[2]]) + (x[[3]] - x[[4]]); and I want to pass it a variable and get back the unevaluated formula with ...
3
votes
2answers
102 views

More general pattern fails to match everything the more specific pattern does.

When I input this: MatchQ[x*SF[a,b] + y*SF[c,d], Plus[Times[x,SF[a,b]],Times[y,SF[c,d]]]] I get "True" as expected. But If I change it to: ...
3
votes
3answers
162 views

Write a function using pattern test to test whether the input is polynomial and print error message if not

I am doing this which is supposed to be an easy problem and which I think my code should be correct but for some reasons it is not working. May anyone help correcting my mistakes? How should I write ...
4
votes
2answers
206 views

Adapting “Find and Replace” functionality to work with Patterns

I was thinking if there is something like Find and Replace (Crtl+F) functionality adapted to work with patterns. Probably not, but it seems possible to create this with some effort and skills in ...
6
votes
1answer
196 views

Pattern definition for replacing plus and subtract

Suppose I have two functions $h_1(x)$ and $h_2(x)$, for which we know their sum and their difference: $$ h_1(x)+h_2(x) = d(x) \qquad h_1(x)-h_2(x) = i f(x) $$ I have a complicated expression, in ...
2
votes
1answer
59 views

Findings powers within strings - Cases function

Any idea which Cases statement will work for both A^2 and CA^4X^36 to find the powers? If I set sym to these two expressions, one requires a curly brackets around the first argument and the other ...
3
votes
1answer
106 views

Why does MemberQ need {} around the expression? [closed]

Suppose I was trying to find out if the sequence 34 appeared in the digits of 344444. I tried to do this with MemberQ and get: ...
6
votes
1answer
189 views

Match sequence of zero or more characters except a given character

This looks like it should be simple: I want to match an "i" followed by an "o" with an arbitrary (possibly zero-length) sequence of characters in between, unless that sequence also contains an "i". ...
1
vote
1answer
123 views

Searching a string that may contain Null

I have an array of triplets that can be approximated by the following code: ...
8
votes
1answer
171 views

How to tell when a job is too big for the pattern matcher?

Pattern matching is great but it has its limitations. Consider sorting a list of numbers from smallest to largest: ...
-1
votes
2answers
170 views

RotateRight ith row of a matrix by i - 1

first time stack exchange poster here! So I was working on a problem, the purpose of which was to write a function in the procedural style to rotate right the ith row of a matrix by i - 1 spaces. Ok, ...
3
votes
2answers
556 views

Classifying poker hands by pattern matching

I am working on this question posed in a Mathematica course revised in 1998. Here are the instructions: Poker Hands You are to define and show working examples of a function "...
15
votes
3answers
479 views

Position of a pattern-matched part of an expression

What you have A pattern, with certain part of it labeled As an example — Label: wally ...
4
votes
1answer
309 views

Extracting text from nested HTML lists <ul> <li>

We wish to analyze the tree structure of HTML documents that consist of nested lists, ie, using the HTML tags <ul> and ...
3
votes
2answers
101 views

How to apply multiple/complicated requirements for a pattern in a function input [duplicate]

In this question. I suggested that to test whether the input of a function was a matrix of numbers one could create the following test: ...
27
votes
0answers
406 views

Why is StringExpression faster than RegularExpression?

Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar: ...
6
votes
2answers
189 views

Variable scoping problem when mapping over delayed replacement

This is something that got me curious while I was playing around with Mathematica. Consider the following (contrived) example: ...
2
votes
1answer
108 views

Flat function with repeated sequence

There are functions like Join[ ] that accept a variable number of arguments. Join is a flat function, as the order of arguments ...
2
votes
2answers
306 views

Write a function that returns the logarithmic derivative

How can we write a function that if we input an expression f, it returns the log derivative $\frac{1}{f} \frac{df}{dx}$. We have to use a conditional or pattern test so that the function accepts any ...
3
votes
0answers
138 views

I am missing a function like HoldPattern

Temporary message: I am planning to edit this question, making the examples less confusing and by moving the things that are confusing to a separate question For any confusion about expressions that ...
14
votes
3answers
163 views

What does `: Hold[$IterationLimit]` mean?

In this answer, what does the : Hold[$IterationLimit] part of the following construct do? ...
10
votes
1answer
460 views

Mathematica equivalent of regex '\w{2,3}'

It beats me to write the Mathematica pattern equivalent to the regex: \w{2,3} Any ideas?
5
votes
4answers
178 views

Extracting sequences from a list?

Suppose I have a list of arbitrary length like {1, 2, 3, "Open", 3, 2, "Close", 9, 3, 4, "Open", 1, 0, "Close", 3, 5} and I am trying to extract the sequences ...
3
votes
2answers
103 views

Select gives different answers in versions 8 and 9

Consider the code below: s = Solve[(3 - Cos[4*x])*(Sin[x] - Cos[x]) == 2, x, InverseFunctions -> True]; Select[s[[All, 1, 2]], Element[#, Reals] &] In MMA ...
34
votes
9answers
1k views

Injecting a sequence of expressions into a held expression

Consider the following toy example: Hold[{1, 2, x}] /. x -> Sequence[3, 4] It will give Hold[{1, 2, Sequence[3, 4]}] ...
4
votes
2answers
213 views

Unexpected behavior of rule matching a pattern

I am a beginner exploring the world of Mathematica. I expected the following code T[6, 5, 4, 1, 2, 3] /. {T[a___, 1, b___] -> Length[List[b]]} should return ...
11
votes
1answer
223 views

I can't use PatternSequence with Cases

I have a list of integers: s = {4, 5, 5, 6, 2, 5, 3, 2, 4, 2, 1, 1, 4, 3, 4, 3, 1, 6, 3, 4}; And if I do this, both occurrences of 4 after 3 are replaced. ...
16
votes
5answers
2k views

The difference between 0. and 0

I have a function for which 0 is a special case: f[A___, 0, B___] := 0 But since I am doing numerics, sometimes in the course ...
9
votes
1answer
214 views

Why is ReplaceAll behaving like this?

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing. For Sqrt[f[x, y]] /. f[___] -> u Mathematica returns ...
4
votes
5answers
732 views

Removing zero values from ordered pairs

I have some processed data, which are basically a collection of ordered pairs in the form: {{x1, y1}, {x2, y2}, ..., {xn, yn}} Ordered pairs which have either a ...