WOLFRAM LANGUAGE TUTORIAL

# Patterns for Some Common Types of Expression

Using the objects described in "Introduction to Patterns", you can set up patterns for many kinds of expressions. In all cases, you must remember that the patterns must represent the structure of the expressions in the Wolfram Language internal form, as shown by FullForm.

Especially for some common kinds of expressions, the standard output format used by the Wolfram Language is not particularly close to the full internal form. But it is the internal form that you must use in setting up patterns.

n_Integer | an integer n |

x_Real | an approximate real number x |

z_Complex | a complex number z |

Complex[x_,y_] | a complex number |

Complex[x_Integer,y_Integer] | a complex number where both real and imaginary parts are integers |

(r_Rational|r_Integer) | rational number or integer r |

Rational[n_,d_] | a rational number |

(x_/;NumberQ[x]&&Im[x]==0) | a real number of any kind |

(x_/;NumberQ[x]) | a number of any kind |

Some typical patterns for numbers.

Here are the full forms of some numbers.

Out[1]//FullForm= |

| |

The rule picks out each piece of the complex numbers.

Out[2]= | |

The fact that these expressions have different full forms means that you cannot use

x_+Iy_ to match a complex number.

Out[3]//FullForm= |

| |

The pattern here matches both ordinary integers, and complex numbers where both the real and imaginary parts are integers.

Out[4]= | |

As discussed in "Symbolic Computation", the Wolfram Language puts all algebraic expressions into a standard form, in which they are written essentially as a sum of products of powers. In addition, ratios are converted into products of powers, with denominator terms having negative exponents, and differences are converted into sums with negated terms. To construct patterns for algebraic expressions, you must use this standard form. This form often differs from the way the Wolfram Language prints out the algebraic expressions. But in all cases, you can find the full internal form using FullForm[expr].

Here is a typical algebraic expression.

Out[5]= | |

This is the full internal form of the expression.

Out[6]//FullForm= |

| |

This is what you get by applying a transformation rule to all powers in the expression.

Out[7]= | |

x_+y_ | a sum of two or more terms |

x_+y_. | a single term or a sum of terms |

n_Integerx_ | an expression with an explicit integer multiplier |

a_.+b_.x_ | a linear expression |

x_^n_ | with |

x_^n_. | with n≠0 |

a_.+b_.x_+c_.x_^2 | a quadratic expression with nonzero linear term |

Some typical patterns for algebraic expressions.

This pattern picks out linear functions of

.

Out[8]= | |

x_List or x:{___} | a list |

x_List/;VectorQ[x] | a vector containing no sublists |

x_List/;VectorQ[x,NumberQ] | a vector of numbers |

x:{___List} or x:{{___}...} | a list of lists |

x_List/;MatrixQ[x] | a matrix containing no sublists |

x_List/;MatrixQ[x,NumberQ] | a matrix of numbers |

x:{{_,_}...} | a list of pairs |

Some typical patterns for lists.

This defines a function whose argument must be a list containing lists with either one or two elements.

The definition applies in the second and third cases.

Out[10]= | |