-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
86 lines (80 loc) · 1.85 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cassert>
#include "util.h"
/**
* Product of a set of numbers modular n
*/
void productMod(fmpz_t res, const ulong *a, ulong k, const fmpz_t n)
{
fmpz_one(res);
for(ulong i = 0; i < k; i++)
{
fmpz_mul_ui(res,res,a[i]);
fmpz_mod(res,res,n);
}
}
/**
* Clear an array of fmpz_t.
*/
void freeArrayOfFmpz(fmpz_t *array, ulong size)
{
for(ulong i = 0; i < size; i++)
fmpz_clear(array[i]);
}
/**
* Get the sum of an array of doubles.
*
* This is tricky and recursive method is used instead of adding them one by
* one because of the properties of floating point computation in computer:
* the precision is badly hurt when summing two floating point numbers significantly
* different in order.
*/
double doublesum(const double array[], ulong l, ulong r)
{
if(r-l == 0) return 0;
if(r-l == 1) return array[l];
return doublesum(array,l,(l+r)/2) + doublesum(array,(l+r)/2,r);
}
/**
* Some print procedures.
*/
void printListOfNumbers(FILE *file, slong *A, ulong s, ulong N)
{
for(ulong i = 0; i < s; i++)
{
if(N && i && i % N == 0) fprintf(file,"\n");
fprintf(file,"%10ld",A[i]);
}
fprintf(file,"\n");
}
void printListOfNumbers(FILE *file, ulong *A, ulong s, ulong N)
{
for(ulong i = 0; i < s; i++)
{
if(N && i && i % N == 0) fprintf(file,"\n");
fprintf(file,"%10ld",A[i]);
}
fprintf(file,"\n");
}
void printListOfPairs(FILE *file, MyPair *A, ulong s, ulong N)
{
for(ulong i = 0; i < s; i++)
{
if(N && i && i % N == 0) fprintf(file,"\n");
fprintf(file,"%10ld%10ld",A[i].r,A[i].p);
}
fprintf(file,"\n");
}
/**
* Select part of an array corresponding to the 1's in a 0-1 vector,
* discard the rest and keep only the selected ones, update the size of array
*/
void select(MyPair *pairs, int *vec, ulong I, ulong &n)
{
ulong loc = 0;
for(ulong i = 0; i < I; i++)
{
if(vec[i]) pairs[loc++] = pairs[i];
}
n = loc;
assert(n > 0);
}