I have been battling with the formula for UTCI. It has such an interesting, regular pattern; I was certain it could be coded into an algorithm more succinctly than coding the full 84-term (1.5 pages worth of page) expression. 22:27, Wednesday August 21st, I have my first (untested) draft of the algorithm:

By working term-by-term through the formula and grouping similar terms, I was able to extract the pattern for the second section (56 terms). This second group can be simplified from 56 terms to 21 terms. Further, the pattern can be expressed in the general form:

An algorithm to generate all 56 terms looks like follows:

int depth = 6;
for (int n = 0; n <= depth; n ++) {
for (int k = (6 - n); k > 0; k --) {
yield (depth - n - k + 1) * Ta**n * Pa**k;
}
}

In Python, this could be written as a generator expression in a single line:

sum((7 - n - k)*Ta**n*Pa**k for n in range(7) for k in range(6 - n, 0, -1))

I have to say I am pretty happy with this. I still have to test it, make sure it actually works 🙂 But the logic is sound. The other challenge I have, is this algorithm doesn’t allow for the weightings applied to each term in the original formula. I need to figure out how to extract these weightings and apply an aggregate weight to the final sum.

### Like this:

Like Loading...

*Related*

Pingback: Run time analysis of UTCI algorithms | :: NickBurns

Note: running time of the python generator expression above was tested, and was slightly slower than defining the generator inside a separate function. At this stage, I imagine this might have something to do with the way Python pre-compiles the code before interpretation, it being slightly more efficient to parse an independent generator block c.f. the inline sum(). There is some anecdotal evidence that loops inside functions perform better than loops in global scope.

LikeLike