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
|
/*****************************************************************
* @LICENSE@
*****************************************************************/
/* sre_math.c
*
* Portability for and extensions to C math library.
* RCS $Id: sre_math.c,v 1.16 2005/01/21 16:36:58 eddy Exp $
*/
#include "squidconf.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "squid.h"
/* Function: Linefit()
*
* Purpose: Given points x[0..N-1] and y[0..N-1], fit to
* a straight line y = a + bx.
* a, b, and the linear correlation coefficient r
* are filled in for return.
*
* Args: x - x values of data
* y - y values of data
* N - number of data points
* ret_a - RETURN: intercept
* ret_b - RETURN: slope
* ret_r - RETURN: correlation coefficient
*
* Return: 1 on success, 0 on failure.
*/
int
Linefit(float *x, float *y, int N, float *ret_a, float *ret_b, float *ret_r)
{
float xavg, yavg;
float sxx, syy, sxy;
int i;
/* Calculate averages, xavg and yavg
*/
xavg = yavg = 0.0;
for (i = 0; i < N; i++)
{
xavg += x[i];
yavg += y[i];
}
xavg /= (float) N;
yavg /= (float) N;
sxx = syy = sxy = 0.0;
for (i = 0; i < N; i++)
{
sxx += (x[i] - xavg) * (x[i] - xavg);
syy += (y[i] - yavg) * (y[i] - xavg);
sxy += (x[i] - xavg) * (y[i] - yavg);
}
*ret_b = sxy / sxx;
*ret_a = yavg - xavg*(*ret_b);
*ret_r = sxy / (sqrt(sxx) * sqrt(syy));
return 1;
}
/* Function: WeightedLinefit()
*
* Purpose: Given points x[0..N-1] and y[0..N-1] with
* variances (measurement errors) var[0..N-1],
* fit to a straight line y = mx + b.
*
* Method: Algorithm from Numerical Recipes in C, [Press88].
*
* Return: (void)
* ret_m contains slope; ret_b contains intercept
*/
void
WeightedLinefit(float *x, float *y, float *var, int N, float *ret_m, float *ret_b)
{
int i;
double s;
double sx, sy;
double sxx, sxy;
double delta;
double m, b;
s = sx = sy = sxx = sxy = 0.;
for (i = 0; i < N; i++)
{
s += 1./var[i];
sx += x[i] / var[i];
sy += y[i] / var[i];
sxx += x[i] * x[i] / var[i];
sxy += x[i] * y[i] / var[i];
}
delta = s * sxx - (sx * sx);
b = (sxx * sy - sx * sxy) / delta;
m = (s * sxy - sx * sy) / delta;
*ret_m = m;
*ret_b = b;
}
/* 2D matrix operations
*/
float **
FMX2Alloc(int rows, int cols)
{
float **mx;
int r;
mx = (float **) MallocOrDie(sizeof(float *) * rows);
mx[0] = (float *) MallocOrDie(sizeof(float) * rows * cols);
for (r = 1; r < rows; r++)
mx[r] = mx[0] + r*cols;
return mx;
}
void
FMX2Free(float **mx)
{
free(mx[0]);
free(mx);
}
double **
DMX2Alloc(int rows, int cols)
{
double **mx;
int r;
mx = (double **) MallocOrDie(sizeof(double *) * rows);
mx[0] = (double *) MallocOrDie(sizeof(double) * rows * cols);
for (r = 1; r < rows; r++)
mx[r] = mx[0] + r*cols;
return mx;
}
void
DMX2Free(double **mx)
{
free(mx[0]);
free(mx);
}
/* Function: FMX2Multiply()
*
* Purpose: Matrix multiplication.
* Multiply an m x p matrix A by a p x n matrix B,
* giving an m x n matrix C.
* Matrix C must be a preallocated matrix of the right
* size.
*/
void
FMX2Multiply(float **A, float **B, float **C, int m, int p, int n)
{
int i, j, k;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
C[i][j] = 0.;
for (k = 0; k < p; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
/* Function: FMX2Copy()
* Incept: LSJ 14 Oct 2003
* incorp of HMMER eweights code;
* SRE, Thu May 20 11:27:05 2004 [St. Louis]
*
* Purpose: Copy mx_src to mx_dest.
* mx_src is m x n (rows x columns).
* mx_dest is too, and must already be allocated.
*
* Args: mx_dest - new copy
* mx_src - matrix to be copied
*
* Returns: (void)
*/
void
FMX2Copy(float **mx_dest, float **mx_src, int m, int n)
{
int row;
for (row = 0; row < m; row++)
FCopy(mx_dest[row], mx_src[row], n);
return;
}
|