# Mathematical notation for computer codes

How to write in mathematical notation the following procedure?

1) For each element of matrix A assign the sum of distances between it and all other elements. Distance is simply difference in indices.
2) Let be b sum of all elements of A.

so for 1) the output is a matrix but for 2) it is just a scalar.

Finally, is there any guidance to write computer code as mathematical notation?

-
How do you define the distance between indices? What, for example, is $D((1, 1), (3, 4))$? What about $D((4, 3), (1, 6))$? – Rick Decker Sep 3 '13 at 12:47
@RickDecker Say, i as y and j as x axes. So Euclidean distance will be used as (dj^2+di^2)^0.5. – Developer Sep 3 '13 at 13:08

You are trying to explain your program, not really to translate the code into mathematics. As such, standard notation works fine. It is up to you to make sure that your code does what you say it does. One thing to watch is that in many languages the indices start at $0$ while for most mathematical presentations of matrices they start at $1$. Using $1$ as the base, you are very close: I would say $$a_{ij}=\sum_{v=1}^n\sum_{w=1}^m D(i,j,v,w)$$ defining $D(i,j,i,j)=0$ so you don't mind having it in the sum. You can use whatever distance function $D$ you want. You could define the distance function as operating on ordered pairs, so it looks like $D((i,j),(v,w))$ if you want. It emphasizes the fact that it is the distance between two points. Then to add them up $$b=\sum_{i=1}^n\sum_{j=1}^ma_{ij}$$ I would like to see it stated somewhere that $a_{ij}$ is the $i,j$ element of $A$ rather than assuming that the reader will understand that.