1 /*
2 test file for eval
3 
4 Copyright (C) 2020 Andreas Enge
5 
6 This file is part of the MPFRCX Library.
7 
8 The MPFRCX Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The MPFRCX Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16 License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the MPFRCX library; see the file COPYING.LESSER.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA.
22 */
23 
24 #include <stdio.h>
25 #include "mpfrcx.h"
26 
27 
check_eval(mpcx_t f,mpc_t x)28 static void check_eval (mpcx_t f, mpc_t x) {
29    /* checks whether evaluation in place works; assumes that the real and
30       imaginary parts of x have the same precision; replaces x by f(x) */
31    mpc_t r;
32 
33    mpc_init2 (r, mpc_get_prec (x));
34 
35    /* check for difference between normal and in place operation */
36    mpcx_eval (r, f, x);
37    mpcx_eval (x, f, x);
38    if (mpc_cmp (r, x)) {
39       fprintf (stderr, "Error in eval: f(x) yields different values\nf: ");
40       mpcx_out_str (stderr, 10, 0, f);
41       fprintf (stderr, "\nout of place: ");
42       mpc_out_str (stderr, 10, 0, r, MPC_RNDNN);
43       fprintf (stderr, "\nin place:     ");
44       mpc_out_str (stderr, 10, 0, x, MPC_RNDNN);
45       fprintf (stderr, "\n");
46       exit (1);
47    }
48 
49    mpc_clear (r);
50 }
51 
52 
check_eval_random(gmp_randstate_t state)53 static void check_eval_random (gmp_randstate_t state) {
54    int deg;
55    mpcx_t f;
56    mpc_t x;
57 
58    mpcx_init (f, 10, 103);
59    mpc_init2 (x, 103);
60 
61    for (deg = -1; deg <= 1000; deg += 200) {
62       mpcx_urandom (f, deg, state);
63       mpc_urandom (x, state);
64       check_eval (f, x);
65    }
66 
67    mpcx_clear (f);
68    mpc_clear (x);
69 }
70 
71 
check_example()72 static void check_example () {
73    mpcx_t f;
74    mpc_t x, r;
75    mpfr_prec_t p;
76    int i;
77 
78    p = 11;
79    mpcx_init (f, 5, p);
80    mpc_init2 (x, p);
81    mpc_init2 (r, p);
82 
83    mpcx_set_deg (f, 4);
84    for (i = 4; i > 0; i--)
85       mpc_set_ui_ui (mpcx_get_coeff (f, i), i, i, MPC_RNDNN);
86    mpc_set_ui_ui (mpcx_get_coeff (f, 0), 2, 4, MPC_RNDNN);
87    mpc_set_ui_ui (x, 3, 4, MPC_RNDNN);
88 
89    mpcx_eval (r, f, x);
90 
91    if (   mpfr_cmp_si (mpc_realref (r), -1308)
92        || mpfr_cmp_si (mpc_imagref (r), -3626)) {
93       fprintf (stderr, "Error in eval:\nf: ");
94       mpcx_out_str (stderr, 10, 0, f);
95       fprintf (stderr, "\nx: ");
96       mpc_out_str (stderr, 10, 0, x, MPC_RNDNN);
97       fprintf (stderr, "\nexpected: (-1308, -3626)\ngot:      ");
98       mpc_out_str (stderr, 10, 0, r, MPC_RNDNN);
99       exit (1);
100    }
101 
102    mpcx_clear (f);
103    mpc_clear (x);
104    mpc_clear (r);
105 }
106 
main(void)107 int main (void) {
108    gmp_randstate_t state;
109 
110    gmp_randinit_default (state);
111 
112    check_eval_random (state);
113 
114    check_example  ();
115 
116    return 0;
117 }
118