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

learn more… | top users | synonyms (1)

6
votes
3answers
73 views

Transformation rule for arbitrary number of argument expressions

I am failing to write a Mathematica transformation rule that replaces e.g. f[a + 3, b + 3, c + 3] with f[a, b, c] + 3 for an ...
14
votes
0answers
53 views

What has changed in pattern matching functions with the Orderless attribute?

In making an attempt to answer this question, we ran into a version-dependent pattern-matching issue. An extended discussion in the comments transpired, and some inconsistencies arose. Consider the ...
4
votes
1answer
88 views

Better use of pattern matching in replacing elements of a list

I have a list of reals and I'm replacing those which are contained in a given interval with two different values depending on wheter the selected reals are positive or negative. I came up with this ...
4
votes
4answers
146 views

How to scope `Pattern` labels in rules/set?

Module[{x}, f@x_ = x; p@x_ := x; {x, x_, x_ -> x, x_ :> x} ] ?f ?p gives ...
47
votes
3answers
1k views

How is pattern specificity decided?

Mathematica has a notion of pattern specificity, which is a partial ordering on patterns. The rules (e.g. DownValues, SubValues,...
3
votes
1answer
66 views

Rule for derivatives

I have a differential equation of the form $$f'(x)=g(x,f(x))$$ (where $g$ is has a known explicit form, e.g. $x f(x)$) and I have an expression which contains several derivatives of $f$. I want to ...
1
vote
2answers
62 views

Use cases to exclude data values over a certain threshold

I am trying to parse data output from a fluorometer. My output is basically signal intensity over time. So I have a list of 2 value lists with the first value time and the second signal e.g., ...
0
votes
1answer
48 views

Replacing Non-Constant Elements from List with Pattern [closed]

I have a list list = {0, 0}, 0, {0, 0}, 0, 1}, {{0, 0}, 0, {0, 0}, 0, 2}, {{7,7}, 20, {7, 7}, 3}, {{8,8}, 30, {8, 9}, 4}} And I want to remove all elements which ...
0
votes
0answers
40 views

Generate rule that replaces if string matches pattern

I have let's say a function f[x], but for x I pass a string. And I want to replace that string with a number, if that string ...
4
votes
1answer
45 views

What is the function of OrderedQ in these expressions?

Could someone please let me why the output of the two expressions change with OrderedQ in place. The first expression is as follows: ...
3
votes
1answer
120 views

Why the pattern match don't work as expected

When I try to figure out this problem,I write such code: ...
2
votes
0answers
56 views

How to Set Priority Between Longest and Shortest [closed]

As is shown in this post's answer, it seems that though you can set priority of Longest (or Shortest) by ...
1
vote
1answer
64 views

Collect partial derivatives and rewrite them with delta/nabla

Ultimately, I wish to get the higher generalized product rule for Laplacian. That is, we have $\Delta(fg)=f\Delta g+2\nabla f\cdot\nabla g+g\Delta f$ but what is $\Delta^n(fg)$? I am tying to guess ...
4
votes
0answers
39 views
20
votes
2answers
601 views

Mysterious behaviour of Pick

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

Longest and Shortest won't work in ReplaceList?

I'm trying to make a pattern that's easy to preceive by human but hard to write out by Mathematica when I came across this problem. (Check the original problem here) Let's check this simple case: I'...
3
votes
1answer
117 views

How to Find out the Recurring Period by Pattern Matching?

Note: It's not hard to solve this problem AT ALL, so please, try to explain how to use PATTERN MATCHING instead of how to find out the recurring period. We can easily extract real digits from a ...
2
votes
1answer
82 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 ...
6
votes
2answers
83 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
64 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 ...
8
votes
1answer
102 views

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

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
68 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
193 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
95 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
162 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, ...
9
votes
2answers
213 views

Position and string patterns

I am trying to get Position work with patterns. I have the following code: ...
2
votes
4answers
150 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
186 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
296 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
114 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
131 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
492 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
129 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
237 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
60 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
43 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 )...