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

learn more… | top users | synonyms (1)

15
votes
2answers
405 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: <...
7
votes
1answer
87 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
52 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 ...
1
vote
1answer
83 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: ...
8
votes
2answers
208 views

Position and string patterns

I am trying to get Position work with patterns. I have the following code: ...
3
votes
3answers
158 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, ...
5
votes
2answers
92 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: ...
5
votes
3answers
184 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: ...
9
votes
1answer
109 views

Nested association values validation. POJsO handling

Sometimes it is convenient to work in your package/app with "objects" like: ...
12
votes
2answers
201 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 ...
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 ...
4
votes
1answer
129 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 ...
0
votes
2answers
55 views

Can I use StringExpression or RegularExpression to manipulate Symbolic XML?

Please, consider an XML document containing these fields: ...
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
219 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
37 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
108 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 ...
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 )...
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 ...
4
votes
1answer
60 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
119 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 ...
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
90 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 ...
7
votes
1answer
81 views

How to match Association[]?

The following input: Cases[{"key" -> Association[]}, HoldPattern["key" -> Association[]]] Returns {}. Why does it not ...
2
votes
0answers
43 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, ...
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 ...
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.) ...
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 ...
1
vote
1answer
42 views

How to use collect with arbitrary powers?

I want to use of collect for this code: ...
12
votes
0answers
125 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
86 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
304 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
213 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
68 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
27 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
85 views

Simplifying expressions

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

Can't access `AnyOrder` named parameters

Straight from AnyOrder documentation ...
4
votes
0answers
55 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
59 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
63 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 ...