File: secular.c

package info (click to toggle)
mpsolve 3.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,100 kB
  • sloc: ansic: 25,748; sh: 4,925; cpp: 3,155; makefile: 914; python: 407; yacc: 158; lex: 85; xml: 41
file content (163 lines) | stat: -rw-r--r-- 3,856 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Example code for libmps
 *
 * This code computes the zeros of the secular equation 
 *
 *  S(x) = 1 / (x - 2) + 3 / (x - 4) + 5 / (x - 6) - 1
 *
 * using its representation as floating point, high precision
 * floating point, and rational coefficients. 
 *
 * Can be compiled with:
 *   gcc -o secular -lm -lgmp -lmps secular.c
 *
 * Author: Leonardo Robol <leonardo.robol@unipi.it>
 */


#include <mps/mps.h>
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include <string.h>

int
main (int argc, char **argv)
{
  mps_context * ctx = NULL;
  int i;
  long wp = 128;

  cplx_t *af = NULL, *bf = NULL;
  mpc_t *am = NULL, *bm = NULL;
  mpq_t *aqr = NULL, *bqr = NULL;
  mpq_t qzero;

  cplx_t *roots = NULL;
  mpc_t  *mroots = NULL;
  mps_secular_equation * p = NULL;

  /* First version: floating point coefficients. */
  printf("== Computing the roots using floating point coefficients \n");
  af = cplx_valloc(3);
  bf = cplx_valloc(3);

  for (i = 0; i < 3; i++) {
    cplx_set_d(af[i], 2.0 * i + 1, 0.0);
    cplx_set_d(bf[i], 2.0 * i + 2.0, 0.0);
  }

  ctx = mps_context_new();
  p = mps_secular_equation_new (ctx, af, bf, 3);

  mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p));

  mps_mpsolve(ctx);

  mps_context_get_roots_d (ctx, &roots, NULL);

  for (i = 0; i < 3; i++) {
    printf("Root %d: %f + %f I\n", i, cplx_Re(roots[i]), cplx_Im(roots[i]));
  }

  free (roots);
  mps_polynomial_free (ctx, MPS_POLYNOMIAL (p));
  mps_context_free(ctx);
  
  free(af);
  free(bf);

  /* Version 2: higher precision coefficients and approximations */
  printf("\n\n== Computing the roots using multiprecision floating-point coefficients (asking 40 digits)\n");
  am = mpc_valloc(3);
  bm = mpc_valloc(3);

  for (i = 0; i < 3; i++) {
    mpc_init2(am[i], wp);
    mpc_set_ui(am[i], 2U * i + 1U, 0U);
    mpc_init2(bm[i], wp);
    mpc_set_ui(bm[i], 2U * i + 2U, 0U);    
  }

  ctx = mps_context_new();
  p = mps_secular_equation_new_raw (ctx, 3);

  for (i = 0; i < 3; i++) {
    mps_secular_equation_set_coefficient_f (ctx, p, i, am[i], bm[i]);
  }  

  mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p));
  mps_context_set_output_goal (ctx, MPS_OUTPUT_GOAL_APPROXIMATE);
  mps_context_set_output_prec (ctx, 40);
  mps_mpsolve(ctx);

  mps_context_get_roots_m (ctx, &mroots, NULL);

  for (i = 0; i < 3; i++) {
    printf("Root %d: ", i);
    mpc_out_str(stdout, 10, wp, mroots[i]);
    printf("\n");
    mpc_clear(mroots[i]);

    mpc_clear(am[i]);
    mpc_clear(bm[i]);    
  }

  free (mroots); mroots = NULL;
  mps_polynomial_free (ctx, MPS_POLYNOMIAL (p));
  mps_context_free(ctx);
  
  free(am);
  free(bm);

  /* Version 3: higher precision coefficients and approximations */
  printf("\n\n== Computing the roots using rational coefficients (asking 240 digits)\n");
  aqr = mpq_valloc(3);
  bqr = mpq_valloc(3);
  mpq_init(qzero);

  mpq_set_ui (qzero, 0U, 1U);

  for (i = 0; i < 3; i++) {
    mpq_init(aqr[i]);
    mpq_set_ui(aqr[i], 2U * i + 1U, 1U);
    mpq_init(bqr[i]);
    mpq_set_ui(bqr[i], 2U * i + 2U, 1U);    
  }

  ctx = mps_context_new();
  p = mps_secular_equation_new_raw (ctx, 3);

  for (i = 0; i < 3; i++) {
    mps_secular_equation_set_coefficient_q (ctx, p, i, aqr[i], qzero, bqr[i], qzero);
  }

  mpq_clear(qzero);

  mps_context_set_input_poly (ctx, MPS_POLYNOMIAL (p));
  mps_context_set_output_goal (ctx, MPS_OUTPUT_GOAL_APPROXIMATE);
  mps_context_set_output_prec (ctx, 240);
  mps_mpsolve(ctx);

  mps_context_get_roots_m (ctx, &mroots, NULL);

  for (i = 0; i < 3; i++) {
    printf("Root %d: ", i);
    mpc_out_str(stdout, 10, wp, mroots[i]);
    printf("\n");
    
    mpc_clear(mroots[i]);

    mpq_clear(aqr[i]);
    mpq_clear(bqr[i]);
  }

  free (mroots);
  mps_polynomial_free (ctx, MPS_POLYNOMIAL (p));
  mps_context_free(ctx);
  
  free(aqr);
  free(bqr);    
  
  return EXIT_SUCCESS;
}