aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
lrslong.h
Go to the documentation of this file.
1#ifndef DOXYGEN_SHOULD_SKIP_THIS
2
3/* lrslong.h (lrs long integer arithmetic library */
4/* Copyright: David Avis 2000, avis@cs.mcgill.ca */
5/* Version 4.0, February 17, 2000 */
6
7/* This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20 */
21/******************************************************************************/
22/* See http://cgm.cs.mcgill.ca/~avis/C/lrs.html for lrs usage instructions */
23/******************************************************************************/
24/* This package contains the extended precision routines used by lrs
25 and some other miscellaneous routines. The maximum precision depends on
26 the parameter MAX_DIGITS defined below, with usual default of 255L. This
27 gives a maximum of 1020 decimal digits on 32 bit machines. The procedure
28 lrs_mp_init(dec_digits) may set a smaller number of dec_digits, and this
29 is useful if arrays or matrices will be used.
30 */
31
32
33#ifdef PLRS
34#include <string>
35using namespace std;
36#endif
37
38
39/***********/
40/* defines */
41/***********/
42/*
43 this is number of longwords. Increasing this won't cost you that much
44 since only variables other than the A matrix are allocated this size.
45 Changing affects running time in small but not very predictable ways.
46 */
47
48#define MAX_DIGITS 255L
49
50/*
51 this is in decimal digits, you pay in memory if you increase this,
52 unless you override by a line with
53 digits n
54 before the begin line of your file.
55 */
56#define DEFAULT_DIGITS 100L
57
58
59// 64 bits for windows (long is 32 bits)
60#ifdef _MSC_VER
61typedef __int64 int64_t;
62typedef unsigned __int64 uint64_t;
63#else
64#include <stdint.h>
65#endif
66
67/**********MACHINE DEPENDENT CONSTANTS***********/
68/* MAXD is 2^(k-1)-1 where k=16,32,64 word size */
69/* MAXD must be at least 2*BASE^2 */
70/* If BASE is 10^k, use "%k.ku" for FORMAT */
71/* INTSIZE is number of bytes for integer */
72/* 32/64 bit machines */
73/***********************************************/
74#ifdef B32
75/*32 bit machines */
76#define FORMAT "%4.4u"
77#define MAXD 2147483647L
78#define BASE 10000L
79#define BASE_DIG 4
80#define INTSIZE 8L
81#define BIT "32bit"
82#else
83/* 64 bit machines */
84#define MAXD 9223372036854775807L
85#define BASE 1000000000L
86#define FORMAT "%9.9u"
87#define BASE_DIG 9
88#define INTSIZE 16L
89#define BIT "64bit"
90#endif
91
92#define MAXINPUT 1000 /*max length of any input rational */
93
94#define POS 1L
95#define NEG -1L
96#ifndef TRUE
97#define TRUE 1L
98#endif
99#ifndef FALSE
100#define FALSE 0L
101#endif
102#define ONE 1L
103#define TWO 2L
104#define ZERO 0L
105
106/**********************************/
107/* MACROS */
108/* dependent on mp implementation */
109/**********************************/
110
111#define addint(a, b, c) *(c) = *(a) + *(b)
112#define changesign(a) (*(a) = -*(a))
113#define copy(a, b) ((a)[0] = (b)[0])
114#define decint(a, b) *(a) = *(a) - *(b)
115#define divint(a, b, c) \
116 *(c) = *(a) / *(b); \
117 *(a) = *(a) % *(b)
118#define exactdivint(a, b, c) *(c) = *(a) / *(b);
119#define mp_greater(a, b) (*(a) > *(b))
120#define itomp(in, a) *(a) = in
121#define linint(a, ka, b, kb) *(a) = *(a)*ka + *(b)*kb
122#define mulint(a, b, c) *(c) = *(a) * *(b)
123#define one(a) (*(a) == 1)
124#define negative(a) (*(a) < 0)
125#define normalize(a) ()0
126#define positive(a) (*(a) > 0)
127#define sign(a) (*(a) < 0 ? NEG : POS)
128#define storesign(a, sa) (*(a) = labs(*(a)) * sa)
129#define subint(a, b, c) *(c) = *(a) - *(b)
130#define iszero(a) (*(a) == 0)
131
132
133/*
134 * convert between decimal and machine (longword digits). Notice lovely
135 * implementation of ceiling function :-)
136 */
137#define DEC2DIG(d) ((d) % BASE_DIG ? (d) / BASE_DIG + 1 : (d) / BASE_DIG)
138#define DIG2DEC(d) ((d)*BASE_DIG)
139
140#ifndef OMIT_SIGNALS
141#include <signal.h>
142#include <stdlib.h> /* labs */
143#define errcheck(s, e) \
144 if ((int64_t)(e) == -1L) { \
145 perror(s); \
146 exit(1); \
147 }
148#endif
149
150#define CALLOC(n, s) xcalloc(n, s, __LINE__, __FILE__)
151
152/*************/
153/* typedefs */
154/*************/
155
156typedef int64_t lrs_mp[1]; /* type lrs_mp holds one long integer */
157typedef int64_t* lrs_mp_t;
158typedef int64_t** lrs_mp_vector;
159typedef int64_t*** lrs_mp_matrix;
160
161/*********************/
162/*global variables */
163/*********************/
164
165extern int64_t lrs_digits; /* max permitted no. of digits */
166extern int64_t lrs_record_digits; /* this is the biggest acheived so far. */
167
168extern FILE* lrs_ifp; /* input file pointer */
169extern FILE* lrs_ofp; /* output file pointer */
170
171/*********************************************************/
172/* Initialization and allocation procedures - must use! */
173/******************************************************* */
174
175int64_t lrs_mp_init(int64_t dec_digits,
176 FILE* lrs_ifp,
177 FILE* lrs_ofp); /* max number of decimal digits, fps */
178
179#define lrs_alloc_mp(a)
180#define lrs_clear_mp(a)
181
182lrs_mp_t lrs_alloc_mp_t(); /* dynamic allocation of lrs_mp */
183lrs_mp_vector
184lrs_alloc_mp_vector(int64_t n); /* allocate lrs_mp_vector for n+1 lrs_mp numbers */
185lrs_mp_matrix
186lrs_alloc_mp_matrix(int64_t m,
187 int64_t n); /* allocate lrs_mp_matrix for m+1 x n+1 lrs_mp */
188
189void lrs_clear_mp_vector(lrs_mp_vector a, int64_t n);
190void lrs_clear_mp_matrix(lrs_mp_matrix a, int64_t m, int64_t n);
191
192
193/*********************************************************/
194/* Core library functions - depend on mp implementation */
195/******************************************************* */
196void atomp(const char s[], lrs_mp a); /* convert string to lrs_mp integer */
197int64_t compare(lrs_mp a, lrs_mp b); /* a ? b and returns -1,0,1 for <,=,> */
198void gcd(lrs_mp u, lrs_mp v); /* returns u=gcd(u,v) destroying v */
199void mptodouble(lrs_mp a, double* x); /* convert lrs_mp to double */
200int64_t mptoi(lrs_mp a); /* convert lrs_mp to long integer */
201#ifdef PLRS
202string pmp(char name[], lrs_mp a); /* print the long precision integer a */
203string prat(char name[], lrs_mp Nt, lrs_mp Dt); /* reduce and print Nt/Dt */
204char* cprat(char name[], lrs_mp Nt, lrs_mp Dt); /* C version of prat */
205int64_t
206plrs_readrat(lrs_mp Na,
207 lrs_mp Da,
208 const char* rat); /* take a rational number and convert to lrs_mp */
209#else
210void pmp(char name[], lrs_mp a); /* print the long precision integer a */
211void prat(char name[], lrs_mp Nt, lrs_mp Dt); /* reduce and print Nt/Dt */
212#endif
213void readmp(lrs_mp a); /* read an integer and convert to lrs_mp */
214int64_t readrat(lrs_mp Na,
215 lrs_mp Da); /* read a rational or int and convert to lrs_mp */
216void reduce(lrs_mp Na, lrs_mp Da); /* reduces Na Da by gcd(Na,Da) */
217
218/*********************************************************/
219/* Standard arithmetic & misc. functions */
220/* should be independent of mp implementation */
221/******************************************************* */
222
223void atoaa(const char in[],
224 char num[],
225 char den[]); /* convert rational string in to num/den strings */
226int64_t atos(char s[]); /* convert s to integer */
227int64_t comprod(lrs_mp Na,
228 lrs_mp Nb,
229 lrs_mp Nc,
230 lrs_mp Nd); /* +1 if Na*Nb > Nc*Nd,-1 if Na*Nb > Nc*Nd else 0 */
231void divrat(lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc);
232/* computes Nc/Dc = (Na/Da) /( Nb/Db ) and reduce */
233void getfactorial(lrs_mp factorial, int64_t k); /* compute k factorial in lrs_mp */
234void linrat(lrs_mp Na,
235 lrs_mp Da,
236 int64_t ka,
237 lrs_mp Nb,
238 lrs_mp Db,
239 int64_t kb,
240 lrs_mp Nc,
241 lrs_mp Dc);
242void lcm(lrs_mp a, lrs_mp b); /* a = least common multiple of a, b; b is saved */
243void mulrat(lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc);
244/* computes Nc/Dc=(Na/Da)*(Nb/Db) and reduce */
245int64_t myrandom(int64_t num,
246 int64_t nrange); /* return a random number in range 0..nrange-1 */
247void notimpl(char s[]); /* bail out - help! */
248void rattodouble(lrs_mp a,
249 lrs_mp b,
250 double* x); /* convert lrs_mp rational to double */
251void reduceint(lrs_mp Na, lrs_mp Da); /* divide Na by Da and return it */
252void reducearray(lrs_mp_vector p,
253 int64_t n); /* find gcd of p[0]..p[n-1] and divide through by */
254void scalerat(lrs_mp Na, lrs_mp Da, int64_t ka); /* scales rational by ka */
255
256/**********************************/
257/* Miscellaneous functions */
258/******************************** */
259
260void lrs_getdigits(int64_t* a, int64_t* b); /* send digit information to user */
261
262void stringcpy(char* s, char* t); /* copy t to s pointer version */
263
264void* calloc();
265void* malloc();
266void* xcalloc(int64_t n, int64_t s, int64_t l, char* f);
267
268void lrs_default_digits_overflow();
269
270/* end of lrs_mp.h (vertex enumeration using lexicographic reverse search) */
271
272#endif // DOXYGEN_SHOULD_SKIP_THIS
STL namespace.