This suggests the following matrix calculation algorithm, as a first draft: Initialize all distance/relationship partial sums to zero.For each marker, calculate and save the seven possible increments in a lookup table, and then refer to the table when updating partial sums. This replaces several floating point adds/multiplies in the inner loop with a single addition operation.