-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpfi_matrixalg.c
163 lines (130 loc) · 3.28 KB
/
mpfi_matrixalg.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
============================================================================
Name : mpfi_matrixalg.c
Author : Anastasia Lozanova Volkova
Version : 0.1
Copyright : This file contains support functions for matrix representation
in MPFI
============================================================================
*/
#include "mpfi_matrixalg.h"
/* Allocates size bytes of memory; on failure prints an error message
and calls exit(1)
*/
void *safeMalloc(size_t size) {
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
fprintf(stderr, "Could not allocate %zd bytes of memory\n", size);
exit(1);
}
return ptr;
}
/* Allocates an array of nmemb elements, each of which occupies size
bytes of memory; on failure prints an error message and calls
exit(1)
*/
void *safeCalloc(size_t nmemb, size_t size)
{
void *ptr;
ptr = calloc(nmemb, size);
if (ptr == NULL) {
fprintf(stderr, "Could not allocate an array of %zd elements with %zd bytes of memory each\n", nmemb, size);
exit(1);
}
return ptr;
}
/* Reallocates size bytes of memory at the location pointed by ptr;
on failure prints an error message and calls exit(1)
*/
void *safeRealloc(void *ptr, size_t size)
{
void *newPtr;
newPtr = realloc(ptr, size);
if ((newPtr == NULL) && (!((size == 0) && (ptr != NULL)))) {
fprintf(stderr, "Could not rellocate %zd bytes of memory at address %p\n", size, ptr);
exit(1);
}
return newPtr;
}
/* Frees the memory at the location pointed by ptr */
void safeFree(void *ptr) {
free(ptr);
}
/* Allocates a n * m matrix and initializes all entries to prec
bits
*/
mpfi_t *allocateMPFIMatrix(uint64_t n,uint64_t m, mp_prec_t prec) {
mpfi_t *A;
uint64_t i, j;
A = (mpfi_t *) safeCalloc(n * m, sizeof(mpfi_t));
for (i=0;i<n;i++) {
for (j=0;j<m;j++) {
mpfi_init2(A[i * m + j], prec);
}
}
return A;
}
/* Clears all entries of a n * m matrix A and frees the memory
allocated to that matrix
*/
void freeMPFIMatrix(mpfi_t *A, uint64_t n, uint64_t m) {
uint64_t i, j;
for (i=0;i<n;i++) {
for (j=0;j<m;j++) {
mpfi_clear(A[i * m + j]);
}
}
safeFree(A);
}
/* Sets a n * m matrix A to all zeros */
void setMPFIMatrixZero(mpfi_t *A, uint64_t n, uint64_t m) {
uint64_t i, j;
for (i=0;i<n;i++) {
for (j=0;j<m;j++) {
mpfi_set_si(A[i * m + j], 0);
}
}
}
/* Sets a n * n matrix A to identity */
void setMPFIMatrixIdentity(mpfi_t *A, uint64_t n) {
uint64_t i, j;
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
mpfi_set_si(A[i * n + j], 0);
}
mpfi_set_si(A[i * n + i], 1);
}
}
/* Converts element-by-element MPFR matrix A
into MPFI latrix B.
Matrix B is assumed preallocated outside the function. */
void MPFItoMPFRMatrix(mpfi_t *B, mpfr_t *A, uint64_t m, uint64_t n)
{
int i,j;
mp_prec_t prec;
for(i = 0; i < m; ++i)
{
for(j = 0; j < n; ++j)
{
prec = mpfr_get_prec(A[i * n + j]);
mpfi_set_prec(B[i * n + j], prec);
mpfi_set_fr(B[i * n + j], A[i * n + j]);
}
}
}
mp_prec_t getMaxPrecMPFIMatrix(mpfi_t *A, uint64_t m, uint64_t n)
{
int i,j;
mp_prec_t current = 0;
mp_prec_t max = 0;
for(i = 0; i < m; ++i)
{
for(j = 0; j < n; ++j)
{
current = mpfi_get_prec(A[i * n + j]);
if(current >= max) max = current;
}
}
return max;
}