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

learn more… | top users | synonyms (1)

4
votes
2answers
221 views

Pattern Matching for Rules [duplicate]

Consider a set of rules, e.g. {a -> aa, b -> bb, c -> cc, d -> dd, e -> ee} I want to remove from this list all patterns of the form e->_ and to ...
1
vote
1answer
43 views

How to use collect with arbitrary powers?

I want to use of collect for this code: ...
12
votes
0answers
130 views

Logical AND of multiple patterns

Mathematica provides an equivalent for "logical OR" when pattern matching, using Alternatives or the infix | operator. For ...
6
votes
1answer
106 views

StringPattern to remove markers around text

I am not able to figure the StringPattern to use to remove markers in string. This is the input. ...
1
vote
0answers
80 views

Matching All Functions [duplicate]

Is there any way to match all functions? There's an existing answer for matching functions of one parameter (_@_) but it doesn't work for trying to test if a symbol ...
0
votes
2answers
87 views

List of all Variables that match an specific pattern

I'd like to make a list of all constants that match an specific pattern, wich includes all terms that multiply an specific variable. For example, I'd like to take all terms that multiply t in: ...
11
votes
2answers
306 views

How can one represent a string pattern with prohibited substrings?

Say I have the following string: "<hello>something WRONG</hello><hello>something else</hello>" I am trying to find code that works like the ...
9
votes
2answers
214 views

Test for non-numeric variables

I have a table, testdata, with numeric and non-numeric data. testData = {{1, 2, 3, no, 5}, {5, 6, 7, 8, 9.1}, {1, y, y, n, n}}; I can locate the positions of ...
5
votes
1answer
70 views

Matching products with optional factors with definite heads

I need to match a products with exactly one factor F, an arbitrary number of factors of head A, and possibly additional inert ...
4
votes
1answer
28 views

How to get the VertexWeight list or EdgeCapacity list in a graph

The graph of Mathematica.We have VertexList or EdgeList.So we can get it convenient like ...
0
votes
1answer
87 views

Simplifying expressions

Let's define a function $\Omega(x,y)$ ...
6
votes
1answer
69 views

Can't access `AnyOrder` named parameters

Straight from AnyOrder documentation ...
4
votes
0answers
56 views

How to get f[x] to match with f[c_., x] when OneIdentity doesn't match

After setting: SetAttributes[f, OneIdentity] Default[f] = 1; We have the following successful matches: ...
0
votes
2answers
61 views

replace a part of equation by a symbol

