| 1 |
/* |
| 2 |
* Example code for libmps |
| 3 |
* |
| 4 |
* This code computes the zeros of the secular equation |
| 5 |
* |
| 6 |
* S(x) = 1 / (x - 2) + 3 / (x - 4) + 5 / (x - 6) - 1 |
| 7 |
* |
| 8 |
* using its representation as floating point, high precision |
| 9 |
* floating point, and rational coefficients. |
| 10 |
* |
| 11 |
* Can be compiled with: |
| 12 |
* gcc -o secular -lm -lgmp -lmps secular.c |
| 13 |
* |
| 14 |
* Author: Leonardo Robol <[email protected]> |
| 15 |
*/ |
| 16 |
|
| 17 |
|
| 18 |
#include <mps/mps.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <gmp.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
int |
| 25 |
main (int argc, char **argv) |
| 26 |
{ |
| 27 |
mps_context * ctx = NULL; |
| 28 |
int i; |
| 29 |
long wp = 128; |
| 30 |
|
| 31 |
cplx_t *af = NULL, *bf = NULL; |
| 32 |
mpc_t *am = NULL, *bm = NULL; |
| 33 |
mpq_t *aqr = NULL, *bqr = NULL; |
| 34 |
mpq_t qzero; |
| 35 |
|
| 36 |
cplx_t *roots = NULL; |
| 37 |
mpc_t *mroots = NULL; |
| 38 |
mps_secular_equation * p = NULL; |
| 39 |
|
| 40 |
/* First version: floating point coefficients. */ |
| 41 |
printf("== Computing the roots using floating point coefficients \n"); |
| 42 |
af = cplx_valloc(3); |
| 43 |
bf = cplx_valloc(3); |
| 44 |
|
| 45 |
for (i = 0; i < 3; i++) { |
| 46 |
cplx_set_d(af[i], 2.0 * i + 1, 0.0); |
| 47 |
cplx_set_d(bf[i], 2.0 * i + 2.0, 0.0); |
| 48 |
} |
| 49 |
|
| 50 |
ctx = mps_context_new(); |
| 51 |
p = mps_secular_equation_new (ctx, af, bf, 3); |
| 52 |
|
| 53 |
mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p)); |
| 54 |
|
| 55 |
mps_mpsolve(ctx); |
| 56 |
|
| 57 |
mps_context_get_roots_d (ctx, &roots, NULL); |
| 58 |
|
| 59 |
for (i = 0; i < 3; i++) { |
| 60 |
printf("Root %d: %f + %f I\n", i, cplx_Re(roots[i]), cplx_Im(roots[i])); |
| 61 |
} |
| 62 |
|
| 63 |
free (roots); |
| 64 |
mps_polynomial_free (ctx, MPS_POLYNOMIAL (p)); |
| 65 |
mps_context_free(ctx); |
| 66 |
|
| 67 |
free(af); |
| 68 |
free(bf); |
| 69 |
|
| 70 |
/* Version 2: higher precision coefficients and approximations */ |
| 71 |
printf("\n\n== Computing the roots using multiprecision floating-point coefficients (asking 40 digits)\n"); |
| 72 |
am = mpc_valloc(3); |
| 73 |
bm = mpc_valloc(3); |
| 74 |
|
| 75 |
for (i = 0; i < 3; i++) { |
| 76 |
mpc_init2(am[i], wp); |
| 77 |
mpc_set_ui(am[i], 2U * i + 1U, 0U); |
| 78 |
mpc_init2(bm[i], wp); |
| 79 |
mpc_set_ui(bm[i], 2U * i + 2U, 0U); |
| 80 |
} |
| 81 |
|
| 82 |
ctx = mps_context_new(); |
| 83 |
p = mps_secular_equation_new_raw (ctx, 3); |
| 84 |
|
| 85 |
for (i = 0; i < 3; i++) { |
| 86 |
mps_secular_equation_set_coefficient_f (ctx, p, i, am[i], bm[i]); |
| 87 |
} |
| 88 |
|
| 89 |
mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p)); |
| 90 |
mps_context_set_output_goal (ctx, MPS_OUTPUT_GOAL_APPROXIMATE); |
| 91 |
mps_context_set_output_prec (ctx, 40); |
| 92 |
mps_mpsolve(ctx); |
| 93 |
|
| 94 |
mps_context_get_roots_m (ctx, &mroots, NULL); |
| 95 |
|
| 96 |
for (i = 0; i < 3; i++) { |
| 97 |
printf("Root %d: ", i); |
| 98 |
mpc_out_str(stdout, 10, wp, mroots[i]); |
| 99 |
printf("\n"); |
| 100 |
mpc_clear(mroots[i]); |
| 101 |
|
| 102 |
mpc_clear(am[i]); |
| 103 |
mpc_clear(bm[i]); |
| 104 |
} |
| 105 |
|
| 106 |
free (mroots); mroots = NULL; |
| 107 |
mps_polynomial_free (ctx, MPS_POLYNOMIAL (p)); |
| 108 |
mps_context_free(ctx); |
| 109 |
|
| 110 |
free(am); |
| 111 |
free(bm); |
| 112 |
|
| 113 |
/* Version 3: higher precision coefficients and approximations */ |
| 114 |
printf("\n\n== Computing the roots using rational coefficients (asking 240 digits)\n"); |
| 115 |
aqr = mpq_valloc(3); |
| 116 |
bqr = mpq_valloc(3); |
| 117 |
mpq_init(qzero); |
| 118 |
|
| 119 |
mpq_set_ui (qzero, 0U, 1U); |
| 120 |
|
| 121 |
for (i = 0; i < 3; i++) { |
| 122 |
mpq_init(aqr[i]); |
| 123 |
mpq_set_ui(aqr[i], 2U * i + 1U, 1U); |
| 124 |
mpq_init(bqr[i]); |
| 125 |
mpq_set_ui(bqr[i], 2U * i + 2U, 1U); |
| 126 |
} |
| 127 |
|
| 128 |
ctx = mps_context_new(); |
| 129 |
p = mps_secular_equation_new_raw (ctx, 3); |
| 130 |
|
| 131 |
for (i = 0; i < 3; i++) { |
| 132 |
mps_secular_equation_set_coefficient_q (ctx, p, i, aqr[i], qzero, bqr[i], qzero); |
| 133 |
} |
| 134 |
|
| 135 |
mpq_clear(qzero); |
| 136 |
|
| 137 |
mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p)); |
| 138 |
mps_context_set_output_goal (ctx, MPS_OUTPUT_GOAL_APPROXIMATE); |
| 139 |
mps_context_set_output_prec (ctx, 240); |
| 140 |
mps_mpsolve(ctx); |
| 141 |
|
| 142 |
mps_context_get_roots_m (ctx, &mroots, NULL); |
| 143 |
|
| 144 |
for (i = 0; i < 3; i++) { |
| 145 |
printf("Root %d: ", i); |
| 146 |
mpc_out_str(stdout, 10, wp, mroots[i]); |
| 147 |
printf("\n"); |
| 148 |
|
| 149 |
mpc_clear(mroots[i]); |
| 150 |
|
| 151 |
mpq_clear(aqr[i]); |
| 152 |
mpq_clear(bqr[i]); |
| 153 |
} |
| 154 |
|
| 155 |
free (mroots); |
| 156 |
mps_polynomial_free (ctx, MPS_POLYNOMIAL (p)); |
| 157 |
mps_context_free(ctx); |
| 158 |
|
| 159 |
free(aqr); |
| 160 |
free(bqr); |
| 161 |
|
| 162 |
return EXIT_SUCCESS; |
| 163 |
} |