Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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

0
votes
0answers
15 views

ReplaceAll does nothing with the rgb values of a picture

In the book "Programming with Mathematica: An Introduction" at the beginning of the chapter on Patterns and rules an example is given where the colors of an image are transformed according to ...
9
votes
2answers
214 views

About shorthand notation for conditional pattern (Paul Wellin “Essentials of Programming in Mathematica”)

I am reading Paul Wellin's "Essentials of Programming in Mathematica". Mr. Wellin wrote the following in his book: There is a convenient shorthand notation for conditional patterns that is ...
6
votes
2answers
81 views

Identifying Infinity, Indeterminate, etc

I am doing a calculation and sometimes in the middle of the calculation a parameter evaluates to the following ...
1
vote
1answer
29 views

Why does ReplaceAll match pattern while MatchQ fails

Why does the following pattern with MatchQ fail MatchQ[1+Sin[1] I, a_Complex /; Im[a] > 0] (* False *) Whereas, the same ...
0
votes
0answers
35 views

Simplify by defining new functions

if I have a formula like ...
4
votes
1answer
57 views

Subtle order-of-evaluation issues when pattern-matching with attributes

Related question concerning unrolling the tests that are shown below: Big and Little surprises when unrolling tests of pattern-matching and attributes Questions researched before posting this one: ...
3
votes
1answer
61 views

Big and Little surprises when unrolling tests of pattern-matching and attributes

Related question concerning interpretation of the substitutions: Subtle order-of-evaluation issues when pattern-matching with attributes Questions researched before posting this one: Orderless ...
1
vote
2answers
81 views

Pattern transformation to implement “factoring out”

I'm attempting to put together some code that would do pattern based expression manipulation (I know it's already implemented in mathematica), but I'm getting stuck on a pattern to deal with "...
3
votes
0answers
63 views

Match Polish letters

I am quite confused about the following cases, in Mathematica 9.0.1.0: LetterQ["ł"] StringMatchQ["ł", WordCharacter ..] The first case gives ...
7
votes
3answers
170 views

Finding items satisfying a condition in a list using patterns

Why the following command Cases[{1, 2, 6, 3, 4, 8, 5}, {x_, y_, z_} /; (y > Max[x, z]):>y] doesn't return the y values that are greater than their ...
4
votes
3answers
85 views

Repeated replacing elements in the list

I have a large list of the following form {{1, 2} -> -1, {1, 1} -> 1, {1, 2} -> -1, {1, 6} -> 1} and I want to combine all of the pairs ...
1
vote
1answer
57 views

Create mirror function using pattern matching

I want to implement pattern matching for a few simple types. In particular, I want to define a function mirror that mirrors circles and rectangles. That is, ...
0
votes
1answer
69 views

`:` versus `:=`

I am going through the Fast introduction for programmers tutorial on Mathematica. The page on patterns has the below line: x_ (short for x:_) stands for a pattern whose value will be named x ...
3
votes
1answer
46 views

How can I replace non-reals in a multidimensional list?

Ok. I'm obviously not understanding something about pattern matching. I have a list called t like the following: ...
2
votes
2answers
53 views

Construct a function like Coefficient

I'am trying to construct a function that will work like Coefficient as an exercise. ...
8
votes
1answer
129 views

Why does MatchQ[a, r_ /; Head[r] != Plus] evaluate to False?

I'm trying to understand. Why is it that: MatchQ[a, r_ /; Head[r] != Plus] Evaluates to: False ? For me, I would think ...
1
vote
1answer
63 views

Working of MatchQ[] with Blank, BlankSequence, BlankNullSequence [closed]

This code: In[76]:= Trace[MatchQ[{1,2,3}, {_?NumberQ}]] gives: Out[76]= {MatchQ[{1,2,3},{_?NumberQ}],False} This code: ...
13
votes
3answers
405 views

Generating a pattern from user-defined rules

I want to generate a list of $L$ and $S$ according to the following rule: upon each iteration, replace $S \rightarrow L$ $L \rightarrow LS$ Such that, starting with $L$: $ L \rightarrow LS \...
9
votes
2answers
154 views

Find pattern with Position

I have a list a = {L, S, L, L, S} and I can find the position of of either L or S by: ...
6
votes
2answers
406 views

What does _. mean in patterns? [closed]

Here is a quick one, hopefully. I searched through different tutorials and documentation articles but haven't been able to find anything yet. What does _. mean? As ...
0
votes
1answer
40 views

Replacing one of the integrals in the multiple integral with an analytical solution

Problem consist in simplifying complicated multiple numerical integral, where I know analytical solution for a subset of them. Integrand is of the form ...
2
votes
1answer
29 views

Why does N result in some inconsistency in this context?

In the following, why does N not produce the expected decimal output, while Round does? ...
8
votes
2answers
170 views

How can you Unset the following UpValues?

This expression is taken from a talk given by the late Robby Villegas. The UpValues to some symbol a is defined below as: ...
2
votes
1answer
34 views

Matching combinations of StringExpressions and general patterns i

Is there a clean way to match patterns that combine general patterns and ?StringExpression For example, Cases will not match ...
1
vote
1answer
44 views

Pattern match sum of terms

I am trying to define a replacement rule for an arbitrary sum of terms in an equation, that returns the x[i] terms along with their coefficients. For example, ...
2
votes
0answers
44 views

Why do I get this warning when replacing pattern with list element? [closed]

