# Tagged Questions

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

102 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 ...
114 views

### How to define Similarity Rules?

I have a question about Mathematica's SimilarityRules defining (for e.g. SequenceAlignment function). So help centre clearly ...
247 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: ...
216 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 ...
219 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 ...
1k 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 ...
413 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 ...
115 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: ...
421 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 ...
250 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 ...
303 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 ...
73 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 ...
167 views

### Searching a string that may contain Null

I have an array of triplets that can be approximated by the following code: ...
251 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". ...
188 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: ...
895 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 "...
183 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, ...
255 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: ...
461 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 ...
113 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: ...
604 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: ...
124 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 ...
240 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: ...
499 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 ...
165 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 ...
194 views

882 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?
247 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 ...
105 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 ...
338 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. ...
752 views

### How to collect terms with positive powers in polynomial

I am trying to collect all terms with non-negative powers of $x$ in polynomials like $\frac{1}{x^2}\left(a x^2+x^{\pi }+x+z\right)^2$ First expand the polynomial ...
307 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 ...
291 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 ...
2k 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 ...
183 views

### Problem with | operator in pattern matching

My first question here as I only got into Mathematica programming recently. Basically I have a large symbolic algebraic expression where I know certain variables with head pε appear with degree 2 in ...
732 views

### Adjacency List representation of a graph

I am looking to draw a graph knowing a simple adjacency representation of the graph like: A = {{1, 3}, {2, 3}, {3, 4, 5}, {4, 5}, {5}} Where ...
48 views

### transformation patterns don't work with constants

I want to do the reverse of PowerExpand and convert 2*Log[x+y] to Log[(x+y)^2]. The rule: t_ Log[x_] -> Log[x^t] works for ...
301 views

### Why does this pattern for combining two lists require triple underscores (___) to work?

I understand that __ is a list of one or more elements, and that ___ is 0 or more elements, but when I try this rule with 2 ...
149 views

### Named patterns as function argument descriptors

I have a function timeToMinutes that takes a 24-hour time as a string argument (in digital clock format, e.g., "15:47") and converts it to the number of minutes ...
364 views

### Keep Ordering of Bra Ket When Printing

I would like to keep the ordering of the bra ket-print but right now this doesn't happen. Any Idea? I tried playing with HoldForm but then i could evaluate the expression inside properly ...
240 views

### Convert logical relational expression to / from disjunctive and conjunctive forms?

I would like to convert logical relational expressions in disjunctive form, e.g., $$(x \lt -1) \lor (-1 \lt x \lt +1) \lor (x \gt +1)$$ into conjunctive form, e.g., $$(x \ne -1) \land (x \ne +1)$$ ...
338 views

### Question about using a pattern in ReplacePart

I noticed that "ReplacePart" can use pattern. For example: ReplacePart[{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {i_, i_} -> x] {{x, 0, 0}, {0, x, 0}, {0, 0, x}} So ...
540 views

### Soft-Match String Comparison

I have a list of names in which I would like to check for duplicates. However, within the list, the duplicates may not show up as exact duplicates of each other - for instance, ...
164 views

### Extending D for four-derivatives

I'm currently trying to consistently define rules for extending D[] to four-derivatives. As 'backend' I'm using the package TRACER (http://library.wolfram.com/infocenter/MathSource/2987/), which can ...
260 views

### Deep Pattern matching with repeating arguments

I have a function that accepts a single argument that is described by the following grammar: ...
240 views

### Using transformation rules to manipulate expressions

I'm trying out Mathematica as a replacement for LaTeX for typesetting math. It works very well, but I would like to take advantage of Mathematica's computer algebra capabilities as well. Specifically, ...
159 views

### Downvalues vs. Scoping for Functions

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
181 views

### How to achieve Set+Part like behaviour in custom Set function?

So I've been toiling away on my DataFrame package. I've been trying to get Set to work with it, but it seems it either can't be done (ie via ...