Sunday, March 14, 2021

A Pi Day vi Trick

It is Pi Day, and the nerd Code of Honor means I am contractually obligated to post something pi related today. In past years, I told you about David Boll's discovery that pi is in the Mandelbrot set. Unfortunately, that exhausts LabKitty's knowledge of amusing pi factoids (March 14th is also Albert Einstein's birthday, but you already knew that).

The other difficulty is, arriving so close on the heels of World Eigenvalue Day, the WED hangover is still lingering come Pi Day morn. Which is kind of a buzzkill both figuratively and literally, what with motivation waning to scour troves for a topic I might parlay into pagehits from civilians feigning interest in the mathogonical arts one day a year (if all y'all really want to help support the mathogonical arts, get your gorram congressperson to start properly funding the NSF).

But I digress.

Which brings us to vi.



Perhaps you, too, would like to explore the digits of pi for fun and whatever. The obvious prerequisite is you first obtain the digits of pi. This would seem to present no obstacle, as typing "digits of pi" at Google will return many (many) pages listing about as many (many) digits of pi as one could possibly require.

The difficulty, alas, is these are typically formatted in a less than helpful format. For example, joyofpi.com lists the first ten thousand digits of pi. Here's a screen grab of the first few lines:

digits of pi

This is not useful for analysis. The data of interest are the individual digits, which this ain't. In other words, we don't want this:

  3.
  1415926...

We want this:

  3 1 4 1 5 9 2 6 ...

What to do?

There are many solutions, but if you have vi you can fix this right up using a little vi regex black magic:

Step 1: Fire up vi, go into insert mode, and cut and paste the pi text of interest from the website into the document. Exit insert mode.

Step 2: Enter : to go into command mode and use the following command to remove all whitespace from the file:

   %s/\ //g

Step 3: Enter : to go into command mode and use the following command to insert a single space between all the digits:

   %s/\(\S\)/ \1/g

Viola! You have a clean set of digits to work with (presumably you can figure out how to remove the decimal point on your own). You may now cut and paste the digits into your analysis app of choice (Matlab, presumably, unless you're some sort of freak). Plot. Datamine. Whatever. Go nuts.

Optional Steps: Depending on how the copypasta is originally formatted, you may want/need to remove newline characters before Steps 2 and 3. You can do so with the command:

   %s/\n/

However, there is a possible new problem: The data is now one ginormously long line which may not play nice with cutting and pasting into your analysis app. No problemo: simply reinsert linebreaks at regular intervals. For example, the following command will reformat one long line as a 2D block of 60 character lines:

   %s/.\{60}/&\r/g

One final optional tweak: If you plan on pasting a 2D block of text into Matlab, you need to add a continuation ellipsis at the end of each line so Matlab understands this is a 1 x whatever vector. Easy peasy:

   %s/$/ ...

(You'll prolly need to remove the ellipsis from the last line, although I suspect there's a clever regex trick to do that for you. I'll leave that as an exercise for the reader...)

It's a Pi Day miracle.

LabKitty Skull Logo


Note added in proof: If you'd like to get good at vi regex black magic, get yourself a copy of Arnold Robbins' muy excellente vi pocket reference (the one with the creepy loris on the cover). You'll thank me later.

No comments:

Post a Comment