| 1 |
% This file is part of MetaPost; |
| 2 |
% the MetaPost program is in the public domain. |
| 3 |
% See the <Show version...> code in mpost.w for more info. |
| 4 |
|
| 5 |
% Here is TeX material that gets inserted after \input webmac |
| 6 |
|
| 7 |
\font\tenlogo=logo10 % font used for the METAFONT logo |
| 8 |
\font\logos=logosl10 |
| 9 |
\def\MF{{\tenlogo META}\-{\tenlogo FONT}} |
| 10 |
\def\MP{{\tenlogo META}\-{\tenlogo POST}} |
| 11 |
\def\pct!{{\char`\%}} % percent sign in ordinary text |
| 12 |
\def\psqrt#1{\sqrt{\mathstrut#1}} |
| 13 |
|
| 14 |
|
| 15 |
\def\title{Math support functions for MPFI based math} |
| 16 |
\pdfoutput=1 |
| 17 |
|
| 18 |
@ Introduction. |
| 19 |
|
| 20 |
@c |
| 21 |
#include "mpconfig.h" |
| 22 |
#include <w2c/config.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <math.h> |
| 27 |
#include "mpmathinterval.h" /* internal header */ |
| 28 |
#define ROUND(a) floor((a)+0.5) |
| 29 |
@h |
| 30 |
|
| 31 |
@ @c |
| 32 |
@<Declarations@> |
| 33 |
|
| 34 |
@ @(mpmathinterval.h@>= |
| 35 |
#ifndef MPMATHINTERVAL_H |
| 36 |
#define MPMATHINTERVAL_H 1 |
| 37 |
#include "mplib.h" |
| 38 |
#include "mpmp.h" /* internal header */ |
| 39 |
#include <gmp.h> |
| 40 |
#include <mpfr.h> |
| 41 |
#include <mpfi.h> |
| 42 |
#include <mpfi_io.h> |
| 43 |
|
| 44 |
#ifdef HAVE_CONFIG_H |
| 45 |
#define MP_STR_HELPER(x) #x |
| 46 |
#define MP_STR(x) MP_STR_HELPER(x) |
| 47 |
//const char * const COMPILED_gmp_version = MP_STR(__GNU_MP_VERSION) "." MP_STR( __GNU_MP_VERSION_MINOR) "." MP_STR(__GNU_MP_VERSION_PATCHLEVEL); |
| 48 |
//#else |
| 49 |
//const char * const COMPILED_gmp_version = "unknown"; |
| 50 |
#endif |
| 51 |
|
| 52 |
const char *COMPILED_MPFI_VERSION_STRING = MPFI_VERSION_STRING; |
| 53 |
//int COMPILED__GNU_MP_VERSION = __GNU_MP_VERSION ; |
| 54 |
//int COMPILED__GNU_MP_VERSION_MINOR = __GNU_MP_VERSION_MINOR ; |
| 55 |
//int COMPILED__GNU_MP_VERSION_PATCHLEVEL = __GNU_MP_VERSION_PATCHLEVEL ; |
| 56 |
|
| 57 |
@<Internal library declarations@> |
| 58 |
#endif |
| 59 |
|
| 60 |
@* Math initialization. |
| 61 |
|
| 62 |
First, here are some very important constants. |
| 63 |
|
| 64 |
@d MPFI_ROUNDING MPFI_RNDN |
| 65 |
@d MPFR_ROUNDING MPFR_RNDN |
| 66 |
@d E_STRING "2.7182818284590452353602874713526624977572470936999595749669676277240766303535" |
| 67 |
@d PI_STRING "3.1415926535897932384626433832795028841971693993751058209749445923078164062862" |
| 68 |
@d fraction_multiplier 4096 |
| 69 |
@d angle_multiplier 16 |
| 70 |
|
| 71 |
@ Here are the functions that are static as they are not used elsewhere |
| 72 |
|
| 73 |
@<Declarations@>= |
| 74 |
|
| 75 |
static void mp_interval_scan_fractional_token (MP mp, integer64 n); |
| 76 |
static void mp_interval_scan_numeric_token (MP mp, integer64 n); |
| 77 |
static void mp_interval_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d); |
| 78 |
static void mp_ab_vs_cd (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c, mp_number d); |
| 79 |
static void mp_interval_crossing_point (MP mp, mp_number *ret, mp_number a, mp_number b, mp_number c); |
| 80 |
static void mp_interval_number_modulo (mp_number *a, mp_number b); |
| 81 |
static void mp_interval_print_number (MP mp, mp_number n); |
| 82 |
static char * mp_interval_number_tostring (MP mp, mp_number n); |
| 83 |
static void mp_interval_slow_add (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig); |
| 84 |
static void mp_interval_square_rt (MP mp, mp_number *ret, mp_number x_orig); |
| 85 |
static void mp_interval_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin); |
| 86 |
static void mp_init_randoms (MP mp, int seed); |
| 87 |
static void mp_number_angle_to_scaled (mp_number *A); |
| 88 |
static void mp_number_fraction_to_scaled (mp_number *A); |
| 89 |
static void mp_number_scaled_to_fraction (mp_number *A); |
| 90 |
static void mp_number_scaled_to_angle (mp_number *A); |
| 91 |
static void mp_interval_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig); |
| 92 |
static void mp_interval_m_norm_rand (MP mp, mp_number *ret); |
| 93 |
static void mp_interval_m_exp (MP mp, mp_number *ret, mp_number x_orig); |
| 94 |
static void mp_interval_m_log (MP mp, mp_number *ret, mp_number x_orig); |
| 95 |
static void mp_interval_pyth_sub (MP mp, mp_number *r, mp_number a, mp_number b); |
| 96 |
static void mp_interval_pyth_add (MP mp, mp_number *r, mp_number a, mp_number b); |
| 97 |
static void mp_interval_n_arg (MP mp, mp_number *ret, mp_number x, mp_number y); |
| 98 |
static void mp_interval_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, mp_number cf, mp_number t); |
| 99 |
static void mp_set_interval_from_int(mp_number *A, integer64 B); |
| 100 |
static void mp_set_interval_from_boolean(mp_number *A, integer64 B); |
| 101 |
static void mp_set_interval_from_scaled(mp_number *A, integer64 B); |
| 102 |
static void mp_set_interval_from_addition(mp_number *A, mp_number B, mp_number C); |
| 103 |
static void mp_set_interval_from_substraction (mp_number *A, mp_number B, mp_number C); |
| 104 |
static void mp_set_interval_from_div(mp_number *A, mp_number B, mp_number C); |
| 105 |
static void mp_set_interval_from_mul(mp_number *A, mp_number B, mp_number C); |
| 106 |
static void mp_set_interval_from_int_div(mp_number *A, mp_number B, integer64 C); |
| 107 |
static void mp_set_interval_from_int_mul(mp_number *A, mp_number B, integer64 C); |
| 108 |
static void mp_set_interval_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C); |
| 109 |
static void mp_number_negate(mp_number *A); |
| 110 |
static void mp_number_add(mp_number *A, mp_number B); |
| 111 |
static void mp_number_substract(mp_number *A, mp_number B); |
| 112 |
static void mp_number_half(mp_number *A); |
| 113 |
static void mp_number_halfp(mp_number *A); |
| 114 |
static void mp_number_double(mp_number *A); |
| 115 |
static void mp_number_add_scaled(mp_number *A, integer64 B); /* also for negative B */ |
| 116 |
static void mp_number_multiply_int(mp_number *A, integer64 B); |
| 117 |
static void mp_number_divide_int(mp_number *A, integer64 B); |
| 118 |
static void mp_interval_abs(mp_number *A); |
| 119 |
static void mp_number_clone(mp_number *A, mp_number B); |
| 120 |
static void mp_number_swap(mp_number *A, mp_number *B); |
| 121 |
static integer64 mp_round_unscaled(mp_number x_orig); |
| 122 |
static integer64 mp_number_to_int(mp_number A); |
| 123 |
static integer64 mp_number_to_scaled(mp_number A); |
| 124 |
static integer64 mp_number_to_boolean(mp_number A); |
| 125 |
static double mp_number_to_double(mp_number A); |
| 126 |
static int mp_number_odd(mp_number A); |
| 127 |
static int mp_number_equal(mp_number A, mp_number B); |
| 128 |
static int mp_number_greater(mp_number A, mp_number B); |
| 129 |
static int mp_number_less(mp_number A, mp_number B); |
| 130 |
static int mp_number_nonequalabs(mp_number A, mp_number B); |
| 131 |
static void mp_number_floor (mp_number *i); |
| 132 |
static void mp_interval_fraction_to_round_scaled (mp_number *x); |
| 133 |
static void mp_interval_number_make_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 134 |
static void mp_interval_number_make_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 135 |
static void mp_interval_number_take_fraction (MP mp, mp_number *r, mp_number p, mp_number q); |
| 136 |
static void mp_interval_number_take_scaled (MP mp, mp_number *r, mp_number p, mp_number q); |
| 137 |
static void mp_new_number (MP mp, mp_number *n, mp_number_type t) ; |
| 138 |
static void mp_free_number (MP mp, mp_number *n) ; |
| 139 |
static void mp_set_interval_from_double(mp_number *A, double B); |
| 140 |
static void mp_free_interval_math (MP mp); |
| 141 |
static void mp_interval_set_precision (MP mp); |
| 142 |
static void mp_check_mpfi_t (MP mp, mpfi_t dec); |
| 143 |
static int interval_number_check (mpfi_t dec); |
| 144 |
static char * mp_intervalnumber_tostring (mpfi_t n); |
| 145 |
static void init_interval_constants (void); |
| 146 |
static void free_interval_constants (void); |
| 147 |
static mpfr_prec_t precision_digits_to_bits(double i); |
| 148 |
static double precision_bits_to_digits (mpfr_prec_t i); |
| 149 |
/* math interval new primitives */ |
| 150 |
static void mp_interval_m_get_left_endpoint (MP mp, mp_number *ret, mp_number x_orig); |
| 151 |
static void mp_interval_m_get_right_endpoint (MP mp, mp_number *ret, mp_number x_orig); |
| 152 |
static void mp_interval_m_interval_set (MP mp, mp_number *ret, mp_number a, mp_number b); |
| 153 |
|
| 154 |
|
| 155 |
static int mpfi_remainder (mpfi_t ROP, mpfi_t OP1, mpfi_t OP2) ; |
| 156 |
static int mpfi_remainder_1 (mpfi_t ROP, mpfi_t OP1, mpfr_t OP2) ; |
| 157 |
static int mpfi_remainder_2 (mpfi_t ROP, mpfi_t OP1, mpfi_t OP2) ; |
| 158 |
|
| 159 |
@ We do not want special numbers as return values for functions, so: |
| 160 |
|
| 161 |
@d mpfi_negative_p(a) (mpfi_is_strictly_neg((a))>0) |
| 162 |
@d mpfi_positive_p(a) (mpfi_is_strictly_pos((a))>0) |
| 163 |
@d mpfi_overlaps_zero_p(a) (mpfi_cmp_ui(a,0)==0) |
| 164 |
@d mpfi_nonnegative_p(a) (mpfi_is_nonneg((a))>0) |
| 165 |
@d checkZero(dec) if (mpfi_has_zero(dec) ) { // CHECK or mpfi_is_zero ? |
| 166 |
mpfi_set_d(dec,0.0); |
| 167 |
} |
| 168 |
|
| 169 |
@d mpfi_zero_p(a) mpfi_has_zero(a) |
| 170 |
|
| 171 |
@c |
| 172 |
int interval_number_check (mpfi_t dec) |
| 173 |
{ |
| 174 |
int test = false; |
| 175 |
if (mpfi_nan_p(dec)!=0 || mpfi_is_empty(dec)!=0 ) { |
| 176 |
test = true; |
| 177 |
mpfi_set_d(dec,0.0); |
| 178 |
/*if (mpfi_inf_p(dec)) { |
| 179 |
int neg ; |
| 180 |
neg = mpfi_is_neg(dec); |
| 181 |
mpfi_set(dec, EL_GORDO_mpfi_t); |
| 182 |
if (neg) { |
| 183 |
mpfi_neg(dec, dec); |
| 184 |
} |
| 185 |
} else { // Nan |
| 186 |
mpfi_set_d(dec,0.0); |
| 187 |
}*/ |
| 188 |
} |
| 189 |
return test; |
| 190 |
} |
| 191 |
void mp_check_mpfi_t (MP mp, mpfi_t dec) |
| 192 |
{ |
| 193 |
mp->arith_error = interval_number_check (dec); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@ Precision IO uses |double| because |MPFI_PREC_MAX| overflows int. |
| 199 |
|
| 200 |
@c |
| 201 |
static double precision_bits; |
| 202 |
mpfr_prec_t precision_digits_to_bits (double i) |
| 203 |
{ |
| 204 |
return (mpfr_prec_t)(i/log10(2)); |
| 205 |
} |
| 206 |
double precision_bits_to_digits (mpfr_prec_t d) |
| 207 |
{ |
| 208 |
return (double)d*log10(2); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
@ Implement remainder for interval arithmetic. |
| 213 |
|
| 214 |
@c |
| 215 |
/* https://stackoverflow.com/questions/31057473/ */ |
| 216 |
/* calculating-the-modulo-of-two-intervals */ |
| 217 |
|
| 218 |
/* def mod1([a,b], m): */ |
| 219 |
/* // (1): empty interval */ |
| 220 |
/* if a > b || m == 0: */ |
| 221 |
/* return [] */ |
| 222 |
/* // (2): compute modulo with positive interval and negate */ |
| 223 |
/* else if b < 0: */ |
| 224 |
/* return -mod1([-b,-a], m) */ |
| 225 |
/* // (3): split into negative and non-negative interval, compute and join */ |
| 226 |
/* else if a < 0: */ |
| 227 |
/* return mod1([a,-1], m) u mod1([0,b], m) */ |
| 228 |
/* // (4): there is no k > 0 such that a < k*m <= b */ |
| 229 |
/* else if b-a < |m| && a % m <= b % m: */ |
| 230 |
/* return [a % m, b % m] */ |
| 231 |
/* // (5): we can't do better than that */ |
| 232 |
/* else */ |
| 233 |
/* return [0,|m|-1] */ |
| 234 |
|
| 235 |
|
| 236 |
/* def mod2([a,b], [m,n]): */ |
| 237 |
/* // (1): empty interval */ |
| 238 |
/* if a > b || m > n: */ |
| 239 |
/* return [] */ |
| 240 |
/* // (2): compute modulo with positive interval and negate */ |
| 241 |
/* else if b < 0: */ |
| 242 |
/* return -mod2([-b,-a], [m,n]) */ |
| 243 |
/* // (3): split into negative and non-negative interval, compute, and join */ |
| 244 |
/* else if a < 0: */ |
| 245 |
/* return mod2([a,-1], [m,n]) u mod2([0,b], [m,n]) */ |
| 246 |
/* // (4): use the simpler function from before */ |
| 247 |
/* else if m == n: */ |
| 248 |
/* return mod1([a,b], m) */ |
| 249 |
/* // (5): use only non-negative m and n */ |
| 250 |
/* else if n <= 0: */ |
| 251 |
/* return mod2([a,b], [-n,-m]) */ |
| 252 |
/* // (6): similar to (5), make modulus non-negative */ |
| 253 |
/* else if m <= 0: */ |
| 254 |
/* return mod2([a,b], [1, max(-m,n)]) */ |
| 255 |
/* // (7): compare to (4) in mod1, check b-a < |modulus| */ |
| 256 |
/* else if b-a >= n: */ |
| 257 |
/* return [0,n-1] */ |
| 258 |
/* // (8): similar to (7), split interval, compute, and join */ |
| 259 |
/* else if b-a >= m: */ |
| 260 |
/* return [0, b-a-1] u mod2([a,b], [b-a+1,n]) */ |
| 261 |
/* // (9): modulo has no effect */ |
| 262 |
/* else if m > b: */ |
| 263 |
/* return [a,b] */ |
| 264 |
/* // (10): there is some overlapping of [a,b] and [n,m] */ |
| 265 |
/* else if n > b: */ |
| 266 |
/* return [0,b] */ |
| 267 |
/* // (11): either compute all possibilities and join, or be imprecise */ |
| 268 |
/* else: */ |
| 269 |
/* return [0,n-1] // imprecise */ |
| 270 |
|
| 271 |
int mpfi_remainder (mpfi_t r, mpfi_t x, mpfi_t y) { |
| 272 |
// CHECK |
| 273 |
int test,ret_val; |
| 274 |
mpfr_t m,yd; |
| 275 |
mpfr_inits2((mpfr_prec_t)precision_bits, m, yd,(mpfr_ptr) 0); |
| 276 |
test=mpfi_diam(yd, y); |
| 277 |
if (test==0 && mpfr_zero_p(yd)>0){ |
| 278 |
mpfi_get_fr(m, y); |
| 279 |
ret_val = mpfi_remainder_1(r,x,m); |
| 280 |
} else { |
| 281 |
ret_val = mpfi_remainder_2(r,x,y); |
| 282 |
} |
| 283 |
mpfr_clears(m, yd, (mpfr_ptr) 0); |
| 284 |
return ret_val; |
| 285 |
} |
| 286 |
|
| 287 |
static int mpfi_remainder_1 (mpfi_t r, mpfi_t x, mpfr_t m) { |
| 288 |
int ret_val; |
| 289 |
ret_val=-1; |
| 290 |
if (mpfi_is_strictly_neg(x)>0) { |
| 291 |
/*return -mod1([-b,-a], m)*/ |
| 292 |
mpfi_neg (x, x); |
| 293 |
ret_val = mpfi_remainder_1(r,x,m); |
| 294 |
mpfi_neg (r, r); |
| 295 |
return ret_val; |
| 296 |
} else { |
| 297 |
mpfr_t a,b; |
| 298 |
mpfr_inits2((mpfr_prec_t)precision_bits,a,b,(mpfr_ptr) 0); |
| 299 |
mpfi_get_left(a,x); mpfi_get_right(b,x); |
| 300 |
if (mpfr_sgn(a)<0) { |
| 301 |
/*return mod1([a,-1], m) u mod1([0,b], m) */ |
| 302 |
/* which is the same of mod1([1,-a], m) u mod1([0,b], m) */ |
| 303 |
mpfi_t l1,l2,ret1, ret2 ; |
| 304 |
mpfr_t one,zero ; |
| 305 |
mpfi_inits2((mpfr_prec_t)precision_bits, ret1, ret2, l1, l2, (mpfi_ptr) 0); |
| 306 |
mpfr_inits2((mpfr_prec_t)precision_bits, one, zero, (mpfr_ptr) 0); |
| 307 |
mpfr_set_si(one, 1, MPFR_RNDN); mpfr_set_si(zero, 0, MPFR_RNDN); |
| 308 |
mpfr_neg(a, a, MPFR_RNDN); |
| 309 |
mpfi_interv_fr(l1, one, a); mpfi_interv_fr(l2, zero, b); |
| 310 |
mpfi_remainder_1(ret1,l1,m); mpfi_remainder_1(ret2,l2,m); |
| 311 |
ret_val = mpfi_union(r,ret1,ret2); |
| 312 |
mpfi_clears(ret1, ret2, l1,l2,(mpfi_ptr)0); |
| 313 |
mpfr_clears(one,zero,(mpfr_ptr)0); |
| 314 |
} else { |
| 315 |
/*if b-a < |m| && a % m <= b % m:*/ |
| 316 |
mpfr_t d,abs_m, rem_a,rem_b,zero,one,abs_m_1; |
| 317 |
mpfr_inits2((mpfr_prec_t)precision_bits, d, abs_m, rem_a, rem_b, zero, one, abs_m_1,(mpfr_ptr) 0); |
| 318 |
mpfr_sub(d, b, a, MPFR_RNDN); |
| 319 |
mpfr_abs(abs_m, m, MPFR_RNDN); |
| 320 |
/*mpfr_remainder(rem_a, a, m, MPFR_RNDN); mpfr_remainder(rem_b, b, m, MPFR_RNDN); */ |
| 321 |
/* This is consistent with scaled mode */ |
| 322 |
mpfr_fmod(rem_a, a, m, MPFR_RNDN); mpfr_fmod(rem_b, b, m, MPFR_RNDN); |
| 323 |
if (mpfr_less_p(d,abs_m) && mpfr_lessequal_p(rem_a,rem_b)) { |
| 324 |
/*return [a % m, b % m] */ |
| 325 |
ret_val= mpfi_interv_fr(r, rem_a, rem_b); |
| 326 |
} else { |
| 327 |
/* return [0,|m|-1] */ |
| 328 |
mpfr_set_si(one, 1, MPFR_RNDN); mpfr_set_si(zero, 0, MPFR_RNDN); mpfr_set_si(zero, 0, MPFR_RNDN); |
| 329 |
mpfr_sub(abs_m_1, abs_m, one, MPFR_RNDN); |
| 330 |
ret_val = mpfi_interv_fr(r, zero, abs_m_1); |
| 331 |
} |
| 332 |
mpfr_clears(d, abs_m, rem_a, rem_b, zero, one, abs_m_1, (mpfr_ptr)0); |
| 333 |
} |
| 334 |
mpfr_clears(a, b, (mpfr_ptr)0); |
| 335 |
} |
| 336 |
return ret_val; |
| 337 |
} |
| 338 |
|
| 339 |
static int mpfi_remainder_2 (mpfi_t r, mpfi_t x, mpfi_t m) { |
| 340 |
int ret_val; |
| 341 |
if (mpfi_is_strictly_neg(x)>0) { |
| 342 |
/*return -mod2([-b,-a], [m,n])*/ |
| 343 |
mpfi_neg (x, x); |
| 344 |
ret_val = mpfi_remainder_2(r,x,m); |
| 345 |
mpfi_neg (r, r); |
| 346 |
return ret_val; |
| 347 |
} else { |
| 348 |
mpfr_t a,b,n1,m1; |
| 349 |
mpfr_t one, zero ; |
| 350 |
mpfr_inits2((mpfr_prec_t)precision_bits, one, zero, (mpfr_ptr) 0); |
| 351 |
mpfr_set_si(one, 1, MPFR_RNDN); mpfr_set_si(zero, 0, MPFR_RNDN); |
| 352 |
mpfr_inits2((mpfr_prec_t)precision_bits,a,b,(mpfr_ptr) 0); |
| 353 |
mpfi_get_left(a,x); mpfi_get_right(b,x); |
| 354 |
mpfi_get_left(m1,m); mpfi_get_right(n1,m); |
| 355 |
if (mpfr_sgn(a)<0) { |
| 356 |
/* return mod2([a,-1], [m,n]) u mod2([0,b], [m,n]) */ |
| 357 |
mpfi_t l1, l2, ret1, ret2 ; |
| 358 |
mpfi_inits2((mpfr_prec_t)precision_bits, ret1, ret2, l1, l2, (mpfi_ptr) 0); |
| 359 |
mpfr_neg(a,a,MPFR_RNDN); |
| 360 |
mpfi_interv_fr(l1, one, a); mpfi_interv_fr(l2, zero, b); |
| 361 |
mpfi_remainder_2(ret1,l1,m); mpfi_remainder_2(ret2,l2,m); |
| 362 |
ret_val = mpfi_union(r,ret1,ret2); |
| 363 |
mpfi_clears(ret1, ret2, l1,l2,(mpfi_ptr)0); |
| 364 |
} else { |
| 365 |
int test; |
| 366 |
mpfr_t yd; |
| 367 |
mpfr_inits2((mpfr_prec_t)precision_bits, yd, (mpfr_ptr) 0); |
| 368 |
test=mpfi_diam(yd, m); |
| 369 |
if(test==0 && mpfr_zero_p(yd)>0){ |
| 370 |
/*return mod1([a,b], m)*/ |
| 371 |
mpfr_t m2; |
| 372 |
mpfr_inits2((mpfr_prec_t)precision_bits, m2,(mpfr_ptr) 0); |
| 373 |
mpfi_get_fr(m2, m); |
| 374 |
ret_val = mpfi_remainder_1(r,x,m2); |
| 375 |
mpfr_clears(m2, (mpfi_ptr)0); |
| 376 |
} else if (mpfr_sgn(n1)<=0) { |
| 377 |
/*return mod2([a,b], [-n,-m]) */ |
| 378 |
mpfi_neg(m, m); |
| 379 |
ret_val = mpfi_remainder_2(r,x,m); |
| 380 |
} else if(mpfr_sgn(m1)<=0) { |
| 381 |
/* return mod2([a,b], [1, max(-m,n)]) */ |
| 382 |
mpfr_t r1; |
| 383 |
mpfi_t l1; |
| 384 |
mpfi_inits2((mpfr_prec_t)precision_bits, l1, (mpfi_ptr) 0); |
| 385 |
mpfr_inits2((mpfr_prec_t)precision_bits, r1, (mpfr_ptr) 0); |
| 386 |
mpfr_neg(m1,m1,MPFR_RNDN); |
| 387 |
/*mpfr_max (r1, m1, n1, MPFR_RNDN);*/ |
| 388 |
if(mpfr_greater_p(m1,n1)){ |
| 389 |
mpfr_set(r1,m1,MPFR_RNDN); |
| 390 |
} else { |
| 391 |
mpfr_set(r1,n1,MPFR_RNDN); |
| 392 |
} |
| 393 |
mpfi_interv_fr(l1, one, r1); |
| 394 |
ret_val = mpfi_remainder_2(r,x,l1); |
| 395 |
mpfr_clears(r1, (mpfr_ptr)0); |
| 396 |
mpfi_clears(l1, (mpfi_ptr)0); |
| 397 |
} else { |
| 398 |
mpfr_t d; |
| 399 |
mpfr_inits2((mpfr_prec_t)precision_bits, d, (mpfr_ptr) 0); |
| 400 |
mpfr_sub(d, b, a, MPFR_RNDN); |
| 401 |
if(mpfr_greaterequal_p(d, n1)!=0){ |
| 402 |
/*return [0,n-1]*/ |
| 403 |
mpfr_sub(n1,n1,one, MPFR_RNDN); |
| 404 |
ret_val = mpfi_interv_fr(r, zero, n1); |
| 405 |
} else if(mpfr_greaterequal_p(d, m1)!=0){ |
| 406 |
/*return [0, b-a-1] u mod2([a,b], [b-a+1,n])*/ |
| 407 |
mpfi_t r1,l1,l2; |
| 408 |
mpfi_inits2((mpfr_prec_t)precision_bits, l1, l2,(mpfi_ptr) 0); |
| 409 |
mpfr_sub(d, d, one, MPFR_RNDN); |
| 410 |
mpfi_interv_fr(l1, zero, d);/* [0, b-a-1] */ |
| 411 |
mpfi_interv_fr(l2, d, n1);/* [b-a+1,n] */ |
| 412 |
mpfi_remainder_2(r1,x,l2);/* mod2([a,b], [b-a+1,n])*/ |
| 413 |
ret_val = mpfi_union(r,l1,r1); /* [0, b-a-1] u mod2([a,b], [b-a+1,n])*/ |
| 414 |
mpfi_clears(r1, l1, l2, (mpfi_ptr)0); |
| 415 |
} else if(mpfr_greater_p(m1,b)!=0){ |
| 416 |
/* return [a,b]*/ |
| 417 |
ret_val = mpfi_set(r,x); |
| 418 |
} else if (mpfr_greater_p(n1,b)!=0) { |
| 419 |
/*return [0,b]*/ |
| 420 |
mpfi_t l1; |
| 421 |
mpfi_inits2((mpfr_prec_t)precision_bits, l1,(mpfi_ptr) 0); |
| 422 |
mpfi_interv_fr(l1, zero, b); |
| 423 |
ret_val = mpfi_set(r,l1); |
| 424 |
mpfi_clears(l1, (mpfi_ptr)0); |
| 425 |
} else { |
| 426 |
/*return [0,n-1] // imprecise */ |
| 427 |
mpfi_t l1; |
| 428 |
mpfi_inits2((mpfr_prec_t)precision_bits, l1,(mpfi_ptr) 0); |
| 429 |
mpfr_sub(n1, n1, one, MPFR_RNDN); |
| 430 |
mpfi_interv_fr(l1, zero, n1); |
| 431 |
ret_val = mpfi_set(r,l1); |
| 432 |
mpfi_clears(l1, (mpfi_ptr)0); |
| 433 |
} |
| 434 |
mpfr_clears(d, (mpfr_ptr)0); |
| 435 |
} |
| 436 |
} |
| 437 |
mpfr_clears(one,zero,(mpfr_ptr)0); |
| 438 |
} |
| 439 |
return ret_val; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
@ And these are the ones that {\it are} used elsewhere |
| 444 |
|
| 445 |
@<Internal library declarations@>= |
| 446 |
void * mp_initialize_interval_math (MP mp); |
| 447 |
|
| 448 |
@ |
| 449 |
|
| 450 |
@d unity 1 |
| 451 |
@d two 2 |
| 452 |
@d three 3 |
| 453 |
@d four 4 |
| 454 |
@d half_unit 0.5 |
| 455 |
@d three_quarter_unit 0.75 |
| 456 |
@d coef_bound ((7.0/3.0)*fraction_multiplier) /* |fraction| approximation to 7/3 */ |
| 457 |
@d fraction_threshold 0.04096 /* a |fraction| coefficient less than this is zeroed */ |
| 458 |
@d half_fraction_threshold (fraction_threshold/2) /* half of |fraction_threshold| */ |
| 459 |
@d scaled_threshold 0.000122 /* a |scaled| coefficient less than this is zeroed */ |
| 460 |
@d half_scaled_threshold (scaled_threshold/2) /* half of |scaled_threshold| */ |
| 461 |
@d near_zero_angle (0.0256*angle_multiplier) /* an angle of about 0.0256 */ |
| 462 |
@d p_over_v_threshold 0x80000 /* TODO */ |
| 463 |
@d equation_threshold 0.001 |
| 464 |
@d tfm_warn_threshold 0.0625 |
| 465 |
@d warning_limit pow(2.0,52.0) /* this is a large value that can just be expressed without loss of precision */ |
| 466 |
@d epsilon pow(2.0,-173.0) /* almost "1E-52" */ |
| 467 |
@d epsilonf pow(2.0,-52.0) |
| 468 |
@d EL_GORDO "1E1000000" /* the largest value that \MP\ likes. */ |
| 469 |
@d one_third_EL_GORDO (EL_GORDO/3.0) |
| 470 |
|
| 471 |
@<Declarations@>= |
| 472 |
static mpfi_t zero; |
| 473 |
static mpfi_t one; |
| 474 |
static mpfi_t minusone; |
| 475 |
static mpfi_t two_mpfi_t; |
| 476 |
static mpfi_t three_mpfi_t; |
| 477 |
static mpfi_t four_mpfi_t; |
| 478 |
static mpfi_t fraction_multiplier_mpfi_t; |
| 479 |
static mpfi_t angle_multiplier_mpfi_t; |
| 480 |
static mpfi_t fraction_one_mpfi_t; |
| 481 |
static mpfi_t fraction_one_plus_mpfi_t; |
| 482 |
static mpfi_t PI_mpfi_t; |
| 483 |
static mpfi_t epsilon_mpfi_t; |
| 484 |
static mpfi_t EL_GORDO_mpfi_t; |
| 485 |
static boolean initialized = false; |
| 486 |
|
| 487 |
|
| 488 |
@ @c |
| 489 |
void init_interval_constants (void) { |
| 490 |
if (!initialized) { |
| 491 |
mpfi_inits2 ((mpfr_prec_t)precision_bits, one, minusone, zero, two_mpfi_t, three_mpfi_t, four_mpfi_t, fraction_multiplier_mpfi_t, |
| 492 |
fraction_one_mpfi_t, fraction_one_plus_mpfi_t, angle_multiplier_mpfi_t, PI_mpfi_t, |
| 493 |
epsilon_mpfi_t, EL_GORDO_mpfi_t, (mpfi_ptr) 0); |
| 494 |
mpfi_set_si (one, 1); |
| 495 |
mpfi_set_si (minusone, -1); |
| 496 |
mpfi_set_si (zero, 0); |
| 497 |
mpfi_set_si (two_mpfi_t, two); |
| 498 |
mpfi_set_si (three_mpfi_t, three); |
| 499 |
mpfi_set_si (four_mpfi_t, four); |
| 500 |
mpfi_set_si (fraction_multiplier_mpfi_t, fraction_multiplier); |
| 501 |
mpfi_set_si (fraction_one_mpfi_t, fraction_one); |
| 502 |
mpfi_set_si (fraction_one_plus_mpfi_t, (fraction_one+1)); |
| 503 |
mpfi_set_si (angle_multiplier_mpfi_t, angle_multiplier); |
| 504 |
mpfi_set_str (PI_mpfi_t, PI_STRING, 10); |
| 505 |
mpfi_set_d(epsilon_mpfi_t, epsilon ); |
| 506 |
mpfi_set_str (EL_GORDO_mpfi_t, EL_GORDO, 10); //); |
| 507 |
initialized = true; |
| 508 |
} |
| 509 |
} |
| 510 |
void free_interval_constants (void) { |
| 511 |
/* For sake of speed, we accept this memory leak. */ |
| 512 |
/*mpfi_clears (one, minusone, zero, two_mpfi_t, three_mpfi_t, four_mpfi_t, fraction_multiplier_mpfi_t,*/ |
| 513 |
/* fraction_one_mpfi_t, fraction_one_plus_mpfi_t, angle_multiplier_mpfi_t, PI_mpfi_t, */ |
| 514 |
/* epsilon_mpfi_t, EL_GORDO_mpfi_t, (mpfi_ptr) 0); */ |
| 515 |
/*mpfi_free_cache ();*/ |
| 516 |
} |
| 517 |
|
| 518 |
@ |precision_max| is limited to 1000, because the precision of already initialized |
| 519 |
|mpfi_t| numbers cannot be raised, only lowered. The value of 1000.0 is a tradeoff |
| 520 |
between precision and allocation size / processing speed. |
| 521 |
|
| 522 |
@d MAX_PRECISION 1000.0 |
| 523 |
@d DEF_PRECISION 34.0 |
| 524 |
|
| 525 |
@c |
| 526 |
void * mp_initialize_interval_math (MP mp) { |
| 527 |
math_data *math = (math_data *)mp_xmalloc(mp,1,sizeof(math_data)); |
| 528 |
precision_bits = (double)precision_digits_to_bits(MAX_PRECISION); |
| 529 |
init_interval_constants(); |
| 530 |
/* alloc */ |
| 531 |
math->allocate = mp_new_number; |
| 532 |
math->free = mp_free_number; |
| 533 |
mp_new_number (mp, &math->precision_default, mp_scaled_type); |
| 534 |
mpfi_set_d(math->precision_default.data.num, DEF_PRECISION); |
| 535 |
mp_new_number (mp, &math->precision_max, mp_scaled_type); |
| 536 |
mpfi_set_d(math->precision_max.data.num, MAX_PRECISION); |
| 537 |
mp_new_number (mp, &math->precision_min, mp_scaled_type); |
| 538 |
/* really should be |precision_bits_to_digits(MPFI_PREC_MIN)| but that produces a horrible number */ |
| 539 |
mpfi_set_d(math->precision_min.data.num, 2.0 ); |
| 540 |
/* here are the constants for |scaled| objects */ |
| 541 |
mp_new_number (mp, &math->epsilon_t, mp_scaled_type); |
| 542 |
mpfi_set (math->epsilon_t.data.num, epsilon_mpfi_t); |
| 543 |
mp_new_number (mp, &math->inf_t, mp_scaled_type); |
| 544 |
mpfi_set (math->inf_t.data.num, EL_GORDO_mpfi_t); |
| 545 |
mp_new_number (mp, &math->warning_limit_t, mp_scaled_type); |
| 546 |
mpfi_set_d (math->warning_limit_t.data.num, warning_limit); |
| 547 |
mp_new_number (mp, &math->one_third_inf_t, mp_scaled_type); |
| 548 |
mpfi_div (math->one_third_inf_t.data.num, math->inf_t.data.num, three_mpfi_t); |
| 549 |
mp_new_number (mp, &math->unity_t, mp_scaled_type); |
| 550 |
mpfi_set (math->unity_t.data.num, one); |
| 551 |
mp_new_number (mp, &math->two_t, mp_scaled_type); |
| 552 |
mpfi_set_si(math->two_t.data.num, two); |
| 553 |
mp_new_number (mp, &math->three_t, mp_scaled_type); |
| 554 |
mpfi_set_si(math->three_t.data.num, three); |
| 555 |
mp_new_number (mp, &math->half_unit_t, mp_scaled_type); |
| 556 |
mpfi_set_d(math->half_unit_t.data.num, half_unit); |
| 557 |
mp_new_number (mp, &math->three_quarter_unit_t, mp_scaled_type); |
| 558 |
mpfi_set_d (math->three_quarter_unit_t.data.num, three_quarter_unit); |
| 559 |
mp_new_number (mp, &math->zero_t, mp_scaled_type); |
| 560 |
/*mpfi_set_zero (math->zero_t.data.num, 1);*/ |
| 561 |
mpfi_set_d (math->zero_t.data.num, 0.0); |
| 562 |
/* |fractions| */ |
| 563 |
mp_new_number (mp, &math->arc_tol_k, mp_fraction_type); |
| 564 |
{ |
| 565 |
mpfi_div_si (math->arc_tol_k.data.num, one, 4096); |
| 566 |
/* quit when change in arc length estimate reaches this */ |
| 567 |
} |
| 568 |
mp_new_number (mp, &math->fraction_one_t, mp_fraction_type); |
| 569 |
mpfi_set_si(math->fraction_one_t.data.num, fraction_one); |
| 570 |
mp_new_number (mp, &math->fraction_half_t, mp_fraction_type); |
| 571 |
mpfi_set_si(math->fraction_half_t.data.num, fraction_half); |
| 572 |
mp_new_number (mp, &math->fraction_three_t, mp_fraction_type); |
| 573 |
mpfi_set_si(math->fraction_three_t.data.num, fraction_three); |
| 574 |
mp_new_number (mp, &math->fraction_four_t, mp_fraction_type); |
| 575 |
mpfi_set_si(math->fraction_four_t.data.num, fraction_four); |
| 576 |
/* |angles| */ |
| 577 |
mp_new_number (mp, &math->three_sixty_deg_t, mp_angle_type); |
| 578 |
mpfi_set_si(math->three_sixty_deg_t.data.num, 360 * angle_multiplier); |
| 579 |
mp_new_number (mp, &math->one_eighty_deg_t, mp_angle_type); |
| 580 |
mpfi_set_si(math->one_eighty_deg_t.data.num, 180 * angle_multiplier); |
| 581 |
/* various approximations */ |
| 582 |
mp_new_number (mp, &math->one_k, mp_scaled_type); |
| 583 |
mpfi_set_d(math->one_k.data.num, 1.0/64); |
| 584 |
mp_new_number (mp, &math->sqrt_8_e_k, mp_scaled_type); |
| 585 |
{ |
| 586 |
mpfi_set_d(math->sqrt_8_e_k.data.num, 112428.82793 / 65536.0); |
| 587 |
/* $2^{16}\sqrt{8/e}\approx 112428.82793$ */ |
| 588 |
} |
| 589 |
mp_new_number (mp, &math->twelve_ln_2_k, mp_fraction_type); |
| 590 |
{ |
| 591 |
mpfi_set_d(math->twelve_ln_2_k.data.num, 139548959.6165 / 65536.0); |
| 592 |
/* $2^{24}\cdot12\ln2\approx139548959.6165$ */ |
| 593 |
} |
| 594 |
mp_new_number (mp, &math->coef_bound_k, mp_fraction_type); |
| 595 |
mpfi_set_d(math->coef_bound_k.data.num,coef_bound); |
| 596 |
mp_new_number (mp, &math->coef_bound_minus_1, mp_fraction_type); |
| 597 |
mpfi_set_d(math->coef_bound_minus_1.data.num,coef_bound - 1 / 65536.0); |
| 598 |
mp_new_number (mp, &math->twelvebits_3, mp_scaled_type); |
| 599 |
{ |
| 600 |
mpfi_set_d(math->twelvebits_3.data.num, 1365 / 65536.0); |
| 601 |
/* $1365\approx 2^{12}/3$ */ |
| 602 |
} |
| 603 |
mp_new_number (mp, &math->twentysixbits_sqrt2_t, mp_fraction_type); |
| 604 |
{ |
| 605 |
mpfi_set_d(math->twentysixbits_sqrt2_t.data.num, 94906265.62 / 65536.0); |
| 606 |
/* $2^{26}\sqrt2\approx94906265.62$ */ |
| 607 |
} |
| 608 |
mp_new_number (mp, &math->twentyeightbits_d_t, mp_fraction_type); |
| 609 |
{ |
| 610 |
mpfi_set_d(math->twentyeightbits_d_t.data.num, 35596754.69 / 65536.0); |
| 611 |
/* $2^{28}d\approx35596754.69$ */ |
| 612 |
} |
| 613 |
mp_new_number (mp, &math->twentysevenbits_sqrt2_d_t, mp_fraction_type); |
| 614 |
{ |
| 615 |
mpfi_set_d(math->twentysevenbits_sqrt2_d_t.data.num, 25170706.63 / 65536.0); |
| 616 |
/* $2^{27}\sqrt2\,d\approx25170706.63$ */ |
| 617 |
} |
| 618 |
/* thresholds */ |
| 619 |
mp_new_number (mp, &math->fraction_threshold_t, mp_fraction_type); |
| 620 |
mpfi_set_d(math->fraction_threshold_t.data.num, fraction_threshold); |
| 621 |
mp_new_number (mp, &math->half_fraction_threshold_t, mp_fraction_type); |
| 622 |
mpfi_set_d(math->half_fraction_threshold_t.data.num, half_fraction_threshold); |
| 623 |
mp_new_number (mp, &math->scaled_threshold_t, mp_scaled_type); |
| 624 |
mpfi_set_d(math->scaled_threshold_t.data.num, scaled_threshold); |
| 625 |
mp_new_number (mp, &math->half_scaled_threshold_t, mp_scaled_type); |
| 626 |
mpfi_set_d(math->half_scaled_threshold_t.data.num, half_scaled_threshold); |
| 627 |
mp_new_number (mp, &math->near_zero_angle_t, mp_angle_type); |
| 628 |
mpfi_set_d(math->near_zero_angle_t.data.num, near_zero_angle); |
| 629 |
mp_new_number (mp, &math->p_over_v_threshold_t, mp_fraction_type); |
| 630 |
mpfi_set_d(math->p_over_v_threshold_t.data.num, p_over_v_threshold); |
| 631 |
mp_new_number (mp, &math->equation_threshold_t, mp_scaled_type); |
| 632 |
mpfi_set_d(math->equation_threshold_t.data.num, equation_threshold); |
| 633 |
mp_new_number (mp, &math->tfm_warn_threshold_t, mp_scaled_type); |
| 634 |
mpfi_set_d(math->tfm_warn_threshold_t.data.num, tfm_warn_threshold); |
| 635 |
/* functions */ |
| 636 |
math->from_int = mp_set_interval_from_int; |
| 637 |
math->from_boolean = mp_set_interval_from_boolean; |
| 638 |
math->from_scaled = mp_set_interval_from_scaled; |
| 639 |
math->from_double = mp_set_interval_from_double; |
| 640 |
math->from_addition = mp_set_interval_from_addition; |
| 641 |
math->from_substraction = mp_set_interval_from_substraction; |
| 642 |
math->from_oftheway = mp_set_interval_from_of_the_way; |
| 643 |
math->from_div = mp_set_interval_from_div; |
| 644 |
math->from_mul = mp_set_interval_from_mul; |
| 645 |
math->from_int_div = mp_set_interval_from_int_div; |
| 646 |
math->from_int_mul = mp_set_interval_from_int_mul; |
| 647 |
math->negate = mp_number_negate; |
| 648 |
math->add = mp_number_add; |
| 649 |
math->substract = mp_number_substract; |
| 650 |
math->half = mp_number_half; |
| 651 |
math->halfp = mp_number_halfp; |
| 652 |
math->do_double = mp_number_double; |
| 653 |
math->abs = mp_interval_abs; |
| 654 |
math->clone = mp_number_clone; |
| 655 |
math->swap = mp_number_swap; |
| 656 |
math->add_scaled = mp_number_add_scaled; |
| 657 |
math->multiply_int = mp_number_multiply_int; |
| 658 |
math->divide_int = mp_number_divide_int; |
| 659 |
math->to_boolean = mp_number_to_boolean; |
| 660 |
math->to_scaled = mp_number_to_scaled; |
| 661 |
math->to_double = mp_number_to_double; |
| 662 |
math->to_int = mp_number_to_int; |
| 663 |
math->odd = mp_number_odd; |
| 664 |
math->equal = mp_number_equal; |
| 665 |
math->less = mp_number_less; |
| 666 |
math->greater = mp_number_greater; |
| 667 |
math->nonequalabs = mp_number_nonequalabs; |
| 668 |
math->round_unscaled = mp_round_unscaled; |
| 669 |
math->floor_scaled = mp_number_floor; |
| 670 |
math->fraction_to_round_scaled = mp_interval_fraction_to_round_scaled; |
| 671 |
math->make_scaled = mp_interval_number_make_scaled; |
| 672 |
math->make_fraction = mp_interval_number_make_fraction; |
| 673 |
math->take_fraction = mp_interval_number_take_fraction; |
| 674 |
math->take_scaled = mp_interval_number_take_scaled; |
| 675 |
math->velocity = mp_interval_velocity; |
| 676 |
math->n_arg = mp_interval_n_arg; |
| 677 |
math->m_log = mp_interval_m_log; |
| 678 |
math->m_exp = mp_interval_m_exp; |
| 679 |
math->m_unif_rand = mp_interval_m_unif_rand; |
| 680 |
math->m_norm_rand = mp_interval_m_norm_rand; |
| 681 |
math->pyth_add = mp_interval_pyth_add; |
| 682 |
math->pyth_sub = mp_interval_pyth_sub; |
| 683 |
math->fraction_to_scaled = mp_number_fraction_to_scaled; |
| 684 |
math->scaled_to_fraction = mp_number_scaled_to_fraction; |
| 685 |
math->scaled_to_angle = mp_number_scaled_to_angle; |
| 686 |
math->angle_to_scaled = mp_number_angle_to_scaled; |
| 687 |
math->init_randoms = mp_init_randoms; |
| 688 |
math->sin_cos = mp_interval_sin_cos; |
| 689 |
math->slow_add = mp_interval_slow_add; |
| 690 |
math->sqrt = mp_interval_square_rt; |
| 691 |
math->print = mp_interval_print_number; |
| 692 |
math->tostring = mp_interval_number_tostring; |
| 693 |
math->modulo = mp_interval_number_modulo; |
| 694 |
math->ab_vs_cd = mp_ab_vs_cd; |
| 695 |
math->crossing_point = mp_interval_crossing_point; |
| 696 |
math->scan_numeric = mp_interval_scan_numeric_token; |
| 697 |
math->scan_fractional = mp_interval_scan_fractional_token; |
| 698 |
math->free_math = mp_free_interval_math; |
| 699 |
math->set_precision = mp_interval_set_precision; |
| 700 |
/* math interval new primitives */ |
| 701 |
math->m_get_left_endpoint = mp_interval_m_get_left_endpoint; |
| 702 |
math->m_get_right_endpoint = mp_interval_m_get_right_endpoint; |
| 703 |
math->m_interval_set = mp_interval_m_interval_set ; |
| 704 |
|
| 705 |
return (void *)math; |
| 706 |
} |
| 707 |
|
| 708 |
void mp_interval_set_precision (MP mp) { |
| 709 |
double d = mpfi_get_d(internal_value (mp_number_precision).data.num); |
| 710 |
precision_bits = (double)precision_digits_to_bits(d); |
| 711 |
} |
| 712 |
|
| 713 |
void mp_free_interval_math (MP mp) { |
| 714 |
free_number (((math_data *)mp->math)->three_sixty_deg_t); |
| 715 |
free_number (((math_data *)mp->math)->one_eighty_deg_t); |
| 716 |
free_number (((math_data *)mp->math)->fraction_one_t); |
| 717 |
free_number (((math_data *)mp->math)->zero_t); |
| 718 |
free_number (((math_data *)mp->math)->half_unit_t); |
| 719 |
free_number (((math_data *)mp->math)->three_quarter_unit_t); |
| 720 |
free_number (((math_data *)mp->math)->unity_t); |
| 721 |
free_number (((math_data *)mp->math)->two_t); |
| 722 |
free_number (((math_data *)mp->math)->three_t); |
| 723 |
free_number (((math_data *)mp->math)->one_third_inf_t); |
| 724 |
free_number (((math_data *)mp->math)->inf_t); |
| 725 |
free_number (((math_data *)mp->math)->warning_limit_t); |
| 726 |
free_number (((math_data *)mp->math)->one_k); |
| 727 |
free_number (((math_data *)mp->math)->sqrt_8_e_k); |
| 728 |
free_number (((math_data *)mp->math)->twelve_ln_2_k); |
| 729 |
free_number (((math_data *)mp->math)->coef_bound_k); |
| 730 |
free_number (((math_data *)mp->math)->coef_bound_minus_1); |
| 731 |
free_number (((math_data *)mp->math)->fraction_threshold_t); |
| 732 |
free_number (((math_data *)mp->math)->half_fraction_threshold_t); |
| 733 |
free_number (((math_data *)mp->math)->scaled_threshold_t); |
| 734 |
free_number (((math_data *)mp->math)->half_scaled_threshold_t); |
| 735 |
free_number (((math_data *)mp->math)->near_zero_angle_t); |
| 736 |
free_number (((math_data *)mp->math)->p_over_v_threshold_t); |
| 737 |
free_number (((math_data *)mp->math)->equation_threshold_t); |
| 738 |
free_number (((math_data *)mp->math)->tfm_warn_threshold_t); |
| 739 |
free_interval_constants(); |
| 740 |
free(mp->math); |
| 741 |
} |
| 742 |
|
| 743 |
@ Creating an destroying |mp_number| objects |
| 744 |
|
| 745 |
@ @c |
| 746 |
void mp_new_number (MP mp, mp_number *n, mp_number_type t) { |
| 747 |
(void)mp; |
| 748 |
n->data.num = mp_xmalloc(mp,1,sizeof(mpfi_t)); |
| 749 |
mpfi_init2 ((mpfi_ptr)(n->data.num), (mpfr_prec_t)precision_bits); |
| 750 |
/*mpfi_set_zero((mpfi_ptr)(n->data.num),1); *//* 1 == positive */ |
| 751 |
mpfi_set_d((mpfi_ptr)(n->data.num),0.0); |
| 752 |
n->type = t; |
| 753 |
} |
| 754 |
|
| 755 |
@ |
| 756 |
|
| 757 |
@c |
| 758 |
void mp_free_number (MP mp, mp_number *n) { |
| 759 |
(void)mp; |
| 760 |
if (n->data.num) { |
| 761 |
mpfi_clear (n->data.num); |
| 762 |
n->data.num = NULL; |
| 763 |
} |
| 764 |
n->type = mp_nan_type; |
| 765 |
} |
| 766 |
|
| 767 |
@ Here are the low-level functions on |mp_number| items, setters first. |
| 768 |
|
| 769 |
@c |
| 770 |
void mp_set_interval_from_int(mp_number *A, integer64 B) { |
| 771 |
mpfi_set_si(A->data.num,B); |
| 772 |
} |
| 773 |
void mp_set_interval_from_boolean(mp_number *A, integer64 B) { |
| 774 |
mpfi_set_si(A->data.num,B); |
| 775 |
} |
| 776 |
void mp_set_interval_from_scaled(mp_number *A, integer64 B) { |
| 777 |
mpfi_set_si(A->data.num, B); |
| 778 |
mpfi_div_si(A->data.num, A->data.num, 65536); |
| 779 |
} |
| 780 |
void mp_set_interval_from_double(mp_number *A, double B) { |
| 781 |
mpfi_set_d(A->data.num, B); |
| 782 |
} |
| 783 |
void mp_set_interval_from_addition(mp_number *A, mp_number B, mp_number C) { |
| 784 |
mpfi_add(A->data.num,B.data.num,C.data.num); |
| 785 |
} |
| 786 |
void mp_set_interval_from_substraction (mp_number *A, mp_number B, mp_number C) { |
| 787 |
mpfi_sub(A->data.num,B.data.num,C.data.num); |
| 788 |
} |
| 789 |
void mp_set_interval_from_div(mp_number *A, mp_number B, mp_number C) { |
| 790 |
mpfi_div(A->data.num,B.data.num,C.data.num); |
| 791 |
} |
| 792 |
void mp_set_interval_from_mul(mp_number *A, mp_number B, mp_number C) { |
| 793 |
mpfi_mul(A->data.num,B.data.num,C.data.num); |
| 794 |
} |
| 795 |
void mp_set_interval_from_int_div(mp_number *A, mp_number B, integer64 C) { |
| 796 |
mpfi_div_si(A->data.num,B.data.num,C); |
| 797 |
} |
| 798 |
void mp_set_interval_from_int_mul(mp_number *A, mp_number B, integer64 C) { |
| 799 |
mpfi_mul_si(A->data.num,B.data.num, C); |
| 800 |
} |
| 801 |
void mp_set_interval_from_of_the_way(MP mp, mp_number *A, mp_number t, mp_number B, mp_number C) { |
| 802 |
mpfi_t c, r1; |
| 803 |
mpfi_init2(c, (mpfr_prec_t)precision_bits); |
| 804 |
mpfi_init2(r1, (mpfr_prec_t)precision_bits); |
| 805 |
mpfi_sub (c,B.data.num, C.data.num); |
| 806 |
mp_interval_take_fraction(mp, r1, c, t.data.num); |
| 807 |
mpfi_sub (A->data.num, B.data.num, r1); |
| 808 |
mpfi_clear(c); |
| 809 |
mpfi_clear(r1); |
| 810 |
mp_check_mpfi_t(mp, A->data.num); |
| 811 |
} |
| 812 |
void mp_number_negate(mp_number *A) { |
| 813 |
mpfi_t c; |
| 814 |
mpfi_init2(c, (mpfr_prec_t)precision_bits); |
| 815 |
mpfi_neg (c, A->data.num); |
| 816 |
mpfi_set((mpfi_ptr)A->data.num,c); |
| 817 |
mpfi_clear(c); |
| 818 |
} |
| 819 |
void mp_number_add(mp_number *A, mp_number B) { |
| 820 |
mpfi_add (A->data.num,A->data.num,B.data.num); |
| 821 |
} |
| 822 |
void mp_number_substract(mp_number *A, mp_number B) { |
| 823 |
mpfi_sub (A->data.num,A->data.num,B.data.num); |
| 824 |
} |
| 825 |
void mp_number_half(mp_number *A) { |
| 826 |
mpfi_div_si(A->data.num, A->data.num, 2); |
| 827 |
} |
| 828 |
void mp_number_halfp(mp_number *A) { |
| 829 |
mpfi_div_si(A->data.num,A->data.num, 2); |
| 830 |
} |
| 831 |
void mp_number_double(mp_number *A) { |
| 832 |
mpfi_mul_si(A->data.num,A->data.num, 2); |
| 833 |
} |
| 834 |
void mp_number_add_scaled(mp_number *A, integer64 B) { /* also for negative B */ |
| 835 |
mpfi_add_d (A->data.num,A->data.num, (double)B/65536.0); |
| 836 |
} |
| 837 |
void mp_number_multiply_int(mp_number *A, integer64 B) { |
| 838 |
mpfi_mul_si(A->data.num,A->data.num, B); |
| 839 |
} |
| 840 |
void mp_number_divide_int(mp_number *A, integer64 B) { |
| 841 |
mpfi_div_si(A->data.num,A->data.num, B); |
| 842 |
} |
| 843 |
void mp_interval_abs(mp_number *A) { |
| 844 |
mpfi_abs(A->data.num, A->data.num); |
| 845 |
} |
| 846 |
void mp_number_clone(mp_number *A, mp_number B) { |
| 847 |
/*mpfi_prec_round (A->data.num, precision_bits);*/ |
| 848 |
mpfi_round_prec (A->data.num, (mpfr_prec_t)precision_bits); |
| 849 |
mpfi_set(A->data.num, (mpfi_ptr)B.data.num); |
| 850 |
} |
| 851 |
void mp_number_swap(mp_number *A, mp_number *B) { |
| 852 |
mpfi_swap(A->data.num, B->data.num); |
| 853 |
} |
| 854 |
void mp_number_fraction_to_scaled (mp_number *A) { |
| 855 |
A->type = mp_scaled_type; |
| 856 |
mpfi_div (A->data.num, A->data.num, fraction_multiplier_mpfi_t); |
| 857 |
} |
| 858 |
void mp_number_angle_to_scaled (mp_number *A) { |
| 859 |
A->type = mp_scaled_type; |
| 860 |
mpfi_div (A->data.num, A->data.num, angle_multiplier_mpfi_t); |
| 861 |
} |
| 862 |
void mp_number_scaled_to_fraction (mp_number *A) { |
| 863 |
A->type = mp_fraction_type; |
| 864 |
mpfi_mul (A->data.num, A->data.num, fraction_multiplier_mpfi_t); |
| 865 |
} |
| 866 |
void mp_number_scaled_to_angle (mp_number *A) { |
| 867 |
A->type = mp_angle_type; |
| 868 |
mpfi_mul(A->data.num, A->data.num, angle_multiplier_mpfi_t); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
@* Query functions. |
| 873 |
|
| 874 |
@ Convert a number to a scaled value. |decNumberToInt32| is not |
| 875 |
able to make this conversion properly, so instead we are using |
| 876 |
|decNumberToDouble| and a typecast. Bad! |
| 877 |
|
| 878 |
@c |
| 879 |
integer64 mp_number_to_scaled(mp_number A) { |
| 880 |
double v = mpfi_get_d (A.data.num); |
| 881 |
return (integer64)(v * 65536.0); |
| 882 |
} |
| 883 |
|
| 884 |
@ |
| 885 |
|
| 886 |
@d odd(A) (MPOST_ABS(A)%2==1) |
| 887 |
|
| 888 |
@c |
| 889 |
integer64 mp_number_to_int(mp_number A) { |
| 890 |
integer64 result = 0; |
| 891 |
double temp; |
| 892 |
// CHECK if (mpfi_fits_sint_p(A.data.num)) \{ |
| 893 |
// CHECK result = mpfi_get_si(A.data.num); |
| 894 |
// CHECK \} |
| 895 |
if (mpfi_bounded_p(A.data.num)) { |
| 896 |
temp = ROUND(mpfi_get_d (A.data.num)); |
| 897 |
if( (INT64_MIN <= temp) && (temp <= INT64_MAX) ) { |
| 898 |
result = (integer64)(temp); |
| 899 |
} |
| 900 |
} |
| 901 |
return result; |
| 902 |
} |
| 903 |
integer64 mp_number_to_boolean(mp_number A) { |
| 904 |
integer64 result = 0; |
| 905 |
double temp; |
| 906 |
// CHECK if (mpfi_fits_sint_p(A.data.num)) \{ |
| 907 |
// CHECK result = mpfi_get_si(A.data.num); |
| 908 |
// CHECK \} |
| 909 |
if (mpfi_bounded_p(A.data.num)) { |
| 910 |
temp = ROUND(mpfi_get_d (A.data.num)); |
| 911 |
if( (INT64_MIN <= temp) && (temp <= INT64_MAX) ) { |
| 912 |
result = (integer64)(temp); |
| 913 |
} |
| 914 |
} |
| 915 |
return result; |
| 916 |
} |
| 917 |
double mp_number_to_double(mp_number A) { |
| 918 |
double res = 0.0; |
| 919 |
if (mpfi_bounded_p (A.data.num)) { |
| 920 |
res = mpfi_get_d(A.data.num); |
| 921 |
} |
| 922 |
return res; |
| 923 |
} |
| 924 |
int mp_number_odd(mp_number A) { |
| 925 |
return odd(mp_number_to_int(A)); |
| 926 |
} |
| 927 |
int mp_number_equal(mp_number A, mp_number B) { |
| 928 |
//CHECK return mpfi_equal_p(A.data.num,B.data.num); |
| 929 |
mpfr_t lA,rA,lB,rB; |
| 930 |
int la,ra,lb, rb; |
| 931 |
mpfr_inits2((mpfr_prec_t)precision_bits,lA,rA,lB,rB,(mpfr_ptr) 0); |
| 932 |
la = mpfi_get_left (lA, A.data.num); |
| 933 |
lb = mpfi_get_left (lB, B.data.num); |
| 934 |
ra = mpfi_get_right (rA, A.data.num); |
| 935 |
rb = mpfi_get_right (rB, B.data.num); |
| 936 |
return ( (la==0 && lb==0 && mpfr_equal_p(lA,lB)!=0) && (ra==0 && rb==0 && mpfr_equal_p(rA,rB)!=0) ); |
| 937 |
} |
| 938 |
int mp_number_greater(mp_number A, mp_number B) { |
| 939 |
//CHECK return mpfi_greater_p(A.data.num,B.data.num); |
| 940 |
return mpfi_cmp(A.data.num,B.data.num)>0; |
| 941 |
} |
| 942 |
int mp_number_less(mp_number A, mp_number B) { |
| 943 |
//CHECK return mpfi_less_p(A.data.num,B.data.num); |
| 944 |
return mpfi_cmp(A.data.num,B.data.num)<0; |
| 945 |
} |
| 946 |
int mp_number_nonequalabs(mp_number A, mp_number B) { |
| 947 |
mpfi_t a,b ; |
| 948 |
int temp; |
| 949 |
mpfi_abs (a, A.data.num); |
| 950 |
mpfi_abs (b, B.data.num); |
| 951 |
//CHECK return !(mpfi_cmpabs(A.data.num, B.data.num)==0); |
| 952 |
temp = mpfi_cmp(a,b); |
| 953 |
return !(temp==0); |
| 954 |
} |
| 955 |
|
| 956 |
@ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples |
| 957 |
of $2^{-16}$. In other words, a binary point is assumed to be sixteen bit |
| 958 |
positions from the right end of a binary computer word. |
| 959 |
|
| 960 |
@ One of \MP's most common operations is the calculation of |
| 961 |
$\lfloor{a+b\over2}\rfloor$, |
| 962 |
the midpoint of two given integers |a| and~|b|. The most decent way to do |
| 963 |
this is to write `|(a+b)/2|'; but on many machines it is more efficient |
| 964 |
to calculate `|(a+b)>>1|'. |
| 965 |
|
| 966 |
Therefore the midpoint operation will always be denoted by `|half(a+b)|' |
| 967 |
in this program. If \MP\ is being implemented with languages that permit |
| 968 |
binary shifting, the |half| macro should be changed to make this operation |
| 969 |
as efficient as possible. Since some systems have shift operators that can |
| 970 |
only be trusted to work on positive numbers, there is also a macro |halfp| |
| 971 |
that is used only when the quantity being halved is known to be positive |
| 972 |
or zero. |
| 973 |
|
| 974 |
@ Here is a procedure analogous to |print_int|. The current version |
| 975 |
is fairly stupid, and it is not round-trip safe, but this is good |
| 976 |
enough for a beta test. |
| 977 |
|
| 978 |
@c |
| 979 |
char * mp_intervalnumber_tostring (mpfi_t n) { |
| 980 |
// CHECK |
| 981 |
// This function probably should be rewritten |
| 982 |
// it's the same as in mpmathbinary |
| 983 |
// |
| 984 |
// |
| 985 |
char *str = NULL, *buffer = NULL; |
| 986 |
mpfr_exp_t exp = 0; |
| 987 |
int neg = 0; |
| 988 |
mpfr_t nn; |
| 989 |
mpfr_init2(nn,(mpfr_prec_t)precision_bits); |
| 990 |
mpfi_mid (nn, n); |
| 991 |
if ((str = mpfr_get_str (NULL, &exp, 10, 0, nn, MPFR_ROUNDING))!=NULL) { |
| 992 |
mpfr_prec_t numprecdigits = (mpfr_prec_t)precision_bits_to_digits((mpfr_prec_t)precision_bits); |
| 993 |
if (*str == '-') { |
| 994 |
neg = 1; |
| 995 |
} |
| 996 |
while (strlen(str)>0 && *(str+strlen(str)-1) == '0' ) { |
| 997 |
*(str+strlen(str)-1) = '\0'; /* get rid of trailing zeroes */ |
| 998 |
} |
| 999 |
buffer = malloc(strlen(str)+13+(unsigned)numprecdigits+1); |
| 1000 |
/* the buffer should also fit at least strlen("E+\%d", exp) or (numprecdigits-2) worth of zeroes, |
| 1001 |
* because with numprecdigits == 33, the str for "1E32" will be "1", and needing 32 extra zeroes, |
| 1002 |
* and the decimal dot. To avoid miscalculations by myself, it is safer to add these |
| 1003 |
* three together. |
| 1004 |
*/ |
| 1005 |
if (buffer) { |
| 1006 |
int i = 0, j = 0; |
| 1007 |
if (neg) { |
| 1008 |
buffer[i++] = '-'; |
| 1009 |
j = 1; |
| 1010 |
} |
| 1011 |
if (strlen(str+j) == 0) { |
| 1012 |
buffer[i++] = '0'; |
| 1013 |
} else { |
| 1014 |
/* non-zero */ |
| 1015 |
if (exp<=numprecdigits && exp > -6) { |
| 1016 |
if (exp>0) { |
| 1017 |
buffer[i++] = str[j++]; |
| 1018 |
while (--exp>0) { |
| 1019 |
buffer[i++] = (str[j] ? str[j++] : '0'); |
| 1020 |
} |
| 1021 |
if (str[j]) { |
| 1022 |
buffer[i++] = '.'; |
| 1023 |
while (str[j]) { |
| 1024 |
buffer[i++] = str[j++]; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
} else { |
| 1028 |
mpfr_exp_t absexp; |
| 1029 |
buffer[i++] = '0'; |
| 1030 |
buffer[i++] = '.'; |
| 1031 |
absexp = -exp; |
| 1032 |
while (absexp-- > 0) { |
| 1033 |
buffer[i++] = '0'; |
| 1034 |
} |
| 1035 |
while (str[j]) { |
| 1036 |
buffer[i++] = str[j++]; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} else { |
| 1040 |
buffer[i++] = str[j++]; |
| 1041 |
if (str[j]) { |
| 1042 |
buffer[i++] = '.'; |
| 1043 |
while (str[j]) { |
| 1044 |
buffer[i++] = str[j++]; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
{ |
| 1048 |
char msg[256]; |
| 1049 |
int k = 0; |
| 1050 |
mp_snprintf (msg, 256, "%s%d", (exp>0?"+":""), (int)(exp>0 ? (exp-1) : (exp-1))); |
| 1051 |
buffer[i++] = 'E'; |
| 1052 |
while (msg[k]) { |
| 1053 |
buffer[i++] = msg[k++]; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
} |
| 1057 |
} |
| 1058 |
buffer[i++] = '\0'; |
| 1059 |
} |
| 1060 |
mpfr_free_str(str); |
| 1061 |
} |
| 1062 |
return buffer; |
| 1063 |
} |
| 1064 |
char * mp_interval_number_tostring (MP mp, mp_number n) { |
| 1065 |
(void)mp; |
| 1066 |
return mp_intervalnumber_tostring(n.data.num); |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
@ @c |
| 1071 |
void mp_interval_print_number (MP mp, mp_number n) { |
| 1072 |
char *str = mp_interval_number_tostring(mp, n); |
| 1073 |
mp_print (mp, str); |
| 1074 |
free (str); |
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
@ Addition is not always checked to make sure that it doesn't overflow, |
| 1081 |
but in places where overflow isn't too unlikely the |slow_add| routine |
| 1082 |
is used. |
| 1083 |
|
| 1084 |
@c |
| 1085 |
void mp_interval_slow_add (MP mp, mp_number *ret, mp_number A, mp_number B) { |
| 1086 |
(void)mp; |
| 1087 |
mpfi_add(ret->data.num,A.data.num,B.data.num); |
| 1088 |
} |
| 1089 |
|
| 1090 |
@ The |make_fraction| routine produces the |fraction| equivalent of |
| 1091 |
|p/q|, given integers |p| and~|q|; it computes the integer |
| 1092 |
$f=\lfloor2^{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are |
| 1093 |
positive. If |p| and |q| are both of the same scaled type |t|, |
| 1094 |
the ``type relation'' |make_fraction(t,t)=fraction| is valid; |
| 1095 |
and it's also possible to use the subroutine ``backwards,'' using |
| 1096 |
the relation |make_fraction(t,fraction)=t| between scaled types. |
| 1097 |
|
| 1098 |
If the result would have magnitude $2^{31}$ or more, |make_fraction| |
| 1099 |
sets |arith_error:=true|. Most of \MP's internal computations have |
| 1100 |
been designed to avoid this sort of error. |
| 1101 |
|
| 1102 |
If this subroutine were programmed in assembly language on a typical |
| 1103 |
machine, we could simply compute |(@t$2^{28}$@>*p)div q|, since a |
| 1104 |
double-precision product can often be input to a fixed-point division |
| 1105 |
instruction. But when we are restricted to int-eger arithmetic it |
| 1106 |
is necessary either to resort to multiple-precision maneuvering |
| 1107 |
or to use a simple but slow iteration. The multiple-precision technique |
| 1108 |
would be about three times faster than the code adopted here, but it |
| 1109 |
would be comparatively long and tricky, involving about sixteen |
| 1110 |
additional multiplications and divisions. |
| 1111 |
|
| 1112 |
This operation is part of \MP's ``inner loop''; indeed, it will |
| 1113 |
consume nearly 10\pct! of the running time (exclusive of input and output) |
| 1114 |
if the code below is left unchanged. A machine-dependent recoding |
| 1115 |
will therefore make \MP\ run faster. The present implementation |
| 1116 |
is highly portable, but slow; it avoids multiplication and division |
| 1117 |
except in the initial stage. System wizards should be careful to |
| 1118 |
replace it with a routine that is guaranteed to produce identical |
| 1119 |
results in all cases. |
| 1120 |
@^system dependencies@> |
| 1121 |
|
| 1122 |
As noted below, a few more routines should also be replaced by machine-dependent |
| 1123 |
code, for efficiency. But when a procedure is not part of the ``inner loop,'' |
| 1124 |
such changes aren't advisable; simplicity and robustness are |
| 1125 |
preferable to trickery, unless the cost is too high. |
| 1126 |
@^inner loop@> |
| 1127 |
|
| 1128 |
@c |
| 1129 |
void mp_interval_make_fraction (MP mp, mpfi_t ret, mpfi_t p, mpfi_t q) { |
| 1130 |
mpfi_div (ret, p, q); |
| 1131 |
mp_check_mpfi_t(mp, ret); |
| 1132 |
mpfi_mul (ret, ret, fraction_multiplier_mpfi_t); |
| 1133 |
} |
| 1134 |
void mp_interval_number_make_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 1135 |
mp_interval_make_fraction (mp, ret->data.num, p.data.num, q.data.num); |
| 1136 |
} |
| 1137 |
|
| 1138 |
@ @<Declarations@>= |
| 1139 |
void mp_interval_make_fraction (MP mp, mpfi_t ret, mpfi_t p, mpfi_t q); |
| 1140 |
|
| 1141 |
@ The dual of |make_fraction| is |take_fraction|, which multiplies a |
| 1142 |
given integer~|q| by a fraction~|f|. When the operands are positive, it |
| 1143 |
computes $p=\lfloor qf/2^{28}+{1\over2}\rfloor$, a symmetric function |
| 1144 |
of |q| and~|f|. |
| 1145 |
|
| 1146 |
This routine is even more ``inner loopy'' than |make_fraction|; |
| 1147 |
the present implementation consumes almost 20\pct! of \MP's computation |
| 1148 |
time during typical jobs, so a machine-language substitute is advisable. |
| 1149 |
@^inner loop@> @^system dependencies@> |
| 1150 |
|
| 1151 |
@c |
| 1152 |
void mp_interval_take_fraction (MP mp, mpfi_t ret, mpfi_t p, mpfi_t q) { |
| 1153 |
(void)mp; |
| 1154 |
mpfi_mul(ret, p, q); |
| 1155 |
mpfi_div(ret, ret, fraction_multiplier_mpfi_t); |
| 1156 |
} |
| 1157 |
void mp_interval_number_take_fraction (MP mp, mp_number *ret, mp_number p, mp_number q) { |
| 1158 |
mp_interval_take_fraction (mp, ret->data.num, p.data.num, q.data.num); |
| 1159 |
} |
| 1160 |
|
| 1161 |
@ @<Declarations@>= |
| 1162 |
void mp_interval_take_fraction (MP mp, mpfi_t ret, mpfi_t p, mpfi_t q); |
| 1163 |
|
| 1164 |
@ When we want to multiply something by a |scaled| quantity, we use a scheme |
| 1165 |
analogous to |take_fraction| but with a different scaling. |
| 1166 |
Given positive operands, |take_scaled| |
| 1167 |
computes the quantity $p=\lfloor qf/2^{16}+{1\over2}\rfloor$. |
| 1168 |
|
| 1169 |
Once again it is a good idea to use a machine-language replacement if |
| 1170 |
possible; otherwise |take_scaled| will use more than 2\pct! of the running time |
| 1171 |
when the Computer Modern fonts are being generated. |
| 1172 |
@^inner loop@> |
| 1173 |
|
| 1174 |
@c |
| 1175 |
void mp_interval_number_take_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 1176 |
(void)mp; |
| 1177 |
mpfi_mul(ret->data.num, p_orig.data.num, q_orig.data.num); |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
@ For completeness, there's also |make_scaled|, which computes a |
| 1182 |
quotient as a |scaled| number instead of as a |fraction|. |
| 1183 |
In other words, the result is $\lfloor2^{16}p/q+{1\over2}\rfloor$, if the |
| 1184 |
operands are positive. \ (This procedure is not used especially often, |
| 1185 |
so it is not part of \MP's inner loop.) |
| 1186 |
|
| 1187 |
@c |
| 1188 |
void mp_interval_number_make_scaled (MP mp, mp_number *ret, mp_number p_orig, mp_number q_orig) { |
| 1189 |
mpfi_div(ret->data.num, p_orig.data.num, q_orig.data.num); |
| 1190 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1191 |
} |
| 1192 |
|
| 1193 |
@ |
| 1194 |
@d halfp(A) (integer)((unsigned)(A) >> 1) |
| 1195 |
|
| 1196 |
@* Scanning numbers in the input. |
| 1197 |
|
| 1198 |
The definitions below are temporarily here. |
| 1199 |
|
| 1200 |
@d set_cur_cmd(A) mp->cur_mod_->type=(A) |
| 1201 |
@d set_cur_mod(A) mpfi_set((mpfi_ptr)(mp->cur_mod_->data.n.data.num),A) |
| 1202 |
|
| 1203 |
@<Declarations...@>= |
| 1204 |
static void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop); |
| 1205 |
|
| 1206 |
@ The check of the precision is based on the article "27 Bits are not enough for 8-Digit accuracy" |
| 1207 |
@ by Bennet Goldberg which roughly says that |
| 1208 |
@ given $p$ digits in base 10 and $q$ digits in base 2, |
| 1209 |
@ conversion from base 10 round-trip through base 2 if and only if $10^p < 2^{q-1}$. |
| 1210 |
@ In our case $p/\log_{10}2 + 1 < q$, or $q\geq a$ |
| 1211 |
@ where $q$ is the current precision in bits and $a=\left\lceil p/\log_{10}2 + 1\right\rceil$. |
| 1212 |
@ Therefore if $a>q$ the required precision could be too high and we emit a warning. |
| 1213 |
@d too_precise(a) (a>precision_bits) |
| 1214 |
@c |
| 1215 |
void mp_wrapup_numeric_token(MP mp, unsigned char *start, unsigned char *stop) { |
| 1216 |
int invalid = 0; |
| 1217 |
mpfi_t result; |
| 1218 |
size_t l; |
| 1219 |
unsigned long lp, lpbit; |
| 1220 |
char *buf; |
| 1221 |
char *bufp; |
| 1222 |
|
| 1223 |
if ((stop-start+1)<0) { |
| 1224 |
const char *hlp[] = {"I could not handle this number specification", |
| 1225 |
"because an error of the internal buffer.", |
| 1226 |
NULL }; |
| 1227 |
mp_error (mp, "Internal buffer error", hlp, false); |
| 1228 |
} |
| 1229 |
l = (size_t)(stop-start+1); |
| 1230 |
buf = mp_xmalloc(mp, l+1, 1); |
| 1231 |
buf[l] = '\0'; |
| 1232 |
bufp = buf; |
| 1233 |
|
| 1234 |
mpfi_init2(result, (mpfr_prec_t)precision_bits); |
| 1235 |
(void)strncpy(buf,(const char *)start, l); |
| 1236 |
invalid = mpfi_set_str(result,buf, 10); |
| 1237 |
/*|fprintf(stdout,"scan of [%s] produced %s, ", buf, mp_intervalnumber_tostring(result));|*/ |
| 1238 |
lp = (unsigned long) l; |
| 1239 |
/* strip leading - or + or 0 or .*/ |
| 1240 |
if ( (*bufp=='-') || (*bufp=='+') || (*bufp=='0') || (*bufp=='.') ) { lp--; bufp++;} |
| 1241 |
/* strip also . */ |
| 1242 |
lp = strchr(bufp,'.') ? lp-1: lp; |
| 1243 |
/* strip also trailing 0s */ |
| 1244 |
bufp = buf+l-1; |
| 1245 |
while(*bufp == '0') {bufp--; lp=( ((lp==0)||(lp==1))?1:lp-1);} |
| 1246 |
/* at least one digit, even if the number is 0 */ |
| 1247 |
lp = lp>0? lp: 1; |
| 1248 |
/* bits needed for buf */ |
| 1249 |
lpbit = (unsigned long)ceil((double)lp/log10(2)+1); |
| 1250 |
free(buf); |
| 1251 |
bufp = NULL; |
| 1252 |
if (invalid == 0) { |
| 1253 |
set_cur_mod(result); |
| 1254 |
/* |fprintf(stdout,"mod=%s\n", mp_interval_number_tostring(mp,mp->cur_mod_->data.n));|*/ |
| 1255 |
if (too_precise(lpbit)) { |
| 1256 |
if (mpfi_positive_p((mpfi_ptr)(internal_value (mp_warning_check).data.num)) && |
| 1257 |
(mp->scanner_status != tex_flushing)) { |
| 1258 |
char msg[256]; |
| 1259 |
const char *hlp[] = {"Continue and I'll try to cope", |
| 1260 |
"with that value; but it might be dangerous.", |
| 1261 |
"(Set warningcheck:=0 to suppress this message.)", |
| 1262 |
NULL }; |
| 1263 |
mp_snprintf (msg, 256, "Required precision is too high (%d vs. numberprecision = %f, required precision=%d bits vs internal precision=%f bits)", (unsigned int)lp,mpfi_get_d(internal_value (mp_number_precision).data.num),(int)lpbit,precision_bits); |
| 1264 |
@.Number is too large@>; |
| 1265 |
mp_error (mp, msg, hlp, true); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} else if (mp->scanner_status != tex_flushing) { |
| 1269 |
const char *hlp[] = {"I could not handle this number specification", |
| 1270 |
"probably because it is out of range. Error:", |
| 1271 |
"", |
| 1272 |
NULL }; |
| 1273 |
hlp[2] = strerror(errno); |
| 1274 |
mp_error (mp, "Enormous number has been reduced.", hlp, false); |
| 1275 |
@.Enormous number...@>; |
| 1276 |
set_cur_mod((mpfi_ptr)(((math_data *)(mp->math))->inf_t.data.num)); |
| 1277 |
} |
| 1278 |
set_cur_cmd((mp_variable_type)mp_numeric_token); |
| 1279 |
mpfi_clear(result); |
| 1280 |
} |
| 1281 |
|
| 1282 |
@ @c |
| 1283 |
static void find_exponent (MP mp) { |
| 1284 |
if (mp->buffer[mp->cur_input.loc_field] == 'e' || |
| 1285 |
mp->buffer[mp->cur_input.loc_field] == 'E') { |
| 1286 |
mp->cur_input.loc_field++; |
| 1287 |
if (!(mp->buffer[mp->cur_input.loc_field] == '+' || |
| 1288 |
mp->buffer[mp->cur_input.loc_field] == '-' || |
| 1289 |
mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class)) { |
| 1290 |
mp->cur_input.loc_field--; |
| 1291 |
return; |
| 1292 |
} |
| 1293 |
if (mp->buffer[mp->cur_input.loc_field] == '+' || |
| 1294 |
mp->buffer[mp->cur_input.loc_field] == '-') { |
| 1295 |
mp->cur_input.loc_field++; |
| 1296 |
} |
| 1297 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1298 |
mp->cur_input.loc_field++; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
} |
| 1302 |
void mp_interval_scan_fractional_token (MP mp, integer64 n) { /* n: scaled */ |
| 1303 |
(void)n; |
| 1304 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 1305 |
unsigned char *stop; |
| 1306 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1307 |
mp->cur_input.loc_field++; |
| 1308 |
} |
| 1309 |
find_exponent(mp); |
| 1310 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 1311 |
mp_wrapup_numeric_token (mp, start, stop); |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
@ We just have to collect bytes. |
| 1316 |
|
| 1317 |
@c |
| 1318 |
void mp_interval_scan_numeric_token (MP mp, integer64 n) { /* n: scaled */ |
| 1319 |
(void)n; |
| 1320 |
unsigned char *start = &mp->buffer[mp->cur_input.loc_field -1]; |
| 1321 |
unsigned char *stop; |
| 1322 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1323 |
mp->cur_input.loc_field++; |
| 1324 |
} |
| 1325 |
if (mp->buffer[mp->cur_input.loc_field] == '.' && |
| 1326 |
mp->buffer[mp->cur_input.loc_field+1] != '.') { |
| 1327 |
mp->cur_input.loc_field++; |
| 1328 |
while (mp->char_class[mp->buffer[mp->cur_input.loc_field]] == digit_class) { |
| 1329 |
mp->cur_input.loc_field++; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
find_exponent(mp); |
| 1333 |
stop = &mp->buffer[mp->cur_input.loc_field-1]; |
| 1334 |
mp_wrapup_numeric_token (mp, start, stop); |
| 1335 |
} |
| 1336 |
|
| 1337 |
@ The |scaled| quantities in \MP\ programs are generally supposed to be |
| 1338 |
less than $2^{12}$ in absolute value, so \MP\ does much of its internal |
| 1339 |
arithmetic with 28~significant bits of precision. A |fraction| denotes |
| 1340 |
a scaled integer whose binary point is assumed to be 28 bit positions |
| 1341 |
from the right. |
| 1342 |
|
| 1343 |
@d fraction_half (fraction_multiplier/2) |
| 1344 |
@d fraction_one (1*fraction_multiplier) |
| 1345 |
@d fraction_two (2*fraction_multiplier) |
| 1346 |
@d fraction_three (3*fraction_multiplier) |
| 1347 |
@d fraction_four (4*fraction_multiplier) |
| 1348 |
|
| 1349 |
@ Here is a typical example of how the routines above can be used. |
| 1350 |
It computes the function |
| 1351 |
$${1\over3\tau}f(\theta,\phi)= |
| 1352 |
{\tau^{-1}\bigl(2+\sqrt2\,(\sin\theta-{1\over16}\sin\phi) |
| 1353 |
(\sin\phi-{1\over16}\sin\theta)(\cos\theta-\cos\phi)\bigr)\over |
| 1354 |
3\,\bigl(1+{1\over2}(\sqrt5-1)\cos\theta+{1\over2}(3-\sqrt5\,)\cos\phi\bigr)},$$ |
| 1355 |
where $\tau$ is a |scaled| ``tension'' parameter. This is \MP's magic |
| 1356 |
fudge factor for placing the first control point of a curve that starts |
| 1357 |
at an angle $\theta$ and ends at an angle $\phi$ from the straight path. |
| 1358 |
(Actually, if the stated quantity exceeds 4, \MP\ reduces it to~4.) |
| 1359 |
|
| 1360 |
The trigonometric quantity to be multiplied by $\sqrt2$ is less than $\sqrt2$. |
| 1361 |
(It's a sum of eight terms whose absolute values can be bounded using |
| 1362 |
relations such as $\sin\theta\cos\theta\L{1\over2}$.) Thus the numerator |
| 1363 |
is positive; and since the tension $\tau$ is constrained to be at least |
| 1364 |
$3\over4$, the numerator is less than $16\over3$. The denominator is |
| 1365 |
nonnegative and at most~6. |
| 1366 |
|
| 1367 |
The angles $\theta$ and $\phi$ are given implicitly in terms of |fraction| |
| 1368 |
arguments |st|, |ct|, |sf|, and |cf|, representing $\sin\theta$, $\cos\theta$, |
| 1369 |
$\sin\phi$, and $\cos\phi$, respectively. |
| 1370 |
|
| 1371 |
@c |
| 1372 |
void mp_interval_velocity (MP mp, mp_number *ret, mp_number st, mp_number ct, mp_number sf, |
| 1373 |
mp_number cf, mp_number t) { |
| 1374 |
mpfi_t acc, num, denom; /* registers for intermediate calculations */ |
| 1375 |
mpfi_t r1, r2; |
| 1376 |
mpfi_t arg1, arg2; |
| 1377 |
mpfi_t i16, fone, fhalf, ftwo, sqrtfive; |
| 1378 |
mpfi_inits2 ((mpfr_prec_t)precision_bits, acc, num, denom, r1, r2, arg1, arg2, i16, fone, fhalf, ftwo, sqrtfive, (mpfi_ptr)0); |
| 1379 |
mpfi_set_si(i16, 16); |
| 1380 |
mpfi_set_si(fone, fraction_one); |
| 1381 |
mpfi_set_si(fhalf, fraction_half); |
| 1382 |
mpfi_set_si(ftwo, fraction_two); |
| 1383 |
mpfi_set_si(sqrtfive, 5); |
| 1384 |
mpfi_sqrt (sqrtfive, sqrtfive); |
| 1385 |
mpfi_div (arg1,sf.data.num, i16); // arg1 = sf / 16 |
| 1386 |
mpfi_sub (arg1,st.data.num, arg1); // arg1 = st - arg1 |
| 1387 |
mpfi_div (arg2,st.data.num, i16); // arg2 = st / 16 |
| 1388 |
mpfi_sub (arg2,sf.data.num, arg2); // arg2 = sf - arg2 |
| 1389 |
mp_interval_take_fraction (mp, acc, arg1, arg2); // acc = (arg1 * arg2) / fmul |
| 1390 |
|
| 1391 |
mpfi_set (arg1, acc); |
| 1392 |
mpfi_sub (arg2, ct.data.num, cf.data.num); // arg2 = ct - cf |
| 1393 |
mp_interval_take_fraction (mp, acc, arg1, arg2); // acc = (arg1 * arg2 ) / fmul |
| 1394 |
|
| 1395 |
mpfi_sqrt(arg1, two_mpfi_t); // arg1 = sqrt(2) |
| 1396 |
mpfi_mul(arg1, arg1, fone); // arg1 = arg1 * fmul |
| 1397 |
mp_interval_take_fraction (mp, r1, acc, arg1); // r1 = (acc * arg1) / fmul |
| 1398 |
mpfi_add(num, ftwo, r1); // num = ftwo + r1 |
| 1399 |
|
| 1400 |
mpfi_sub(arg1,sqrtfive, one); // arg1 = sqrt(5) - 1 |
| 1401 |
mpfi_mul(arg1,arg1,fhalf); // arg1 = arg1 * fmul/2 |
| 1402 |
mpfi_mul(arg1,arg1,three_mpfi_t); // arg1 = arg1 * 3 |
| 1403 |
|
| 1404 |
mpfi_sub(arg2,three_mpfi_t, sqrtfive); // arg2 = 3 - sqrt(5) |
| 1405 |
mpfi_mul(arg2,arg2,fhalf); // arg2 = arg2 * fmul/2 |
| 1406 |
mpfi_mul(arg2,arg2,three_mpfi_t); // arg2 = arg2 * 3 |
| 1407 |
mp_interval_take_fraction (mp, r1, ct.data.num, arg1) ; // r1 = (ct * arg1) / fmul |
| 1408 |
mp_interval_take_fraction (mp, r2, cf.data.num, arg2); // r2 = (cf * arg2) / fmul |
| 1409 |
|
| 1410 |
mpfi_set_si(denom, fraction_three); // denom = 3fmul |
| 1411 |
mpfi_add(denom, denom, r1); // denom = denom + r1 |
| 1412 |
mpfi_add(denom, denom, r2); // denom = denom + r2 |
| 1413 |
|
| 1414 |
//CHECK if (!mpfi_equal_p(t.data.num, one)) \{ // t != 1 |
| 1415 |
if (mpfi_cmp(t.data.num, one)!=0) { // t != 1 |
| 1416 |
mpfi_div(num, num, t.data.num); // num = num / t |
| 1417 |
} |
| 1418 |
mpfi_set(r2, num); // r2 = num / 4 |
| 1419 |
mpfi_div(r2, r2, four_mpfi_t); |
| 1420 |
// CHECK if (mpfi_less_p(denom,r2)) \{ // num/4 >= denom => denom < num/4 |
| 1421 |
if (mpfi_cmp(denom,r2)<0) { // num/4 >= denom => denom < num/4 |
| 1422 |
mpfi_set_si(ret->data.num,fraction_four); |
| 1423 |
} else { |
| 1424 |
mp_interval_make_fraction (mp, ret->data.num, num, denom); |
| 1425 |
} |
| 1426 |
mpfi_clears (acc, num, denom, r1, r2, arg1, arg2, i16, fone, fhalf, ftwo, sqrtfive, (mpfi_ptr)0); |
| 1427 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
@ The following somewhat different subroutine tests rigorously if $ab$ is |
| 1432 |
greater than, equal to, or less than~$cd$, |
| 1433 |
given integers $(a,b,c,d)$. In most cases a quick decision is reached. |
| 1434 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 1435 |
|
| 1436 |
@c |
| 1437 |
void mp_ab_vs_cd (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig, mp_number c_orig, mp_number d_orig) { |
| 1438 |
mpfi_t q, r, test; /* temporary registers */ |
| 1439 |
mpfi_t a, b, c, d; |
| 1440 |
int cmp = 0; |
| 1441 |
(void)mp; |
| 1442 |
mpfi_inits2((mpfr_prec_t)precision_bits, q,r,test,a,b,c,d,(mpfi_ptr)0); |
| 1443 |
mpfi_set(a, (mpfi_ptr)a_orig.data.num); |
| 1444 |
mpfi_set(b, (mpfi_ptr )b_orig.data.num); |
| 1445 |
mpfi_set(c, (mpfi_ptr )c_orig.data.num); |
| 1446 |
mpfi_set(d, (mpfi_ptr )d_orig.data.num); |
| 1447 |
|
| 1448 |
mpfi_mul(q,a,b); |
| 1449 |
mpfi_mul(r,c,d); |
| 1450 |
cmp = mpfi_cmp(q,r); |
| 1451 |
if (cmp==0) { |
| 1452 |
mpfi_set(ret->data.num, zero); |
| 1453 |
goto RETURN; |
| 1454 |
} |
| 1455 |
if (cmp>0) { |
| 1456 |
mpfi_set(ret->data.num, one); |
| 1457 |
goto RETURN; |
| 1458 |
} |
| 1459 |
if (cmp<0) { |
| 1460 |
mpfi_set(ret->data.num, minusone); |
| 1461 |
goto RETURN; |
| 1462 |
} |
| 1463 |
|
| 1464 |
/*TODO: remove this part of the code until RETURN */ |
| 1465 |
@<Reduce to the case that |a,c>=0|, |b,d>0|@>; |
| 1466 |
while (1) { |
| 1467 |
mpfi_div(q,a,d); |
| 1468 |
mpfi_div(r,c,b); |
| 1469 |
cmp = mpfi_cmp(q,r); |
| 1470 |
if (cmp) { |
| 1471 |
if (cmp>1) { |
| 1472 |
mpfi_set(ret->data.num, one); |
| 1473 |
} else { |
| 1474 |
mpfi_set(ret->data.num, minusone); |
| 1475 |
} |
| 1476 |
goto RETURN; |
| 1477 |
} |
| 1478 |
mpfi_remainder(q,a,d); |
| 1479 |
mpfi_remainder(r,c,b); |
| 1480 |
if (mpfi_zero_p(r)) { |
| 1481 |
if (mpfi_zero_p(q)) { |
| 1482 |
mpfi_set(ret->data.num, zero); |
| 1483 |
} else { |
| 1484 |
mpfi_set(ret->data.num, one); |
| 1485 |
} |
| 1486 |
goto RETURN; |
| 1487 |
} |
| 1488 |
if (mpfi_zero_p(q)) { |
| 1489 |
mpfi_set(ret->data.num, minusone); |
| 1490 |
goto RETURN; |
| 1491 |
} |
| 1492 |
mpfi_set(a,b); |
| 1493 |
mpfi_set(b,q); |
| 1494 |
mpfi_set(c,d); |
| 1495 |
mpfi_set(d,r); |
| 1496 |
} /* now |a>d>0| and |c>b>0| */ |
| 1497 |
RETURN: |
| 1498 |
#if MPOST_DEBUG |
| 1499 |
fprintf(stdout, "\n%f = ab_vs_cd(%f,%f,%f,%f)", mp_number_to_double(*ret), |
| 1500 |
mp_number_to_double(a_orig),mp_number_to_double(b_orig), |
| 1501 |
mp_number_to_double(c_orig),mp_number_to_double(d_orig)); |
| 1502 |
#endif |
| 1503 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1504 |
mpfi_clears(q,r,test,a,b,c,d,(mpfi_ptr)0); |
| 1505 |
return; |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
@ @<Reduce to the case that |a...@>= |
| 1510 |
if (mpfi_negative_p(a)) { |
| 1511 |
mpfi_neg(a, a); |
| 1512 |
mpfi_neg(b, b); |
| 1513 |
} |
| 1514 |
if (mpfi_negative_p(c)) { |
| 1515 |
mpfi_neg(c, c); |
| 1516 |
mpfi_neg(d, d); |
| 1517 |
} |
| 1518 |
if (!mpfi_positive_p(d)) { |
| 1519 |
if (!mpfi_negative_p(b)) { |
| 1520 |
if ((mpfi_zero_p(a) || mpfi_zero_p(b)) && (mpfi_zero_p(c) || mpfi_zero_p(d))) |
| 1521 |
mpfi_set(ret->data.num, zero); |
| 1522 |
else |
| 1523 |
mpfi_set(ret->data.num, one); |
| 1524 |
goto RETURN; |
| 1525 |
} |
| 1526 |
if (mpfi_zero_p(d)) { |
| 1527 |
if (mpfi_zero_p(a)) |
| 1528 |
mpfi_set(ret->data.num, zero); |
| 1529 |
else |
| 1530 |
mpfi_set(ret->data.num, minusone); |
| 1531 |
goto RETURN; |
| 1532 |
} |
| 1533 |
mpfi_set(q, a); |
| 1534 |
mpfi_set(a, c); |
| 1535 |
mpfi_set(c, q); |
| 1536 |
mpfi_neg(q, b); |
| 1537 |
mpfi_neg(b, d); |
| 1538 |
mpfi_set(d, q); |
| 1539 |
} else if (!mpfi_positive_p(b)) { |
| 1540 |
if (mpfi_negative_p(b) && mpfi_positive_p(a)) { |
| 1541 |
mpfi_set(ret->data.num, minusone); |
| 1542 |
goto RETURN; |
| 1543 |
} |
| 1544 |
if (mpfi_zero_p(c)) |
| 1545 |
mpfi_set(ret->data.num, zero); |
| 1546 |
else |
| 1547 |
mpfi_set(ret->data.num, minusone); |
| 1548 |
goto RETURN; |
| 1549 |
} |
| 1550 |
|
| 1551 |
@ Now here's a subroutine that's handy for all sorts of path computations: |
| 1552 |
Given a quadratic polynomial $B(a,b,c;t)$, the |crossing_point| function |
| 1553 |
returns the unique |fraction| value |t| between 0 and~1 at which |
| 1554 |
$B(a,b,c;t)$ changes from positive to negative, or returns |
| 1555 |
|t=fraction_one+1| if no such value exists. If |a<0| (so that $B(a,b,c;t)$ |
| 1556 |
is already negative at |t=0|), |crossing_point| returns the value zero. |
| 1557 |
|
| 1558 |
The general bisection method is quite simple when $n=2$, hence |
| 1559 |
|crossing_point| does not take much time. At each stage in the |
| 1560 |
recursion we have a subinterval defined by |l| and~|j| such that |
| 1561 |
$B(a,b,c;2^{-l}(j+t))=B(x_0,x_1,x_2;t)$, and we want to ``zero in'' on |
| 1562 |
the subinterval where $x_0\G0$ and $\min(x_1,x_2)<0$. |
| 1563 |
|
| 1564 |
It is convenient for purposes of calculation to combine the values |
| 1565 |
of |l| and~|j| in a single variable $d=2^l+j$, because the operation |
| 1566 |
of bisection then corresponds simply to doubling $d$ and possibly |
| 1567 |
adding~1. Furthermore it proves to be convenient to modify |
| 1568 |
our previous conventions for bisection slightly, maintaining the |
| 1569 |
variables $X_0=2^lx_0$, $X_1=2^l(x_0-x_1)$, and $X_2=2^l(x_1-x_2)$. |
| 1570 |
With these variables the conditions $x_0\ge0$ and $\min(x_1,x_2)<0$ are |
| 1571 |
equivalent to $\max(X_1,X_1+X_2)>X_0\ge0$. |
| 1572 |
|
| 1573 |
The following code maintains the invariant relations |
| 1574 |
$0\L|x0|<\max(|x1|,|x1|+|x2|)$, |
| 1575 |
$\vert|x1|\vert<2^{30}$, $\vert|x2|\vert<2^{30}$; |
| 1576 |
it has been constructed in such a way that no arithmetic overflow |
| 1577 |
will occur if the inputs satisfy |
| 1578 |
$a<2^{30}$, $\vert a-b\vert<2^{30}$, and $\vert b-c\vert<2^{30}$. |
| 1579 |
|
| 1580 |
@d no_crossing { mpfi_set(ret->data.num, fraction_one_plus_mpfi_t); goto RETURN; } |
| 1581 |
@d one_crossing { mpfi_set(ret->data.num, fraction_one_mpfi_t); goto RETURN; } |
| 1582 |
@d zero_crossing { mpfi_set(ret->data.num, zero); goto RETURN; } |
| 1583 |
|
| 1584 |
@c |
| 1585 |
static void mp_interval_crossing_point (MP mp, mp_number *ret, mp_number aa, mp_number bb, mp_number cc) { |
| 1586 |
mpfi_t a,b,c; |
| 1587 |
double d; /* recursive counter */ |
| 1588 |
mpfi_t x, xx, x0, x1, x2; /* temporary registers for bisection */ |
| 1589 |
mpfi_t scratch; |
| 1590 |
mpfi_inits2 ((mpfr_prec_t)precision_bits, a,b,c, x,xx,x0,x1,x2, scratch,(mpfi_ptr)0); |
| 1591 |
mpfi_set(a, (mpfi_ptr )aa.data.num); |
| 1592 |
mpfi_set(b, (mpfi_ptr )bb.data.num); |
| 1593 |
mpfi_set(c, (mpfi_ptr )cc.data.num); |
| 1594 |
if (mpfi_negative_p(a)) |
| 1595 |
zero_crossing; |
| 1596 |
if (!mpfi_negative_p(c)) { |
| 1597 |
if (!mpfi_negative_p(b)) { |
| 1598 |
if (mpfi_positive_p(c)) { |
| 1599 |
no_crossing; |
| 1600 |
} else if (mpfi_zero_p(a) && mpfi_zero_p(b)) { |
| 1601 |
no_crossing; |
| 1602 |
} else { |
| 1603 |
one_crossing; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
if (mpfi_zero_p(a)) |
| 1607 |
zero_crossing; |
| 1608 |
} else if (mpfi_zero_p(a)) { |
| 1609 |
if (!mpfi_positive_p(b)) |
| 1610 |
zero_crossing; |
| 1611 |
} |
| 1612 |
|
| 1613 |
/* Use bisection to find the crossing point... */ |
| 1614 |
d = epsilonf; |
| 1615 |
mpfi_set(x0, a); |
| 1616 |
mpfi_sub(x1,a, b); |
| 1617 |
mpfi_sub(x2,b, c); |
| 1618 |
do { |
| 1619 |
/* not sure why the error correction has to be >= 1E-12 */ |
| 1620 |
mpfi_add(x, x1, x2); |
| 1621 |
mpfi_div(x, x, two_mpfi_t); |
| 1622 |
mpfi_add_d (x, x, 1E-12); |
| 1623 |
mpfi_sub(scratch, x1, x0); |
| 1624 |
// CHECK if (mpfi_greater_p(scratch, x0)) \{ |
| 1625 |
if (mpfi_cmp(scratch, x0)>0) { |
| 1626 |
mpfi_set(x2, x); |
| 1627 |
mpfi_add(x0, x0, x0); |
| 1628 |
d += d; |
| 1629 |
} else { |
| 1630 |
mpfi_add(xx, scratch, x); |
| 1631 |
// CHECK if (mpfi_greater_p(xx,x0)) \{ |
| 1632 |
if (mpfi_cmp(xx,x0)>0) { |
| 1633 |
mpfi_set(x2,x); |
| 1634 |
mpfi_add(x0, x0, x0); |
| 1635 |
d += d; |
| 1636 |
} else { |
| 1637 |
mpfi_sub(x0, x0, xx); |
| 1638 |
// CHECK if (!mpfi_greater_p(x,x0)) \{ |
| 1639 |
if (!(mpfi_cmp(x,x0)>0)) { |
| 1640 |
mpfi_add(scratch, x, x2); |
| 1641 |
// CHECK if (!mpfi_greater_p(scratch, x0)) |
| 1642 |
if (!(mpfi_cmp(scratch, x0)>0)) |
| 1643 |
no_crossing; |
| 1644 |
} |
| 1645 |
mpfi_set(x1,x); |
| 1646 |
d = d + d + epsilonf; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
} while (d < fraction_one); |
| 1650 |
mpfi_set_d(scratch, d); |
| 1651 |
mpfi_sub(ret->data.num,scratch, fraction_one_mpfi_t); |
| 1652 |
RETURN: |
| 1653 |
#if MPOST_DEBUG |
| 1654 |
fprintf(stdout, "\n%f = crossing_point(%f,%f,%f)", mp_number_to_double(*ret), |
| 1655 |
mp_number_to_double(aa),mp_number_to_double(bb),mp_number_to_double(cc)); |
| 1656 |
#endif |
| 1657 |
mpfi_clears (a,b,c, x,xx,x0,x1,x2, scratch, (mpfi_ptr)0); |
| 1658 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1659 |
return; |
| 1660 |
} |
| 1661 |
|
| 1662 |
|
| 1663 |
@ We conclude this set of elementary routines with some simple rounding |
| 1664 |
and truncation operations. |
| 1665 |
|
| 1666 |
|
| 1667 |
@ |round_unscaled| rounds a |scaled| and converts it to |int| |
| 1668 |
@c |
| 1669 |
integer64 mp_round_unscaled(mp_number x_orig) { |
| 1670 |
double xx = mp_number_to_double(x_orig); |
| 1671 |
integer64 x = (integer64)ROUND(xx); |
| 1672 |
return x; |
| 1673 |
} |
| 1674 |
|
| 1675 |
@ |number_floor| floors a number |
| 1676 |
|
| 1677 |
@c |
| 1678 |
void mp_number_floor (mp_number *i) { |
| 1679 |
//CHECK mpfi_rint_floor(i->data.num, i->data.num, MPFR_RNDD); |
| 1680 |
mpfr_t le,re; |
| 1681 |
mpfr_inits2((mpfr_prec_t)precision_bits, le, re, (mpfr_ptr)0 ); |
| 1682 |
mpfi_get_left (le, i->data.num); mpfi_get_right(re, i->data.num); |
| 1683 |
mpfr_rint_floor(le, le, MPFR_RNDD); mpfr_rint_floor(re, re, MPFR_RNDD); |
| 1684 |
mpfi_interv_fr(i->data.num, le, re); |
| 1685 |
mpfr_clears(le, re, (mpfr_ptr)0); |
| 1686 |
} |
| 1687 |
|
| 1688 |
@ |fraction_to_scaled| rounds a |fraction| and converts it to |scaled| |
| 1689 |
@c |
| 1690 |
void mp_interval_fraction_to_round_scaled (mp_number *x_orig) { |
| 1691 |
x_orig->type = mp_scaled_type; |
| 1692 |
mpfi_div(x_orig->data.num, x_orig->data.num, fraction_multiplier_mpfi_t); |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
@* Algebraic and transcendental functions. |
| 1698 |
\MP\ computes all of the necessary special functions from scratch, without |
| 1699 |
relying on |real| arithmetic or system subroutines for sines, cosines, etc. |
| 1700 |
|
| 1701 |
@ |
| 1702 |
|
| 1703 |
@c |
| 1704 |
void mp_interval_square_rt (MP mp, mp_number *ret, mp_number x_orig) { /* return, x: scaled */ |
| 1705 |
if (!mpfi_nonnegative_p((mpfi_ptr)x_orig.data.num)) { |
| 1706 |
@<Handle square root of zero or negative argument@>; |
| 1707 |
} else { |
| 1708 |
mpfi_sqrt(ret->data.num, x_orig.data.num); |
| 1709 |
} |
| 1710 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
@ @<Handle square root of zero...@>= |
| 1715 |
{ |
| 1716 |
if (mpfi_negative_p((mpfi_ptr)x_orig.data.num)) { |
| 1717 |
char msg[256]; |
| 1718 |
const char *hlp[] = { |
| 1719 |
"Since I don't take square roots of negative numbers,", |
| 1720 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1721 |
NULL }; |
| 1722 |
char *xstr = mp_interval_number_tostring (mp, x_orig); |
| 1723 |
mp_snprintf(msg, 256, "Square root of %s has been replaced by 0", xstr); |
| 1724 |
free(xstr); |
| 1725 |
@.Square root...replaced by 0@>; |
| 1726 |
mp_error (mp, msg, hlp, true); |
| 1727 |
} else if ( mpfi_overlaps_zero_p((mpfi_ptr)x_orig.data.num)) { |
| 1728 |
char msg[256]; |
| 1729 |
const char *hlp[] = { |
| 1730 |
"Since I don't take square roots of intervals that contains negative and positive numbers,", |
| 1731 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1732 |
NULL }; |
| 1733 |
char *xstr = mp_interval_number_tostring (mp, x_orig); |
| 1734 |
mp_snprintf(msg, 256, "Square root of interval [a,b] with a<0 and b>0 that contains %s has been replaced by 0", xstr); |
| 1735 |
free(xstr); |
| 1736 |
mp_error (mp, msg, hlp, true); |
| 1737 |
} |
| 1738 |
/*mpfi_set_zero(ret->data.num,1); *//* 1 == positive */ |
| 1739 |
mpfi_set_d(ret->data.num,0.0); |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
@ Pythagorean addition $\psqrt{a^2+b^2}$ is implemented by a quick hack |
| 1745 |
|
| 1746 |
@c |
| 1747 |
void mp_interval_pyth_add (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1748 |
mpfi_t a, b, asq, bsq; |
| 1749 |
mpfi_inits2((mpfr_prec_t)precision_bits, a,b, asq, bsq, (mpfi_ptr)0); |
| 1750 |
mpfi_set(a, (mpfi_ptr)a_orig.data.num); |
| 1751 |
mpfi_set(b, (mpfi_ptr)b_orig.data.num); |
| 1752 |
/* mpfi_mul(asq, a, a); */ |
| 1753 |
/* mpfi_mul(bsq, b, b); */ |
| 1754 |
mpfi_sqr(asq, a); |
| 1755 |
mpfi_sqr(bsq, b); |
| 1756 |
mpfi_add(a, asq, bsq); |
| 1757 |
mpfi_sqrt(ret->data.num, a); |
| 1758 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1759 |
mpfi_clears(a,b, asq, bsq, (mpfi_ptr)0); |
| 1760 |
} |
| 1761 |
|
| 1762 |
@ Here is a similar algorithm for $\psqrt{a^2-b^2}$. Same quick hack, also. |
| 1763 |
|
| 1764 |
@c |
| 1765 |
void mp_interval_pyth_sub (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig) { |
| 1766 |
mpfi_t a, b, asq, bsq; |
| 1767 |
mpfi_inits2((mpfr_prec_t)precision_bits, a,b, asq, bsq, (mpfi_ptr)0); |
| 1768 |
mpfi_set(a, (mpfi_ptr)a_orig.data.num); |
| 1769 |
mpfi_set(b, (mpfi_ptr)b_orig.data.num); |
| 1770 |
// CHECK if (!mpfi_greater_p(a,b)) \{ |
| 1771 |
if (!(mpfi_cmp(a,b)>0)) { |
| 1772 |
@<Handle erroneous |pyth_sub| and set |a:=0|@>; |
| 1773 |
} else { |
| 1774 |
/* mpfi_mul(asq, a, a); */ |
| 1775 |
/* mpfi_mul(bsq, b, b); */ |
| 1776 |
mpfi_sqr(asq, a); |
| 1777 |
mpfi_sqr(bsq, b); |
| 1778 |
mpfi_sub(a, asq, bsq); |
| 1779 |
mpfi_sqrt(a, a); |
| 1780 |
} |
| 1781 |
mpfi_set(ret->data.num, a); |
| 1782 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
@ @<Handle erroneous |pyth_sub| and set |a:=0|@>= |
| 1787 |
{ |
| 1788 |
//CHECK if (mpfi_less_p(a, b)) \{ |
| 1789 |
if (mpfi_cmp(a, b)<0) { |
| 1790 |
char msg[256]; |
| 1791 |
const char *hlp[] = { |
| 1792 |
"Since I don't take square roots of negative numbers,", |
| 1793 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1794 |
NULL }; |
| 1795 |
char *astr = mp_interval_number_tostring (mp, a_orig); |
| 1796 |
char *bstr = mp_interval_number_tostring (mp, b_orig); |
| 1797 |
mp_snprintf (msg, 256, "Pythagorean subtraction %s+-+%s has been replaced by 0", astr, bstr); |
| 1798 |
free(astr); |
| 1799 |
free(bstr); |
| 1800 |
@.Pythagorean...@>; |
| 1801 |
mp_error (mp, msg, hlp, true); |
| 1802 |
} |
| 1803 |
/*mpfi_set_zero(a,1); */ /* 1 == positive */ |
| 1804 |
mpfi_set_d(a,0.0); |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
@ A new primitive to set an |interval| quantity: |
| 1809 |
|
| 1810 |
@c |
| 1811 |
void mp_interval_m_interval_set(MP mp, mp_number *ret, mp_number a, mp_number b) { |
| 1812 |
(void)mp; |
| 1813 |
mpfi_t ret_val; |
| 1814 |
mpfi_init2(ret_val,(mpfr_prec_t)precision_bits); |
| 1815 |
mpfi_interv_fr(ret_val, a.data.num, b.data.num); |
| 1816 |
mpfi_set(ret->data.num,ret_val); |
| 1817 |
mpfi_clear(ret_val); |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
@ and two new primitives to retrive the left and right endpoint of an |interval| quantity: |
| 1823 |
|
| 1824 |
@c |
| 1825 |
void mp_interval_m_get_left_endpoint (MP mp, mp_number *ret, mp_number x_orig) { |
| 1826 |
(void)mp; |
| 1827 |
mpfr_t ret_val; |
| 1828 |
mpfr_init2(ret_val,(mpfr_prec_t)precision_bits); |
| 1829 |
mpfi_get_left(ret_val, x_orig.data.num); |
| 1830 |
mpfi_set_fr(ret->data.num,ret_val); |
| 1831 |
mpfr_clear(ret_val); |
| 1832 |
} |
| 1833 |
|
| 1834 |
void mp_interval_m_get_right_endpoint (MP mp, mp_number *ret, mp_number x_orig) { |
| 1835 |
(void)mp; |
| 1836 |
mpfr_t ret_val; |
| 1837 |
mpfr_init2(ret_val,(mpfr_prec_t)precision_bits); |
| 1838 |
mpfi_get_right(ret_val, x_orig.data.num); |
| 1839 |
mpfi_set_fr(ret->data.num,ret_val); |
| 1840 |
mpfr_clear(ret_val); |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
@ Here is the routine that calculates $2^8$ times the natural logarithm |
| 1845 |
of a |scaled| quantity; |
| 1846 |
|
| 1847 |
@c |
| 1848 |
void mp_interval_m_log (MP mp, mp_number *ret, mp_number x_orig) { |
| 1849 |
if (!mpfi_positive_p((mpfi_ptr)x_orig.data.num)) { |
| 1850 |
@<Handle non-positive logarithm@>; |
| 1851 |
} else { |
| 1852 |
mpfi_log(ret->data.num, x_orig.data.num); |
| 1853 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1854 |
mpfi_mul_si(ret->data.num, ret->data.num, 256); |
| 1855 |
} |
| 1856 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1857 |
} |
| 1858 |
|
| 1859 |
@ @<Handle non-positive logarithm@>= |
| 1860 |
{ |
| 1861 |
char msg[256]; |
| 1862 |
const char *hlp[] = { |
| 1863 |
"Since I don't take logs of non-positive numbers,", |
| 1864 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1865 |
NULL }; |
| 1866 |
char *xstr = mp_interval_number_tostring (mp, x_orig); |
| 1867 |
mp_snprintf (msg, 256, "Logarithm of %s has been replaced by 0", xstr); |
| 1868 |
free (xstr); |
| 1869 |
@.Logarithm...replaced by 0@>; |
| 1870 |
mp_error (mp, msg, hlp, true); |
| 1871 |
/*mpfi_set_zero(ret->data.num,1); *//* 1 == positive */ |
| 1872 |
mpfi_set_d(ret->data.num,0.0); |
| 1873 |
} |
| 1874 |
|
| 1875 |
|
| 1876 |
@ Conversely, the exponential routine calculates $\exp(x/2^8)$, |
| 1877 |
when |x| is |scaled|. |
| 1878 |
|
| 1879 |
@c |
| 1880 |
void mp_interval_m_exp (MP mp, mp_number *ret, mp_number x_orig) { |
| 1881 |
mpfi_t temp; |
| 1882 |
mpfi_init2(temp, (mpfr_prec_t)precision_bits); |
| 1883 |
mpfi_div_si(temp, x_orig.data.num, 256); |
| 1884 |
mpfi_exp(ret->data.num, temp); |
| 1885 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1886 |
mpfi_clear (temp); |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
@ Given integers |x| and |y|, not both zero, the |n_arg| function |
| 1891 |
returns the |angle| whose tangent points in the direction $(x,y)$. |
| 1892 |
|
| 1893 |
@c |
| 1894 |
void mp_interval_n_arg (MP mp, mp_number *ret, mp_number x_orig, mp_number y_orig) { |
| 1895 |
if (mpfi_zero_p((mpfi_ptr )x_orig.data.num) && mpfi_zero_p((mpfi_ptr )y_orig.data.num)) { |
| 1896 |
@<Handle undefined arg@>; |
| 1897 |
} else { |
| 1898 |
mpfi_t atan2val, oneeighty_angle; |
| 1899 |
mpfi_init2(atan2val, (mpfr_prec_t)precision_bits); |
| 1900 |
mpfi_init2(oneeighty_angle, (mpfr_prec_t)precision_bits); |
| 1901 |
ret->type = mp_angle_type; |
| 1902 |
mpfi_set_si(oneeighty_angle, 180 * angle_multiplier); |
| 1903 |
mpfi_div(oneeighty_angle, oneeighty_angle, PI_mpfi_t); |
| 1904 |
/*checkZero((mpfi_ptr)y_orig.data.num);*/ |
| 1905 |
/*checkZero((mpfi_ptr)x_orig.data.num);*/ |
| 1906 |
mpfi_atan2(atan2val, y_orig.data.num, x_orig.data.num); |
| 1907 |
mpfi_mul(ret->data.num, atan2val, oneeighty_angle); |
| 1908 |
checkZero((mpfi_ptr)ret->data.num); |
| 1909 |
mpfi_clear(atan2val); |
| 1910 |
mpfi_clear(oneeighty_angle); |
| 1911 |
} |
| 1912 |
mp_check_mpfi_t(mp, ret->data.num); |
| 1913 |
} |
| 1914 |
|
| 1915 |
|
| 1916 |
@ @<Handle undefined arg@>= |
| 1917 |
{ |
| 1918 |
const char *hlp[] = { |
| 1919 |
"The `angle' between two identical points is undefined.", |
| 1920 |
"I'm zeroing this one. Proceed, with fingers crossed.", |
| 1921 |
NULL }; |
| 1922 |
mp_error (mp, "angle(0,0) is taken as zero", hlp, true); |
| 1923 |
@.angle(0,0)...zero@>; |
| 1924 |
/*mpfi_set_zero(ret->data.num,1); *//* 1 == positive */ |
| 1925 |
mpfi_set_d(ret->data.num,0.0); |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
@ Conversely, the |n_sin_cos| routine takes an |angle| and produces the sine |
| 1930 |
and cosine of that angle. The results of this routine are |
| 1931 |
stored in global integer variables |n_sin| and |n_cos|. |
| 1932 |
|
| 1933 |
@ Calculate sines and cosines. |
| 1934 |
|
| 1935 |
@c |
| 1936 |
void mp_interval_sin_cos (MP mp, mp_number z_orig, mp_number *n_cos, mp_number *n_sin) { |
| 1937 |
mpfi_t rad; |
| 1938 |
mpfi_t one_eighty; |
| 1939 |
mpfi_init2(rad, (mpfr_prec_t)precision_bits); |
| 1940 |
mpfi_init2(one_eighty, (mpfr_prec_t)precision_bits); |
| 1941 |
mpfi_set_si(one_eighty, 180 * 16); |
| 1942 |
mpfi_mul (rad, z_orig.data.num, PI_mpfi_t); |
| 1943 |
mpfi_div (rad, rad, one_eighty); |
| 1944 |
|
| 1945 |
mpfi_sin (n_sin->data.num, rad); |
| 1946 |
mpfi_cos (n_cos->data.num, rad); |
| 1947 |
|
| 1948 |
mpfi_mul (n_cos->data.num,n_cos->data.num, fraction_multiplier_mpfi_t); |
| 1949 |
mpfi_mul (n_sin->data.num,n_sin->data.num, fraction_multiplier_mpfi_t); |
| 1950 |
mp_check_mpfi_t(mp, n_cos->data.num); |
| 1951 |
mp_check_mpfi_t(mp, n_sin->data.num); |
| 1952 |
mpfi_clear (rad); |
| 1953 |
mpfi_clear (one_eighty); |
| 1954 |
} |
| 1955 |
|
| 1956 |
@ This is the http://www-cs-faculty.stanford.edu/~uno/programs/rng.c |
| 1957 |
with small cosmetic modifications. |
| 1958 |
|
| 1959 |
@c |
| 1960 |
#define KK 100 /* the long lag */ |
| 1961 |
#define LL 37 /* the short lag */ |
| 1962 |
#define MM (1L<<30) /* the modulus */ |
| 1963 |
#define mod_diff(x,y) (((x)-(y))&(MM-1)) /* subtraction mod MM */ |
| 1964 |
/* */ |
| 1965 |
static long ran_x[KK]; /* the generator state */ |
| 1966 |
/* */ |
| 1967 |
static void ran_array(long aa[],int n) /* put n new random numbers in aa */ |
| 1968 |
/* long aa[] destination */ |
| 1969 |
/* int n array length (must be at least KK) */ |
| 1970 |
{ |
| 1971 |
register int i,j; |
| 1972 |
for (j=0;j<KK;j++) aa[j]=ran_x[j]; |
| 1973 |
for (;j<n;j++) aa[j]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1974 |
for (i=0;i<LL;i++,j++) ran_x[i]=mod_diff(aa[j-KK],aa[j-LL]); |
| 1975 |
for (;i<KK;i++,j++) ran_x[i]=mod_diff(aa[j-KK],ran_x[i-LL]); |
| 1976 |
} |
| 1977 |
/* */ |
| 1978 |
/* the following routines are from exercise 3.6--15 */ |
| 1979 |
/* after calling |ran_start|, get new randoms by, e.g., |x=ran_arr_next()| */ |
| 1980 |
/* */ |
| 1981 |
#define QUALITY 1009 /* recommended quality level for high-res use */ |
| 1982 |
static long ran_arr_buf[QUALITY]; |
| 1983 |
static long ran_arr_dummy=-1, ran_arr_started=-1; |
| 1984 |
static long *ran_arr_ptr=&ran_arr_dummy; /* the next random number, or -1 */ |
| 1985 |
/* */ |
| 1986 |
#define TT 70 /* guaranteed separation between streams */ |
| 1987 |
#define is_odd(x) ((x)&1) /* units bit of x */ |
| 1988 |
/* */ |
| 1989 |
static void ran_start(long seed) /* do this before using |ran_array| */ |
| 1990 |
/* long seed selector for different streams */ |
| 1991 |
{ |
| 1992 |
register int t,j; |
| 1993 |
long x[KK+KK-1]; /* the preparation buffer */ |
| 1994 |
register long ss=(seed+2)&(MM-2); |
| 1995 |
for (j=0;j<KK;j++) { |
| 1996 |
x[j]=ss; /* bootstrap the buffer */ |
| 1997 |
ss<<=1; if (ss>=MM) ss-=MM-2; /* cyclic shift 29 bits */ |
| 1998 |
} |
| 1999 |
x[1]++; /* make x[1] (and only x[1]) odd */ |
| 2000 |
for (ss=seed&(MM-1),t=TT-1; t; ) { |
| 2001 |
for (j=KK-1;j>0;j--) x[j+j]=x[j], x[j+j-1]=0; /* "square" */ |
| 2002 |
for (j=KK+KK-2;j>=KK;j--) |
| 2003 |
x[j-(KK-LL)]=mod_diff(x[j-(KK-LL)],x[j]), |
| 2004 |
x[j-KK]=mod_diff(x[j-KK],x[j]); |
| 2005 |
if (is_odd(ss)) { /* "multiply by z" */ |
| 2006 |
for (j=KK;j>0;j--) x[j]=x[j-1]; |
| 2007 |
x[0]=x[KK]; /* shift the buffer cyclically */ |
| 2008 |
x[LL]=mod_diff(x[LL],x[KK]); |
| 2009 |
} |
| 2010 |
if (ss) ss>>=1; else t--; |
| 2011 |
} |
| 2012 |
for (j=0;j<LL;j++) ran_x[j+KK-LL]=x[j]; |
| 2013 |
for (;j<KK;j++) ran_x[j-LL]=x[j]; |
| 2014 |
for (j=0;j<10;j++) ran_array(x,KK+KK-1); /* warm things up */ |
| 2015 |
ran_arr_ptr=&ran_arr_started; |
| 2016 |
} |
| 2017 |
/* */ |
| 2018 |
#define ran_arr_next() (*ran_arr_ptr>=0? *ran_arr_ptr++: ran_arr_cycle()) |
| 2019 |
static long ran_arr_cycle(void) |
| 2020 |
{ |
| 2021 |
if (ran_arr_ptr==&ran_arr_dummy) |
| 2022 |
ran_start(314159L); /* the user forgot to initialize */ |
| 2023 |
ran_array(ran_arr_buf,QUALITY); |
| 2024 |
ran_arr_buf[KK]=-1; |
| 2025 |
ran_arr_ptr=ran_arr_buf+1; |
| 2026 |
return ran_arr_buf[0]; |
| 2027 |
} |
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
@ To initialize the |randoms| table, we call the following routine. |
| 2033 |
|
| 2034 |
@c |
| 2035 |
void mp_init_randoms (MP mp, int seed) { |
| 2036 |
int j, jj, k; /* more or less random integers */ |
| 2037 |
int i; /* index into |randoms| */ |
| 2038 |
j = abs (seed); |
| 2039 |
while (j >= fraction_one) { |
| 2040 |
j = j/2; |
| 2041 |
} |
| 2042 |
k = 1; |
| 2043 |
for (i = 0; i <= 54; i++) { |
| 2044 |
jj = k; |
| 2045 |
k = j - k; |
| 2046 |
j = jj; |
| 2047 |
if (k<0) |
| 2048 |
k += fraction_one; |
| 2049 |
mpfi_set_si(mp->randoms[(i * 21) % 55].data.num, j); |
| 2050 |
} |
| 2051 |
mp_new_randoms (mp); |
| 2052 |
mp_new_randoms (mp); |
| 2053 |
mp_new_randoms (mp); /* ``warm up'' the array */ |
| 2054 |
|
| 2055 |
ran_start ((unsigned long)seed); |
| 2056 |
|
| 2057 |
} |
| 2058 |
|
| 2059 |
@ @c |
| 2060 |
void mp_interval_number_modulo (mp_number *a, mp_number b) { |
| 2061 |
mpfi_remainder (a->data.num, a->data.num, b.data.num); |
| 2062 |
} |
| 2063 |
|
| 2064 |
@ To consume a random integer for the uniform generator, the program below will say `|next_unif_random|'. |
| 2065 |
|
| 2066 |
@c |
| 2067 |
static void mp_next_unif_random (MP mp, mp_number *ret) { |
| 2068 |
mp_number rop; |
| 2069 |
unsigned long int op; |
| 2070 |
float flt_op ; |
| 2071 |
(void)mp; |
| 2072 |
mp_new_number (mp, &rop, mp_scaled_type); |
| 2073 |
op = (unsigned)ran_arr_next(); |
| 2074 |
flt_op = (float)((double)op/(MM*1.0)); |
| 2075 |
mpfi_set_d ((mpfi_ptr)(rop.data.num), flt_op); |
| 2076 |
mp_number_clone (ret, rop); |
| 2077 |
free_number (rop); |
| 2078 |
} |
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
@ To consume a random fraction, the program below will say `|next_random|'. |
| 2083 |
|
| 2084 |
@c |
| 2085 |
static void mp_next_random (MP mp, mp_number *ret) { |
| 2086 |
if ( mp->j_random==0 ) |
| 2087 |
mp_new_randoms(mp); |
| 2088 |
else |
| 2089 |
mp->j_random = mp->j_random-1; |
| 2090 |
mp_number_clone (ret, mp->randoms[mp->j_random]); |
| 2091 |
} |
| 2092 |
|
| 2093 |
@ To produce a uniform random number in the range |0<=u<x| or |0>=u>x| |
| 2094 |
or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here. |
| 2095 |
|
| 2096 |
Note that the call of |take_fraction| will produce the values 0 and~|x| |
| 2097 |
with about half the probability that it will produce any other particular |
| 2098 |
values between 0 and~|x|, because it rounds its answers. |
| 2099 |
|
| 2100 |
@c |
| 2101 |
static void mp_interval_m_unif_rand (MP mp, mp_number *ret, mp_number x_orig) { |
| 2102 |
// CHECK |
| 2103 |
// This function probably should be rewritten |
| 2104 |
// it's the same as in mpmathbinary |
| 2105 |
// |
| 2106 |
// |
| 2107 |
mp_number y; /* trial value */ |
| 2108 |
mp_number x, abs_x; |
| 2109 |
mp_number u; |
| 2110 |
char *r ;mpfr_exp_t e; |
| 2111 |
mpfr_t ret_val; |
| 2112 |
double ret_d; |
| 2113 |
mpfr_init2(ret_val,(mpfr_prec_t)precision_bits); |
| 2114 |
new_fraction (y); |
| 2115 |
new_number (x); |
| 2116 |
new_number (abs_x); |
| 2117 |
new_number (u); |
| 2118 |
mp_number_clone (&x, x_orig); |
| 2119 |
mp_number_clone (&abs_x, x); |
| 2120 |
mp_interval_abs (&abs_x); |
| 2121 |
mp_next_unif_random(mp, &u); |
| 2122 |
mpfi_mul (y.data.num, abs_x.data.num, u.data.num); |
| 2123 |
free_number (u); |
| 2124 |
if (mp_number_equal(y, abs_x)) { |
| 2125 |
mp_number_clone (ret, ((math_data *)mp->math)->zero_t); |
| 2126 |
} else if (mp_number_greater(x, ((math_data *)mp->math)->zero_t)) { |
| 2127 |
mp_number_clone (ret, y); |
| 2128 |
} else { |
| 2129 |
mp_number_clone (ret, y); |
| 2130 |
mp_number_negate (ret); |
| 2131 |
} |
| 2132 |
r = mpfr_get_str(NULL, /* |char *str|, */ |
| 2133 |
&e, /* |mpfi_exp_t *expptr|,*/ |
| 2134 |
10, /* |int b|, */ |
| 2135 |
0, /* |size_t n|, */ |
| 2136 |
ret_val, /* |mpfi_t op|, */ |
| 2137 |
MPFR_ROUNDING /* |mpfi_rnd_t rnd|*/ |
| 2138 |
); |
| 2139 |
mpfr_free_str(r); |
| 2140 |
free_number (abs_x); |
| 2141 |
free_number (x); |
| 2142 |
free_number (y); |
| 2143 |
ret_d = mpfr_get_d(ret_val, MPFR_ROUNDING); |
| 2144 |
mpfi_set_d (ret->data.num, ret_d); |
| 2145 |
} |
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
@ Finally, a normal deviate with mean zero and unit standard deviation |
| 2150 |
can readily be obtained with the ratio method (Algorithm 3.4.1R in |
| 2151 |
{\sl The Art of Computer Programming\/}). |
| 2152 |
|
| 2153 |
@c |
| 2154 |
static void mp_interval_m_norm_rand (MP mp, mp_number *ret) { |
| 2155 |
mp_number ab_vs_cd; |
| 2156 |
mp_number abs_x; |
| 2157 |
mp_number u; |
| 2158 |
mp_number r; |
| 2159 |
mp_number la, xa; |
| 2160 |
new_number (ab_vs_cd); |
| 2161 |
new_number (la); |
| 2162 |
new_number (xa); |
| 2163 |
new_number (abs_x); |
| 2164 |
new_number (u); |
| 2165 |
new_number (r); |
| 2166 |
|
| 2167 |
do { |
| 2168 |
do { |
| 2169 |
mp_number v; |
| 2170 |
new_number (v); |
| 2171 |
mp_next_random(mp, &v); |
| 2172 |
mp_number_substract (&v, ((math_data *)mp->math)->fraction_half_t); |
| 2173 |
mp_interval_number_take_fraction (mp,&xa, ((math_data *)mp->math)->sqrt_8_e_k, v); |
| 2174 |
free_number (v); |
| 2175 |
mp_next_random(mp, &u); |
| 2176 |
mp_number_clone (&abs_x, xa); |
| 2177 |
mp_interval_abs (&abs_x); |
| 2178 |
} while (!mp_number_less(abs_x, u)); |
| 2179 |
mp_interval_number_make_fraction (mp, &r, xa, u); |
| 2180 |
mp_number_clone (&xa, r); |
| 2181 |
mp_interval_m_log (mp,&la, u); |
| 2182 |
mp_set_interval_from_substraction(&la, ((math_data *)mp->math)->twelve_ln_2_k, la); |
| 2183 |
mp_interval_ab_vs_cd (mp,&ab_vs_cd, ((math_data *)mp->math)->one_k, la, xa, xa); |
| 2184 |
} while (mp_number_less(ab_vs_cd,((math_data *)mp->math)->zero_t)); |
| 2185 |
mp_number_clone (ret, xa); |
| 2186 |
free_number (ab_vs_cd); |
| 2187 |
free_number (r); |
| 2188 |
free_number (abs_x); |
| 2189 |
free_number (la); |
| 2190 |
free_number (xa); |
| 2191 |
free_number (u); |
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
@ The following subroutine is used only in |norm_rand| and tests if $ab$ is |
| 2197 |
greater than, equal to, or less than~$cd$. |
| 2198 |
The result is $+1$, 0, or~$-1$ in the three respective cases. |
| 2199 |
|
| 2200 |
@c |
| 2201 |
static void mp_interval_ab_vs_cd (MP mp, mp_number *ret, mp_number a_orig, mp_number b_orig, mp_number c_orig, mp_number d_orig) { |
| 2202 |
mpfi_t a, b, c, d; |
| 2203 |
mpfi_t ab, cd; |
| 2204 |
|
| 2205 |
int cmp = 0; |
| 2206 |
(void)mp; |
| 2207 |
mpfi_inits2((mpfr_prec_t)precision_bits, a,b,c,d,ab,cd,(mpfi_ptr)0); |
| 2208 |
mpfi_set(a, (mpfi_ptr )a_orig.data.num); |
| 2209 |
mpfi_set(b, (mpfi_ptr )b_orig.data.num); |
| 2210 |
mpfi_set(c, (mpfi_ptr )c_orig.data.num); |
| 2211 |
mpfi_set(d, (mpfi_ptr )d_orig.data.num); |
| 2212 |
|
| 2213 |
mpfi_mul(ab,a,b); |
| 2214 |
mpfi_mul(cd,c,d); |
| 2215 |
|
| 2216 |
mpfi_set(ret->data.num, zero); |
| 2217 |
cmp = mpfi_cmp(ab,cd); |
| 2218 |
if (cmp) { |
| 2219 |
if (cmp>0) |
| 2220 |
mpfi_set(ret->data.num, one); |
| 2221 |
else |
| 2222 |
mpfi_set(ret->data.num, minusone); |
| 2223 |
} |
| 2224 |
mp_check_mpfi_t(mp, ret->data.num); |
| 2225 |
mpfi_clears(a,b,c,d,ab,cd,(mpfi_ptr)0); |
| 2226 |
return; |
| 2227 |
} |
| 2228 |
|
| 2229 |
|