# pattern-matching's questions - English 1answer

889 pattern-matching questions.

### 2 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]^-cAnd I want to create a rule to convert negative exponents to denominators. In ...

### 2 Why _Line works in this Cases as pattern?

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

### 4 Change matrix elements at specific locations?

Let's say I have an arraya={{1,2,3}, {4,5,6}, {7,8,9}}I want to have all elements along the diagonal multiplied by 2. ...

### 4 Create pattern condition dynamically

Suppose i have a functioncondition[pattern_,list_]:=With[ {x=Unique["x"]}, Condition[Pattern[Evaluate[x],pattern],Not@StringContainsQ[x,list]] ];If i ...

### 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. ...

### 1 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 ...

### 6 Put edges of a matrix to zero

I have to set the first and last rows and columns to zero rows and columns. I tried with the first column: ...

### 1 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 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 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 StringContainsQ And

3 answers, 297 views pattern-matching string-manipulation
I'm looking to test whether a string contains all substrings in it, independent of each substrings location. For example: ...

### 5 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 How to match exponent that might be 1? [duplicate]

I want to apply the following rulem^p_ :> (m^p - Log[n]^p)to m^x where x may be one. ...

### 6 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 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 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 How to use RuleDelayed`?

1 answers, 76 views pattern-matching replacement
I get what I expect from the following codeReplace[{a, b, c}, b_ -> (w /; x > b), {1}] (* {w /; x > a, w /; x > b, w /; x > c} *)However, when I ...

### 1 Symbol Level pattern matching and rule replacement

I want to parse a Mathematica Notebook to get some information regarding the relative use of symbols. So first things first: ...

### StringPattern: Not one of pattern?

I have read in a Mathematica notebook and have assigned its content to the variable file. I have created the Strings corresponding to Wolfram Language symbols as ...

### 2 Commutator of block matrices

1 answers, 56 views pattern-matching operators
How to implement a commutator of matrices composed of operators? Background: Let $\hat A_{ij}$, $\hat B_{kl}$ be some sets of some operators. $\hat I$ is the identity operator. Their commutators are ...

### Limiting scope of StringPattern to first match for efficiency?

I have imported a Mathematica Notebook and assigned it to the variable file. In addition, I have gathered the strings corresponding to all Wolfram Language symbols:...

### 12 Is it possible to separately call the default value of a variable which isn't at the end of the sequence?

After reading this post I recalled an old question I once met. We know that we can set default values for variables of a function: ...

### 2 Retain summands in an expression that match a given rule

Suppose we have the following expressionA[j_] := Subscript[A, j] EXPR=A[1]^2*A[2] + A[1]^4*A[3] + A[2]^2As stated in the title, I am struggling to retain some ...

### Why is RuleDelayed only working partially?

0 answers, 34 views pattern-matching rule
Based on help that I got from rhermans here, I wrote a module to download financial data, format it and export it to Excel. ...

### 11 Is it possible to set multiple type to a variable?

Consider thisf[x_Real] := 2 xf[1] f[1]f[1.0] 2.0 I want ...

### 2 DeleteCases not working as expected

I am trying to reproduce the example of Data Cleaning from here. I had to make a slight change in input 13 to ...

### 3 How to return a default value if no match is found in search of string

Suppose I have a string like "a-1_b-2_c-3_d-4" with numbers, labels, and separators, and I want to get ...

### 1 DeleteCases and Pattern Matching With NumberQ [closed]

If I have a list:list:={{1,1}, {2,2}, {3,3}, {4,"a"}}And I would like to delete the row with a string character, the following works: ...

### 2 All possible pattern match and replacement

Given a list, how to get all possible new lists replacing a matched pattern with a new pattern (every time only one replacement)? For example, I have a list like this ...

### 3 How to take a part from a list without evaluating it

Consider I have a list l ...

### 3 Making strict replacements in Mathematica

My question is quite a simple one, but I have not been able to find a solution yet. Say I have terms of the form e.g1/n1/n2/n3/n4and ...

### 10 PatternSequence, Optional and _Symbol

0 answers, 100 views bugs pattern-matching
Bug introduced in 9.0 or earlier, partially fixed in 10.1 and persisting through 11.3 [CASE: 4075992] confirmed Note: See (72290) for the fixed part of the bug. Consider the following example:<...

