Cases

Consider the list of (x,y,z) coordinates below.

Clear[data, x] ; data = {           {12, 2, 4}, {3 ...  {2, 8, 5}, {7, 13, 12}, {3, 22, 5}, {11, 2, 7}, {10, 0, 14}, {6, 2, 23}, {8, 9, 4}, {6, 3, 7}} ;

The line below picks out all elements of data where the second of three in a list is greater than 7.

Cases[data, {x_, y_, z_}/;y>7]

{{2, 8, 5}, {7, 13, 12}, {3, 22, 5}, {8, 9, 4}}

Now the next line will pick the points where y>7 and return the list {x, y, (x^2 + y^2)^(1/2)}.

Cases[data, {x_, y_, z_}/;y>7 {x, y, (x^2 + y^2)^(1/2)}]

{{2, 8, 2 17^(1/2)}, {7, 13, 218^(1/2)}, {3, 22, 493^(1/2)}, {8, 9, 145^(1/2)}}

The method above works as long as (x) doesn't have a global value.  In the cell below (x) has a global value and one doesn't get the expected result.

x = π/4 ; Cases[data, {x_, y_, z_}/;y>7 {x, y, (x^2 + y^2)^(1/2)}]

{{π/4, 8, (64 + π^2/16)^(1/2)}, {π/4, 13, (169 + π^2/16)^(1/2)}, {π/4, 22, (484 + π^2/16)^(1/2)}, {π/4, 9, (81 + π^2/16)^(1/2)}}

The problem above can be avoided by using the line below. The difference is the use of (expr :→ x) instead of (expr → x).

Cases[data, {x_, y_, z_}/;y>7 {x, y, (x^2 + y^2)^(1/2)}]

{{2, 8, 2 17^(1/2)}, {7, 13, 218^(1/2)}, {3, 22, 493^(1/2)}, {8, 9, 145^(1/2)}}

Notice (x) still has a global variable.

x

π/4

In the previous examples involving Cases[data,{x_,y_,z_}....] all occurrences that met the pattern were returned.  Cases can take a fourth argument which indicates the maximum number of elements it should return.  In the next line the fourth argument is 1 so Cases only returns {x, y, (x^2 + y^2)^(1/2)} for the first element where (y>7).  Notice that a third argument was necessary since we wanted to use the fourth argument.  In this example the third argument provided is the default level specification (1).

Cases[data, {x_, y_, z_}/;y>7 {x, y, (x^2 + y^2)^(1/2)}, 1, 1]

{{2, 8, 2 17^(1/2)}}

In the previous examples Cases had only two arguments.  Cases can take a level specification as a third argument.  The default level specification is 1 or {1} (both are equivalent).  In the next line the default level specification is used, and Cases doesn't look inside each term of the sum.

Clear[a, w, y, u, z] ; Cases[a + E^(-x^2) + Exp[y^2] + w^5^x + u^2 + 1/z^2 + 1/4, x_^y_]

{^(-π^2/16), ^y^2, u^2, w^5^(π/4), 1/z^2}

The next example is from the online Help Browser that comes with Mathematica.  Here the level specification is {1,∞} and Cases looks at every level (except level 0).  As a result the list returned by Cases includes  x^2, y^2 or  5^x .

Cases[a + E^(-x^2) + Exp[y^2] + w^5^x + u^2 + 1/z^2 + 1/4, x_^y_, {1, ∞}]

{π^2, ^(-π^2/16), y^2, ^y^2, u^2, 5^(π/4), w^5^(π/4), 1/z^2}

I don't provide further examples, but any pattern matching constructs can be used in Cases. Nuances of pattern matching are discussed in another section.

Heads Option

Cases has a Heads option with the default setting (Heads→False). This default setting almost always gives the desired effect.  In the next cell Derivative[_] is only used as a head and gives an example where the setting (Heads→True) is needed to find the use of Derivative.

ClearAll[f, x] ; Cases[f[x] + 2f '[x] - 3f''[x] + 4g '[x], Derivative[_], {0, -1}, HeadsTrue]

{Derivative[1], Derivative[1], Derivative[2]}


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page