Advertisement
Guest User

C mpsolve library example

a guest
Oct 22nd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #include <gmp.h>
  2. #include <stdio.h>
  3. #include <mps/mps.h>
  4.  
  5.  
  6. void
  7. solve_poly(long int degree, mpq_t *coeffs_re, mpq_t *coeffs_im,
  8.            long output_prec)
  9. {
  10.   mpc_t *roots = NULL;
  11.   rdpe_t *rds = NULL;
  12.   static mps_context *ctx;
  13.   mps_monomial_poly *mn_pl;
  14.  
  15.   ctx = mps_context_new();
  16.   mn_pl = mps_monomial_poly_new (ctx, degree);
  17.   for (int m = 0; m < degree + 1; m++) {
  18.     mps_monomial_poly_set_coefficient_q(ctx, mn_pl, m, coeffs_re[m], coeffs_im[m]);
  19.   }
  20.   mps_context_set_input_poly(ctx, MPS_POLYNOMIAL(mn_pl));
  21.   mps_context_select_algorithm (ctx, MPS_ALGORITHM_SECULAR_GA);
  22.   mps_context_set_output_goal(ctx, MPS_OUTPUT_GOAL_APPROXIMATE);
  23.   mps_context_set_output_prec(ctx, output_prec);
  24.   printf("Enter solver\n");
  25.   mps_mpsolve(ctx);
  26.   printf("Leave solver\n");
  27.   mps_context_get_roots_m(ctx, &roots, &rds);
  28.   for (int m = 0; m < degree; m++) {
  29.     fprintf(stdout, "%.17e + I*%.17e\n", mpf_get_d(mpc_Re(roots[m])),
  30.             mpf_get_d(mpc_Im(roots[m])));
  31.   }
  32.   mpc_vclear(roots, degree);
  33.   free(roots);
  34.   free(rds);
  35.   mps_monomial_poly_free(ctx, MPS_POLYNOMIAL(mn_pl));
  36.   mps_context_free(ctx);
  37. }
  38.  
  39. void
  40. test_mps(void)
  41. {
  42.   int M = 16;
  43.   long output_prec = 100;
  44.   mpq_t *coeffs_re = NULL, *coeffs_im = NULL;
  45.  
  46.   coeffs_re = malloc(sizeof(mpq_t)*(M + 1));
  47.   coeffs_im = malloc(sizeof(mpq_t)*(M + 1));
  48.   for (int m = 0; m < M+1; m++) {
  49.     mpq_init(coeffs_re[m]);
  50.     mpq_init(coeffs_im[m]);
  51.   }
  52.   mpq_set_si(coeffs_re[0], 1, 1);
  53.   mpq_set_si(coeffs_re[M], -1, 1);
  54.   solve_poly(M, coeffs_re, coeffs_im, output_prec);
  55.   mpq_vclear(coeffs_re, (unsigned)M + 1);
  56.   mpq_vclear(coeffs_im, (unsigned)M + 1);
  57.   free(coeffs_re);
  58.   free(coeffs_im);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement