3
$\begingroup$

I have the following code, and have been asked to interpret it:

Cases[{1, 3.1, 2/3, x, 3 + I, "Yellow"}, _Integer | _Rational | _Real]

I tried to play around the code, but it seems that it only gives one answer per pattern. For instance, if replaced by:

    Cases[{1, 3.1, 2/3, x, 3 + I, "Yellow"}, _Real]

The answer will be 3.1 instead of 1!

Could you please explain the types of patterns in Mathematica?

$\endgroup$
  • 4
    $\begingroup$ inspect Head /@ {1, 3.1, 2/3, x, 3 + I, "Yellow"} and see Real >> Details, Rational >> Details and Integer >> Details in the docs. $\endgroup$ – kglr Oct 23 '19 at 2:07
  • $\begingroup$ The element 1 has the head Integer. $\endgroup$ – Sâu Oct 23 '19 at 11:34
  • 1
    $\begingroup$ The two examples and the remark "one answer per pattern" suggest to me that the confusion is also with Alternatives[] (shorthand: |). $\endgroup$ – Michael E2 Oct 23 '19 at 12:06
5
$\begingroup$

Each of the elements in the list only has a single set catogerisation ('Head'). As kglr says, using

Head /@ {1, 3.1, 2/3, x, 3 + I, "Yellow"}

Will show what each of these elements are catogerised as. In this case it is {Integer, Real, Rational, Symbol, Complex, String}, respectively.

Head:

  • Integer is only used for... well... integers.

  • Real is being used for floating point numbers.

  • Rational is being used for rational numbers being expressed as a
    fraction without a floating point in and that is not equivalent to an integer.

  • Symbol is generally speaking, things that appear in blue in the
    workbook. They start with letters and are followed by letters and numbers. Not inside of speech marks.

  • Complex is any mix of real (or the subsets of real) mixed with an imaginary number. Here it is reading 'I' as the imaginary unit.

  • String is any sequence in speech marks, "Like so".

Essentially, the reason it is not working for you, is because you and Mathematica are using different definitions of real (and rational).

Mathematica is using real to mean the set of real numbers excluding integers and numbers expressed as fractions without a floating point in, for example. Whereas I assume you are looking for the full definition of real numbers which would include these subsets. The fact that it is returning only 1 element exactly for each of those patterns is just a coincidence that that list happens to have 1 of each Head listed in it.

Ways round it:

  • Use kglr's head code above, to show all the heads for each thing in your list, and just manually sort them.
  • If you're just interested in Real's, you can do it with a single pattern search, by inserting floating points in the integers and rationals e.g.

    Cases[{1., 3.1, 2./3, x, 3 + I, "Yellow"}, _Rational | _Real]

  • Easiest is probably what you've shown in the question though and just search for Integer or Real or Rational.

As an aside, you can also use tests such as:

IntegerQ[3.1]

Hopefully this provides some clarity on the patterns, as requested. If there is a specific pattern condition you need, let me know and I can try to help putting something together with you if you would like. And welcome to Mathematica Stack Exchange!

$\endgroup$
  • 1
    $\begingroup$ I don't think your "dual condition" _Integer && _Real works the way you imply. Consider Cases[{1 && 3.5, 2. && 3.5, 3 && 4}, _Integer && _Real]. Of course, 1 && 3.5 is a sort of nonsense expression, since the arguments to And[] do not represent boolean expressions; but that is the sort of expression that is matched. $\endgroup$ – Michael E2 Oct 23 '19 at 12:11
  • $\begingroup$ Indeed, you are correct. I shall edit the answer to reflect this. $\endgroup$ – Epideme Oct 23 '19 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.