[GRASS-dev] adding lib_arraystats

Glynn Clements glynn at gclements.plus.com
Wed Feb 13 16:27:06 EST 2008


Moritz Lennert wrote:

> I've finally gotten around to continue working on the d.thematic.* 
> modules, and more specifically on the classification code. As mentioned 
> earlier, I think it makes sense to make the latter into a library, so I 
> decided to create lib_arraystats which contains functions for collecting 
> basic statistics and for finding class breaks in arrays of doubles. In 
> the future this could be filled with more statistical functions on such 
> arrays.
> 
> Could the gurus please have a look and tell me if the attached files are 
> decent enough (except for the lacking documentation) to be committed to 
> svn for further development ? Once that's done, I can also commit the 
> d.thematic.area and v.class modules.

class_stdev() should probably just return the scale rather than a
formatted message. The caller can then construct the message if it so
desires.

Some stylistic comments:

1. "x += k", "x -= k" etc are clearer than "x = x + k" etc. The former
make it immediately clear that the variable (or array element, etc) is
being modified according to a particular idiom (i.e. having some
quantity added to or subtracted from it), rather than being assigned
the result of some arbitrary expression.

2. class_discont() looks like it might be more clear using an array of
structures for num/no/zz/co, rather than parallel arrays. E.g.

	struct class {
		int num;
		double no;
		double zz;
		double co;
	} *classes;

Then, code which accesses multiple fields doesn't need to keep
specifying the index. E.g.

	for (j = 1; j <= i; j++) {
	    no[j] = num[j];
	    zz[j] = x[num[j]] * rangemax + min;
	    if (j == i)
		continue;
	    if (co[j] > co[j + 1]) {
		zz[j] = zz[j] + rangemin;
		continue;
	    }
	    zz[j] = zz[j] - rangemin;
	    no[j] = no[j] - 1;
	}

becomes:

	for (j = 1; j <= i; j++) {
	    struct class *c = &classes[j];
	    struct class *next = &classes[j + 1];
	    c->no = c->num;
	    c->zz = x[c->num] * rangemax + min;
	    if (j == i)
		continue;
	    if (c->co > next->co) {
		c->zz += rangemin;
		continue;
	    }
	    c->zz -= rangemin;
	    c->no -= 1;
	}

The relieves the reader of the need to visually check all of those
[j]s to ensure that there isn't an [i] hidden amongst them.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list