hmmer-3.3/easel/esl_vectorops.c
/* Function:  esl_vec_{DF}Validate()
 * Synopsis:  Verifies that vector is p-vector.
 * Incept:    ER, Tue Dec  5 09:38:54 EST 2006 [janelia]
 *
 * Purpose:   Validate a probability vector <vec> of length <n>.
 *            Each element has to be between 0 and 1, and
 *            the sum of all elements has to be suitably close
 *            to 1 (<fabs(1-sum) <= tol>).
 *
 * Args:      v      - p vector to validate.
 *            n      - dimensionality of v
 *            tol    - absolute difference; convergence criterion applied to sum of v
 *            errbuf - NULL, or a failure message buffer allocated
 *                     for at least <eslERRBUFSIZE> chars. 
 *
 * Returns:   <eslOK> on success, or <eslFAIL> on validation failure.
 *            Upon failure, if caller provided a non-<NULL> <errbuf>,
 *            an informative message is left there.
 */
esl_vec_{DF}Validate()
Synopsis:  Verifies that vector is p-vector.

Validate a probability vector  of length .
Each element has to be between 0 and 1, and
the sum of all elements has to be suitably close
to 1 ().

v      - p vector to validate.
n      - dimensionality of v
tol    - absolute difference; convergence criterion applied to sum of v
errbuf - NULL, or a failure message buffer allocated
for at least  chars.

 on success, or  on validation failure.
Upon failure, if caller provided a non- ,
an informative message is left there.
esl_vec_ { DF } Validate () Synopsis : Verifies that vector is p - vector . Validate a probability vector of length . Each element has to be between 0 and 1 and the sum of all elements has to be suitably close to 1 () . v - p vector to validate . n - dimensionality of v tol - absolute difference ; convergence criterion applied to sum of v errbuf - NULL or a failure message buffer allocated for at least chars . on success or on validation failure . Upon failure if caller provided a non - an informative message is left there .
int
esl_vec_DValidate(const double *vec, int n, double tol, char *errbuf)
{
  double sum = 0.;
  int    i;
  if (errbuf) *errbuf = 0;
  if (n == 0) return eslOK;
  for (i = 0; i < n; i++) {
    if (! isfinite(vec[i]) || vec[i] < 0.0 || vec[i] > 1.0)
	ESL_FAIL(eslFAIL, errbuf, "value %d (%g) is not a probability between 0..1", i, vec[i]);
    sum += vec[i];
  }
  if (fabs(sum - 1.0) > tol)  ESL_FAIL(eslFAIL, errbuf, "vector does not sum to 1.0");
  return eslOK;
}
int esl_vec_DValidate ( const double * vec , int n , double tol , char * errbuf ) { double sum = 0. ; int i ; if ( errbuf ) * errbuf = 0 ; if ( n == 0 ) return eslOK ; for ( i = 0 ; i < n ; i ++ ) { if ( ! isfinite ( vec [ i ] ) || vec [ i ] < 0.0 || vec [ i ] > 1.0 ) ESL_FAIL ( eslFAIL , errbuf , " " , i , vec [ i ] ) ; sum += vec [ i ] ; } if ( fabs ( sum - 1.0 ) > tol ) ESL_FAIL ( eslFAIL , errbuf , " " ) ; return eslOK ; }
Function:  esl_vec_{DF}Validate()
 Synopsis:  Verifies that vector is p-vector.
Function : esl_vec_ { DF } Validate () Synopsis : Verifies that vector is p - vector .