I have an equation: $$ \frac{d\text{$\phi $0}}{dx} \left(-\frac{\text{A1}(x) \frac{d\text{A0}}{dx}}{\text{A0}(x)^2}+\frac{\frac{d\text{A1}}{dx}}{\text{A0}(x)}+\frac{\text{B1}(x) \frac{d\text{B0}}...
8
votes
2answers
196 views

Odd behavior in pattern matching for functional form [duplicate]

Consider the two seemingly closely related operations: ...
2
votes
2answers
66 views

How to make a replacement, provided an inner rule applies

Consider: expr /. (b[a_] * c_f /; !FreeQ[c, d[a]] :> (c /. (d[a] :> e) )) Here's how it works: in expr, the products <...
3
votes
2answers
51 views

Strange behavior with Inequality and pattern matching

An expression like this one a < x < b is normally represented as Less[a, x, b] while an expression like ...
0
votes
4answers
108 views

How to use Select according to a list of criteria to produce a list of subsets of another list obeying those criteria [duplicate]

The code Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 0 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 1 &]]; Print[Select[{1, 4, 3, 9}, Mod[#, 3] == 2 &]] ...
0
votes
1answer
60 views

Extracting attributes from a function [duplicate]

From an expression describing a mathematical function, I would like to extract its parameters. For example, from the expression 4 Sin[2 a x^2 + b x + c], assuming ...
0
votes
1answer
78 views

Pattern matching using Cases: why does this fail? [closed]

I have objects that look like, for example: F2 = 2 d[x] ^ d[y] + 3 d[x] ^ d[z] where ^ is ...
19
votes
1answer
261 views

Version inconsistency with optional arguments: what if the default value doesn't match the pattern?

I just came across some weird behaviour. Take this function definition: ClearAll[f] f[vs_List : All] := "match" The default value of ...
7
votes
1answer
88 views

Why can't I replace I with HoldPattern@I?

Just try this sample: I /. HoldPattern@I -> a (* I *) Why does it fail to replace I with ...
15
votes
2answers
195 views

Unexpected behaviour when pattern matching with Longest

I came across some unexpected behaviour today when using the Longest function when trying to do some pattern matching. ...
3
votes
0answers
63 views

Searching a deeply nested list [closed]

I have a deep nested ragged list. ...
6
votes
2answers
103 views

Why does this recursive pattern guard not work?

I have the following list: ...
4
votes
1answer
110 views

Trim a list of elements [closed]

I suppose this is a trivial question to many of you, please bear with me. I have a long list of words, list1. I would like to "remove all words that contain a specific letter, say "o". ...
4
votes
2answers
200 views

Replacing numbers of which you only know certain digits

If I have a list of numbers like this: list = {0.3565127, 0.8656421, 0.231479879, 0.39698787, 0.536987651}, which are all between 0 and 1, each having lots of ...
1
vote
0answers
78 views

Pattern for a function of say 2 variables [closed]

How can I specify a pattern for an expression f for which say f[x,y] returns a value, ie is defined as a function taking two ...
4
votes
1answer
40 views

help with string patterns and RemoveCellTags

I am building a table of contents using AuthorTools. I used it to create cell tags so the TOC can make hyperlinks, but then I decided I wanted to change them. But I am having trouble removing the ...
2
votes
0answers
60 views

Pattern Matching and overloading NonCommutativeMultiply [duplicate]

My goal right now is to implement some rules like linearity etc for noncommuting operators (arbitrary matrices etc). This is partly an exercise for me to understand MMA better so giving me a package ...
12
votes
2answers
301 views

why MatchQ is fast and Replace slow?

Consider a large expression, say a polynomial with 25 terms expr=Product[Unique["a"],{i,1,25}]; to which you apply the following replacement rule: ...
11
votes
4answers
199 views

Evaluation of Alternatives

I recently stumbled upon a few examples that suggest patterns containing Alternatives do not evaluate for individual alternatives. For example: ...
3
votes
2answers
78 views

Except[] with levelspec

I am trying to determine all the variables used in list. For This I use: DeleteDuplicates[Cases[l1,_Symbol,-1]] This is great except that ...
1
vote
0answers
91 views

Need help interpreting a small block of code [closed]

I have recently "inherited" some Mathematica scripts which I need to interpret to write in some other languages (Maple and C++). I have never used Mathematica before so I have been learning on the job....
3
votes
1answer
124 views

The basic primitive recursive functions

How can the basic primitive recursive functions be expressed in the Wolfram Language? Apparently here's an example for primitive recursion: ...
20
votes
3answers
607 views

Detecting components in timeseries

I'm looking at this sequence of values: I'd like to detect the points where the center of the time-series shifts (around x=1000 and x=2000). Many of the transforms and smoothing methods I have ...
4
votes
2answers
57 views

Is it possible to match a sequence with multiple heads without using Condition

Suppose I have: expr = Times[2, a[x,r], a[y,1], c[z,0,0], d[w,m]]; If I want to match a sequence of expressions with the same head ...
8
votes
2answers
121 views

Why is a longer pattern matched rather than a short pattern?

I was reading the Mathematica V5 Book and am a bit perplexed on the behavior of a particular pattern matching example where it seems to be choosing a longer pattern rather than a shorter pattern. The ...
3
votes
1answer
64 views

How to collect elements in one list with the constraints provided in a second list

I wish to collect the items in the first list provided the constraints in the second list. For example in the lists shown below the first list shows a bunch of strings and the second list shows the ...
4
votes
2answers
154 views

What kind of pattern should be used

I was trying to make a simple question but made it trivial. So the original setting is as follows: expr = {{a}, b, {{c}, {e}}, d, {}} I need that all lists are ...
1
vote
1answer
50 views

Pattern that matches numbers in the $\epsilon$-neighborhood of a given number?

I dfined the following function to create a pattern that matches the $\epsilon$-neighborhood of a given number. ep[x_, e_] := _?(x - e <= # <= x + e &) ...
2
votes
2answers
63 views

Remove an element of a list [duplicate]

I want to check if any element of a list contains Missing["NotAvailable"] If so I want to delete the entire list. If I am correct I need to have some ...
3
votes
1answer
80 views

How to prevent fractions in polynomial quotients?

(This was a hard question to give a succinct title to, so feel free to edit it.) When I divide polynomials, I would like Mathematica to NOT create negative powers of variables. For example: ...
1
vote
0answers
55 views

Pattern placeholder for zero or more matching items in list [closed]

I'm working through the exercises in Stephen Wolfram's book "An Elementary Introduction to the Wolfram Language." In Chapter 32 on "Patterns", Exercise 32.1 is "Find lists beginning with 1 and ending ...
5
votes
2answers
155 views

How to name a repeated pattern sequence inside Plus?

Here is a sample expression that contains a bunch of f and some other stuff: ...
7
votes
4answers
105 views

Problem with handling a factor of $1$ in pattern matching

I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching ...
12
votes
2answers
195 views

Optional throws error when called from a package. Workaround?

Bug introduced in 8.0 or earlier and persisting through 10.3 or later. Case number: 305932. In the front-end notebook (Mathematica 9.0 and 10.2): ...
4
votes
2answers
85 views

How can I extract something from an expression and assign it without changing the expression?

I am researching how to extract a certain value from an expression by using a pattern mechanism without actually changing the original expression. The paragraph above could be very ambiguous, so here ...
2
votes
1answer
72 views

Replacement producing unexpected result

If I use a replacement rule like ...
3
votes
1answer
91 views

How to use `Shortest` for pattern matching? [duplicate]

For the string ((!@#)), the shortest match for the pattern "("~~__~~")" should be "(!@#)", ...