Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • dd9e837
  • /
  • src
  • /
  • utilities.h
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:d2d91a163cc7150dcb24069ed780f565517317d2
directory badge
swh:1:dir:38733194ca6dcbf9bd971e3c209c7fa4d2e0f08d

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
utilities.h
/**
 * @file utilities.h
 * @brief header files for utility functions 
 * @author Wayne Zhang                         
*/

#ifndef CPLM_UTILS_H
#define CPLM_UTILS_H

/* inline functions */

/**
 * cumulative sum of a vector of double 
 *
 * @param x double vector to be summed
 * @param n number of elements
 *
 * @return cumulative sum
 */
static R_INLINE double dcumsum(double *x, int n){
  double sm = 0.0 ;
  for (int i = 0; i < n; i++)  sm += x[i] ;
  return sm ;
}

/**
 * get the maximum value of a double vector
 *
 * @param x a double vector
 * @param n length of the vector
 *
 * @return the maximum value
 */
static R_INLINE double dmax (double *x, int n){
  double s = x[0] ;
  for (int i = 1; i < n; i++)
      if (x[i] > s) s = x[i] ; 
  return s ;
}

/**
 * get the minmimum value of a double vector
 *
 * @param x a double vector
 * @param n length of the vector
 *
 * @return the minimum value
 */
static R_INLINE double dmin(double *x, int n){
    double s = x[0];
    for (int i = 1; i < n; i++){
	if (x[i] < s) s = x[i];
    }
    return s;
}

/**
 * get the maximum value of an int vector
 *
 * @param x an int vector
 * @param n length of the vector
 *
 * @return the maximum value
 */
static R_INLINE int imax (int *x, int n){
  int s = x[0] ;
  for (int i = 1; i < n; i++)
      if (x[i] > s) s = x[i] ; 
  return s ;
}


/**
 * Compute sample mean
 *
 * @param n number of samples
 * @param x samples in long vector 
 *
 * @return mean 
 */
static R_INLINE double mean(double *x, int n){
  return dcumsum(x, n) / n ;
}

/**
 * inverse link function
 *
 * @param eta linear predictors 
 * @param lp  link power
 *
 * @return mean
 */ 
static R_INLINE double link_inv(double eta, double lp){
    return (lp == 0) ? exp(eta) : pow(eta, 1.0 / lp);
}

/**
 * derivative d(mu) / d(eta)
 *
 * @param eta linear predictors 
 * @param lp  link power
 *
 * @return d(mu)/d(eta)
 */ 
static R_INLINE double mu_eta(double eta, double lp){
    return (lp == 0) ? exp(eta) : pow(eta, 1.0 / lp - 1.0) / lp ;
}

/**
 * Return the sum of squares of the first n elements of x
 *
 * @param n
 * @param x
 *
 * @return sum of squares
 */
static R_INLINE double sqr_length(double *x, int n)
{
    double ans = 0;
    for (int i = 0; i < n; i++) ans += x[i] * x[i];
    return ans;
}

/**
 * Return the index of the term associated with parameter index ind
 *
 * @param ind an index in [0, Gp[nt] - 1]
 * @param nt total number of terms
 * @param Gp group pointers, a vector of length nt+1 with Gp[0] = 0
 *
 * @return sum of squares
 */
static R_INLINE int Gp_grp(int ind, int nt, const int *Gp)
{
    for (int i = 0; i < nt; i++) if (ind < Gp[i + 1]) return i;
    error(("invalid row index %d (max is %d)"), ind, Gp[nt]);
    return -1;                  /* -Wall */
}



/* prototypes defined in utilities.c */

double var(double *x, int n) ;
void cov(int n, int p, double *x, double *ans);

// matrix computation
void solve_po(int d, double *v, double *iv) ;
void mult_xtx(int m, int n, double *x, double *out) ;
void mult_mv(char *trans, int m, int n, double *A,
             double *x, double *out) ;
void chol(int d, double *v, double *iv);
void solve_po(int d, double *v, double *iv);

// numerical derivatives
void grad(int n, double *x, 
          double (*myfunc)(double *x, void *data), 
          void *data, double *ans) ;
void hess(int n, double *x, 
          double (*myfunc)(double *x, void *data), 
          void *data, double *ans) ;

// wishart simulation
void rwishart(int d, double nu, double *scal, double *out);


#endif

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API