{"id":102,"date":"2009-06-03T18:10:05","date_gmt":"2009-06-03T23:10:05","guid":{"rendered":"http:\/\/stoney.sb.org\/wordpress\/?p=102"},"modified":"2016-02-28T12:13:58","modified_gmt":"2016-02-28T17:13:58","slug":"defining-new-format-wrappers-in-mathematica","status":"publish","type":"post","link":"https:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/","title":{"rendered":"Defining new format wrappers in Mathematica"},"content":{"rendered":"<p>One problem I had with the <a href=\"http:\/\/stoney.sb.org\/wordpress\/converting-symbolic-mathematica-expressions-to-c-code\/\">expression-to-C converter<\/a> is that CForm converts negations of terms into a negation of parenthesized terms.<\/p>\n<p><!--more--><\/p>\n<p>For example:<\/p>\n<p>In[1]:= <b>-a b \/\/ CForm<\/b><\/p>\n<p>Out[1]\/\/CForm= -(a*b)<\/p>\n<p>That looks ugly. I tried to define a new format for Times[-1, v], but this requires unprotecting Times, and led to an infinite recursion when I tried to use it. I finally found a suggestion on how to define whole new format wrappers on the comp.soft-sys.math.mathematica list, in a message posted by Carl Woll (a Wolfram employee). This is how it works:<\/p>\n<p>This assignment marks CCForm (my new formatter) as a format wrapper.<\/p>\n<p>In[2]:= <b>Format[CCForm[expr_], CCForm] := expr<\/b><\/p>\n<p>Note that the Out[3] text shows the new formatter name.<\/p>\n<p>In[3]:= <b>2 + 3 \/\/ CCForm<\/b><\/p>\n<p>Out[3]\/\/CCForm= CCForm[5]<\/p>\n<p>It&#8217;s showing the CCForm wrapper, though, which we don&#8217;t want to look at, so we define a top-level format for CCForm that replaces it with its argument.<\/p>\n<p>In[4]:= <b>Format[CCForm[expr_]] := expr<\/b><\/p>\n<p>Now, the output looks correct.<\/p>\n<p>In[5]:= <b>2 + 3 \/\/ CCForm<\/b><\/p>\n<p>Out[5]\/\/CCForm= 5<\/p>\n<p>The case I&#8217;m trying to fix isn&#8217;t helped by this. We need to make it produce the CForm.<\/p>\n<p>In[6]:= <b>-a b \/\/ CCForm<\/b><\/p>\n<p>Out[6]\/\/CCForm= -a b<\/p>\n<p>In[7]:= <b>Format[CCForm[expr_]] := CForm[expr]<\/b><\/p>\n<p>In[8]:= <b>-a b \/\/ CCForm<\/b><\/p>\n<p>Out[8]\/\/CCForm= -(a*b)<\/p>\n<p>We don&#8217;t want the result to be evaluated, so we add a HoldForm.<\/p>\n<p>In[9]:= <b>Format[CCForm[expr_]] := HoldForm[CForm[expr]]<\/b><\/p>\n<p>In[10]:= <b>-a b \/\/ CCForm<\/b><\/p>\n<p>Out[10]\/\/CCForm= -(a*b)<\/p>\n<p>Now it&#8217;s in CForm, but we still have the parenthesis. I don&#8217;t understand just why this works, but making CCForm HoldAll fixes the problem.<\/p>\n<p>In[11]:= <b>SetAttributes[CCForm, HoldAll]<\/b><\/p>\n<p>In[12]:= <b>-a b \/\/ CCForm<\/b><\/p>\n<p>Out[12]\/\/CCForm= -a * b<\/p>\n<p>This also prevents evaluation of the expression being formatted.<\/p>\n<p>In[13]:= <b>2 + 3 \/\/ CCForm<\/b><\/p>\n<p>Out[13]\/\/CCForm= 2 + 3<\/p>\n<p>Note that CForm does what I want if it&#8217;s made HoldAll.<\/p>\n<p>In[14]:= <b>-a b \/\/ CForm<\/b><\/p>\n<p>Out[14]\/\/CForm= -(a*b)<\/p>\n<p>In[15]:= <b>SetAttributes[CForm, HoldAll]<\/b><\/p>\n<p>In[16]:= <b>-a b \/\/ CForm<\/b><\/p>\n<p>Out[16]\/\/CForm= -a*b<\/p>\n<p>Although it&#8217;s not styled as nicely as the CCForm version, and could mess up existing code, so we won&#8217;t do that.<\/p>\n<p>In[17]:= <b>ClearAttributes[CForm, HoldAll]<\/b><\/p>\n<p>Now I can use CCForm instead of CForm for the output formatting of my converter, and have nicer-looking results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One problem I had with the expression-to-C converter is that CForm converts negations of terms into a negation of parenthesized terms.<\/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>Defining new format wrappers in Mathematica - 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\/defining-new-format-wrappers-in-mathematica\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Defining new format wrappers in Mathematica - Stoney&#039;s Zone\" \/>\n<meta property=\"og:description\" content=\"One problem I had with the expression-to-C converter is that CForm converts negations of terms into a negation of parenthesized terms.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/\" \/>\n<meta property=\"og:site_name\" content=\"Stoney&#039;s Zone\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-03T23:10:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-02-28T17:13:58+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\/defining-new-format-wrappers-in-mathematica\/\",\"url\":\"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/\",\"name\":\"Defining new format wrappers in Mathematica - Stoney's Zone\",\"isPartOf\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#website\"},\"datePublished\":\"2009-06-03T23:10:05+00:00\",\"dateModified\":\"2016-02-28T17:13:58+00:00\",\"author\":{\"@id\":\"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1\"},\"breadcrumb\":{\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/stoney.sb.org\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Defining new format wrappers in Mathematica\"}]},{\"@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":"Defining new format wrappers in Mathematica - 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\/defining-new-format-wrappers-in-mathematica\/","og_locale":"en_US","og_type":"article","og_title":"Defining new format wrappers in Mathematica - Stoney's Zone","og_description":"One problem I had with the expression-to-C converter is that CForm converts negations of terms into a negation of parenthesized terms.","og_url":"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/","og_site_name":"Stoney's Zone","article_published_time":"2009-06-03T23:10:05+00:00","article_modified_time":"2016-02-28T17:13:58+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\/defining-new-format-wrappers-in-mathematica\/","url":"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/","name":"Defining new format wrappers in Mathematica - Stoney's Zone","isPartOf":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#website"},"datePublished":"2009-06-03T23:10:05+00:00","dateModified":"2016-02-28T17:13:58+00:00","author":{"@id":"https:\/\/stoney.sb.org\/wordpress\/#\/schema\/person\/d587bd16a4b79558c4f6e01c078fbdb1"},"breadcrumb":{"@id":"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/stoney.sb.org\/wordpress\/defining-new-format-wrappers-in-mathematica\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/stoney.sb.org\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Defining new format wrappers in Mathematica"}]},{"@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-1E","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/102"}],"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=102"}],"version-history":[{"count":3,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":155,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/posts\/102\/revisions\/155"}],"wp:attachment":[{"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stoney.sb.org\/wordpress\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}