forked from klusta-team/klustakwik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
78 lines (68 loc) · 1.49 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
/*
* util.cpp
*
* Various utility functions.
*
* Created on: 11 Nov 2011
* Author: dan
*/
// Disable some Visual Studio warnings
#define _CRT_SECURE_NO_WARNINGS
#include "util.h"
#include<stdlib.h>
#include<stdarg.h>
#include "numerics.h"
#include<math.h>
const scalar HugeScore = (scalar)1e32;
/* integer random number between min and max*/
integer irand(integer min, integer max)
{
return (rand() % (max - min + 1) + min);
}
FILE *fopen_safe(char *fname, char *mode) {
FILE *fp;
fp = fopen(fname, mode);
if (!fp) {
fprintf(stderr, "Could not open file %s\n", fname);
abort();
}
return fp;
}
// Print a matrix
void MatPrint(FILE *fp, scalar *Mat, integer nRows, integer nCols) {
integer i, j;
for (i=0; i<nRows; i++) {
for (j=0; j<nCols; j++) {
fprintf(fp, "%.5g ", Mat[i*nCols + j]);
Output("%.5g ", Mat[i*nCols + j]);
}
fprintf(fp, "\n");
Output("\n");
}
}
void CompareVectors(scalar *A, scalar *B, integer N)
{
scalar meanerr = 0.0;
scalar maxerr = 0.0;
integer nerr = 0;
integer ntotal = 0;
for (integer i = 0; i < N; i++)
{
scalar err = fabs(A[i] - B[i]);
ntotal++;
if (err > 0)
{
nerr++;
meanerr += err;
if (err > maxerr)
maxerr = err;
}
}
if (nerr)
{
meanerr /= nerr;
cout << "Comparison error n=" << nerr << " (" << (100.0*nerr) / ntotal << "%), mean=" << meanerr << ", max=" << maxerr << endl;
}
else
cout << "No comparison error." << endl;
}