{"id":6162,"date":"2016-05-04T12:22:56","date_gmt":"2016-05-04T16:22:56","guid":{"rendered":"http:\/\/blog.originlab.com\/?p=6162"},"modified":"2016-12-08T17:36:02","modified_gmt":"2016-12-08T22:36:02","slug":"creating-ulam-spirals-with-originc-part-i","status":"publish","type":"post","link":"https:\/\/blog.originlab.com\/ja\/creating-ulam-spirals-with-originc-part-i","title":{"rendered":"Creating Ulam Spirals with Origin C &#8211; Part I"},"content":{"rendered":"<p>I would like to thank my colleagues Eric Parent &amp; Zheng Shao for writing the Origin C scripts that made this post\u00a0possible.<\/p>\n<hr \/>\n<p>In the middle of a long scientific presentation in 1963, renowned mathematician and inventor <a href=\"https:\/\/en.wikipedia.org\/wiki\/Stanislaw_Ulam\">Stanislaw Ulam<\/a> (pronounced oo-lam) was doodling on a pad of paper and began listing integers starting with 1 in a counterclockwise spiral. Like so-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6214\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept.png\" alt=\"ulam_concept\" width=\"233\" height=\"221\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept.png 500w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept-300x284.png 300w\" sizes=\"auto, (max-width: 233px) 100vw, 233px\" \/><\/p>\n<p>As he drew this spiral he began circling the prime numbers, only to notice this-<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6217\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_2.png\" alt=\"ulam_concept_2\" width=\"239\" height=\"227\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_2.png 500w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_2-300x284.png 300w\" sizes=\"auto, (max-width: 239px) 100vw, 239px\" \/><\/a><\/p>\n<p>There seemed to be &#8220;lines&#8221; of prime numbers going outward in a distinct pattern. Primes\u00a0which,\u00a0by definition, are indivisible by any number but 1 and themselves, may seem to occur at random\u00a0intervals at first, but there is in fact a certain logic behind this chaos, as we can see in the grid below.<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_003.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6216\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_003.png\" alt=\"ulam_003\" width=\"407\" height=\"395\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_003.png 625w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_003-300x291.png 300w\" sizes=\"auto, (max-width: 407px) 100vw, 407px\" \/><\/a><br \/>\nUlam spirals are largely mathematical art, however they have shown promise for illustrating not only the general behaviors of <a href=\"http:\/\/www.numbersaplenty.com\/set\/Ulam_number\/\">various<\/a>\u00a0<a href=\"http:\/\/www.numbersaplenty.com\/set\/economical_number\/\">number<\/a> <a href=\"http:\/\/www.numbersaplenty.com\/set\/Ulam_number\/\">series<\/a>, but also an older <a href=\"https:\/\/en.wikipedia.org\/wiki\/Ulam_spiral#Hardy_and_Littlewood.27s_Conjecture_F\">mathematic conjecture that may explain the frequency\u00a0of prime numbers<\/a>. In this blog, I&#8217;ll be showing you how a matrix of any size can be combined with a C script in Origin to generate a Ulam spiral. This spiral can be used to illustrate primes as well as any other number series expressed in OriginC.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6303\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_concept_014.png\" alt=\"ulam_concept_014\" width=\"117\" height=\"25\" \/><\/p>\n<p>Let&#8217;s begin by opening up the Code Builder and creating\u00a0a new C++ file, which can be saved\u00a0in either your User Files (by default) or anywhere you have user permissions.<\/p>\n<p>One of our\u00a0initial challenges is finding a way to place 1 in the center\u00a0of a\u00a0matrix of equal width and height, and count outward until it reaches the value of\u00a0the total number of cells in the entire grid- regardless of the matrix&#8217;s size. Pasting\u00a0the following code into our C++ file will generate this spiral.\u00a0To understand what each section does, you can follow along with the\u00a0<span style=\"color: green;\">\/\/comments<\/span> in the script below.<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;Origin.h&gt;\r\n#include &lt;GetNBox.h&gt;\r\n\r\n\r\n\/\/----------------------------------------------------------------------------\r\n\/\/ CSpiral\r\n\/\/ The base class that does all the complicated stuff.\r\n\/\/ By itself it will do an Index spiral.\r\n\/\/ All other spirals should be derived from CSpiral.\r\n\/\/----------------------------------------------------------------------------\r\nclass CSpiral\r\n{\r\npublic:\r\n CSpiral()\r\n {\r\n }\r\n\r\n ~CSpiral()\r\n {\r\n }\r\n\r\n\/\/ Creates the CSpiral class to be called on by Prime and other related functions.\r\n\r\n int Run()\r\n {\r\n if( AttachActive() )\r\n run();\r\n return 0;\r\n }\r\n\r\n virtual int Equation()\r\n {\r\n m_M[m_row][m_col] = m_index;\r\n return 0;\r\n }\r\n\r\n\/\/ This takes the product of the rows and columns of the matrix and creates integers based on them\r\n\r\nprotected:\r\n Matrix&lt;double&gt; m_M;\r\n int m_numRows, m_numCols;\r\n int m_index, m_row, m_col;\r\n\r\n BOOL AttachActive()\r\n {\r\n MatrixLayer ml = Project.ActiveLayer();\r\n if( !ml.IsValid() )\r\n return FALSE;\r\n return m_M.Attach(ml);\r\n }\r\n\r\n\/\/ This confirms that a matrix is present in which these values may be placed\r\n\r\n int run()\r\n {\r\n int left, right, top, bottom;\r\n\r\n run_init();\r\n \r\n left = right = m_col;\r\n top = bottom = m_row;\r\n\r\n while( true )\r\n {\r\n while( m_col &lt;= right )\r\n {\r\n Equation();\r\n m_col++;\r\n m_index++;\r\n }\r\n if( m_col == m_numCols )\r\n break;\r\n right++;\r\n\r\n while( m_row &gt;= top )\r\n {\r\n Equation();\r\n m_row--;\r\n m_index++;\r\n }\r\n if( m_row &lt; 0 )\r\n break;\r\n top--;\r\n\r\n while( m_col &gt;= left )\r\n {\r\n Equation();\r\n m_col--;\r\n m_index++;\r\n }\r\n if( m_col &lt; 0 )\r\n break;\r\n left--;\r\n\r\n while( m_row &lt;= bottom )\r\n {\r\n Equation();\r\n m_row++;\r\n m_index++;\r\n }\r\n if( m_row == m_numRows )\r\n break;\r\n bottom++;\r\n }\r\n return 0;\r\n }\r\n\r\n void run_init()\r\n {\r\n m_numRows = m_M.GetNumRows();\r\n m_numCols = m_M.GetNumCols();\r\n if( m_numRows != m_numCols )\r\n {\r\n if( m_numRows &lt; m_numCols )\r\n m_numCols = m_numRows;\r\n else\r\n m_numRows = m_numCols;\r\n }\r\n\r\n m_row = m_numRows &gt;&gt; 1;\r\n m_col = m_numCols &gt;&gt; 1;\r\n if( (m_numCols &amp; 1) == 0 )\r\n m_col--;\r\n \r\n m_index = 1;\r\n }\r\n};\r\n\r\n\/\/This last section determines the placement of these values relative to aspect ratio of the matrix\r\n\/\/if the matrix is not square then the spiral will form in the largest square that can be drawn\r\n\/\/and will otherwise return the missing number sign (--).\r\n\r\n<\/pre>\n<p>This code generates the spiral, however we need a way to call this as a function from Origin&#8217;s\u00a0script window. To do this, in Code Builder we&#8217;ll add the following lines to the end of our script-<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/----------------------------------------------------------------------------\r\n\/\/ UlamSpiral\r\n\/\/ The main function for calling the different types of spirals.\r\n\/\/----------------------------------------------------------------------------\r\n\r\nenum {\r\n UlamSpiral_Help = 0,\r\n UlamSpiral_Index,\r\n UlamSpiral_Unknown\r\n};\r\n\r\n\/\/ This creates an index allowing you to call the UlamSpiral help with UlamSpiral(0), generate\r\n\/\/ the Ulam Spiral itself with UlamSpiral(1), or return a null \"unknown\" error for \r\n\/\/ any other other value.\r\n\r\nint UlamSpiral(int type = 0)\r\n{\r\n switch( type )\r\n {\r\n case UlamSpiral_Help:\r\n printf(\"%d = Help\\n\", UlamSpiral_Help);\r\n printf(\"%d = Index\\n\", UlamSpiral_Index);\r\n break;\r\n case UlamSpiral_Index:\r\n {\r\n CSpiral spiral;\r\n spiral.Run();\r\n }\r\n break;\r\n }\r\n return 0;\r\n}\r\n\r\n\/\/ This section generates \"help\" for option 0, listing the 3 options, \r\n\/\/ runs the CSpiral function on our previous script for option 1,\r\n\/\/ and returns a text break for any other value representing an \"unknown\" function\r\n<\/pre>\n<p>To use this in Origin, the next thing\u00a0we need to do is right-click on the User [Autoload] file or another &#8220;User files&#8221; folder in the\u00a0Codebuilder\u00a0workspace window \u00a0and select &#8220;Add Files&#8230;&#8221;. Locate the .cpp file in its folder, and click okay. Then select your file in the workspace, and go to Build&gt;Build (F8). This will compile your code and should generate a working function that can be executed from Origin&#8217;s script window.<\/p>\n<p>To test this function create a new matrix window, and then open the Script Window. In the script window type UlamSpiral(1), and you should see that the center cell is populated with 1 with values increasing by with each cell in a counter-clockwise order. If this didn&#8217;t work you may want to either copy\/paste the script above or go back to the original file included in this post&#8217;s ZIP folder.<\/p>\n<p><a href=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_002.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6212\" src=\"http:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_002.png\" alt=\"ulam_002\" width=\"703\" height=\"514\" srcset=\"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_002.png 1028w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_002-300x219.png 300w, https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_002-1024x749.png 1024w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/a><\/p>\n<p>This is how you would generate the base values for a Ulam Spiral. In <a href=\"http:\/\/blog.originlab.com\/graphing\/creating-ulam-spirals-with-originc-part-ii\">another post<\/a>, we&#8217;ll be covering how to set it to display only primes\u00a0as well as odd\/even numbers or numbers divisible by different integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would like to thank my colleagues Eric Parent &amp; &hellip; <\/p>","protected":false},"author":1070,"featured_media":6216,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_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","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[5],"tags":[],"class_list":["post-6162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-oc"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7DvU2-1Bo","jetpack_likes_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.originlab.com\/wp-content\/uploads\/2016\/04\/ulam_003.png","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/users\/1070"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/comments?post=6162"}],"version-history":[{"count":42,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6162\/revisions"}],"predecessor-version":[{"id":7435,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/posts\/6162\/revisions\/7435"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media\/6216"}],"wp:attachment":[{"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/media?parent=6162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/categories?post=6162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.originlab.com\/ja\/wp-json\/wp\/v2\/tags?post=6162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}