/* Returns the boundaries of a Dvector, that is [min, max]. It ignores
   NaN and will complain if the Dvector contains only NaN.
   
    v = Dvector[0.0/0.0, 0.0/0.0, 1,2,4,5,9,0.0/0.0,0.1]
    v.bounds -> [0.1, 9]

*/
static VALUE dvector_bounds(VALUE self)
{
  double min, max;
  VALUE ret;
  long len;
  double * data = Dvector_Data_for_Read(self, &len);
  /* skip all NaNs at the beginning */
  while(len-- > 0)
    if(!isnan(*data++))
       break;
  if(len>=0)
    {
      min = max = *(data-1);
      while(len-- > 0)
        {
          if(! isnan(*data))
            {
              if(*data < min)
                min = *data;
              if(*data > max)
                max = *data;
            }
          data++;
        }
      ret = rb_ary_new2(2);
      rb_ary_store(ret, 0, rb_float_new(min));
      rb_ary_store(ret, 1, rb_float_new(max));
      return ret;
    }
  else
    rb_raise(rb_eRuntimeError, 
             "bounds called on an array containing only NaN");
  return Qnil;
}