Questions tagged [pattern-matching]

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

1,306 questions
Filter by
Sorted by
Tagged with
116 views

I understand mol but meet difficulty in understanding how pdetoode atumatically generate pde-to-ode-rules by using this strange pattern and rule?

I often solve pdes for my research, and years ago I found pdetoode in this forum is very handy. Although it is a small piece of code, it solves several interesting ...
38 views

Convert complex exponential to real exponentials, sines and cosines

While taking the inverse Laplace transform of certain expressions, Mathematica yields complex exponentials. For example, using the following code: ...
94 views

Search speed of downvalues when indexing with strings

I'm writing a lot of print functions for different parts in a package and at the moment am using something like myprint[ identifierString_String ][ data_ ] := ... ...
140 views

Finding the index of certain strings in a list

I'm actually using a dataset of much greater dimensions and length but I simplified my problem of string patterns in order to ask this question. ...
199 views

Pattern matching problem in version 11.01

Consider this simple example: ...
267 views

Can we change a pattern once we throw them into pattern matcher?

Main Problem Mathematica's pattern matching mechanism seems fairly well in most cases but in some situations, I would like to ask for a bit more freedom. We can easily call for extra evaluation while ...
1k views

Quick way to use conditioned patterns when defining multi-argument function?

I have a simple function that is supposed to only accept numeric values (i.e. complex/real numbers and constant symbols e.g. Pi, E). $$f(a,b,c)=a+b+c$$ Edit: I should have chosen a less simple ...
3k views

Using patterns in pure functions

Pure functions may be handy if you don't want to assign your function a name. For example I would calculate $x(x-1)$ for some numbers $x$ by ...
44 views

How to test for arguments for a pattern that is of a positive Integer OR Infinity so that both filter through to the definition on RHS?

How can I filter an argument of a function so that definition works on positive Integers (y >= 0) or Infinity? It will ...
49 views

How to specify complicated patterns in function arguments?

Unfortunately, I have not found in documentation how to create complicated patterns in arguments of a function Consider a toy example. Say, I would like to have a simple function ...
131 views

ReplaceList Except Alternatives bug

Bug introduced in 11.3 or earlier, persisting through 12.0 or later and fixed in 12.3.1 or earlier [CASE:4352435] and [CASE:4354378] In Mathematica 11.3 and 12.0, ...
41 views

Store pattern names in variables, and use them in Condition (/;)

I am trying to create patterns whose form is not hard-coded, and which are not explicitly directly written. For that I want to use one list of pattern names to name some parts of a ...
169 views

Encode expression into a list

I have an expression of the form: $$c {\partial^{k+l} \Phi(\theta,\phi,n) \over \partial \theta^k \partial \phi^l}$$ $n,k,l$ are (positive) integers. $c$ is some constant. It is also possible that $c$ ...
30 views

How do I modify all the list elements as patterns

Is there any easier way to change all the list elements as elements with patterns? Here is an example: list={a,b,c,d} I want to transform it as a list of patterns ...
45 views

pattern test on position

At the moment I'm using something like this to get the positions of entries of a matrix satisfying a specific value: ...
157 views

Permutation avoidance function

It is common for me to see if a permutation of 1,2,...,n, avoids some fixed permutation pattern. For example, the permutation [1,4,2,3,5] contains the pattern 1,3,2, as the elements [1,4,3] appear in ...
93 views

Patterns are not Sequences are not Lists, or: Global Function Argument Reversal

H[z_] := z /. {F[p__] -> F[Reverse[p]]} F[{x, y}] + F[{x, z}] + F[{}] // H Almost. It gives an annoying "Nonatomic Reverse" error message at the ...
25 views

N on equation with numbered variables [duplicate]

Say I have a system of polynomial equations in the variables x,...,x[n]. Sometimes want to make such equations numerical (as in: the coefficients become floats) ...
129 views

Why the two outputs are different?

My first code is {a, b, b, a, b, a, b, a, a, b} /. {___, x : Longest[PatternSequence[a, b] ..], ___} :> {x} and its output is {a,b,a,b}. The second code is ...
85 views

Equality of expressions with different variable names

I have some multivariate systems of expressions that I want to check for equality up to allowing relabelling of the variables. For example the two expressions below would be the same: ...
71 views

why combining 2 patterns in Alternatives in Cases do not give same output as when each pattern is used separately?

Given expression expr = y'[x]^3 + y'[x]*Log[y[x]*y'[x]^2] + y'[x]^(1/2) + x/y'[x]^7 $$y'(x)^3+\sqrt{y'(x)}+\frac{x}{y'(x)^7}+y'(x) \log \left(y(x) y'(x)^2\right)$$ ...
63 views

Finding a mapping between two types of (generalized) hypergeometric series

I am given two functions, one is of the form $2F1(a,b,c;z)$, where $2F1$ is a hypergeometric series. The other one is a generalized hypergeometric series $3F2(d,e,f;g,h;w)$, where the characters are ...
159 views

