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

learn more… | top users | synonyms (1)

2
votes
2answers
61 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
71 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 ...
8
votes
2answers
70 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 ...
5
votes
1answer
71 views

How to match Association[]?

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

Matching HoldPattern expressions

My original problem is to delete all rules starts with a[1] from SubValues[a] For example, ...
4
votes
2answers
194 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
11 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
305 views
4
votes
2answers
76 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
40 views
5
votes
1answer
97 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. ...
10
votes
0answers
97 views

Logical AND of multiple patterns

Mathematica provides an equivalent for "logical OR" when pattern matching, using Alternatives or the infix | operator. For ...
1
vote
0answers
78 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
82 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: ...
10
votes
2answers
300 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 ...
8
votes
2answers
203 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 ...
3
votes
1answer
126 views

Specifying string patterns in a plain text

I need to extract three data, like that: (1.05, 1.37,A) from an imported list of plain texts: "... Syntax of parallax is : 'value quality [error] bibcode' value : parallax value quality : flag ...
4
votes
1answer
63 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 ...
3
votes
1answer
26 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 ...
5
votes
1answer
217 views

How to Override the Head of a Symbol

I was wondering if it is possible to override the head of symbols/operators. As a motivating example if I run: Then Ket will be returned. But lets say I want Bob to be returned instead of Ket. Is ...
5
votes
1answer
66 views
4
votes
0answers
53 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: ...
2
votes
4answers
140 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. fg+ Dt[f,t]g +Dt[g,{t,2}]f+ What I would like to find ...
0
votes
2answers
58 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) ...
8
votes
2answers
192 views

Odd behavior in pattern matching for functional form

Consider the two seemingly closely related operations: ...
45
votes
2answers
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, ...
3
votes
2answers
50 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
1answer
59 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 ...
17
votes
2answers
486 views

MatchQ-ing Associations (MMA 10)

Consider {x->1} which has FullForm List[Rule[x, 1]] and, therefore, ...
0
votes
4answers
100 views

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

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 &]] ...
9
votes
4answers
253 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 ...
0
votes
1answer
75 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 ...
13
votes
2answers
300 views

Named patterns in Except

Bug introduced in 8 or earlier and fixed in 10.4.0 Bug isn't present in version 5.2 I don't think I quite understand how Except works. I want to define $f(a,b) ...
19
votes
1answer
258 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 ...
13
votes
2answers
164 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. ...
7
votes
1answer
85 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 ...
6
votes
1answer
74 views
3
votes
0answers
56 views
4
votes
1answer
106 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
198 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 ...
19
votes
1answer
254 views

What is the difference between defining a function and specifying the type of argument, versus applying a test to that argument?

Say I want to create a function that evaluates differently based on what type of argument is given. I've found two ways of doing this, ...
13
votes
6answers
898 views

Calling Table with custom iterator

I often find myself in situations where I, for example, need to build a table for some expression, but want to set the number of points rather then the step size, so the code ends up looking like this ...
4
votes
1answer
38 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 ...
1
vote
0answers
77 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 ...
2
votes
1answer
146 views

Why is there a difference when formulating patterns for Select and FirstPosition?

Select[{1, 2, 4, 7, 6, 2}, # > 2 &] Gives {4, 7, 6}. OK! So one should think that the following would work: ...
7
votes
2answers
291 views

Why doesn't this pattern work?

I want the in-built function Count to count the elements in a list which are greater than a value. Example: ...
7
votes
2answers
139 views

How to define patterns to filter lists of replacement rules?

I have a list of replacement rules like list˘of˘replacement˘rules = {a -> 1, b -> 2, c -> 3} and I want to filter all the elements where ‘b’ is replaced ...
2
votes
0answers
58 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 ...