{"id":1659,"date":"2009-08-30T13:11:00","date_gmt":"2009-08-30T12:11:00","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=1659"},"modified":"2009-08-30T13:11:00","modified_gmt":"2009-08-30T12:11:00","slug":"rationalq-testing-for-rationals-in-mathematica","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=1659","title":{"rendered":"RationalQ &#8211; Testing for rationals in Mathematica"},"content":{"rendered":"<p><a href=\"http:\/\/www.wolfram.com\/\">Mathematica<\/a> has a large set of functions that you can use to test the properties of numbers.\u00a0 For example<\/p>\n<p><strong>IntegerQ[x]<\/strong><\/p>\n<p>returns True if x is an integer and False if it isn&#8217;t.\u00a0 Of course you are not just restricted to asking if x is an integer or not.\u00a0 For example, you can ask if it is an even number<\/p>\n<p><strong>EvenQ[x]<\/strong><\/p>\n<p>or an odd number<\/p>\n<p><strong>OddQ[x]<\/strong><\/p>\n<p>Perhaps you are wondering if x is prime<\/p>\n<p><strong>PrimeQ[x]<\/strong><\/p>\n<p>or even if it is an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Algebraic_integer\">algebraic integer<\/a><\/p>\n<p><strong>AlgebraicIntegerQ[x]<\/strong><\/p>\n<p>The observant reader will notice that all of these functions end with a capital Q and a way of remembering this is to think that you are asking a question of the variable x.\u00a0 So the question &#8216;<em><strong>Is x an integer<\/strong><\/em>&#8216; becomes, in Mathematica notation, <strong>IntegerQ[x]<\/strong>.<\/p>\n<p>I am currently working on a piece of code where I need to determine whether or not a particular number belongs to the<a href=\"http:\/\/en.wikipedia.org\/wiki\/Rational_number\"> set of rationals<\/a> and I assumed that a suitable function would exist in Mathematica and that it would be called <strong>RationalQ[]<\/strong> so I was rather surprised to see that there is no such function in Mathematica 7.<\/p>\n<p>So, I&#8217;ll just have to come up with my own.\u00a0 A quick search resulted in the following function definition from Bob Hanlon<\/p>\n<p><strong>RationalQ[x_] := (Head[x] === Rational)<\/strong><\/p>\n<p>Which almost does what I need.\u00a0 It handles the following correctly<\/p>\n<p><strong>RationalQ[1\/2] <\/strong>(gives True)<\/p>\n<p><strong>RationalQ[Sqrt[2]]<\/strong> (Gives False)<\/p>\n<p>but I needed a version of RationalQ that also returned True when passed an integer.\u00a0 After all, the integers are just a subset of the rationals.\u00a0 A moments thought resulted in<\/p>\n<p><strong>RationalQ[x_] := (Head[x] === Rational || IntegerQ[x]);<\/strong><\/p>\n<p>Which seems to work perfectly.\u00a0 So, I offer the above function for anyone who is googling for a RationalQ function and I also ask the following questions to any Mathematica gurus who might be reading this<\/p>\n<ul>\n<li>Is there anything wrong with the above definition?<\/li>\n<li>Why isn&#8217;t such an obvious function not included in Mathematica as standard?<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Mathematica has a large set of functions that you can use to test the properties of numbers.\u00a0 For example IntegerQ[x] returns True if x is an integer and False if it isn&#8217;t.\u00a0 Of course you are not just restricted to asking if x is an integer or not.\u00a0 For example, you can ask if it [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8,21],"tags":[],"class_list":["post-1659","post","type-post","status-publish","format-standard","hentry","category-mathematica","category-problem-of-the-week"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-qL","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1659"}],"version-history":[{"count":2,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1659\/revisions"}],"predecessor-version":[{"id":1661,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1659\/revisions\/1661"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}