## Matrix Operations in Swift

I recently completed 10 Days of Statistics on HackerRank. Yay!

In the process, I needed some matrix operations for a medium-difficulty problem. And here they are, code style be damned :

That’s the identity matrix popping out at the end, which validates my implementation.

But what’s this? A 60-line method? Uncle Bob would not be pleased.

Comments should not take the place of good variable/method names. Those section comments give clues as to where my methods should be :

Better. Uncle Bob would be proud (or give me credit for trying, anyway).

One more thing : Thanks to StackOverflow user Alexander, I have an even better way to express that pivot loop :

## iOS Williamsburg – Swift Basics

Do you want to learn Swift syntax, have fun, and meet an enthusiastic group of developers? Come by the Williamsburg Library at 515 Scotland Street, Williamsburg, VA, on Saturday, August 12, 2017. We will be going over Swift basics. We have many experienced developers who are eager to share tips and help newcomers.

### iOS development basics.

Saturday, Aug 12, 2017, 10:00 AM

Location details are available to members only.

11 Members Attending

Room c

I hope to see you there!

## iOS Dev Camp DC 2017

It’s tomorrow! See you at iOS Dev Camp DC on Friday, August 3, 2017.

I am looking forward to meeting many talented developers and learning all about who you are and what you do.

## iOS Swift Developers in Williamsburg, VA

### Get together to meet, discuss our plans for future meet ups and learn some Swift

Saturday, Jul 22, 2017, 10:00 AM

Williamsburg Library
515 Scotland Street Williamsburg, VA

7 Members Attending

Bring your laptop, we can go over the basics of swift and see where everyone is skills wise.

If you are near the Williamsburg Library on Scotland St. this Saturday, July 22 2017, at 10AM, and you have an interest in iOS development using the Swift programming language, please stop by. I will be giving a presentation on language basics and the development environment. We have a diverse group of enthusiasts with newbies and app store veterans alike.

## Swift Fun with ArraySlice

There is a lovely article by Luna An describing ArraySlice objects in more detail than I do here. It covers Swift 3 at the moment, and you should note that Swift 4 includes support for single-ended ranges, so you can create slices ala [..<count].

I found a good use for an ArraySlice while trying to find quartiles in a set of data.

The problem of finding each quartile is essentially the same problem of finding the median from three different data sets, one being the original input set, the other two being the upper and lower half of the set after removing the original median element, if it exists.

Here, I use findMedian to perform all three tasks. I found that I had to do a bit of extra work because the array slice is not indexed starting at zero. I wonder why they chose to implement slices in this way?

## Command Design Pattern in Swift

It is the summer of Swift.

I was perusing some wonderful design patterns from Oktawian Chojnacki, and I decided to play around with the Command pattern. The Command pattern represents commands as objects to go between a Caller, which references the commands, and a Receiver, which is referenced by the commands.

I’m reminded of a game :

Here, each command object operates on a Robo, telling it to make a single move. The commands are collected into a program.

The first draft of this code had the Commands store their target Robo in a property. I realized a problem with this in that my program would accept commands for any Robo, when only one Robo belongs to the program. My solution for this was to give control of the command target to the program itself.

Of course, now the commands are little more than glorified functions, which can be stored in arrays in Swift anyway.

## Swift Dictionary Reduce

Why is it so hard to find examples of Swift dictionary reduce operations? Examples for arrays abound :

See this if you’re looking for a detailed HowTo.

But dictionary is the more advanced problem. How do I reduce when I have keys and values?

The key here is to note that a dictionary yields the contents of its sequence in tuples containing pairs of values. We still have our familiar $0,$1 arguments from reduce as above, where $0 is the partial sum and$1 is the individual element value. But now $0 and$1 are both tuples, and you get to their contents through $0.0,$0.1, $1.0, and$1.1.

This example concatenates the strings and adds the integers, and the two separate data types just happen to use the same symbol for the two operations.

## React.JS

React and Angular are popping up a lot in job interviews these days. I decided to try React, since it is rumored to be the simpler of the two. So I created the old Chinese stone game Go in a Codepen. Normally I post code and talk about it in this case, but I think I will just let the Codepen speak for itself. Here, I report my experiences.

There is a lot of learning material out there, but the best resources start out right away with productive examples that get you working quickly. This tutorial video from Traversy Video does a very good job. I used this React demo from Facebook to model my architecture after.

