1 /* glplib03.c (miscellaneous library routines) */
3 /***********************************************************************
4 * This code is part of GLPK (GNU Linear Programming Kit).
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7 * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
9 * E-mail: <mao@gnu.org>.
11 * GLPK is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
23 ***********************************************************************/
28 /***********************************************************************
31 * str2int - convert character string to value of int type
36 * int str2int(const char *str, int *val);
40 * The routine str2int converts the character string str to a value of
41 * integer type and stores the value into location, which the parameter
42 * val points to (in the case of error content of this location is not
47 * The routine returns one of the following error codes:
50 * 1 - value out of range;
51 * 2 - character string is syntactically incorrect. */
53 int str2int(const char *str, int *_val)
54 { int d, k, s, val = 0;
55 /* scan optional sign */
58 else if (str[0] == '-')
62 /* check for the first digit */
63 if (!isdigit((unsigned char)str[k])) return 2;
65 while (isdigit((unsigned char)str[k]))
68 { if (val > INT_MAX / 10) return 1;
70 if (val > INT_MAX - d) return 1;
74 { if (val < INT_MIN / 10) return 1;
76 if (val < INT_MIN + d) return 1;
80 /* check for terminator */
81 if (str[k] != '\0') return 2;
82 /* conversion has been done */
87 /***********************************************************************
90 * str2num - convert character string to value of double type
95 * int str2num(const char *str, double *val);
99 * The routine str2num converts the character string str to a value of
100 * double type and stores the value into location, which the parameter
101 * val points to (in the case of error content of this location is not
106 * The routine returns one of the following error codes:
109 * 1 - value out of range;
110 * 2 - character string is syntactically incorrect. */
112 int str2num(const char *str, double *_val)
115 /* scan optional sign */
116 k = (str[0] == '+' || str[0] == '-' ? 1 : 0);
117 /* check for decimal point */
120 /* a digit should follow it */
121 if (!isdigit((unsigned char)str[k])) return 2;
125 /* integer part should start with a digit */
126 if (!isdigit((unsigned char)str[k])) return 2;
127 /* scan integer part */
128 while (isdigit((unsigned char)str[k])) k++;
129 /* check for decimal point */
130 if (str[k] == '.') k++;
131 frac: /* scan optional fraction part */
132 while (isdigit((unsigned char)str[k])) k++;
133 /* check for decimal exponent */
134 if (str[k] == 'E' || str[k] == 'e')
136 /* scan optional sign */
137 if (str[k] == '+' || str[k] == '-') k++;
138 /* a digit should follow E, E+ or E- */
139 if (!isdigit((unsigned char)str[k])) return 2;
141 /* scan optional exponent part */
142 while (isdigit((unsigned char)str[k])) k++;
143 /* check for terminator */
144 if (str[k] != '\0') return 2;
145 /* perform conversion */
147 val = strtod(str, &endptr);
148 if (*endptr != '\0') return 2;
150 /* check for overflow */
151 if (!(-DBL_MAX <= val && val <= +DBL_MAX)) return 1;
152 /* check for underflow */
153 if (-DBL_MIN < val && val < +DBL_MIN) val = 0.0;
154 /* conversion has been done */
159 /***********************************************************************
162 * strspx - remove all spaces from character string
166 * #include "glplib.h"
167 * char *strspx(char *str);
171 * The routine strspx removes all spaces from the character string str.
175 * The routine returns a pointer to the character string.
179 * strspx(" Errare humanum est ") => "Errarehumanumest"
181 * strspx(" ") => "" */
183 char *strspx(char *str)
185 for (s = t = str; *s; s++) if (*s != ' ') *t++ = *s;
190 /***********************************************************************
193 * strtrim - remove trailing spaces from character string
197 * #include "glplib.h"
198 * char *strtrim(char *str);
202 * The routine strtrim removes trailing spaces from the character
207 * The routine returns a pointer to the character string.
211 * strtrim("Errare humanum est ") => "Errare humanum est"
213 * strtrim(" ") => "" */
215 char *strtrim(char *str)
217 for (t = strrchr(str, '\0') - 1; t >= str; t--)
218 { if (*t != ' ') break;
224 /***********************************************************************
227 * strrev - reverse character string
231 * #include "glplib.h"
232 * char *strrev(char *s);
236 * The routine strrev changes characters in a character string s to the
237 * reverse order, except the terminating null character.
241 * The routine returns the pointer s.
247 * strrev("Today is Monday") => "yadnoM si yadoT" */
249 char *strrev(char *s)
252 for (i = 0, j = strlen(s)-1; i < j; i++, j--)
253 t = s[i], s[i] = s[j], s[j] = t;
257 /***********************************************************************
260 * gcd - find greatest common divisor of two integers
264 * #include "glplib.h"
265 * int gcd(int x, int y);
269 * The routine gcd returns gcd(x, y), the greatest common divisor of
270 * the two positive integers given.
274 * The routine gcd is based on Euclid's algorithm.
278 * Don Knuth, The Art of Computer Programming, Vol.2: Seminumerical
279 * Algorithms, 3rd Edition, Addison-Wesley, 1997. Section 4.5.2: The
280 * Greatest Common Divisor, pp. 333-56. */
282 int gcd(int x, int y)
284 xassert(x > 0 && y > 0);
286 r = x % y, x = y, y = r;
290 /***********************************************************************
293 * gcdn - find greatest common divisor of n integers
297 * #include "glplib.h"
298 * int gcdn(int n, int x[]);
302 * The routine gcdn returns gcd(x[1], x[2], ..., x[n]), the greatest
303 * common divisor of n positive integers given, n > 0.
307 * The routine gcdn is based on the following identity:
309 * gcd(x, y, z) = gcd(gcd(x, y), z).
313 * Don Knuth, The Art of Computer Programming, Vol.2: Seminumerical
314 * Algorithms, 3rd Edition, Addison-Wesley, 1997. Section 4.5.2: The
315 * Greatest Common Divisor, pp. 333-56. */
317 int gcdn(int n, int x[])
320 for (j = 1; j <= n; j++)
331 /***********************************************************************
334 * lcm - find least common multiple of two integers
338 * #include "glplib.h"
339 * int lcm(int x, int y);
343 * The routine lcm returns lcm(x, y), the least common multiple of the
344 * two positive integers given. In case of integer overflow the routine
349 * The routine lcm is based on the following identity:
351 * lcm(x, y) = (x * y) / gcd(x, y) = x * [y / gcd(x, y)],
353 * where gcd(x, y) is the greatest common divisor of x and y. */
355 int lcm(int x, int y)
359 if (x > INT_MAX / y) return 0;
363 /***********************************************************************
366 * lcmn - find least common multiple of n integers
370 * #include "glplib.h"
371 * int lcmn(int n, int x[]);
375 * The routine lcmn returns lcm(x[1], x[2], ..., x[n]), the least
376 * common multiple of n positive integers given, n > 0. In case of
377 * integer overflow the routine returns zero.
381 * The routine lcmn is based on the following identity:
383 * lcmn(x, y, z) = lcm(lcm(x, y), z),
385 * where lcm(x, y) is the least common multiple of x and y. */
387 int lcmn(int n, int x[])
390 for (j = 1; j <= n; j++)
401 /***********************************************************************
404 * round2n - round floating-point number to nearest power of two
408 * #include "glplib.h"
409 * double round2n(double x);
413 * Given a positive floating-point value x the routine round2n returns
414 * 2^n such that |x - 2^n| is minimal.
418 * round2n(10.1) = 2^3 = 8
419 * round2n(15.3) = 2^4 = 16
420 * round2n(0.01) = 2^(-7) = 0.0078125
424 * Let x = f * 2^e, where 0.5 <= f < 1 is a normalized fractional part,
425 * e is an integer exponent. Then, obviously, 0.5 * 2^e <= x < 2^e, so
426 * if x - 0.5 * 2^e <= 2^e - x, we choose 0.5 * 2^e = 2^(e-1), and 2^e
427 * otherwise. The latter condition can be written as 2 * x <= 1.5 * 2^e
428 * or 2 * f * 2^e <= 1.5 * 2^e or, finally, f <= 0.75. */
430 double round2n(double x)
435 return ldexp(1.0, f <= 0.75 ? e-1 : e);
438 /***********************************************************************
441 * fp2rat - convert floating-point number to rational number
445 * #include "glplib.h"
446 * int fp2rat(double x, double eps, double *p, double *q);
450 * Given a floating-point number 0 <= x < 1 the routine fp2rat finds
451 * its "best" rational approximation p / q, where p >= 0 and q > 0 are
452 * integer numbers, such that |x - p / q| <= eps.
456 * The routine fp2rat returns the number of iterations used to achieve
457 * the specified precision eps.
461 * For x = sqrt(2) - 1 = 0.414213562373095 and eps = 1e-6 the routine
462 * gives p = 408 and q = 985, where 408 / 985 = 0.414213197969543.
466 * It is well known that every positive real number x can be expressed
467 * as the following continued fraction:
470 * ------------------------
479 * a[k] = 1, k = 0, 1, 2, ...
481 * b[k] = floor(x[k]), k = 0, 1, 2, ...
485 * x[k] = 1 / frac(x[k-1]), k = 1, 2, 3, ...
487 * To find the "best" rational approximation of x the routine computes
488 * partial fractions f[k] by dropping after k terms as follows:
490 * f[k] = A[k] / B[k],
494 * A[-1] = 1, A[0] = b[0], B[-1] = 0, B[0] = 1,
496 * A[k] = b[k] * A[k-1] + a[k] * A[k-2],
498 * B[k] = b[k] * B[k-1] + a[k] * B[k-2].
504 * has been satisfied, the routine reports p = A[k] and q = B[k] as the
507 * In the table below here is some statistics obtained for one million
508 * random numbers uniformly distributed in the range [0, 1).
510 * eps max p mean p max q mean q max k mean k
511 * -------------------------------------------------------------
512 * 1e-1 8 1.6 9 3.2 3 1.4
513 * 1e-2 98 6.2 99 12.4 5 2.4
514 * 1e-3 997 20.7 998 41.5 8 3.4
515 * 1e-4 9959 66.6 9960 133.5 10 4.4
516 * 1e-5 97403 211.7 97404 424.2 13 5.3
517 * 1e-6 479669 669.9 479670 1342.9 15 6.3
518 * 1e-7 1579030 2127.3 3962146 4257.8 16 7.3
519 * 1e-8 26188823 6749.4 26188824 13503.4 19 8.2
523 * W. B. Jones and W. J. Thron, "Continued Fractions: Analytic Theory
524 * and Applications," Encyclopedia on Mathematics and Its Applications,
525 * Addison-Wesley, 1980. */
527 int fp2rat(double x, double eps, double *p, double *q)
529 double xk, Akm1, Ak, Bkm1, Bk, ak, bk, fk, temp;
530 if (!(0.0 <= x && x < 1.0))
531 xerror("fp2rat: x = %g; number out of range\n", x);
539 /* A[0] = b[0] = floor(x[0]) = 0 */
547 { /* x[k] = 1 / frac(x[k-1]) */
548 temp = xk - floor(xk);
549 xassert(temp != 0.0);
553 /* b[k] = floor(x[k]) */
555 /* A[k] = b[k] * A[k-1] + a[k] * A[k-2] */
556 temp = bk * Ak + ak * Akm1;
557 Akm1 = Ak, Ak = temp;
558 /* B[k] = b[k] * B[k-1] + a[k] * B[k-2] */
559 temp = bk * Bk + ak * Bkm1;
560 Bkm1 = Bk, Bk = temp;
562 /* f[k] = A[k] / B[k] */
565 print("%.*g / %.*g = %.*g", DBL_DIG, Ak, DBL_DIG, Bk, DBL_DIG,
568 if (fabs(x - fk) <= eps) break;
575 /***********************************************************************
578 * jday - convert calendar date to Julian day number
582 * #include "glplib.h"
583 * int jday(int d, int m, int y);
587 * The routine jday converts a calendar date, Gregorian calendar, to
588 * corresponding Julian day number j.
590 * From the given day d, month m, and year y, the Julian day number j
591 * is computed without using tables.
593 * The routine is valid for 1 <= y <= 4000.
597 * The routine jday returns the Julian day number, or negative value if
598 * the specified date is incorrect.
602 * R. G. Tantzen, Algorithm 199: conversions between calendar date and
603 * Julian day number, Communications of the ACM, vol. 6, no. 8, p. 444,
606 int jday(int d, int m, int y)
608 if (!(1 <= d && d <= 31 && 1 <= m && m <= 12 && 1 <= y &&
613 if (m >= 3) m -= 3; else m += 9, y--;
616 j = (146097 * c) / 4 + (1461 * ya) / 4 + (153 * m + 2) / 5 + d +
618 jdate(j, &dd, NULL, NULL);
623 /***********************************************************************
626 * jdate - convert Julian day number to calendar date
630 * #include "glplib.h"
631 * void jdate(int j, int *d, int *m, int *y);
635 * The routine jdate converts a Julian day number j to corresponding
636 * calendar date, Gregorian calendar.
638 * The day d, month m, and year y are computed without using tables and
639 * stored in corresponding locations.
641 * The routine is valid for 1721426 <= j <= 3182395.
645 * If the conversion is successful, the routine returns zero, otherwise
650 * R. G. Tantzen, Algorithm 199: conversions between calendar date and
651 * Julian day number, Communications of the ACM, vol. 6, no. 8, p. 444,
654 int jdate(int j, int *_d, int *_m, int *_y)
655 { int d, m, y, ret = 0;
656 if (!(1721426 <= j && j <= 3182395))
661 y = (4 * j - 1) / 146097;
662 j = (4 * j - 1) % 146097;
664 j = (4 * d + 3) / 1461;
665 d = (4 * d + 3) % 1461;
667 m = (5 * d - 3) / 153;
668 d = (5 * d - 3) % 153;
671 if (m <= 9) m += 3; else m -= 9, y++;
672 if (_d != NULL) *_d = d;
673 if (_m != NULL) *_m = m;
674 if (_y != NULL) *_y = y;
680 { int jbeg, jend, j, d, m, y;
681 jbeg = jday(1, 1, 1);
682 jend = jday(31, 12, 4000);
683 for (j = jbeg; j <= jend; j++)
684 { xassert(jdate(j, &d, &m, &y) == 0);
685 xassert(jday(d, m, y) == j);
687 xprintf("Routines jday and jdate work correctly.\n");