stress_model.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <sparse/general.h>
  2. #include <sparse/SparseMatrix.h>
  3. #include <sfdpgen/spring_electrical.h>
  4. #include <sfdpgen/post_process.h>
  5. #include <sfdpgen/stress_model.h>
  6. #include <stdbool.h>
  7. #include <util/alloc.h>
  8. void stress_model(int dim, SparseMatrix B, double **x, int maxit_sm, int *flag) {
  9. int m;
  10. int i;
  11. SparseMatrix A = B;
  12. if (!SparseMatrix_is_symmetric(A, false) || A->type != MATRIX_TYPE_REAL){
  13. if (A->type == MATRIX_TYPE_REAL){
  14. A = SparseMatrix_symmetrize(A, false);
  15. A = SparseMatrix_remove_diagonal(A);
  16. } else {
  17. A = SparseMatrix_get_real_adjacency_matrix_symmetrized(A);
  18. }
  19. }
  20. A = SparseMatrix_remove_diagonal(A);
  21. *flag = 0;
  22. m = A->m;
  23. if (!x) {
  24. *x = gv_calloc(m * dim, sizeof(double));
  25. srand(123);
  26. for (i = 0; i < dim*m; i++) (*x)[i] = drand();
  27. }
  28. SparseStressMajorizationSmoother sm =
  29. SparseStressMajorizationSmoother_new(A, dim, *x);/* weight the long distances */
  30. if (!sm) {
  31. *flag = -1;
  32. goto RETURN;
  33. }
  34. sm->tol_cg = 0.1; /* we found that there is no need to solve the Laplacian accurately */
  35. sm->scheme = SM_SCHEME_STRESS;
  36. SparseStressMajorizationSmoother_smooth(sm, dim, *x, maxit_sm);
  37. for (i = 0; i < dim*m; i++) {
  38. (*x)[i] /= sm->scaling;
  39. }
  40. SparseStressMajorizationSmoother_delete(sm);
  41. RETURN:
  42. if (A != B) SparseMatrix_delete(A);
  43. }