Progess / Project 702

C# iterators and generators (UTCI algorithm)

Iterators / generator expressions are an ideal way to represent the UTCI algorithm. The Python expression previously posted generates the UTCI terms using a generator, then iterates through these to sum them. This can also be done in C# (reference for this: here)

public static class UTCI 
{
    // generator sequence for the terms in UTCI
    public static IEnumerable<double> UTCIsequence(double Ta, double Pa)
    {
        int depth = 6;
        for (int n = 0; n <= depth; n ++) {
            for (int k = depth - n; k > 0; k --) {
                yield (7 - n - k) * Ta**n * Pa**k;
            }
        }
    }

    // iterate through UTCI sequence and sum the terms
    double UTCIsum = 0;
    foreach(var term in UTCI.UTCIsequence(Ta, Pa)) {
        UTCIsum = UTCIsum + term;
    }

}

NOTE: I am not 100 % sure of the accuracy of this code (haven’t compiled and run it)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s