Succinctly writing some code in vector/mathematical notation - how?

I’m trying to learn how to correctly represent some code I have in vector notation. Apologies if it’s a bit convoluted, keep in mind I’m trying to learn how to better communicate it (!)

The code works on two inputs:

• A vector of vectors of vectors - that we'll call A

• A vector of vectors - called M

The base vector of each of these is a 2D position

The next level up is a vector of these positions – lets say there are 3 (x,y) positions in each. This defines ‘M’, and an example M might look like:

M = {(1,2) , (2,2) , (4,1)}

‘A’ however, has yet another level up, so for instance let’s say there are 4 vector of vectors (similar to the example M), in A - an example A:

A = [ {(1,1),(3,2),(4,1)} {(2,2),(2,5),(3,1)} {(4,2),(2,1),(4,7)} {(3,4),(5,2),(6,1)} ]

The operations I can only describe using my own system of notation, where a vector has three subscripts each numbering the element of the corresponding vector they came from.

For instance: M0x = 1, M0y = 2, M1y = 2, M2x = 4 …

And for A: A00x = 1, A10x = 2, A11y = 5, A32x = 6 …

(you’ll note I’ve used a 0 index origin – and have just labeled x and y exactly that)

In pseudo code the operation is as follows:

for (i = 0; i < 5; i++){
for (j = 0; j < 4; j++){      // <<edit: fixed small error here
Di += (Aijx - Mjx)^2 + (Aijy - Mjy)^2
}
}


I’m summing (+=) up the Euclidian distances each point in M is away from each point in each of the elements of A (not taking the square root however).

A random D might look like:

D = (13.344, 5.674, 4.2334, 12.556)

Question: How would this be written up in mathematical/vector notation?

As it’s in code, the col row thing is pretty arbitrary, so I’m open to ideas about how best to order that.

Without advice I'd just stick with my subscripts, and perhaps summations in place of the loops? But maybe there is some hidden vector stuff going on here or some other standard?

EDIT>> I guess in all fairness I'm interested to see what it might look like if I took square root before summing also ...

1 Answer

The good news is that, for situations like this, the math and the code aren't too distinct from each other. If you treat $M$ merely as a two-dimensional matrix and $A$ as a three-dimensional matrix, then you might have something like:

Let $D: \{0,\dots, 3\} \longrightarrow \mathbb{R}$ be defined as follows:

$$D(i) = \sum_{j=0}^3 (A_{ij0} - M_{j0})^2 + (A_{ij1} - M_{j1})^2$$

I'm not sure you need to reorder anything; what you have seems pretty intuitive.

• The subscripts are fine? Cool... :) easy then. This question I think was inspired by some machine learning subjects where they have a habit of taking (marginally) intuitive algorithms and 'vector-calcing' them into these relatively implementable vector formulas. You hear stuff like: if we zero pad this, make it columns, tweak this bit and transpose its knickers ... Voilà/presto look! dot product! ... Jul 17, 2015 at 0:28
• p.s. I can't upvote yet, but would :) Jul 17, 2015 at 0:29
• Yeah, I had to do something similar in some research. No worries about lack of upvote, but if you like, you can accept the answer by clicking the gray checkmark next to it. :)
– Ken
Jul 17, 2015 at 0:31
• p.s. I just realised a 'sloppy and paste' error in the psuedo code. A fence-post error in the j loop ... Which might have implications for the summation in your answer. (4 should be 3). What research? Mine is a hack/first attempt at a nearest-neighbour comparison algorithm, it's working on facial marker positions in motion capture. Jul 17, 2015 at 0:35
• Hm, well, does this mean that $i$ bounds in your pseudocode need to be changed? Either way, I've changed the answer to conform to the example and your comment.
– Ken
Jul 17, 2015 at 0:44