-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_mat-test.c
226 lines (170 loc) · 5.22 KB
/
d_mat-test.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
d_mat-test.c: test module for matrices with entries that are doubles
Copyright (C) 2010, William Hart
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>
#include <math.h>
#include "flint.h"
#include "memory-manager.h"
#include "long_extras.h"
#include "d_mat.h"
#include "test-support.h"
#define DEBUG 0 // prints debug information
#define DEBUG2 1
double random_d()
{
if (z_randint(2)) return rand()/((double) RAND_MAX + 1);
else return -rand()/((double) RAND_MAX + 1);
}
void random_d_mat(double ** mat, ulong rows, ulong cols)
{
ulong i, j;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
mat[i][j] = random_d();
}
/****************************************************************************
Test code for d_mat Routines
****************************************************************************/
int test_d_mat_init_clear()
{
int result = 1;
ulong count1;
double ** mat;
for (count1 = 0; count1 < 10000; count1++)
{
ulong rows = z_randint(50) + 1;
ulong cols = z_randint(50) + 1;
mat = d_mat_init(rows, cols);
random_d_mat(mat, rows, cols);
d_mat_clear(mat);
}
return result;
}
int test__d_vec_add_sub()
{
int result = 1;
ulong count1, count2;
double ** mat;
double * vec;
for (count1 = 0; count1 < 1000; count1++)
{
ulong rows = z_randint(100) + 1;
ulong cols = z_randint(100) + 2;
mat = d_mat_init(rows, cols);
random_d_mat(mat, rows, cols);
vec = malloc(cols*sizeof(double));
for (count2 = 0; count2 < 1000; count2++)
{
ulong r1 = z_randint(rows);
ulong r2 = z_randint(rows);
_d_vec_add(vec, mat[r1], mat[r2], cols);
_d_vec_sub(vec, vec, mat[r2], cols);
result = (_d_vec_equal(mat[r1], vec, cols, FLINT_D_BITS - 1));
if (!result)
{
printf("Error: a + b - b != a, rows = %ld, cols = %ld\n", rows, cols);
break;
}
}
d_mat_clear(mat);
free(vec);
}
return result;
}
int test__d_vec_scalar_product()
{
int result = 1;
ulong count1, count2;
double ** mat;
for (count1 = 0; count1 < 1000 && result; count1++)
{
ulong rows = z_randint(100) + 1;
ulong cols = z_randint(100) + 2;
mat = d_mat_init(rows, cols);
random_d_mat(mat, rows, cols);
for (count2 = 0; count2 < 1000 && result; count2++)
{
ulong r1 = z_randint(rows);
ulong r2 = z_randint(rows);
double s1 = _d_vec_scalar_product(mat[r1], mat[r2], cols - 1);
double s2 = _d_vec_scalar_product(mat[r1] + cols - 1, mat[r2] + cols - 1, 1);
double s3 = _d_vec_scalar_product(mat[r1], mat[r2], cols);
result = (fabs(s1 + s2 - s3) < 1.0e-14);
if (!result)
{
printf("Error: %lf, %lf, %lf, %lf\n", s1, s2, s3, (s1 + s2) - s3);
break;
}
}
d_mat_clear(mat);
}
return result;
}
int test__d_vec_norm()
{
int result = 1;
ulong count1, count2;
double ** mat;
for (count1 = 0; count1 < 1000 && result; count1++)
{
ulong rows = z_randint(100) + 1;
ulong cols = z_randint(100) + 2;
mat = d_mat_init(rows, cols);
random_d_mat(mat, rows, cols);
for (count2 = 0; count2 < 1000 && result; count2++)
{
ulong r1 = z_randint(rows);
double s1 = _d_vec_norm(mat[r1], cols - 1);
double s2 = _d_vec_norm(mat[r1] + cols - 1, 1);
double s3 = _d_vec_norm(mat[r1], cols);
result = (fabs(s1 + s2 - s3) < 1.0e-14);
if (!result)
{
printf("Error: %lf, %lf, %lf\n", s1, s2, s3);
break;
}
}
d_mat_clear(mat);
}
return result;
}
/****************************************************************************
Main test functions
****************************************************************************/
void d_mat_test_all()
{
int success, all_success = 1;
RUN_TEST(d_mat_init_clear);
RUN_TEST(_d_vec_add_sub);
RUN_TEST(_d_vec_scalar_product);
RUN_TEST(_d_vec_norm);
printf(all_success ? "\nAll tests passed\n" :
"\nAt least one test FAILED!\n");
}
int main()
{
test_support_init();
d_mat_test_all();
test_support_cleanup();
flint_stack_cleanup();
return 0;
}
// end of file ****************************************************************