How not to Simplify square roots

Given t1 = {1/7 (4 + Sqrt), 1/15 (3 + 2 Sqrt), 1/2 (5 + 3 Sqrt)} I do not want to simplify the square roots. I want to get ...
189 views

How can I know if a pattern corresponds to a string pattern?

What would be the best way to detect if a pattern corresponds to a string pattern ? An invalid string pattern p would lead to an error in ...
41 views

Formatting expressions with numeric exponents using pattern matching

Thank you all in advance. Question: I want to change the format of this expression (n + r)^3/n^3 into this (1+r/n)^3using rules ...
284 views

How to extract email addresses from a text file using Mathematica

I have a text file which I have uploaded here. I want to find all email addresses in this text file. I just know that some of them are after "Subscription Date" expression (I don't know if ...
83 views

Mathematica does not recognize pattern when changing by numerical values

The following returns False: MatchQ[(-1 + (1 + E^-x)^0.1 )/(1 + E^-x)^0.2, ((1 + Exp[-x_])^v_. - 1)/(1 + Exp[-x_])^u_.] False And the following returns True: <...
98 views

Matching a Dynamic Argument Pattern

times is a list of times i.e. matching {{hour_, min_, sec_Real}..}. I want to call ...
54 views

How to create a simple transformation function on lists using short form of ReplaceAll?

I can easily do this with Map so the real goal of this exercise is to get a better grip on using transformation rules. Please help with this. The outputs should be ...
36 views

Converting parts of some string into another notation

I have a big string of data and most of them are in the form Name = number^{number}_{-number}, some are in the form Name = number \pm number. I what to convert the latter into the notation. For a ...
36 views

Can't match custom head in function signature [closed]

I have written a financial charting function which has the following signature; ...
52 views

Pattern matching at multiple levels [closed]

Pattern matching is my favorite feature. Is it possible to destructure a symbol but still refer to the whole symbol? I mean something like (this won't work of course) ...
34 views

Removing conditions on patterns from LHS of a rule

Let's consider a rule which takes conditions on the patterns specified on the LHS: In:= rule = j[n1_?NonPositive,n2_?Positive,n3_?Positive]:>AAA I would like ...
200 views

How to set deeper patterns in arguments within function definitions. For example if an argument must be a list of 1 or more strings

Suppose I want to define a function whose argument must be a List of only Strings and List must have at least one member. I know how to require that the Head must be a List but how does one qualify ...
43 views

Define multivariate even general function

I want to define a multivariate even function. This question shows how to do it for a single variable function Defining general odd function but I don't see how to generalize the syntax to multiple ...
44 views

'Cases' command does not recognize specified patterns

I am having a problem with the "Cases" command. If I execute the following code ...
53 views

Derivative with respect to function of any argument

I have the expression L = v1[k1] * M[k1, k2] * v2[k2]. v1, M, and ...
71 views

Association vs Pattern Matching: which one is better for data storage?

Say I have a few key-value pairs. I can store them with Association as ...
68 views

Cutting of terms on denominators of giant expression

I have an immense expression of the form $$\sum_i \frac{f_i(X,Y,x,y)}{(a_iX^2+b_iY^2+c_ix^2+d_iy^2)(e_iX^2+g_iY^2+h_ix^2+k_iy^2)},$$ where at $a_i$ or $b_i$ AND $e_i$ or $g_i$ is different from zero....
100 views

Built-in functions on the LHS won't pattern-match

I have this pattern-matching problem: F[X_,myminus[X_]]:=0 F[1,myminus] Works. ...
89 views

Pattern matching tuples as inputs to pure function

I've been having an issue with functions which are supposed to receive as input a list of tuples. My troubles are two-fold: The function doesn't properly identify the pattern test I laid out in the ...
135 views

...
69 views

How to construct a pattern that match the multiple partial differential of any multi-variable functions?

For example,I want to match a multiple partial derivative of a function $f^{(1,0,1)}[x,y,z]$ which is the same as$\frac{\partial^2{f}}{\partial{x}\partial{z}}$.The only information given is the list ...
212 views

using StringReplace and StringPattern to convert exp(...) to Exp[....]

I am looking at a file generated by another CAS. I'd like to convert it to Mathematica syntax. Only issue is how to convert exp(....) to ...
45 views

How to make a vector of elements the pattern of a function?

if I have an expression that contains a lot of symbolic elements and I would want to make it a function of the vector containing all the elements I've found a solution based on rule delayed: ...
91 views

How to make a function ignore an argument [closed]

Motivation I am trying to call a function f, that has either 1 or 2 (or multiple) parameters, from a function g that uses ...
140 views

Fill in the StringBlank

I have a string that has blanks marked by a marker; (in this case, an underscore): "f_ath_r". I can execute the following command to get the word. ...
56 views

How to write a pattern that matches all linear combinations of functions involved except 0?

I am trying to write the simplest (and efficient) pattern that matches the linear combination of all functions except when the combination produces zero? Consider the following as an example: ...