int main (void) { size_t i, j, n = 100; double min, max, min_out, max_out, k = 0.0, l = 0.0; gsl_matrix * m = gsl_matrix_alloc (n, n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) gsl_matrix_set (m, i, j, sin(i) + cos(j)); min = gsl_matrix_min (m); max = gsl_matrix_max (m); gsl_matrix_minmax (m, &min_out, &max_out); // Minimum is -1.99995, and min and min_out are equal. k += min - min_out; // Maximum is 1.99991, and max and max_out are equal. l += max - max_out; gsl_matrix_free (m); printf ("difference k = %g (should be zero)\n", k); printf ("difference l = %g (should be zero)\n", l); return 0; }
/* The main thing - compute the nmf */ int gsl_matrix_nmf(gsl_matrix *v, int cols, gsl_matrix **w, gsl_matrix **h) { double dist = 1; int iter = 1; double min, max; #ifdef DEBUG printf("\nCols: %d\nv:\n", cols); pp(v); #endif gsl_matrix_minmax(v, &min, &max); #ifdef DEBUG printf("Min: %f, Max: %f\n", min, max); #endif *w = gsl_matrix_alloc(v->size1, cols); initmatrix(*w, min, max/2); // the multiplicative rules tend to increase w *h = gsl_matrix_alloc(cols, v->size2); initmatrix(*h, min, max); while(dist >= THRESH && iter < MAXITER) { dist = update(v, *w, *h); #ifdef DEBUG printf("Iteration: %d, distance: %f\n", iter, dist); printf("\nw:\n"); pp(*w); printf("\nh:\n"); pp(*h); printf("\n"); #endif iter++; } #ifdef DEBUG printf("Ended\n"); #endif return GSL_SUCCESS; }