### 18 Unexpected pattern matching behaviour: PatternSequence vs. Optional

Bug fixed in 10.1 Consider the following pattern-matching expressions: ...

### 4 What are Flat Attribute best practices? [duplicate]

1 answers, 74 views functions pattern-matching attributes
I would like, as an example, to build a Times-like function. Take the following example ...

### 5 How can Mathematica automatically replace NonCommutativeMultiply[x] with x for a single argument x? [duplicate]

By default, an expression like NoncommutativeMultiply[x] does not simplify to x, unlike ...

### 4 default value for the 2nd argument in Power for pattern matching

I'm trying to match terms in power like a^b, including b=1 case. I know that I can do something like ...

### 5 StringCases and DatePattern: something strange

3 answers, 104 views pattern-matching string-manipulation
May be that I did not understand something about Mathematica pattern matching, but I noticed what follow:st is a string, inside the string there is a date (...

### Trouble with UpSet(Delayed) and IgnoringInactive

0 answers, 36 views pattern-matching hold assignment
I'm having problems setting an upvalue for a symbol that is supposed to ignore Inactive:IgnoringInactive[f[g[x]]] ^:= match;...

### 1 ProbabilityDistributionQ

I have had a look through the documentation and I cannot find a ProbabilityDistributionQ function. How can I return True if the ...

### Give Mathematica points in the XY plane and find equation that generates them

I am finding the minima of a 2D cubic periodic function by "brute force", i.e. looking for the (x,y) positions where the both the x and y derivatives are 0. This returns a list of points, in this ...

### 1 GeoGraphics added to a current GeoGraphics object

1 answers, 43 views pattern-matching geographics
I need to make a GeoGraphics[] object then save it to disk, move it to another computer where I need to load in the object then add some more ...

### 3 A pattern condition involving OrderedQ

I have the following simple code:z = {a, b, c} {{1, 2, 3}, {1, 3, 2}, {3, 2, 1}} /. {a_Integer, b_Integer, c_Integer} /; OrderedQ[z] -> 1which returns ...

### 2 Totally orderless partition

This is a slight variation of: How to generate all possible orderless partitions of a list according to another list? Consider each set following a partition a "box" and each element a "ball". What ...

### Pattern test and replacement with pure function [closed]

1 answers, 49 views pattern-matching replacement
This is a very simple question. I have an unnamed pattern and a test function should determine whether to perform replacement on it. How to make the following piece of code to work: ...

### 2 Checking if a list of length $n$ has pairs?

3 answers, 103 views list-manipulation pattern-matching
I have a list of reals and I want to check if for every element $i$ there is another element $j$ such that $\lvert{i-j}\rvert \leq x$ where $x$ is some arbitrary value. Each element can only be "...

### 5 Pattern for k distinct elements of a set of n elements

2 answers, 166 views pattern-matching combinatorics
I would like a pattern which takes as an argument a set with $n$ elements, and an integer $k$ which is less than $n$ and greater than 1, and which matches against any $k$ distinct elements of the set, ...

### Problems on MatchQ[-3*i[1], -x_*i[1]]

In my Mathematica 11.3.MatchQ[-3*i[1], -x_*i[1]]returns False. ButMatchQ[-3*i[1], x_*i[1]]returns True. I don't ...

### 2 Pattern for a vector space basis expansion

1 answers, 63 views pattern-matching vector
I'd like a pattern which matches an expansion of a vector in a given basis, so mathematically any expression of the form $\mathbf{v} = \sum_{i = 1}^n v_n \mathbf{a}_n$. The basis is defined by a ...

### 5 Replacement rules for powers of a symbol

5 answers, 330 views pattern-matching replacement
Suppose, I have an expression like so:In[68]:= expr = 2 + 5*q + q^2 + 8 q^3 + 19 q^7 Out[68]= 2 + 5 q + q^2 + 8 q^3 + 19 q^7Now I want to take all powers of q ...

### 7 KeyValuePattern specificity in DownValues

Bug introduced in 10.4.0 and persisting through 11.3.0 CASE:3824590 [...] Developers are discussing this behavior. [...] I will let you know when I have definite information on this. [...] ...