{"id":99,"date":"2009-06-02T17:49:32","date_gmt":"2009-06-02T22:49:32","guid":{"rendered":"http:\/\/stoney.sb.org\/wordpress\/?p=99"},"modified":"2021-01-24T14:47:02","modified_gmt":"2021-01-24T19:47:02","slug":"converting-symbolic-mathematica-expressions-to-c-code","status":"publish","type":"post","link":"https:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/","title":{"rendered":"Converting symbolic Mathematica expressions to C code"},"content":{"rendered":"<p>I frequently use <a href=\"http:\/\/www.wolfram.com\/products\/mathematica\/index.html\">Mathematica<\/a> to rearrange or solve symbolic equations to use in C++ programs. While Mathematica is quite powerful for that, it has no facility to hoist common subexpressions into variables.<\/p>\n<p><!--more--><\/p>\n<p>After spending some time manually rearranging a closed-form solution to a system of equations, it seemed worthwhile to find a way to hoist common subexpressions into variables. I didn&#8217;t find anything useful via Google, as the results were swamped by references to the common compiler &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Common_subexpression_elimination\">common subexpression elimination<\/a>&#8221; optimization. It looked like it would require <a href=\"http:\/\/en.wikipedia.org\/wiki\/Dataflow_analysis\">data-flow analysis<\/a> to do this, which would be tedious to write and debug.<\/p>\n<p>Some thought and experimentation suggested that it wouldn&#8217;t be all that hard to do a decent job using a straight-forward pattern matching and replacement (especially since I didn&#8217;t care how fast this ran), so I created this <a href=\"http:\/\/stoney.sb.org\/wordpress\/wp-content\/convertingtocwithcse.zip\" title=\"ConvertingToCwithCSE.zip\">Mathematica 7 notebook<\/a> to do just that. The code turned out to be concise, which means &#8220;unreadable&#8221; in Mathematica. I try to stick to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Functional_programming\">functional style<\/a> where I can, which is nice mental exercise, but can be hard to read. In the main function, I opted for iteration rather than a purely functional recursion just because it seemed clearer.<\/p>\n<p>It&#8217;s always interesting to work on a problem like this in Mathematica. The language is very expressive, but often fails at the &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Principle_of_least_astonishment\">Principle of Least Surprise<\/a>&#8220;. In this case, I kept being tripped up by Mathematica matching non-existant arithmetic functions (Plus, Times, etc.) everywhere in an expression, in an overly-helpful attempt to match arithmetic patterns regardless of commutative or associative expression rearrangements. For future reference, it turns out that using HoldPattern will prevent that behavior.<\/p>\n<p>I created a web page from the Mathematica notebook, but there doesn&#8217;t appear to be any way to do that and produce text, rather than pictures of the code. Oh well. <a href=\"\/wordpress\/wp-content\/cvt2c\/\" target=\"_blank\" rel=\"noopener\">Here it is<\/a> anyway. If you want to see this notebook properly and don&#8217;t have Mathematica 7, you can use the free <a href=\"https:\/\/www.wolfram.com\/player\/\">Mathematica Player<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I frequently use Mathematica to rearrange or solve symbolic equations to use in C++ programs. While Mathematica is quite powerful for that, it has no facility to hoist common subexpressions into variables.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[13,20,14],"tags":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Converting symbolic Mathematica expressions to C code - Stoney&#039;s Zone<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Converting symbolic Mathematica expressions to C code - Stoney&#039;s Zone\" \/>\n<meta property=\"og:description\" content=\"I frequently use Mathematica to rearrange or solve symbolic equations to use in C++ programs. While Mathematica is quite powerful for that, it has no facility to hoist common subexpressions into variables.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Stoney&#039;s Zone\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-02T22:49:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-24T19:47:02+00:00\" \/>\n<meta name=\"author\" content=\"stoney\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"stoney\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\",\"url\":\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\",\"name\":\"Converting symbolic Mathematica expressions to C code - Stoney's Zone\",\"isPartOf\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#website\"},\"datePublished\":\"2009-06-02T22:49:32+00:00\",\"dateModified\":\"2021-01-24T19:47:02+00:00\",\"author\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1\"},\"breadcrumb\":{\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/stoney.sb.org\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Converting symbolic Mathematica expressions to C code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#website\",\"url\":\"https:\/\/stoney.sb.org\/wordpress\/\",\"name\":\"Stoney's Zone\",\"description\":\"Thoughts about stuff\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stoney.sb.org\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1\",\"name\":\"stoney\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg\",\"caption\":\"stoney\"},\"sameAs\":[\"http:\/\/\"],\"url\":\"https:\/\/stoney.sb.org\/wordpress\/author\/stoney\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Converting symbolic Mathematica expressions to C code - Stoney's Zone","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/","og_locale":"en_US","og_type":"article","og_title":"Converting symbolic Mathematica expressions to C code - Stoney's Zone","og_description":"I frequently use Mathematica to rearrange or solve symbolic equations to use in C++ programs. While Mathematica is quite powerful for that, it has no facility to hoist common subexpressions into variables.","og_url":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/","og_site_name":"Stoney's Zone","article_published_time":"2009-06-02T22:49:32+00:00","article_modified_time":"2021-01-24T19:47:02+00:00","author":"stoney","twitter_card":"summary_large_image","twitter_misc":{"Written by":"stoney","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/","url":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/","name":"Converting symbolic Mathematica expressions to C code - Stoney's Zone","isPartOf":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#website"},"datePublished":"2009-06-02T22:49:32+00:00","dateModified":"2021-01-24T19:47:02+00:00","author":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1"},"breadcrumb":{"@id":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/stoney.sb.org\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Converting symbolic Mathematica expressions to C code"}]},{"@type":"WebSite","@id":"https:\/\/stoney.sb.org\/wordpress\/#website","url":"https:\/\/stoney.sb.org\/wordpress\/","name":"Stoney's Zone","description":"Thoughts about stuff","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stoney.sb.org\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1","name":"stoney","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/47ff741e5568d1283ae1d29cfa4248fa?s=96&d=mm&r=pg","caption":"stoney"},"sameAs":["http:\/\/"],"url":"https:\/\/stoney.sb.org\/wordpress\/author\/stoney\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p76dm1-1B","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":4,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/99\/revisions\/169"}],"wp:attachment":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}