The least trivial part of the game was the capture algorithm. The game engine must figure out if a newly placed stone results in another stone being surrounded. To figure this out, I implemented a recursive breadcrumb algorithm. The method searches recursively in four directions, resulting in a wide descending tree, but marks visited spots on the matrix to prevent infinite recursion. If at any point an empty space (liberty, in game terms) is found, the call stack starts bubbling up with that information. The recursive call will say whether or not a liberty was ever found. If it was, I run a second method to erase all visited points on the matrix, otherwise I run a different method to restore the marked spots back to original.

Here is my reaction (?) to React :

• JavaScript is fun for web programming in limited amounts, but Object-Oriented frameworks like ES2015, Babel, etc. inevitably make it worse. this.method.bind(this)? Yucko.

• Hidden inherited methods are traps waiting to happen. My IDE (Codepen) did not alert me to inherited methods, so this may be my own fault for not using a JetBrains tool or some such. Pick unique method names just in case.

• I don’t really see the payoff. I understand the principle of data flow and limiting state on classes, but a real class-driven language does this in a more flexible way without need of a framework.

I know a little bit of CSS and HTML, so I was able to decorate it a bit, so that it kind of looks like an old wood board.

## National Labs

Sometimes writing software for a national lab can be cool.

## Big O and friends

All software developers know about Big O. For instance, contemporary sorting algorithms usually have $\mathcal{O}(n\log{}n)$ complexity. Something like $\mathcal{O}(n\log{}n)$ looks like a function of a function, but it’s really a classification system.

We start by defining the complexity of an algorithm to be an estimate of the number of steps required to solve a problem. This count is going to vary by the size of the problem, and so the complexity must be a function of the problem size.

Sometimes this can result in a nasty looking functions like $f(x)=3x^2+2x+7$ or $f(x)=5x^2-999$. We say that both of these functions are in the category $\mathcal{O}(x^2)$ because they have similar growth properties. This is called Bachmann-Landau notation.

In mathematics, this notation describes the asymptotic behavior of a function. if $f(n) = \mathcal{O}(g(n))$, then we have a guarantee that after a certain point, the graph of $f(n)$ will fall below the graph of $g(n)$, if you’re willing to rotate $g(n)$ a bit. In computer science, we use this to understand how the performance of an algorithm will change as the size of the problem or input grows. There are actually several Bachmann-Landau notations, each of which gives us some kind of indication on the limits of the growth of a function.

Name Notation Analogy
Big O $f(n)$ is $\mathcal{O}(g(n))$ $f(n) \le g(n)$
Little o $f(n)$ is $o(g(n))$ $f(n)$ < $g(n)$
Big Omega $f(n)$ is $\Omega(g(n))$ $f(n) \ge g(n)$
Little omega $f(n)$ is $\omega(g(n))$ $f(n) > g(n)$
Theta $f(n)$ is $\Theta(g(n))$ $f(n) = g(n)$

Credit goes to MIT for the excellent table and analogies. The definitions of each can be found in the referenced material.

Additionally, some of these definitions can be expressed using calculus. We start with the definition of $o$ in the material says that $f(n)$ is $o(g(n))$ if :

$\displaystyle \forall C > 0 , \exists k \,|\, ( x > k \rightarrow |f(n)| \le C|g(n)| )$

That’s a symbolic mouthful. It means, “For all positive values of C, there exists some k such that, if x>k, the absolute value of f(n) is less than or equal to the absolute value of g(n) multiplied by C.

Separately, any calculus text will give this definition for a limit at infinity. I like Paul’s Online Calculus Notes. It says that $\lim_{x\to\infty} f(x) = L$ if

$\displaystyle \forall \epsilon > 0, \exists N \forall x>N : |f(x)-L|$ < $\epsilon$

Again in plain English, “For any positive value of $\epsilon$, there must exist some value for N such that for any value of x that is greater than N, this inequality holds.”

Let’s make some substitutions of variable names in the above equation. If we change out f(x) for f(x)/g(x), 0 for L, C for $\epsilon$, n for x, and k for N :

$\displaystyle \forall C > 0, \exists k \forall n>k : |f(n)/g(n)|$ < $C$

or

$\displaystyle \forall C > 0, \exists k \forall n>k : |f(n)|$ < $C |g(n)|$

This boils down to an alternative definition of $o$. $f(n)$ is $o(g(n))$ if :

$\displaystyle \lim_{x\to\infty} f(x)/g(x) = 0$

This allows us to use other mathematical tricks, like L’Hopitals Rule, to figure out if two given functions follow this relationship.