The input x^2 + x^3 /. x^i_ -> i yields: 5 The input u = {1, 2, 3} x^2 + x^3 /. x^i_ -> u[[i]] still yields the ...
10
votes
4answers
343 views

Making a list of patterns of arbitrary length

I would like to create a list of arbitrary length: {x1_, x2_, ...} Where each of the elements of the list has the full form: ...
1
vote
1answer
50 views

How to select terms which contain certain letters?

I would like to select the term Subscript[Cos\[CapitalDelta], Subscript[p, B]] of expression ...
2
votes
1answer
40 views

How to specify optional string parameters in a function signature and identify the pattern

As a newbie, I've got a rather simple question. I would like to define a function that accepts one of a few possible strings. I.e., I wish to do the following. ...
9
votes
3answers
212 views

Different behaviours of Cases

Why does Cases[] output an element only in the first of the following lines? ...
4
votes
2answers
109 views

Pattern matching list of lists containing only numbers

I'm trying to define a pattern that will match lists of lists, where the inner lists contains only numbers (I want the sum of each list). Like this ...
1
vote
2answers
71 views

Apply operator to certain functions

I have the following term a[om1] b[om2] c[om3] d[om4] f1[om1] f2[om2] f3[om3] f4[om4] I want to apply an operator ...
5
votes
0answers
40 views

Pattern for string that does not contain x, constrained to shortest matches only [duplicate]

I need a pattern for a string that does not contain a specified string, while at the same time respecting the mandate to match only shortest sequences. For example strings bracketed by ...
1
vote
3answers
60 views

Elimination of terms not containing at least one variable from a set

I have an expression with each term therein either containing at least one $n_i \in \left\{ n_1 , n_2 \dots , n_{5} \right\}$ or no $n_i$ at all. I would like to easily eliminate all the terms that ...
9
votes
1answer
179 views

Optional pattern behaves differently in version 8, 9, and 10+

We recently ran into an issue when testing Rubi on older versions. It boils down to this simple example which I didn't dare to simplify further as I cannot test it in versions < 10 ...
3
votes
1answer
47 views

Replacement rule to display negative exponents as denominators

Suppose I have an expression of the form: expr=f[t]^b + f[t]^a g[t]^-a + g[t]^-c And I want to create a rule to convert negative exponents to denominators. In ...
2
votes
1answer
57 views

Why _Line works in this Cases as pattern?

I got this from examples, and it works, but I can not understand it first the initial data f1[x_] := 4 x^3 + 0.1 x^2 - 7 x - 1 pss = Plot[f1[x], {x, -1.3, 1.53}] ...
0
votes
0answers
37 views

Unexpected behavior from RuleDelayed

RuleDelayed gives different results when list length is 3 than the results given when list length is 1 ,2 or 4. ...
4
votes
1answer
85 views

Create pattern condition dynamically

Suppose i have a function condition[pattern_,list_]:=With[ {x=Unique["x"]}, Condition[Pattern[Evaluate[x],pattern],Not@StringContainsQ[x,list]] ]; If i ...
1
vote
1answer
74 views

Can Simplify TransformationFunctions make multiple or branching suggestions?

Imagine, as a simple toy example, that it's valid to replace some list with any one of its elements. How could I write this into Simplify ...
1
vote
0answers
38 views

Simplifying results of a vector function to be nested (SECOND UPDATE)

(Oddly enough, the title for another question that ended up dealing with an unrelated problem could have worked better here, but I didn't want to plagiarize.) Given a symbolic vector function ...
3
votes
4answers
63 views

Splitting Piecewise with vectors to components

I have a piecewise function defined with vectors, e.g. ab = Piecewise[{ {{a1, a2, a3}, 0 < r < 1}, {{b1, b2, b3}, 1 < r < 2} }, {c1, c2, c3}] The ...
4
votes
1answer
75 views

Concerning the Attribute Flat

Suppose we have a function f with the Attribute Flat. From my understanding, the following command should return all possible ways of rewriting ...
11
votes
3answers
300 views

StringContainsQ And

I'm looking to test whether a string contains all substrings in it, independent of each substrings location. For example: ...
5
votes
0answers
55 views

How to match -1 at arbitrary precision?

Suppose a want a pattern to match: -1 -1. -1.`42 (or any other value for the precision) with a pattern. What would be the best pattern to use? I guess one could ...
2
votes
1answer
37 views

How to match exponent that might be 1? [duplicate]

I want to apply the following rule m^p_ :> (m^p - Log[n]^p) to m^x where x may be one. ...
6
votes
2answers
183 views

What makes a pattern more general?

This is a rewording of the question. The original post is below. How come that a pattern for an expression with a specific head is not more specific than a pattern for an expression with a generic ...
2
votes
1answer
62 views

How to use `Pattern` and `Slot` to match n-element conditions?

Extract the qualifying sublists. – Condition: The elements are all in the Reals and greater than zero. Question If the sublist contains many elements, how can it ...
3
votes
1answer
58 views

Why does GreaterThan with pattern test not work [duplicate]

I'm stuck in a seemingly easy question, but I can't figure out why the GreaterThan function and the pattern test do not work. I have tried to debug it for a while, ...
5
votes
1answer
80 views

How to use `RuleDelayed`?

I get what I expect from the following code Replace[{a, b, c}, b_ -> (w /; x > b), {1}] (* {w /; x > a, w /; x > b, w /; x > c} *) However, when I ...