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

learn more… | top users | synonyms (1)

2
votes
1answer
71 views

Using Associations and Pattern matching in numerical functions possibly broken

It looks like the very young feature of pattern matching (/replacing) within Associations (http://mathematica.stackexchange.com/a/119542/6804), with or without ...
5
votes
2answers
68 views

Association matching still not working perfectly?

Up until 10.4 you could not match on internal Association structure at all: http://mathematica.stackexchange.com/a/119542/6804 Now you can, but it seems flawed ...
3
votes
0answers
58 views

Is pattern matching just systematic guessing and checking?

How does the pattern matcher work? how does it match this example? f[3, h[4]] /. f[x_, z_[y_]] :> {x, y, z} My guess is that the pattern matcher collects all ...
19
votes
2answers
533 views

Mysterious behaviour of Pick

As everyone knows, Pick can do amazing things, The most facinating phenomenon is that it can pick though all sorts of lists: The basic & pattern matching: <...
8
votes
1answer
93 views

How does a construct like `i : func[arg_] := i = an expression using arg` work?

Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg I have two questions on this: Can ...
3
votes
1answer
56 views

How to implement search patterns and take into account language statistics?

Patterns in Mathematica I struggle some with patterns while trying to extract words out of large texts. Let us say that I have a long text (Strindberg) that we can call ...
6
votes
3answers
187 views

Removing non-word characters in certain parts of a string

I would like to ask how I can remove non-word characters from a string, but only in certain cases. I have read this article, so I know how to get the words out of a string. My text is however a bit ...
5
votes
2answers
93 views

How do I construct a Pattern with Alternatives and PatternSequence without getting Pattern::patvar messages?

Bug introduced 8.0 or earlier and persisting through 10.4.1 [CASE:3631078] In Mathematica 8 and 9, defining this rule: ...
6
votes
1answer
114 views

Error when combining Except, “*” and an additional character

Bug introduced in 5 or earlier and persists through 10.4.1 Anyone knows why ...
1
vote
1answer
84 views

How to define a function which takes another function as input in Module

Let $f$ be a function of $n$. I would like to define a function $A$ with module which takes $f[n]$ as input. So for example: ...
3
votes
3answers
161 views

MatchQ[x y, (x | y) (x | y)] returns False

MatchQ[x y, (x | y) (x | y)] It returns false. Why? I want to eliminate terms like x^2, ...
8
votes
2answers
209 views

Position and string patterns

I am trying to get Position work with patterns. I have the following code: ...
2
votes
4answers
149 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. f g+ Dt[f, t] g +Dt[g, {t, 2}] f What I would like to ...
5
votes
3answers
185 views

Delete all strings that are prefixes of another string

I have a long list of strings and I'm trying to find a more efficient way of removing those elements that are prefixes of another: ...
12
votes
2answers
205 views

Opposite of Alternatives in a Rule

Mathematica provides Alternatives, to match one of several patterns. In a rule, this is used as p1|p2|p3:>replacement. I ...
9
votes
4answers
294 views

Pattern match for nested Association

Suppose I have Association testAssoc=Association[a->Association[1->1,2->2],b->Association[1->1,2->2]] which ...
9
votes
1answer
112 views

Nested association values validation. POJsO handling

Sometimes it is convenient to work in your package/app with "objects" like: ...
1
vote
0answers
28 views

Pattern intersection [duplicate]

Given a list of patterns {x, y, z} I want a pattern that matches if and only if each pattern matches. If instead of getting the intersection of patterns I wanted ...
4
votes
2answers
68 views

Why does `Repeated` not work in this case?

I think I don't understand how to use Repeated. I have a list for example list={a,b,a,b,a,b,a,a,b} I want to detect whether ...
6
votes
2answers
103 views
3
votes
1answer
154 views

Is this a bug in NCAlgebra library?

In course of my work with NCAlgebra package for Mathematica, http://math.ucsd.edu/~ncalg/ , I encountered unexpected behavior of replacements. I am using ...
4
votes
1answer
130 views

How can one match a function in a replacement rule

I'm trying to write a delayed replacement rule that is capable of matching both the functions and their parameters, like ...
26
votes
5answers
2k views

Fetching data from HTML source

I want to generate a couple of plots/graphs with Area 51 statistics. Since Area 51 doesn't work with the SE API, I'm forced to find another way to get the information I want. That other way is with <...
16
votes
4answers
489 views

How to enumerate all possible binary associations?

Suppose I have a list of symbols like: {a,b,c,d} I would like to enumerate all possible binary associations (combining symbols and/or sublists pairwise): ...
8
votes
1answer
127 views

What kind of pattern should I use with StringCases? [duplicate]

Consider the string str = "ABCaDEFbcABCdefDEF" I'd like to take out all substrings surrounded by the substrings "ABC" and <...
2
votes
1answer
55 views

Why is head evaluation inconsistent in pattern replacement?

I noticed the following unexpected result: In[90]:= "foo" /. x_String -> Head[x] Out[90]= Symbol Why is it that Head[x] ...
12
votes
1answer
222 views

Longest common substring for multiple strings?

I'm looking for a way to get the longest common substring for multiple strings such as {home/dir1/dir2/jmoasd.txt,home/dir1/dir2/ivbnoxcihv.txt,home/dir1/dir2/siuhgiuchv.txt} should yield ...
4
votes
3answers
58 views

pattern matching for third degree reciprocal equation

I am trying to create a pattern for third degree reciprocal equation. I try this a_ + b_ x_ + b_ x_^2 + a_ x^3 This matches ...
4
votes
0answers
38 views

Pattern match Shortest BlankNullSequence in SequencePosition

Apologies if this has an answer somewhere but I can't find it. From the examples for SequencePosition we are shown that ...
1
vote
2answers
111 views

Filtering and summing sublists with Cases

I've declared a list: list = {{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}, {a, b, c}, {e, f}, {g}, {}, {Sin[a], Cos[b]}}; now I've made an rule which add 2 pairs ...
5
votes
0answers
123 views

Same input,different result? [duplicate]

Using this code,you can see,same input, but the output are different. So weird! If I copy this code here, it outputs normally. So what's the problem? Ps:I use ...
0
votes
1answer
62 views

Pattern which will transform a given expression [closed]

Find the Pattern which when applied to this expression expr = {a^2, a^b, x^4, (x - 1)^2}; yields this result: ...
1
vote
0answers
41 views

Rule/Replace/Patterns [closed]

I need help with doing this question. given: Sin[x+y] Cos[x+y+z] Define using Patterns a rule (specifically a list of 2 rules ) , the two adding rules for trigonometric functions sin (a + b )...
4
votes
1answer
61 views

How to check if an expression contains a symbol ONLY after a given head?

I have just started programming in Mathematica and still have to get a hang of it (and rule-based programming), so I apologize if this question may seem stupid. I will explain it with a concrete ...
10
votes
2answers
120 views

Using Cases to pick out entries of a SparseArray

I'm trying to use a SparseArray for some large homological algebra calculations. I'd like to find indices of a SparseArray ...
3
votes
1answer
78 views

Replace pattern for exponentials

Ultimately, I want to replace every Exp[(const) w[i,j]] to T[i,j] and Exp[-(const) w[i,j]] to 1/T[i,j] in the following expression ...
21
votes
7answers
1k views

Using patterns in pure functions

Pure functions may be handy if you don't want to assign your function a name. For example I would calculate $x(x-1)$ for some numbers $x$ by ...
2
votes
2answers
64 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 <...
1
vote
0answers
82 views

How to “INJECT” with holding Evaluation, specifically into a WhenEvent

I am importing a set of differential and stochastic equations from an external package, and want to use Mathematica to analyse this. I am struggling building the set of equations that I can use with <...
10
votes
2answers
91 views

How to use `Except[]` inside of `Pick[]` [duplicate]

Pick[{1, 2}, {0, 1}, Except[0]] returns {1, 2}, the first element of which I did expect to be excluded. What is the thing I ...
8
votes
1answer
85 views

How to match Association[]?

The following input: Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] Returns {}. Why does it not ...
2
votes
0answers
44 views

Replacement rule for summation with Kronecker delta

I want to perform some symbolic computations in index notation, without explicit reference to sums, so that in expressions of the type f[i,j]g[j,k] summation over <...
8
votes
1answer
90 views

Matching HoldPattern expressions

My original problem is to delete all rules starts with a[1] from SubValues[a] For example, ...
4
votes
2answers
213 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 ...
0
votes
0answers
13 views

How to match sum of two or more objects? [duplicate]

I am trying to match expressions that are the sum of two or more objects with a specific head. However I can not get this work properly. For example ...
19
votes
2answers
322 views
4
votes
2answers
79 views

MatchQ failing in some cases with Hold [duplicate]

Could someone clarify why the first of these MatchQ finds a match whereas the second does not? (I'm using version 10.0, in case that matters.) ...
1
vote
1answer
42 views
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. ...