spacepaste

caml_pam.c

  1.  
  2. #include <string.h>
  3. #include <caml/bigarray.h>
  4. #include <caml/mlvalues.h>
  5. #include <caml/memory.h>
  6. #include <caml/alloc.h>
  7. #include <caml/custom.h>
  8. #include <gsl/gsl_matrix.h>
  9. #include <stdio.h>
  10. size_t *pam(gsl_matrix *, size_t, /*OUT*/ float *);
  11. /* rows for leaves; columns for masses */
  12. CAMLprim value caml_pam(value k_value, value dist_value)
  13. {
  14. CAMLparam2(k_value, dist_value);
  15. double *dist = Data_bigarray_val(dist_value);
  16. gsl_matrix_view m;
  17. float work;
  18. size_t *medoids, *medoids_ptr;
  19. size_t nrow, ncol;
  20. value res_bigarr, *res_ptr;
  21. int i, k;
  22. nrow = Bigarray_val(dist_value)->dim[0];
  23. ncol = Bigarray_val(dist_value)->dim[1];
  24. m = gsl_matrix_view_array(dist, nrow, ncol);
  25. k = Int_val(k_value);
  26. medoids = pam(&m.matrix, k, &work);
  27. res_bigarr = alloc_bigarray_dims(BIGARRAY_CAML_INT | BIGARRAY_C_LAYOUT, 1, NULL, k);
  28. res_ptr = Data_bigarray_val(res_bigarr);
  29. medoids_ptr = medoids;
  30. for (i = 0; i < k; ++i) {
  31. *res_ptr++ = Val_long(*medoids_ptr++);
  32. }
  33. free(medoids);
  34. CAMLreturn(res_bigarr);
  35. }
  36.  

pam_solver.ml

  1.  
  2. module BA = Bigarray
  3. module BA1 = Bigarray.Array1
  4. type int_vector = (int, BA.int_elt, BA.c_layout) BA1.t
  5. external c_pam: int -> Matrix.matrix -> int_vector = "caml_pam"
  6.