Fix overflows of index_t for large matrices

When using e.g. a custom kernel matrix of 48,000x48,000 (i.e. more then 2^31 elements), then the index computations of SGMatrix overflow in a number of places. I've tried to address all of them in SGMatrix.
Sometimes it was cleaner code-wise to change the iteration variable in the loop.