{"id":7291,"date":"2015-03-14T00:01:00","date_gmt":"2015-03-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/14\/weekend-scripter-approximate-the-value-of-pi-from-a-polygon\/"},"modified":"2019-02-18T10:30:17","modified_gmt":"2019-02-18T17:30:17","slug":"weekend-scripter-approximate-the-value-of-pi-from-a-polygon","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-approximate-the-value-of-pi-from-a-polygon\/","title":{"rendered":"Weekend Scripter: Approximate the Value of Pi from a Polygon"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft senior software engineer, Firaz Samet, shows how to use Windows PowerShell to approximate the value of pi from a polygon.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog post from Firaz Samet in honor of Pi (&pi;) Day. I will turn the blog over&hellip;<\/p>\n<p>The Scripting Guy ran across my PowerShell Math Module on CodePlex, and he asked me if I would be interested in writing a blog post for Pi Day. I was also incidentally thinking about Pi Day, so this came at the right time. I hope you enjoy reading it.<\/p>\n<p>First a little bit about me&#8230;<\/p>\n<p style=\"margin-left:30px\">I am a senior software engineer at Microsoft where I work on <a href=\"http:\/\/azure.microsoft.com\/en-us\/services\/stream-analytics\/\" target=\"_blank\">Azure Stream Analytics<\/a>. I have been playing around with Windows PowerShell since version 1.0 where I worked on SharePoint search management cmdlets. I love the power it provides by accessing the full .NET Framework and its rich set of built-in cmdlets and filtering capabilities. I concede that its syntax needed some time to get used to, but after I went past that, it became a natural way to solve my most common scripting and programming needs. One thing that has always frustrated me is that mathematical notation is a bit cumbersome. One reason I wrote a wrapper around Math.NET is to help ease interactive matrix manipulation.<\/p>\n<p>Pi Day is celebrated around the world on March 3 (3.14). It aims to raise interest in math and especially &pi;. This year&rsquo;s will be even more significant (3.1415&hellip;). For more information, see <a href=\"http:\/\/en.wikipedia.org\/wiki\/Pi_Day\" target=\"_blank\">Pi Day<\/a> on Wikipedia.<\/p>\n<p>In this post, I will talk about using Windows PowerShell to approximate &pi; with polygons. I will also introduce matrix calculus with Math.NET Numerics, using a thin Windows PowerShell wrapper called <a href=\"https:\/\/mathnetpowershell.codeplex.com\/\" target=\"_blank\">Math.NET PowerShell<\/a>.<\/p>\n<h2>Approximating &pi;<\/h2>\n<p>The approximation of &pi; that I introduce here starts with a square with sides that measure 2 units. It then approximates the area of the inscribed unit circle (radius is 1 unit, of which the area equals &pi;) by constructing a regular polygon with double the number of sides with each iteration. This is done by chopping off triangles from each corner area, while keeping the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Apothem\" title=\"Apothem\" target=\"_blank\">apothem<\/a> to 1.<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-14-15-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3-14-15-1.png\" alt=\"Image of shapes\" width=\"400\" height=\"91\" title=\"Image of shapes\" \/><\/a><\/p>\n<p>By using the triangle properties and the trigonometric <a href=\"http:\/\/en.wikipedia.org\/wiki\/List_of_trigonometric_identities#Double-angle.2C_triple-angle.2C_and_half-angle_formulae\" target=\"_blank\">half-angle formulae<\/a> for the tangent, we can deduce the following formula:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1731.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/1731.2.PNG\" alt=\"Image of equation\" width=\"450\" height=\"215\" title=\"Image of equation\" \/><\/a><\/p>\n<h2>Expressing the formula in Windows PowerShell<\/h2>\n<p>We now want to express the previous formula with Windows PowerShell. We will mainly use the [math] library&rsquo;s power method, <strong>Pow(x, y)<\/strong>. But to make the notation simpler, we will introduce a Windows PowerShell filter to make calling the <strong>Pow<\/strong> method closer to the mathematical notation:<\/p>\n<p style=\"margin-left:30px\">filter ^([double] $y){[math]::Pow($_,$y)}<\/p>\n<p>This means that expressing 2&sup3; would look like (2 |^ 3) instead of [math]::Pow(2, 3). Parentheses are still required to prevent the pipeline syntax from affecting the remainder of the code.&nbsp;<span style=\"font-size:12px\">Similarly,<\/span><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5305.3.PNG\" style=\"font-size:12px\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5305.3.PNG\" alt=\"Image of numbers\" width=\"20\" height=\"15\" title=\"Image of numbers\" \/><\/a><span style=\"font-size:12px\">&nbsp; will become (2 |^ .5).<\/span><\/p>\n<p>We now use Windows PowerShell to develop the formula:<\/p>\n<p style=\"margin-left:30px\">function halfTan($t){$t \/ (((($t |^ 2) + 1) |^ .5) + 1)}<\/p>\n<p style=\"margin-left:30px\">function triangleArea($t){2 * ($t |^ 3) \/ (-($t |^ 2) + 1)}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$t = 1<\/p>\n<p style=\"margin-left:30px\">$t = halfTan $t<\/p>\n<p style=\"margin-left:30px\">$a = ((2 |^ .5) &#8211; 1) |^ 2<\/p>\n<p style=\"margin-left:30px\">$p = 4<\/p>\n<p style=\"margin-left:30px\">0..25 | %{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $i = $_;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $p-= $a * 4 * (2 |^ $i);<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; &quot;{0} t: {1}&nbsp; a: {2}&nbsp; ~pi:{3}&quot; -f $i,$t,$a,$p;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $t = halfTan $t;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $a = triangleArea $t;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>On iteration 25, we reach the value of [math]::Pi.<\/p>\n<p style=\"margin-left:30px\">24 t: 2.34066892682746E-08&nbsp; a: 2.56477909373991E-23&nbsp; ~pi:<b>3.14159265358979<\/b><\/p>\n<h2>Matrix expression<\/h2>\n<p>The iterative nature of<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7103.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7103.4.PNG\" alt=\"Image of equation\" width=\"20\" height=\"15\" title=\"Image of equation\" \/><\/a>makes using matrices an interesting proposition. For this reason, we will express it in the following matrix formula:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3113.5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3113.5.PNG\" alt=\"Image of equation\" width=\"450\" height=\"53\" title=\"Image of equation\" \/><\/a><\/p>\n<p>We will then use a thin Windows PowerShell wrapper around Math.NET Numerics called <a href=\"https:\/\/mathnetpowershell.codeplex.com\/\" target=\"_blank\">Math.NET PowerShell<\/a> to calculate it in Windows PowerShell. The Windows PowerShell module offers a familiar way to initialize matrices (such as m &quot;1 0;0 1&quot; to initialize the identity matrix of order 2) and an easy approach to using the adequate Math.NET Numerics matrix type.<\/p>\n<p>By default, Math.NET PowerShell uses the most memory conservative matrix type (sparse matrix with single precision). But the precision of this type is too low for our needs. Therefore, we will select the double precision type. This is achieved by using the following command:<\/p>\n<p style=\"margin-left:30px\">Set-MathNetMatrixType Double<\/p>\n<p>When this is done, we express the formula using the previous matrix notation as follows:<\/p>\n<p style=\"margin-left:30px\">function halfTan($t){$t \/ (((($t |^ 2) + 1) |^ .5) + 1)}<\/p>\n<p style=\"margin-left:30px\">function triangleArea($t){2 * ($t |^ 3) \/ (-($t |^ 2) + 1)}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">$t = 1<\/p>\n<p style=\"margin-left:30px\">$t = halfTan $t;<\/p>\n<p style=\"margin-left:30px\">$p = m &quot;4 -4;0 0&quot;<\/p>\n<p style=\"margin-left:30px\">$area = m &quot;1 0;0 2&quot;<\/p>\n<p style=\"margin-left:30px\">$area[1,0]=((2 |^ .5) &#8211; 1) |^ 2<\/p>\n<p style=\"margin-left:30px\">$p=$p*$area<\/p>\n<p style=\"margin-left:30px\">0..25 | %{<\/p>\n<p style=\"margin-left:30px\">&quot;{0} t: {1}&nbsp; a: {2}&nbsp; ~pi:{3}&quot; -f $_,$t,$area[1,0],$p[0,0];<\/p>\n<p style=\"margin-left:30px\">$t = halfTan $t;<\/p>\n<p style=\"margin-left:30px\">$area[1,0] = triangleArea $t<\/p>\n<p style=\"margin-left:30px\">$p=$p*$area<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>Running this script yields the same approximation for &pi;.<\/p>\n<p>~ Firaz<\/p>\n<p>Thank you for writing a most interesting article, Firaz.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft senior software engineer, Firaz Samet, shows how to use Windows PowerShell to approximate the value of pi from a polygon. Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog post from Firaz Samet in honor of Pi (&pi;) Day. I will turn the blog over&hellip; The Scripting Guy ran [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[164,3,4,61,45],"class_list":["post-7291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-math","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft senior software engineer, Firaz Samet, shows how to use Windows PowerShell to approximate the value of pi from a polygon. Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest blog post from Firaz Samet in honor of Pi (&pi;) Day. I will turn the blog over&hellip; The Scripting Guy ran [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=7291"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=7291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}