{"id":2283,"date":"2010-02-17T18:34:27","date_gmt":"2010-02-17T17:34:27","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2283"},"modified":"2010-02-17T18:34:27","modified_gmt":"2010-02-17T17:34:27","slug":"integrating-absx-with-mathematica","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2283","title":{"rendered":"Integrating Abs(x) with Mathematica"},"content":{"rendered":"<p>Someone recently emailed me to say that they thought Mathematica sucked because it couldn&#8217;t integrate abs(x) where abs stands for absolute value.\u00a0 The result he was expecting was<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/walkingrandomly.com\/wp-content\/ql-cache\/quicklatex.com-a2de209a885e5a86eea64ff59db72274_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#105;&#110;&#116;&#124;&#120;&#124;&#32;&#92;&#44;&#100;&#120;&#32;&#61;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#124;&#120;&#124;&#120;&#125;&#123;&#50;&#125;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"25\" width=\"107\" style=\"vertical-align: -6px;\"\/><\/p>\n<p style=\"text-align: left;\">When you try to do this in Mathematica 7.0.1, it appears that it simply can&#8217;t do it. The command<\/p>\n<pre>Integrate[Abs[x], x]<\/pre>\n<p>just returns the integral unevaluated<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/integrate_abs_mathematica.png\" alt=\"unevaluated Integral of abs(x)\" \/><br \/>\nI&#8217;ve come across this issue before and many people assume that Mathematica is just stupid&#8230;after all it appears that it can&#8217;t even do an integral expected of a high school student.  Well, the issue is that Mathematica is not a high school student and it assumes that x is a complex variable.  For complex x, this indefinite integral doesn&#8217;t have a solution!<\/p>\n<p>So, let&#8217;s tell Mathematica that x is real<\/p>\n<pre>Integrate[Abs[x], x, Assumptions :&gt; Element[x, Reals]]<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/integrate_abs_mathematica1.png\" alt=\"Evaluated Integral of abs(x)\" \/><br \/>\nwhich is Mathematica&#8217;s way of saying that the answer is -x^2\/2 for x&lt;=0 and x^2\/2 otherwise, i.e. when x&gt;0.  It&#8217;s not quite in the form we were originally expecting but a moments thought should convince you that they are the same thing.<\/p>\n<p>Interestingly, it seems that Wolfram Alpha guesses that you probably mean real x since it just <a href=\"http:\/\/www.wolframalpha.com\/input\/?i=integrate+abs%28x%29\">evaluates the integral of abs(x) directly<\/a>.  It does, however, give the result in yet another form: in terms of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sign_function\">signum function<\/a>, sgn(x):<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica7\/integrate_abs_mathematica2.png\" alt=\"Wolfram Alpha Integral of abs(x)\" \/><\/p>\n<p>A couple of weeks ago I am pretty sure that Wolfram Alpha gave exactly the same result as Mathematica 7.0.1 so I wonder if they have quietly upgraded the back-end Kernel of Wolfram Alpha.\u00a0 Perhaps this is how Mathematica version 8 will evaluate this result?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone recently emailed me to say that they thought Mathematica sucked because it couldn&#8217;t integrate abs(x) where abs stands for absolute value.\u00a0 The result he was expecting was When you try to do this in Mathematica 7.0.1, it appears that it simply can&#8217;t do it. The command Integrate[Abs[x], x] just returns the integral unevaluated I&#8217;ve [&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":[6,4,8,33],"tags":[],"class_list":["post-2283","post","type-post","status-publish","format-standard","hentry","category-general-math","category-math-software","category-mathematica","category-wolfram-alpha"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-AP","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2283","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=2283"}],"version-history":[{"count":24,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2283\/revisions"}],"predecessor-version":[{"id":2347,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2283\/revisions\/2347"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}