Weekend Scripter: Approximate the Value of Pi from a Polygon

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 (π) Day. I will turn the blog over…

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.

First a little bit about me…

I am a senior software engineer at Microsoft where I work on Azure Stream Analytics. 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.

Pi Day is celebrated around the world on March 3 (3.14). It aims to raise interest in math and especially π. This year’s will be even more significant (3.1415…). For more information, see Pi Day on Wikipedia.

In this post, I will talk about using Windows PowerShell to approximate π with polygons. I will also introduce matrix calculus with Math.NET Numerics, using a thin Windows PowerShell wrapper called Math.NET PowerShell.

Approximating π

The approximation of π 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 π) 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 apothem to 1.

By using the triangle properties and the trigonometric half-angle formulae for the tangent, we can deduce the following formula:

Expressing the formula in Windows PowerShell

We now want to express the previous formula with Windows PowerShell. We will mainly use the [math] library’s power method, Pow(x, y). But to make the notation simpler, we will introduce a Windows PowerShell filter to make calling the Pow method closer to the mathematical notation:

filter ^([double] $y){[math]::Pow($_,$y)}

This means that expressing 2³ 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. Similarly, will become (2 |^ .5).

Matrix expression

The iterative nature ofmakes using matrices an interesting proposition. For this reason, we will express it in the following matrix formula:

We will then use a thin Windows PowerShell wrapper around Math.NET Numerics called Math.NET PowerShell to calculate it in Windows PowerShell. The Windows PowerShell module offers a familiar way to initialize matrices (such as m "1 0;0 1" to initialize the identity matrix of order 2) and an easy approach to using the adequate Math.NET Numerics matrix type.

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:

Set-MathNetMatrixType Double

When this is done, we express the formula using the previous matrix notation as follows: