vectorops.c

来自「这是一个基于HMM 模型的生物多序列比对算法的linux实现版本。hmmer」· C语言 代码 · 共 300 行

C
300
字号
/* vectorops.c * Operations on vectors of floats or doubles. *  * DSet(), FSet()       - set all items in vector to value. * DScale(), FScale()   - multiply all items in vector by scale * DSum(), FSum()       - return sum of values in vector * DAdd(), FAdd()       - add vec2 to vec1. * DCopy(), FCopy()     - set vec1 to be same as vec2.  * DDot(), FDot()       - return dot product of two vectors. * DMax(), FMax()       - return value of maximum element in vector * DMin(), FMin()       - return value of minimum element in vector  * DArgMax(), FArgMax() - return index of maximum element in vector * DArgMin(), FArgMin() - return index of minimum element in vector *  * DNorm(), FNorm()     - normalize a probability vector of length n. * DLog(), FLog()       - convert to log probabilities  * DExp(), FExp()       - convert log p's back to probabilities * DLogSum(), FLogSum() - given vector of log p's; return log of summed p's. *                         * SRE, Tue Oct  1 15:23:25 2002 [St. Louis] * CVS $Id: vectorops.c,v 1.4 2003/04/14 16:00:16 eddy Exp $                        */                        #include "squidconf.h"#include <stdlib.h>#include <math.h>#include <float.h>#include "vectorops.h"voidDSet(double *vec, int n, double value){  int x;   for (x = 0; x < n; x++) vec[x] = value;}voidFSet(float *vec, int n, float value){  int x;   for (x = 0; x < n; x++) vec[x] = value;}voidDScale(double *vec, int n, double scale){  int x;  for (x = 0; x < n; x++) vec[x] *= scale;}voidFScale(float *vec, int n, float scale){  int x;  for (x = 0; x < n; x++) vec[x] *= scale;}double DSum(double *vec, int n){  double sum = 0.;  int    x;  for (x = 0; x < n; x++) sum += vec[x];  return sum;}float FSum(float *vec, int n){  float sum = 0.;  int   x;  for (x = 0; x < n; x++) sum += vec[x];  return sum;}voidDAdd(double *vec1, double *vec2, int n){  int x;  for (x = 0; x < n; x++) vec1[x] += vec2[x];}voidFAdd(float *vec1, float *vec2, int n){  int x;  for (x = 0; x < n; x++) vec1[x] += vec2[x];}voidDCopy(double *vec1, double *vec2, int n){  int x;  for (x = 0; x < n; x++) vec1[x] = vec2[x];}voidFCopy(float *vec1, float *vec2, int n){  int x;  for (x = 0; x < n; x++) vec1[x] = vec2[x];}doubleDDot(double *vec1, double *vec2, int n){  double result = 0.;  int x;  for (x = 0; x < n; x++) result += vec1[x] * vec2[x];  return result;}floatFDot(float *vec1, float *vec2, int n){  float result = 0.;  int x;  for (x = 0; x < n; x++) result += vec1[x] * vec2[x];  return result;}doubleDMax(double *vec, int n){  int i;  double best;  best = vec[0];  for (i = 1; i < n; i++)    if (vec[i] > best) best = vec[i];  return best;}floatFMax(float *vec, int n){  int   i;  float best;  best = vec[0];  for (i = 1; i < n; i++)    if (vec[i] > best) best = vec[i];  return best;}doubleDMin(double *vec, int n){  int i;  double best;  best = vec[0];  for (i = 1; i < n; i++)    if (vec[i] < best) best = vec[i];  return best;}floatFMin(float *vec, int n){  int   i;  float best;  best = vec[0];  for (i = 1; i < n; i++)    if (vec[i] < best) best = vec[i];  return best;}intDArgMax(double *vec, int n){  int i;  int best = 0;  for (i = 1; i < n; i++)    if (vec[i] > vec[best]) best = i;  return best;}intFArgMax(float *vec, int n){  int i;  int best = 0;  for (i = 1; i < n; i++)    if (vec[i] > vec[best]) best = i;  return best;}intDArgMin(double *vec, int n){  int i;  int best = 0;  for (i = 1; i < n; i++)    if (vec[i] < vec[best]) best = i;  return best;}intFArgMin(float *vec, int n){  int   i;  int   best = 0;  for (i = 1; i < n; i++)    if (vec[i] < vec[best]) best = i;  return best;}voidDNorm(double *vec, int n){  int    x;  double sum;  sum = DSum(vec, n);  if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;  else            for (x = 0; x < n; x++) vec[x] = 1. / (double) n;}voidFNorm(float *vec, int n){  int    x;  float  sum;  sum = FSum(vec, n);  if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;  else            for (x = 0; x < n; x++) vec[x] = 1. / (float) n;}voidDLog(double *vec, int n){  int x;  for (x = 0; x < n; x++)     if (vec[x] > 0.) vec[x] = log(vec[x]);    else vec[x] = -DBL_MAX;}voidFLog(float *vec, int n){  int x;  for (x = 0; x < n; x++)     if (vec[x] > 0.) vec[x] = log(vec[x]);    else vec[x] = -FLT_MAX;}voidDExp(double *vec, int n){  int x;  for (x = 0; x < n; x++) vec[x] = exp(vec[x]);}voidFExp(float *vec, int n){  int x;  for (x = 0; x < n; x++) vec[x] = exp(vec[x]);}doubleDLogSum(double *vec, int n){  int x;  double max, sum;    max = DMax(vec, n);  sum = 0.0;  for (x = 0; x < n; x++)    if (vec[x] > max - 50.)      sum += exp(vec[x] - max);  sum = log(sum) + max;  return sum;}floatFLogSum(float *vec, int n){  int x;  float max, sum;    max = FMax(vec, n);  sum = 0.0;  for (x = 0; x < n; x++)    if (vec[x] > max - 50.)      sum += exp(vec[x] - max);  sum = log(sum) + max;  return sum;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?