Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

matvec Namespace Reference


Detailed Description

Namespace matvec.


Compounds

class  AlleleFounderSet
 This is the "Allele Founder set" class. More...

class  AlleleOriginNode
 This is the allele origin node class. More...

class  AllelePenetranceSet
 This is the "Allele penetrance set" class. More...

class  AlleleStateNode
 This is the allele state node class. More...

class  Anterior_c
 something here More...

class  BetaDist
struct  BGNodeStruct
 This is used in SparseMatrix. More...

class  BinomialDist
class  ChiSquareDist
class  ChromStruct
 chromosome structure More...

struct  compareGNodes
 This is the comparison criterion to rank Gnodes in GNodeList based on the size of the alleleStateVector or genotypeVector. More...

struct  compareGNodesPeelId
struct  compareGNodesWeight
class  CutSet
 This is the "cut set" class. More...

class  BG
class  BGMatrix
class  Chromosome
 A chromosome. More...

class  Data
 A set of rows of data records. More...

class  DataNode
 a node in a data structure More...

class  Dblock
 This is a array of Matrix. More...

class  DisAllelePenetranceSet
 This is the "Disease Allele penetrance set" class. More...

class  DiscreteUniformDist
class  DisGenoPenetranceSet
 This is the "Disease Genotype penetrance set" class. More...

class  doubleMatrix
class  exception
class  ExponentialDist
class  FDist
class  Field
 a column for a data set More...

class  FieldStruct
 a structure for a data column More...

class  Fpmm
class  GammaDist
class  GeneticDist
 a base for Genetic distributions More...

class  GenoFounderSet
 This is the "Genotype Founder set" class. More...

class  Genome
 a genome consists of chromosomes More...

class  GenoPenetranceSet
 This is the "Genotype penetrance set" class. More...

class  GenotypeNode
 This is the genotype node class. More...

class  GeometricDist
class  GLMM
class  GNode
 This is the base graph node class. More...

struct  GNode_info
 Stores the id and distance from the cut of the GNode that will be sampled first. More...

class  GNodeList
 This is the "list of graph nodes" class. More...

class  GNodeSet
 This is the base "set of graph nodes" class. More...

class  HashNode
 a node for the hashtable More...

class  HashTable
 a hash table More...

class  IndexVector
class  Individual
 an individual consists of genomes More...

class  InvalidSample
struct  klotz0_1_
struct  klotz1_1_
class  Locus
 locus More...

exception  std
class  LocusStruct
 a structure for a locus More...

class  LogNormalDist
class  MaternalPaternalAlleles
 This is the building block class for the GenotypeNode class. More...

class  MaternalPaternalRQTLAlleles
 This is the building block class for dealing with RQTL. More...

class  Matrix
 A vector is a on-dimensional array with double precision. More...

class  Matrixwbg
class  Minimizer
class  Mixed_post_vec
class  Model
 a generalized linear mixed model More...

class  ModelTerm
 a class to hold term definition for a model More...

class  NegativeBinomialDist
struct  NodeStruct
 This is used in SparseMatrix. More...

class  NormalDist
class  NuFamily
 a nuclear family in a pedigree More...

class  Observe
struct  Parm_Elem
 It is used in Model. More...

class  ParmMap
class  Pedigree
 a pedigree More...

struct  PedNode
class  Plotter
 an object to plot curves More...

class  PoissonDist
class  Population
 a genetic population consisting of individuals More...

struct  pos_val_node
 used to store mme positions, x-values, and trait values for one obs. More...

class  Posterior_c
 something here More...

class  RAlleleFounderSet
 This is the "RAllele Founder set" class. More...

class  RAllelePenetranceSet
 This is the "RAllele penetrance set" class. More...

class  RDisAllelePenetranceSet
 This is the "RDisAllele penetrance set" class. More...

class  RecombinationSet
 This is the "Recombination set" class. More...

class  RegExp
struct  RNormal
class  RSamplerParms
class  RTransmissionSet
 This is the "RTransmission set" class. More...

class  Session
class  SparseBGMatrix
 a SparseBGMatrix is a sparse matrix of BG objects More...

class  SparseMatrix
 a sparse matrix is a matrix with very sparse elements More...

class  StatDistBase
class  Sym2x2
class  Sym3x3
class  Sym4x4
class  tDist
class  TermList
 a class to hold a list of terms in a model More...

class  TransitionSet
 This is the "Transition set" class. More...

class  TransmissionSet
 This is the "Transmission set" class. More...

class  UniformDist
class  UnknownDist
 a dummy genetic distribution More...

struct  UNormal
class  Vector

Typedefs

typedef void(* displaytype )(const void *)
typedef double(* penetranceType )(const Individual *, const double **)
typedef int(* cmp_func )(const void *, const void *)
typedef int idxtype

Functions

BG operator+ (const BG &a, const BG &b)
BG operator- (const BG &a, const BG &b)
BG operator * (const BG &a, const BG &b)
BG operator/ (double a, const BG &b)
BG operator- (const BG &a)
BG operator/ (const BG &a, const BG &b)
BG sqrt (const BG &a)
BG log (const BG &a)
BG exp (const BG &a)
BG operator+ (double a, const BG &b)
BG operator- (double a, const BG &b)
BG operator * (double a, const BG &b)
BG operator+ (const BG &a, double b)
BG operator- (const BG &a, double b)
BG operator * (const BG &a, double b)
BG operator/ (const BG &a, double b)
BGoperator+= (BG &a, const BG &b)
BGoperator-= (BG &a, const BG &b)
BGoperator *= (BG &a, const BG &b)
BGoperator/= (BG &a, const BG &b)
BGoperator+= (BG &a, double b)
BGoperator-= (BG &a, double b)
BGoperator *= (BG &a, double b)
BGoperator/= (BG &a, double b)
void initial_BGarray (BG *v, unsigned n, double a)
void ludcmp (BG **a, int n, int *indx, int &d, double tol)
void lubksb (BG **a, int n, int *indx, BG *b)
bool psdefinite (const BG **mat, const unsigned m, const unsigned n, const double tol)
Chromosomenew_Chromosome_vec (const unsigned n)
std::ostream & operator<< (std::ostream &stream, Data &A)
std::ostream & operator<< (std::ostream &stream, const DataNode &A)
std::ostream & operator<< (std::ostream &stream, Dblock &D)
int nonsymm_eigen (const int job, double **A, const int n, double *rr, double *ri, double **vr, double **vi, const int maxiter=50)
int symm_eigen (double *vals, double **A, const int n)
void svdcmp (double *a[], int m, int n, double w[], double *v[])
void ludcmp (double **a, int n, int *indx, int &d, double tol)
void lubksb (double **a, int n, int *indx, double *b)
void sweep (const int m, const int n, double **a, const int i0, const int i1, const double tol)
bool psdefinite (const double **mat, const unsigned m, const unsigned n, const double tol)
Vector< bool > operator== (const double a, const Field &b)
Vector< bool > operator!= (const Field &a, const double b)
Vector< bool > operator!= (const double a, const Field &b)
Vector< bool > operator> (const Field &a, const double b)
Vector< bool > operator> (const double a, const Field &b)
Vector< bool > operator<= (const Field &a, const double b)
Vector< bool > operator<= (const double a, const Field &b)
Vector< bool > operator>= (const Field &a, const double b)
Vector< bool > operator>= (const double a, const Field &b)
Vector< bool > operator< (const Field &A, const double x)
Vector< bool > operator< (const double x, const Field &A)
Field operator+ (const Field &A, const Field &B)
Field operator- (const Field &A, const Field &B)
Field operator * (const Field &A, const Field &B)
Field operator/ (const Field &A, const Field &B)
Field operator+ (const Field &A, const double s)
Field operator- (const Field &A, const double s)
Field operator * (const Field &A, const double s)
Field operator/ (const Field &A, const double s)
Field operator+ (const double s, const Field &B)
Field operator- (const double s, const Field &B)
Field operator * (const double s, const Field &B)
Field operator/ (const double s, const Field &B)
Field sin (Field &a)
Field asin (Field &a)
Field cos (Field &a)
Field acos (Field &a)
Field tan (Field &a)
Field atan (Field &a)
Field ceil (Field &a)
Field floor (Field &a)
Field log (Field &a)
Field log10 (Field &a)
Field exp (Field &a)
Field sqrt (Field &a)
Field abs (Field &a)
Field erf (Field &a)
Field erfc (Field &a)
int compare_DataNode (const void *A, const void *B)
std::ostream & operator<< (std::ostream &stream, const Field &V)
int fieldindex (const std::string &fw, const FieldStruct *field_vec, const unsigned n)
Genomenew_Genome_vec (const unsigned m, GeneticDist *D)
unsigned ginverse1 (double **a, const unsigned n, double &lgdet, int mode, const double tol)
unsigned ginverse1 (BG **a, const unsigned n, BG &lgdet, int mode, const double tol)
void sweep (const int m, const int n, BG **a, const int i0, const int i1, const BG tol)
doubleMatrixKP (Data *D, doubleMatrix &SKM, const std::string &lpl, const std::string &censor)
doubleMatrix operator * (Vector< double > &a, Vector< double > &b)
void bound_pd (double *varnew, int ntrait, doubleMatrix &P)
void getlambda (double **lambda, const int n)
void normal_loginClasses (matvec::Observe &observe)
int squeeze (unsigned len, int *ia, int *ja, double *a)
void hsort_ija (unsigned n, int *ia, int *ja, double *a)
long next_prime (long n)
HashNodenew_HashNode_vec (const unsigned n, const size_t s)
double ranf (void)
int compare_ind_pt (const void *x, const void *y)
int compare_ind_id (const void *a, const void *b)
int compare_ind_gid (const void *a, const void *b)
int compare_mother_id (const void *x, const void *y)
int compare_father_id (const void *x, const void *y)
RNormalnew_n_posterior_vec (const int m)
template<class T> Matrix< T > operator/ (const T &a, const Matrix< T > &b)
template<class T> Vector< T > operator * (const Vector< T > &a, const Matrix< T > &b)
template<class T> Matrix< T > hadjoin (const Matrix< T > &a, const Matrix< T > &b)
template<class T> Matrix< T > hadjoin (const Matrix< T > &a, const Vector< T > &b)
template<class T> Matrix< T > hadjoin (const Vector< T > &a, const Matrix< T > &b)
template<class T> Matrix< T > vadjoin (const Matrix< T > &a, const Matrix< T > &b)
template<class T> Matrix< T > vadjoin (const Matrix< T > &a, const Vector< T > &b)
template<class T> Matrix< T > vadjoin (const Vector< T > &a, const Matrix< T > &b)
template<class T> Matrix< T > diag (const Vector< T > &a)
template<class T> Matrix< T > diag (const Matrix< T > &a)
template<class T> Matrix< T > sin (const Matrix< T > &a)
template<class T> Matrix< T > asin (const Matrix< T > &a)
template<class T> Matrix< T > cos (const Matrix< T > &a)
template<class T> Matrix< T > acos (const Matrix< T > &a)
template<class T> Matrix< T > tan (const Matrix< T > &a)
template<class T> Matrix< T > atan (const Matrix< T > &a)
template<class T> Matrix< T > ceil (const Matrix< T > &a)
template<class T> Matrix< T > floor (const Matrix< T > &a)
template<class T> Matrix< T > log (const Matrix< T > &a)
template<class T> Matrix< T > log10 (const Matrix< T > &a)
template<class T> Matrix< T > exp (const Matrix< T > &a)
template<class T> Matrix< T > sqrt (const Matrix< T > &a)
template<class T> Matrix< T > abs (const Matrix< T > &a)
template<class T> Matrix< T > erf (const Matrix< T > &a)
template<class T> Matrix< T > erfc (const Matrix< T > &a)
template<class T> bool all (const Matrix< T > &a)
template<class T> bool any (const Matrix< T > &a)
template<class T> Matrix< T > operator+ (const T &a, const Matrix< T > &b)
template<class T> Matrix< T > operator- (const T &a, const Matrix< T > &b)
template<class T> Matrix< T > operator * (const T &a, const Matrix< T > &b)
template<class T> std::ostream & operator<< (std::ostream &os, const Matrix< T > &a)
void linmin (double *p, double *xi, int n, double &fret)
double f1dim (double x)
double brent (double ax, double bx, double cx, double(*f)(double), double tol, double &xmin)
void mnbrak (double &ax, double &bx, double &cx, double &fa, double &fb, double &fc, double(*func)(double))
double funk (Vector< double > &x)
double nr_powell (Data *D, Model *M, Vector< double > &p, Matrix< double > &xi, int n, int &iter, double ftol)
void svd (const int n, double eps, const double tol, double **ab, double *q)
void sort (const int n, double *d, double **v)
void print (const int n, const Vector< double > &x)
void flin (const double l, const int j, const Vector< double > &x, const int n, const Matrix< double > &v, Vector< double > &w, const Matrix< double > &q01)
void nest_xaction (std::string &s)
unsigned lidx (const unsigned i, const unsigned j, const unsigned n)
void ginverse2 (double *a, const unsigned n, const double tol)
void preconditioning (double *result, const double **M, const double *r, const unsigned n)
void ABCt (double **A, const unsigned ma, const unsigned na, double **B, const unsigned mb, const unsigned nb, double **C, const unsigned mc, const unsigned nc, double **OUT, const unsigned mo, const unsigned no)
void balance (double **mat, const int n, int *low, int *hi, double *scale, const double base)
void elemhess (int job, double **mat, const int n, const int low, const int hi, double **vr, double **vi)
void unbalance (const int n, double **vr, double **vi, const int low, const int hi, double *scale)
int nonsymm_eigen_core (const int job, double **mat, const int n, const int low, const int hi, double *valr, double *vali, double **vr, double **vi, const double eps, const int maxiter)
int compare_ind_ptr (const void *a, const void *b)
NuFamilynew_NuFamily_vec (const unsigned m)
int pedcp1 (const void *a, const void *b)
int pedcp2 (const void *a, const void *b)
std::ostream & operator<< (std::ostream &stream, Pedigree &A)
unsigned sampling (double *dist_prob, const unsigned size)
void G_to_gamete (const unsigned G_id, unsigned k1, unsigned k2, unsigned size)
int pdm_val (const unsigned *goff, const unsigned *gsire, const unsigned *gdam, Matrix< double > &pdm, const int ori)
void compute_pdq (const double er, const Matrix< double > &pdm, Matrix< double > &pdq)
double pr_osd (const unsigned goff[], const unsigned gsire[], const unsigned gdam[], const int ori)
int inva22 (Matrix< double > &a22)
double penetrance (const Individual *I, const double **res_var)
int compare_seq_id (const void *a, const void *b)
int compare_nuf_ptr (const void *a, const void *b)
int comp1 (const int *i, const int *j)
void gs_ioc (int *ia, int *ja, BG *a, const BG *rhs, BG *sol, const int n, const double relax, const double stopval, const int mxiter, const double tol, unsigned &cov)
int squeeze (unsigned len, int *iiv, int *jjv, BG *aav)
void hsort_ija (unsigned n, int *iiv, int *jjv, BG *aav)
int operator== (const SparseBGMatrix &A, const SparseBGMatrix &B)
int operator!= (const SparseBGMatrix &A, const SparseBGMatrix &B)
Vector< BGoperator * (const SparseBGMatrix &A, const Vector< BG > &v)
SparseBGMatrix operator * (const SparseBGMatrix &A, const BG s)
SparseBGMatrix operator * (const BG s, const SparseBGMatrix &A)
void sparse_dense (unsigned n, int *ia, int *ja, BG *a, BG **matx)
int comp (const int *i, const int *j)
void gs_ioc (int *ia, int *ja, double *a, const double *rhs, double *sol, const int n, const double relax, const double stopval, const int mxiter, const double tol, unsigned &cov)
int operator== (const SparseMatrix &A, const SparseMatrix &B)
int operator!= (const SparseMatrix &A, const SparseMatrix &B)
Vector< double > operator * (const SparseMatrix &A, const Vector< double > &v)
SparseMatrix operator * (const SparseMatrix &A, const double s)
SparseMatrix operator * (const double s, const SparseMatrix &A)
void sparse_dense (unsigned n, int *ia, int *ja, double *a, double **matx)
double snorm (void)
double sexpo (void)
double sgamma (const double a)
double genbet (const double aa, const double bb)
double genchi (const int df)
double genexp (const double av)
double genf (const int dfn, const int dfd)
double gengam (const double a, const double r)
double gennch (const int df, const double xnonc)
double gennf (const int dfn, const int dfd, const double xnonc)
double gennor (const double av, const double sd)
double genunf (const double low, const double high)
long ignbin (const long n, const double pp)
long ignpoi (const double mu)
long ignuin (const long low, const long high)
void set_seed (const unsigned iseed)
double ran1 (void)
double gasdev (void)
double gamdev (const double A, const double R)
double factrl (const long n)
double BinCoef (const long n, const long k)
double gammln (const double xx)
double gammin (const double x, const double a)
double betain (const double x, const double a, const double b, const double beta)
double Normal_cdf (const double x)
double Gamma_cdf (const double g, const double alfa, const double theta)
double Beta_cdf (const double x, const double alfa, const double beta, const double lambda, int &errcode)
double F_cdf (const double f, const double df1, const double df2, const double nc, int &errcode)
double ChiSquare_cdf (const double x, const double df, const double theta, int &errcode)
double t_cdf (const double t, const double df, const double delta)
double t_expected_value (const double df, const double nc)
double Normal_inv (const double p)
double ChiSquare_inv (const double p, const double df, const double nc)
double t_inv (const double p, const double df, const double nc)
double F_inv (const double p, const double df1, const double df2, const double nc)
void warning (const char format[],...)
double betacf (const double a, const double b, const double x)
int zufall_ (int n, double *a)
int zufalli_ (int seed)
double fsign (double num, double sign)
void tred2 (double **a, int n, double *d, double *e)
void tqli (double *d, double *e, int n, double **z)
void check_ptr (void *a)
int split (const std::string &str, const std::string &sep, std::vector< std::string > *strvec)
bool validline (char *line)
unsigned est_nze (const unsigned dim, const unsigned ntrait, const int pedsize)

Variables

int DISCRETE_TRAIT
doubleMatrix PENETRANCE_MATRIX
double at
double bt
double ct
double maxarg1
double maxarg2
doubleMatrixP_Mat = NULL
double global_mu
double global_vg
double global_ve
DatamyD
ModelmyM
int nfunk
double sqrarg
int ncom = 0
double * pcom = 0
double * xicom = 0
double qd [2]
double fx
double qf1
double macheps
double t
double m2
double m4
double SmaLL
double vsmall
double vlarge
double large
double dmin
double ldt
double h
int nf
int nl
double MIXED_TOL = 0.00000000001
int INPUT_LINE_WIDTH = 1024
double MY_NEG_HUGE = -1.0e+20
Session SESSION
struct {
   int   fill_1 [1214]
   int   e_2
klotz0_
struct {
   double   fill_1 [1024]
   int   e_2 [2]
   double   e_3
klotz1_
long RAND_STATE_ARRAY [64]
const int MATVEC_MESSAGE_BUF_LEN = 1024


Typedef Documentation

typedef int(* matvec::cmp_func)(const void *, const void *)
 

Definition at line 43 of file sparsebgmatrix.h.

Referenced by matvec::SparseMatrix::minfilsymelm_node_list(), and matvec::SparseBGMatrix::minfilsymelm_node_list().

typedef void(* matvec::displaytype)(const void*)
 

Definition at line 60 of file hashtable.h.

Referenced by matvec::HashTable::display().

typedef int matvec::idxtype
 

Definition at line 55 of file sparsebgmatrix.h.

Referenced by matvec::SparseMatrix::minfilsymelm_node_list(), and matvec::SparseBGMatrix::minfilsymelm_node_list().

typedef double(* matvec::penetranceType)(const Individual*, const double **)
 

Definition at line 40 of file individual.h.

Referenced by matvec::Population::setPenetrance(), and matvec::Individual::setPenetrance().


Function Documentation

void ABCt double **    A,
const unsigned    ma,
const unsigned    na,
double **    B,
const unsigned    mb,
const unsigned    nb,
double **    C,
const unsigned    mc,
const unsigned    nc,
double **    OUT,
const unsigned    mo,
const unsigned    no
 

template<class T>
Matrix<T> abs const Matrix< T > &    a
 

Definition at line 1114 of file matrix.h.

References matvec::Matrix< T >::apply().

01114 { return a.apply(std::abs); }

Field abs Field   a
 

Definition at line 774 of file field.cpp.

References matvec::Field::map().

Referenced by matvec::doubleMatrix::norm().

00774 {return a.map(std::fabs);}

template<class T>
Matrix<T> acos const Matrix< T > &    a
 

Definition at line 1105 of file matrix.h.

References matvec::Matrix< T >::apply().

01105 { return a.apply(std::acos); }

Field acos Field   a
 

Definition at line 765 of file field.cpp.

References matvec::Field::map().

00765 {return a.map(std::acos);}

template<class T>
bool all const Matrix< T > &    a
 

Definition at line 1118 of file matrix.h.

References matvec::Matrix< T >::all().

01118 { return a.all();}

template<class T>
bool any const Matrix< T > &    a
 

Definition at line 1119 of file matrix.h.

References matvec::Matrix< T >::any().

01119 { return a.any();}

template<class T>
Matrix<T> asin const Matrix< T > &    a
 

Definition at line 1103 of file matrix.h.

References matvec::Matrix< T >::apply().

01103 { return a.apply(std::asin); }

Field asin Field   a
 

Definition at line 763 of file field.cpp.

References matvec::Field::map().

00763 {return a.map(std::asin);}

template<class T>
Matrix<T> atan const Matrix< T > &    a
 

Definition at line 1107 of file matrix.h.

References matvec::Matrix< T >::apply().

01107 { return a.apply(std::atan); }

Field atan Field   a
 

Definition at line 767 of file field.cpp.

References matvec::Field::map().

00767 {return a.map(std::atan);}

void balance double **    mat,
const int    n,
int *    low,
int *    hi,
double *    scale,
const double    base
 

Definition at line 28 of file nonsymm_eigen.cpp.

Referenced by nonsymm_eigen().

00030 {
00031    ////////////////////////////////////////////////////////////////////
00032    // Balance a matrix for calculation of eigenvalues and eigenvectors
00033    //
00034    ////////////////////////////////////////////////////////////////////
00035 
00036    double c,f,g,r,s;
00037    int i,j,k,l,done;
00038         /* search for rows isolating an eigenvalue and push them down */
00039    for (k = n - 1; k >= 0; k--) {
00040       for (j = k; j >= 0; j--) {
00041          for (i = 0; i <= k; i++) {
00042             if (i != j && fabs(mat[j][i]) != 0) break;
00043          }
00044 
00045          if (i > k) {
00046             scale[k] = j;
00047             if (j != k) {
00048                for (i = 0; i <= k; i++) {
00049                   c = mat[i][j];
00050                   mat[i][j] = mat[i][k];
00051                   mat[i][k] = c;
00052                }
00053 
00054                for (i = 0; i < n; i++) {
00055                   c = mat[j][i];
00056                   mat[j][i] = mat[k][i];
00057                   mat[k][i] = c;
00058                }
00059             }
00060             break;
00061          }
00062       }
00063       if (j < 0) break;
00064    }
00065 
00066     /* search for columns isolating an eigenvalue and push them left */
00067 
00068    for (l = 0; l <= k; l++) {
00069       for (j = l; j <= k; j++) {
00070          for (i = l; i <= k; i++) {
00071             if (i != j && fabs(mat[i][j]) != 0) break;
00072          }
00073 
00074          if (i > k) {
00075             scale[l] = j;
00076 
00077             if (j != l) {
00078                for (i = 0; i <= k; i++) {
00079                   c = mat[i][j];
00080                   mat[i][j] = mat[i][l];
00081                   mat[i][l] = c;
00082                }
00083                for (i = l; i < n; i++) {
00084                   c = mat[j][i];
00085                   mat[j][i] = mat[l][i];
00086                   mat[l][i] = c;
00087                }
00088             }
00089             break;
00090          }
00091       }
00092       if (j > k) break;
00093    }
00094 
00095    *hi = k;
00096    *low = l;
00097 
00098     /* balance the submatrix in rows l through k */
00099 
00100    for (i = l; i <= k; i++) scale[i] = 1;
00101    do {
00102       for (done = 1,i = l; i <= k; i++) {
00103          for (c = 0,r = 0,j = l; j <= k; j++) {
00104             if (j != i) {
00105                c += fabs(mat[j][i]);
00106                r += fabs(mat[i][j]);
00107             }
00108          }
00109 
00110          if (c != 0 && r != 0) {
00111             g = r / base;
00112             f = 1;
00113             s = c + r;
00114 
00115             while (c < g) {
00116                f *= base;
00117                c *= base * base;
00118             }
00119             g = r * base;
00120             while (c >= g) {
00121                f /= base;
00122                c /= base * base;
00123             }
00124 
00125             if ((c + r) / f < 0.95 * s) {
00126                done = 0;
00127                g = 1 / f;
00128                scale[i] *= f;
00129 
00130                for (j = l; j < n; j++) mat[i][j] *= g;
00131                for (j = 0; j <= k; j++) mat[j][i] *= f;
00132             }
00133          }
00134       }
00135    } while (!done);
00136 }

double matvec::Beta_cdf const double    x,
const double    alfa,
const double    beta,
const double    lambda,
int &    errcode
 

Definition at line 491 of file stat1.cpp.

References betain(), ERRMAX, exp(), gammln(), ITMAX, log(), sqrt(), and warning().

Referenced by matvec::BetaDist::cdf(), and F_cdf().

00493 {
00494    ////////////////////////////////////////////////////////////////////
00495    //  ALGORITHM AS226 APPL. STATIST. (1987) VOL. 36, NO. 2
00496    //  Incorporates modification AS R84 from AS vol. 39, pp311-2, 1990
00497    //
00498    //  Returns the cumulative probability of X for the non-central beta
00499    //  distribution with parameters A, B and non-centrality LAMBDA
00500    //
00501    //  Auxiliary routines required: ALOGAM - log-gamma function (ACM
00502    //  291 or AS 245), and BETAIN - incomplete-beta function (AS 63)
00503    //
00504    //  errcode   = 0  no error
00505    //            = 1  parameter range error
00506    //            = 2  fail to converge
00507    /////////////////////////////////////////////////////////////////////
00508    if (alfa <= 0.0 || beta <= 0.0) throw exception(" Beta_cdf(): bad args, value > 0 expected");
00509    errcode = 0;
00510    if (x <= 0.0) return 0.0;
00511    if (x >= 1.0) return 1.0;
00512    double lnbeta, betanc = 0.0;
00513    if (lambda == 0.0) {
00514       lnbeta = gammln(alfa) + gammln(beta) - gammln(alfa+beta);
00515       betanc = betain(x,alfa,beta,lnbeta);
00516       return betanc;
00517    }
00518    double c,x0,gx,q,xj,ax,sumq,temp,alfa0,errbd;
00519    c = lambda*0.5;
00520    x0 = 0.0;              // Initialize the series ...
00521    q = c - 5.0*sqrt(c);
00522    if (q > 0.0) x0 = static_cast<int>(q);
00523    alfa0 = alfa + x0;
00524    lnbeta = gammln(alfa0) + gammln(beta) - gammln(alfa0 + beta);
00525    temp = betain(x,alfa0,beta,lnbeta);
00526    gx = exp(alfa0*log(x) + beta*log(1.0 - x) - lnbeta - log(alfa0));
00527    if (alfa0 > alfa) {
00528       q = exp(-c + x0*log(c) - gammln(x0 + 1.0));
00529    }
00530    else {
00531       q = exp(-c);
00532    }
00533    xj = 0.0;
00534    ax = q*temp;
00535    sumq = 1.0 - q;
00536    betanc = ax;
00537    do {   // recur over subsequent terms until convergence is achieved...
00538       xj++;
00539       temp -= gx;
00540       gx *= x*(alfa+ beta + xj - 1.0)/(alfa + xj);
00541       q *= c/xj;
00542       sumq -= q;
00543       ax = temp*q;
00544       betanc += ax;
00545       errbd = (temp - gx)*sumq;
00546    } while(static_cast<int>(xj) < ITMAX && errbd > ERRMAX);
00547    if (errbd > ERRMAX) {
00548       warning("Beta_cdf(): fail to converge");
00549       errcode = 2;
00550    }
00551    if (betanc > 1.0) betanc = 1;
00552    return betanc;
00553 }

double betacf const double    a,
const double    b,
const double    x
 

Definition at line 176 of file stat1.cpp.

References matvec::Session::epsilon, ITMAX, SESSION, and warning().

Referenced by betain().

00177 {
00178    double qap,qam,qab,em,tem,d;
00179    double bz,bm=1.0,bp,bpp;
00180    double az=1.0,am=1.0,ap,app,aold;
00181    int m;
00182 
00183    qab=a+b;
00184    qap=a+1.0;
00185    qam=a-1.0;
00186    bz=1.0-qab*x/qap;
00187    for (m=1; m<=ITMAX; m++) {
00188       em = static_cast<double>(m);
00189       tem = em+em;
00190       d = em*(b-em)*x/((qam+tem)*(a+tem));
00191       ap = az+d*am;
00192       bp = bz+d*bm;
00193       d = -(a+em)*(qab+em)*x/((qap+tem)*(a+tem));
00194       app = ap+d*az;
00195       bpp = bp+d*bz;
00196       aold = az;
00197       am = ap/bpp;
00198       bm = bp/bpp;
00199       az = app/bpp;
00200       bz = 1.0;
00201       if (fabs(az-aold) < (10.0*SESSION.epsilon*fabs(az))) return az;
00202    }
00203    warning("betacf(), not converged");
00204    abort();
00205    return 0.0;
00206 }

double matvec::betain const double    x,
const double    a,
const double    b,
const double    beta
 

Definition at line 290 of file stat1.cpp.

References betacf(), matvec::Session::epsilon, exp(), log(), and SESSION.

Referenced by Beta_cdf(), matvec::BinomialDist::sample(), and t_cdf().

00291 {
00292    //////////////////////////////////////////////////////////
00293    // return  incomplete beta function for arguments:
00294    //            x between 0 and 1,
00295    //            a and b positive,
00296    //            beta, log of complete beta function value
00297    ///////////////////////////////////////////////////////////
00298    if (x < 0.0 || x > 1.0) throw exception(" betain(%f): bad arg1, value between (0,1) expected");
00299    if (x == 0.0) return 0.0;
00300    if (x == 1.0) return 1.0;
00301    double fv,bt;
00302    if (x < SESSION.epsilon || fabs(x - 1.0) < SESSION.epsilon) {
00303       bt=0.0;
00304    }
00305    else {
00306       fv = a*log(x) + b*log(1.0-x) - beta;
00307       if (fv < -300) { bt = 0.0;}
00308       else           { bt = exp(fv);}
00309    }
00310    if (x < (a+1.0)/(a+b+1.0)) { fv =  bt*betacf(a,b,x)/a; }
00311    else                       { fv = 1.0-bt*betacf(b,a,1.0-x)/b; }
00312    return fv;
00313 }

double matvec::BinCoef const long    n,
const long    k
 

Definition at line 328 of file stat1.cpp.

References gammln().

00329 {
00330 
00331    if (n<0L || k<0L || k > n) throw exception("BinCoef(): bad args");
00332    double a,b,c;
00333    a = gammln(static_cast<double>(n+1));
00334    b = gammln(static_cast<double>(k+1));
00335    c = gammln(static_cast<double>(n-k));
00336    return std::floor(0.5 + std::exp(a - b - c));
00337 }

void matvec::bound_pd double *    varnew,
int    ntrait,
doubleMatrix   P
 

Definition at line 1276 of file glmm2.cpp.

References diag(), matvec::doubleMatrix::eigen(), matvec::Vector< T >::resize(), and matvec::Matrix< double >::transpose().

01277  {
01278    int i, j, k, t1, t2;
01279    doubleMatrix varbound (numtrait, numtrait);
01280    Vector<double> eig, flg;
01281    flg.resize (numtrait);
01282    for (k = 0, i = 0; i < numtrait; i++)
01283      for (j = i; j < numtrait; j++, k++)
01284        {
01285         varbound[i][j] = varnew[k];
01286         varbound[j][i] = varnew[k];
01287        }
01288    P = varbound;
01289 
01290    eig = P.eigen ();
01291 
01292    for (t1 = 0; t1 < numtrait; t1++)
01293      {
01294        flg[t1] = 0;
01295        if (eig[t1] < 1.e-3)
01296         {
01297           eig[t1] = .99e-3;
01298           flg[t1] = 1;
01299         }
01300        if (eig[t1] > 1.e10)
01301         {
01302           eig[t1] = 1.01e10;
01303           flg[t1] = 0;
01304         }
01305      }
01306    varbound = P * diag(eig) * P.transpose();
01307    for (t1 = 0; t1 < numtrait; t1++)
01308      {
01309        if (flg[t1])
01310         {
01311           for (t2 = 0; t2 < numtrait; t2++)
01312             {
01313               P[t1][t2] = 0;
01314             }
01315         }
01316      }
01317 
01318    for (k = 0, t1 = 0; t1 < numtrait; t1++)
01319      for (t2 = t1; t2 < numtrait; t2++, k++)
01320        {
01321         varnew[k] = varbound[t1][t2];
01322 
01323        }
01324  }

double matvec::brent double    ax,
double    bx,
double    cx,
double(*    f)(double),
double    tol,
double &    xmin
[static]
 

Definition at line 248 of file min_nr_powell.cpp.

References CGOLD, ITMAX, SHFT, SIGN, warning(), and ZEPS.

Referenced by linmin().

00249 {
00250    int iter;
00251    double a,b,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm;
00252    double e=0.0;
00253    double d=0.0;
00254 
00255    a=((ax < cx) ? ax : cx);
00256    b=((ax > cx) ? ax : cx);
00257    x=w=v=bx;
00258    fw=fv=fx=(*f)(x);
00259    for (iter=1;iter<=ITMAX;iter++) {
00260       xm=0.5*(a+b);
00261       tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
00262       if (fabs(x-xm) <= (tol2-0.5*(b-a))) {
00263          xmin=x;
00264          return fx;
00265       }
00266       if (fabs(e) > tol1) {
00267          r=(x-w)*(fx-fv);
00268          q=(x-v)*(fx-fw);
00269          p=(x-v)*q-(x-w)*r;
00270          q=2.0*(q-r);
00271          if (q > 0.0) p = -p;
00272          q=fabs(q);
00273          etemp=e;
00274          e=d;
00275          if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
00276             d=CGOLD*(e=(x >= xm ? a-x : b-x));
00277          else {
00278             d=p/q;
00279             u=x+d;
00280             if (u-a < tol2 || b-u < tol2)
00281                d=SIGN(tol1,xm-x);
00282          }
00283       } else {
00284          d=CGOLD*(e=(x >= xm ? a-x : b-x));
00285       }
00286       u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
00287       fu=(*f)(u);
00288       if (fu <= fx) {
00289          if (u >= x) {
00290             a=x;
00291          } else {
00292             b=x;
00293          }
00294          SHFT(v,w,x,u)
00295          SHFT(fv,fw,fx,fu)
00296       } else {
00297          if (u < x) {
00298             a=u;
00299          } else {
00300             b=u;
00301          }
00302          if (fu <= fw || w == x) {
00303             v=w;
00304             w=u;
00305             fv=fw;
00306             fw=fu;
00307          } else if (fu <= fv || v == x || v == w) {
00308             v=u;
00309             fv=fu;
00310          }
00311       }
00312    }
00313    warning(" Too many iterations in BRENT");
00314    xmin=x;
00315    return fx;
00316 }

template<class T>
Matrix<T> ceil const Matrix< T > &    a
 

Definition at line 1108 of file matrix.h.

References matvec::Matrix< T >::apply().

01108 { return a.apply(std::ceil); }

Field ceil Field   a
 

Definition at line 768 of file field.cpp.

References matvec::Field::map().

00768 {return a.map(std::ceil);}

void matvec::check_ptr void *    a
 

Definition at line 37 of file util.cpp.

Referenced by matvec::Model::build_model_term(), matvec::Population::build_nufamily(), matvec::Population::build_trans_mat(), matvec::RegExp::copy(), matvec::Model::copyfrom(), elemhess(), matvec::RegExp::find(), matvec::Vector< T >::initialize(), matvec::Pedigree::input(), matvec::Pedigree::input_pedsheet(), matvec::RegExp::match(), matvec::HashTable::maxsize(), matvec::GeneticDist::nchrom(), new_Genome_vec(), matvec::Data::newcol(), matvec::Population::peeling_sequence(), matvec::Model::prepare_data(), matvec::Model::prior_dist(), matvec::Model::re_hash_data(), matvec::RegExp::replace(), matvec::Vector< T >::reserve(), matvec::Vector< T >::resize(), matvec::TermList::resize(), matvec::HashTable::resize(), matvec::Data::resize(), matvec::Model::RSamplerPrior_dist(), matvec::Population::sub(), and matvec::Model::variance().

00038 {
00039    ;
00040 }

double matvec::ChiSquare_cdf const double    x,
const double    df,
const double    theta,
int &    errcode
 

Definition at line 360 of file stat1.cpp.

References ERRMAX, exp(), gammin(), gammln(), ITRMAX, and log().

Referenced by matvec::ChiSquareDist::cdf(), ChiSquare_inv(), and matvec::GLMM::contrast().

00362 {
00363    ///////////////////////////////////////////////////////////////////////
00364    //  ALGORITHM AS 275 APPL.STATIST. (1992), VOL.41, NO.2
00365    //
00366    //  Computes the noncentral chi-square cumulative distribution function
00367    //  with positive real degrees of freedom df and nonnegative
00368    //  noncentrality parameter theta
00369    //
00370    //  errcode   = 0  no error
00371    //            = 1  parameter range error
00372    //            = 2  fail to converge
00373    //  Tianlin Wang, UIUC, Mon Jun  6 14:02:11 CDT 1994
00374    ////////////////////////////////////////////////////////////////////////
00375    if (df <= 0.0) throw exception(" ChiSquare_cdf(): bad arg2: %f, value > 0 expected");
00376    if (theta < 0.0) throw exception(" ChiSquare_cdf(): bad arg3, nonnegative value expected");
00377    errcode = 0;
00378    if (x <= 0.0) return 0.0;
00379    double chi2nc = x;
00380    if (theta == 0.0) return gammin(chi2nc*0.5, df*0.5);
00381    double lam,u,v,x2,f2,t,errbd,n2;
00382    lam = 0.5*theta;
00383    unsigned n = 1;         // Evaluate the first term
00384    u = exp(-lam);
00385    v = u;
00386    x2 = 0.5*x;
00387    f2 = 0.5*df;
00388    t = f2*log(x2) - x2 - gammln(f2+1.0);
00389    if (t <= -600.0) return 1.0;         // this is added in case exp(t) == 0
00390    t = exp(t);
00391    chi2nc = v*t;
00392    n2 = static_cast<double>(n + n);
00393    while ((df + n2) <= x) {
00394       u *= lam/static_cast<double>(n);       // Evaluate the next term of the expansion
00395       v += u;
00396       t *= x/(df + n2);
00397       chi2nc += v*t;
00398       n++;
00399       n2 = static_cast<double>(n + n);
00400    }
00401    do {
00402       n2 = static_cast<double>(n + n);
00403       errbd = t*x/(df + n2 -x);
00404       if (n>ITRMAX) throw exception ("ChiSquare_cdf(): fail to converge");
00405       u *= lam/static_cast<double>(n);       // Evaluate the next term of the expansion
00406       v += u;
00407       t *= x/(df + n2);
00408       chi2nc += v*t;
00409       n++;
00410    } while (errbd > ERRMAX);
00411    return chi2nc;
00412 }

double matvec::ChiSquare_inv const double    p,
const double    df,
const double    nc
 

Definition at line 701 of file stat1.cpp.

References ChiSquare_cdf(), exp(), gammin(), gammln(), log(), Normal_inv(), and sqrt().

Referenced by matvec::ChiSquareDist::inv().

00702 {
00703    //////////////////////////////////////////////////////////////////
00704    //
00705    //        Algorithm AS 91   Appl. Statist. (1975) Vol.24, P.35
00706    //
00707    //        To evaluate the percentage points of the chi-squared
00708    //        probability distribution function.
00709    //
00710    //     Incorporates the suggested changes in AS R85 (vol.40(1), 
00711    //     pp.233-5, 1991)
00712    //
00713    //     Auxiliary routines required: PPND = AS 111 (or AS 241) and
00714    //     GAMMAD = AS 239.
00715    // NOTE:
00716    //  ChiSquare_inv(1.0e-16,100,0.0) returns 52.4813, it seems not accurate
00717    ///////////////////////////////////////////////////////////////////////
00718    if (p < 0.0 || p >= 1.0 ) throw exception("ChiSquare_inv(): bad arg1, value in (0,1) expected");
00719    if (df <= 0.0) throw exception(" ChiSquare_inv(): bad arg2, value > 0 expected");
00720 
00721    if (p == 0.0) return 0.0;
00722    int errcode = 0;
00723    double ppchi2 = 0.0;   
00724    if (nc != 0.0) {
00725       double eps = 1.0e-7;
00726       double bot = 0.0;
00727       double top = 26000.0;
00728       double p2;
00729       while (top-bot > eps) {
00730          ppchi2 = (top + bot)*0.5;
00731          p2 = ChiSquare_cdf(ppchi2,df,nc,errcode);
00732          if (errcode != 0) break;
00733          if (p2 < p ) {
00734             bot = ppchi2;
00735          }
00736          else if (p2 > p) {
00737             top = ppchi2;
00738          }
00739          else {
00740             break;
00741          }
00742       }
00743       return ppchi2;
00744    }
00745    double one = 1.0;
00746    double aa,e,a,b,d,ch,p1,p2,q,s1,s2,s3,s4,s5,s6,t,x,xx;
00747    aa = 0.6931471806e+0;
00748    e = 0.5e-6;
00749    double* c = new double [38];
00750     c[0]= 0.01;    c[1]= 0.222222;  c[2]= 0.32;    c[3]= 0.4;     c[4]= 1.24;
00751     c[5]= 2.2;     c[6]= 4.67;      c[7]= 6.66;    c[8]= 6.73;    c[9]= 13.32;
00752    c[10]= 60.0;   c[11]= 70.0;     c[12]= 84.0;   c[13]= 105.0;  c[14]= 120.0;
00753    c[15]= 127.0;  c[16]= 140.0;    c[17]= 1175.0; c[18]= 210.0;  c[19]= 252.0;
00754    c[20]= 2264.0; c[21]= 294.0;    c[22]= 346.0;  c[23]= 420.0;  c[24]= 462.0;
00755    c[25]= 606.0;  c[26]= 672.0;    c[27]= 707.0;  c[28]= 735.0;  c[29]= 889.0;
00756    c[30]= 932.0;  c[31]= 966.0;    c[32]= 1141.0; c[33]= 1182.0; c[34]= 1278.0;
00757    c[35]= 1740.0; c[36]= 2520.0;   c[37]= 5040.0;
00758 
00759    xx = df*0.5;
00760    double g = gammln(xx);
00761    d = xx - one;
00762        //    starting approximation for small chi-squared
00763    if (df < -c[4]*log(p)) {
00764       ch = pow(p*xx*exp(g + xx*aa),one/xx);
00765       if (ch <= e) { 
00766         if(c){
00767           delete [] c; 
00768           c=0;
00769         }
00770         return ch;}
00771    }
00772    else {
00773       if (df < c[2]) {       // starting approximation for df <= 0.32
00774          ch = c[3];
00775          a = log(one-p);
00776          do {
00777             q = ch;
00778             p1 = one + ch*(c[6] + ch);
00779             p2 = ch*(c[8] + ch*(c[7] + ch));
00780             t = (c[6] + 2.0*ch)/p1 - (c[8] + ch*(c[9] + 3.0*ch))/p2 - 0.5;
00781             ch -= (1.0 - exp(a + g + 0.5*ch + d*aa)*p2/p1)/t;
00782          } while (fabs(q/ch - one) > c[0]);
00783       }
00784       else {
00785             //  call to algorithm AS 111 - note that p has been tested above.
00786             // AS 241 could be used as an alternative.
00787          x = Normal_inv(p);
00788            // starting approximation using Wilson and Hilferty estimate
00789          p1 = c[1]/df;
00790          ch = df*pow(x*sqrt(p1) + one - p1,3.0);
00791            // starting approximation for p tending to 1
00792          if (ch > c[5]*df + 6.0) ch = -2.0*(log(one-p) - d*log(ch*0.5) + g);
00793       }
00794    }
00795    int i = 0;
00796    while (i++ < 20) {
00797        // call to algorithm AS 239 and calculation of seven term Taylor series
00798       q = ch;
00799       p1 = 0.5*ch;
00800       p2 = p - gammin(p1,xx);
00801       t = p2*exp(xx*aa + g + p1 - d*log(ch));
00802       b = t/ch;
00803       a = 0.5*t - b*d;
00804       s1 = (c[18]+ a*(c[16]+ a*(c[13]+ a*(c[12]+ a*(c[11]+ c[10]*a)))))/c[23];
00805       s2 = (c[23]+ a*(c[28]+ a*(c[31]+ a*(c[32] + c[34]*a))))/c[36];
00806       s3 = (c[18]+ a*(c[24]+ a*(c[27]+ c[30]*a)))/c[36];
00807       s4 = (c[19]+ a*(c[26]+ c[33]*a)+ d*(c[21]+ a*(c[29]+ c[35]*a)))/c[37];
00808       s5 = (c[12]+ c[20]*a+ d*(c[17]+ c[25]*a))/c[36];
00809       s6 = (c[14]+ d*(c[22]+ c[15]*d))/c[37];
00810       ch += t*(one + 0.5*t*s1- b*d*(s1- b*(s2- b*(s3- b*(s4- b*(s5- b*s6))))));
00811       if (fabs(q/ch - one) > e) break;
00812    }
00813    ppchi2 = ch;
00814    if(c){
00815      delete [] c;
00816      c=0;
00817    }
00818    return ppchi2;
00819 }

int matvec::comp const int *    i,
const int *    j
 

Definition at line 33 of file sparsematrix.cpp.

Referenced by matvec::SparseMatrix::minfilsymelm_node_list().

00033                                      {
00034   return *i-*j;
00035 }

int matvec::comp1 const int *    i,
const int *    j
 

Definition at line 33 of file sparsebgmatrix.cpp.

Referenced by matvec::SparseBGMatrix::minfilsymelm_node_list().

00033                                       {
00034   return *i-*j;
00035 }

int compare_DataNode const void *    A,
const void *    B
 

Definition at line 969 of file field.cpp.

References matvec::DataNode::double_val(), and matvec::DataNode::missing.

Referenced by matvec::Field::sort().

00970 {
00971    DataNode *x = (DataNode *)A;
00972    DataNode *y = (DataNode *)B;
00973    if (x->missing && y->missing) {
00974        return 0;
00975    }
00976    else if (x->missing && !y->missing) {
00977        return 1;
00978    }
00979    else if (!x->missing && y->missing) {
00980       return -1;
00981    }
00982    else {
00983       if (x->double_val() < y->double_val()) {
00984          return -1;
00985       }
00986       else if (x->double_val() == y->double_val()) {
00987          return 0;
00988       }
00989       else {
00990          return 1;
00991       }
00992    }
00993 }

int matvec::compare_father_id const void *    x,
const void *    y
 

Definition at line 80 of file individual.cpp.

Referenced by matvec::Population::build_spouse_info().

00081         {
00082                 Individual **U1,**U2;
00083                 U1 = (Individual **)x;
00084                 U2 = (Individual **)y;
00085                 return ((*U1)->father_id() - (*U2)->father_id());
00086         }

int matvec::compare_ind_gid const void *    a,
const void *    b
 

Definition at line 63 of file individual.cpp.

Referenced by matvec::Population::renum().

00066         {
00067                 Individual **x,**y;
00068                 x = (Individual **)a; y = (Individual **)b;
00069                 return ((*x)->gid() - (*y)->gid());
00070         }

int matvec::compare_ind_id const void *    a,
const void *    b
 

Definition at line 54 of file individual.cpp.

00057         {
00058                 Individual **x,**y;
00059                 x = (Individual **)a; y = (Individual **)b;
00060                 return ((*x)->id() - (*y)->id());
00061         }

int matvec::compare_ind_pt const void *    x,
const void *    y
 

Definition at line 40 of file individual.cpp.

00041         {
00042                 Individual **U1,**U2;
00043                 U1 = (Individual **)x;
00044                 U2 = (Individual **)y;
00045                 int retval = 1;
00046                 if (*U1 < *U2)
00047                         retval = -1;
00048                 else if (*U1 == *U2) {
00049                         retval = 0;
00050                 }
00051                 return retval;
00052         }

int compare_ind_ptr const void *    a,
const void *    b
[static]
 

Definition at line 51 of file nufamily.cpp.

00052 {
00053    Individual **x,**y;
00054    x = (Individual **)a; y = (Individual **)b;
00055    if (x[0] == y[0]) {
00056       return 0;
00057    }
00058    else {
00059       return 1;
00060    }
00061 }

int matvec::compare_mother_id const void *    x,
const void *    y
 

Definition at line 72 of file individual.cpp.

Referenced by matvec::Population::build_spouse_info().

00073         {
00074                 Individual **U1,**U2;
00075                 U1 = (Individual **)x;
00076                 U2 = (Individual **)y;
00077                 return ((*U1)->mother_id() - (*U2)->mother_id());
00078         }

int compare_nuf_ptr const void *    a,
const void *    b
[static]
 

Definition at line 39 of file pop_peeling.cpp.

Referenced by matvec::Population::break_loop().

00040 {
00041    NuFamily **x,**y;
00042    x = (NuFamily **)a; y = (NuFamily **)b;
00043    if (x[0] == y[0]) return 0;
00044    else return 1;
00045 }

int compare_seq_id const void *    a,
const void *    b
 

Definition at line 32 of file pop_peeling.cpp.

References matvec::NuFamily::seq_id.

Referenced by matvec::Population::detect_loop().

00033 {
00034    NuFamily **x,**y;
00035    x = (NuFamily **)a; y = (NuFamily **)b;
00036    return (x[0]->seq_id - y[0]->seq_id);
00037 }

void matvec::compute_pdq const double    er,
const Matrix< double > &    pdm,
Matrix< double > &    pdq
 

Definition at line 831 of file pop_mblup.cpp.

Referenced by matvec::Population::add_Gv_inv(), matvec::Population::relv_inv(), and matvec::Population::relv_nomissing().

00832 {
00833    // C = S*R, where C is the PDQ matrix of 2 by 4
00834    for (int i=0; i<2; i++) {
00835       pdq[i][0] = (1-er)*pdm[i][0] +    er *pdm[i][1];
00836       pdq[i][1] =    er *pdm[i][0] + (1-er)*pdm[i][1];
00837       pdq[i][2] = (1-er)*pdm[i][2] +    er *pdm[i][3];
00838       pdq[i][3] =    er *pdm[i][2] + (1-er)*pdm[i][3];
00839    }
00840 }

template<class T>
Matrix<T> cos const Matrix< T > &    a
 

Definition at line 1104 of file matrix.h.

References matvec::Matrix< T >::apply().

01104 { return a.apply(std::cos); }

Field cos Field   a
 

Definition at line 764 of file field.cpp.

References matvec::Field::map().

00764 {return a.map(std::cos);}

template<class T>
Matrix<T> diag const Matrix< T > &    a [inline]
 

Definition at line 1099 of file matrix.h.

References matvec::Matrix< T >::diag().

01099 { return a.diag(); }

template<class T>
Matrix<T> diag const Vector< T > &    a [inline]
 

Definition at line 1092 of file matrix.h.

References matvec::Vector< T >::size().

Referenced by bound_pd().

01093 {
01094    Matrix<T> temp(a.size(),a.size());
01095    for (int i=0; i<a.size(); ++i) temp[i][i] = a[i];
01096    return temp;
01097 }

void elemhess int    job,
double **    mat,
const int    n,
const int    low,
const int    hi,
double **    vr,
double **    vi
 

Definition at line 139 of file nonsymm_eigen.cpp.

References check_ptr().

Referenced by nonsymm_eigen().

00141 {
00142    ////////////////////////////////////////////////////////////////
00143    // Reduce the submatrix in rows and columns low through hi of
00144    // real matrix mat to Hessenberg form by elementary similarity
00145    // transformations
00146    ////////////////////////////////////////////////////////////////
00147    int i,j,m;
00148    double x,y;
00149    int *array = 0;
00150    if (hi - low > 1) {
00151      if(hi>0){
00152        array = new int [hi];
00153      }
00154      else {
00155        array = 0;
00156      }
00157       check_ptr(array);
00158    }
00159    for (m = low + 1; m < hi; m++) {
00160       for (x = 0,i = m,j = m; j <= hi; j++) {
00161          if (fabs(mat[j][m-1]) > fabs(x)) {
00162             x = mat[j][m-1];
00163             i = j;
00164          }
00165       }
00166 
00167       if ((array[m] = i) != m) {
00168          for (j = m - 1; j < n; j++) {
00169             y = mat[i][j];
00170             mat[i][j] = mat[m][j];
00171             mat[m][j] = y;
00172          }
00173 
00174          for (j = 0; j <= hi; j++) {
00175             y = mat[j][i];
00176             mat[j][i] = mat[j][m];
00177             mat[j][m] = y;
00178          }
00179       }
00180 
00181       if (x != 0) {
00182          for (i = m + 1; i <= hi; i++) {
00183             if ((y = mat[i][m-1]) != 0) {
00184                y = mat[i][m-1] = y / x;
00185                for (j = m; j < n; j++) mat[i][j] -= y * mat[m][j];
00186                for (j = 0; j <= hi; j++) mat[j][m] += y * mat[j][i];
00187             }
00188          }
00189       }
00190    }
00191    if (job) {
00192       for (i=0; i<n; i++) {
00193          for (j=0; j<n; j++) vr[i][j] = 0.0; vi[i][j] = 0.0;
00194          vr[i][i] = 1.0;
00195       }
00196 
00197       for (m = hi - 1; m > low; m--) {
00198          for (i = m + 1; i <= hi; i++) vr[i][m] = mat[i][m-1];
00199          if ((i = array[m]) != m) {
00200             for (j = m; j <= hi; j++) {
00201                vr[m][j] = vr[i][j];
00202                vr[i][j] = 0.0;
00203             }
00204             vr[i][m] = 1.0;
00205          }
00206       }
00207    }
00208    if (array) {
00209      delete [] array;
00210      array=0;
00211    }
00212 }

template<class T>
Matrix<T> erf const Matrix< T > &    a
 

Definition at line 1115 of file matrix.h.

References matvec::Matrix< T >::apply().

01115 { return a.apply(erf); }

Field erf Field   a
 

Definition at line 775 of file field.cpp.

References matvec::Field::map().

Referenced by Normal_cdf().

00775 {return a.map(::erf);}

template<class T>
Matrix<T> erfc const Matrix< T > &    a
 

Definition at line 1116 of file matrix.h.

References matvec::Matrix< T >::apply().

01116 { return a.apply(erfc); }

Field erfc Field   a
 

Definition at line 776 of file field.cpp.

References matvec::Field::map().

00776 {return a.map(::erfc);}

unsigned matvec::est_nze const unsigned    dim,
const unsigned    ntrait,
const int    ped
 

Definition at line 98 of file util.cpp.

Referenced by matvec::Model::prepare_data(), matvec::Population::setup_m_ww(), and matvec::Model::vce_emreml_multi_trait().

00099 {
00100    // ***********************************************
00101    // *   get a suggested number of max_nz elements
00102    // *  (1) calculate max_nz for single trait model
00103    // *  (2) then multiply it by ntrait*ntrait
00104    // ************************************************
00105 
00106    if (dim==0) return 0;
00107    double onetraitdim = static_cast<double>(dim)/static_cast<double>(ntrait);
00108    double nze, halfsize = onetraitdim*(onetraitdim - 1.0)/2.0;
00109    if(halfsize < 1000.0) {
00110       nze = halfsize;
00111    }
00112    else if(halfsize < 5000.0) {
00113       nze = 0.7 * halfsize;
00114    }
00115    else if(halfsize < 1.0e+4) {
00116       nze = 0.4 * halfsize;
00117    }
00118    else if(halfsize < 1.0e+5) {
00119       nze = 5e-1 * halfsize;
00120    }
00121    else if(halfsize < 1.0e+6) {
00122       nze = 5e-2 * halfsize;
00123    }
00124    else if(halfsize < 5.0e+6) {
00125       nze = 1e-2 * halfsize;
00126    }
00127    else if(halfsize < 1.0e+7) {
00128       nze = 8e-3 * halfsize;
00129    }
00130    else if(halfsize < 5.0e+7) {
00131       nze = 6e-3 * halfsize;
00132    }
00133    else if(halfsize < 1.0e+8) {
00134       nze = 4e-3 * halfsize;
00135    }
00136    else if(halfsize < 5.0e+8) {
00137       nze = 2e-3 * halfsize;
00138    }
00139    else if(halfsize < 1.0e+9) {
00140       nze = 9e-4 * halfsize;
00141    }
00142    else if(halfsize < 3.0e+9) {
00143       nze = 7e-4 * halfsize;
00144    }
00145    else if(halfsize < 5.0e+9) {
00146       nze = 5e-4 * halfsize;
00147    }
00148    else if(halfsize < 7.0e+9) {
00149       nze = 3e-4 * halfsize;
00150    }
00151    else if(halfsize < 1.0e+10) {
00152       nze = 9e-5 * halfsize;
00153    }
00154    else if(halfsize < 3.0e+10) {
00155       nze = 7e-5 * halfsize;
00156    }
00157    else if(halfsize < 5.0e+10) {
00158       nze = 5e-5 * halfsize;
00159    }
00160    else if(halfsize < 7.0e+10) {
00161       nze = 3e-5 * halfsize;
00162    }
00163    else if(halfsize < 9.0e+10) {
00164       nze = 1e-5 * halfsize;
00165    }
00166    else {
00167       nze = 1e-6 * halfsize;
00168    }
00169 
00170    nze += static_cast<double>(pedsize*3);
00171    nze = nze*ntrait*(ntrait+1.0)/2.0 + dim;
00172    return static_cast<unsigned>(nze);
00173 }

template<class T>
Matrix<T> exp const Matrix< T > &    a
 

Definition at line 1112 of file matrix.h.

References matvec::Matrix< T >::apply().

01112 { return a.apply(std::exp); }

Field exp Field   a
 

Definition at line 772 of file field.cpp.

References matvec::Field::map().

00772 {return a.map(std::exp);}

BG exp const BG   a
 

Definition at line 144 of file bg.cpp.

References matvec::BG::D, matvec::BG::d, matvec::BG::f, and matvec::Matrix< double >::transpose().

Referenced by Beta_cdf(), betain(), ChiSquare_cdf(), ChiSquare_inv(), factrl(), gamdev(), gammin(), sgamma(), t_cdf(), and t_expected_value().

00144                      {
00145     BG r; 
00146     r.f = std::exp(a.f);
00147     r.d = r.f*a.d;
00148     r.D = r.f*a.D + r.d*a.d.transpose();
00149     return r;
00150   }

double matvec::f1dim double    x [static]
 

Definition at line 167 of file min_nr_powell.cpp.

References funk(), ncom, pcom, and xicom.

Referenced by linmin().

00168 {
00169    Vector<double> xt(ncom);
00170    for (int j=0; j<ncom; j++) xt[j] = pcom[j] + x*xicom[j];
00171    double f = funk(xt);
00172    return f;
00173 }

double matvec::F_cdf const double    f,
const double    df1,
const double    df2,
const double    nc,
int &    errcode
 

Definition at line 557 of file stat1.cpp.

References Beta_cdf().

Referenced by matvec::FDist::cdf(), matvec::GLMM::contrast(), and F_inv().

00559 {
00560    if (df1 <= 0.0 || df2 <= 0.0) throw exception(" F_cdf(): bad args: value > 0 expected");
00561    errcode = 0;
00562    double retval;
00563    if (f <= 0.0) {
00564       retval = 0.0;
00565    } else {
00566       retval = Beta_cdf(df1*f/(df1*f+df2),df1*0.5,df2*0.5,nc,errcode);
00567    }
00568    return retval;
00569 }

double matvec::F_inv const double    p,
const double    df1,
const double    df2,
const double    nc
 

Definition at line 862 of file stat1.cpp.

References F_cdf().

Referenced by matvec::FDist::inv().

00863 {
00864    //////////////////////////////////////////////////////////////
00865    // compute percentage point given lower tail area p.
00866    // binary iteration algoritm, a better algorithm is needed
00867    //
00868    // Tianlin Wang, UIUC, Mon Jun  6 14:58:36 CDT 1994
00869    ////////////////////////////////////////////////////////////
00870    if (p < 0.0 || p >= 1.0 ) throw exception(" F_inv(): bad arg1, value in (0,1) expected");
00871    if (df1 <= 0.0 || df2 <= 0.0 ) throw exception(" F_inv(): bad arg2 or 3, > 0 expected");
00872    if (p==0.0) return 0.0;
00873    double p2,ppf;
00874    if (df2 >= 3) {
00875       ppf = df2*(df1 + nc)/(df1*(df2 - 2.0));     // mean of F(df1,df2,nc)
00876    }
00877    else {
00878       ppf = 0;
00879    }
00880    double eps = 1.0e-7;
00881    double bot = 0.0;
00882    double top = ppf + 26000.0;
00883    int errcode = 0;
00884    while (top-bot > eps) {
00885       ppf = (top + bot)*0.5;
00886       p2 = F_cdf(ppf,df1,df2,nc,errcode);
00887       if (errcode != 0) break;
00888       if (p2 < p ) {
00889          bot = ppf;
00890       }
00891       else if (p2 > p) {
00892          top = ppf;
00893       }
00894       else {
00895          break;
00896       }
00897    }
00898    return ppf;
00899 }

double matvec::factrl const long    n
 

Definition at line 315 of file stat1.cpp.

References exp(), and gammln().

00316 {
00317    static int ntop = 8;
00318    static double a[33]={1.0,1.0,2.0,6.0,24.0,120.0,720.0,5040.0,40320.0};
00319    int j;
00320    if (n > 32) return exp(gammln(static_cast<double>(n+1)));
00321    while (ntop < n) {
00322       j = ntop++;
00323       a[ntop] = a[j]*ntop;
00324    }
00325    return a[n];
00326 }

int matvec::fieldindex const std::string &    fw,
const FieldStruct   field_vec,
const unsigned    n
 

Definition at line 26 of file fieldstruct.cpp.

References matvec::FieldStruct::name().

Referenced by matvec::Model::build_model_term(), matvec::Model::copyfrom(), matvec::Model::covariate(), and matvec::ModelTerm::input().

00027 {
00028    int k = -1;
00029    for (unsigned i=0; i<n; i++) {
00030       if (field_vec[i].name() == fw) {
00031          k = i; break;
00032       }
00033    }
00034    return k;
00035 }

void flin const double    l,
const int    j,
const Vector< double > &    x,
const int    n,
const Matrix< double > &    v,
Vector< double > &    w,
const Matrix< double > &    q01
[static]
 

Definition at line 72 of file min_praxis.cpp.

References qd.

Referenced by matvec::Minimizer::min_first_dir().

00074 {
00075    int i;
00076    double qa,qb,qc;
00077    if (j != -1) {               // linear search
00078       for (i=0; i<n; i++) w[i] = x[i] + l *v[i][j];
00079    }
00080    else {                       // search along parabolic space curve
00081       qa = l*(l-qd[1])/(qd[0]*(qd[0]+qd[1]));
00082       qb = (l+qd[0])*(qd[1]-l)/(qd[0]*qd[1]);
00083       qc = l*(l+qd[0])/(qd[1]*(qd[0]+qd[1]));
00084       for (i=0; i<n; i++) w[i] = qa*q01[0][i]+qb*x[i]+qc*q01[1][i];
00085    }
00086 }

template<class T>
Matrix<T> floor const Matrix< T > &    a
 

Definition at line 1109 of file matrix.h.

References matvec::Matrix< T >::apply().

01109 { return a.apply(std::floor); }

Field floor Field   a
 

Definition at line 769 of file field.cpp.

References matvec::Field::map().

00769 {return a.map(std::floor);}

double fsign double    num,
double    sign
 

Definition at line 60 of file stat2.cpp.

00062 {
00063    if ( ( sign>0.0 && num<0.0 ) || ( sign<0.0 && num>0.0 ) ) return ( -num );
00064    else return ( num );
00065 }

double funk Vector< double > &    x [static]
 

Definition at line 38 of file min_nr_powell.cpp.

References matvec::Model::info(), nfunk, matvec::Model::nterm(), matvec::doubleMatrix::psd(), matvec::Model::residual_var, matvec::Model::restricted_log_likelihood(), matvec::Model::term, and matvec::Model::vec2var().

Referenced by f1dim(), and nr_powell().

00039 {
00040    int i,k;
00041    unsigned nterms = myM->nterm();
00042    myM->vec2var(x);
00043    if (!myM->residual_var.psd()) return 1.0e30;
00044    for (i=0; i<nterms; i++) {
00045       if (myM->term[i].classi() == 'R' || myM->term[i].classi() == 'P') {
00046          k = myM->term[i].prior->var_matrix()->psd();
00047          if (!k) return (1.0e30 + 1.0e10*(i+1));
00048       }
00049    }
00050    nfunk++;
00051    double log_likelihood = myM->restricted_log_likelihood();
00052    if ( (nfunk % 3) == 0) {
00053       std::cout << " powell...# of log_likelihood evaluations = " << nfunk << ", "
00054            << "and its current value = " << log_likelihood << std::endl;
00055       myM->info(std::cout);
00056    }
00057    return log_likelihood*(-1.0);
00058 }

void G_to_gamete const unsigned    G_id,
unsigned    k1,
unsigned    k2,
unsigned    size
 

Definition at line 41 of file pop_gibbs.cpp.

00048 {
00049    k1 = G_id/size;
00050    k2 = G_id - k1*size;
00051 }

double matvec::gamdev const double    A,
const double    R
 

Definition at line 124 of file stat1.cpp.

References exp(), log(), ran1(), and sqrt().

00125 {
00126    /////////////////////////////////////////////////////////////////////
00127    //   Generates random deviates from the standard gamma distribution
00128    //    whose density is
00129    //              1
00130    //       --------------- * X^(A-1) * Exp(-X/R)
00131    //        (R^A)*Gamma(A)
00132    //
00133    //    Chi-Square can be generated as gamdev(df/2,2.0)
00134    //
00135    //    Tianlin Wang at UIUC, May 1,1992
00136    /////////////////////////////////////////////////////////////////////
00137    int K,j;
00138    double am,e,s,v1,v2,x,y;
00139    K = static_cast<int>(A);
00140    if (K < 1) throw exception(" gamdev(): bad arg1, value >= 1 expected ");
00141    if (K < 6) {
00142       x=1.0;
00143       for (j=1;j<=K;j++) x *= ran1();
00144       x = -log(x);
00145    } else {
00146       do {
00147          do {
00148             do {
00149                v1=2.0*ran1()-1.0;
00150                v2=2.0*ran1()-1.0;
00151             } while (v1*v1+v2*v2 > 1.0);
00152             y=v2/v1;
00153             am=K-1;
00154             s=sqrt(2.0*am+1.0);
00155             x=s*y+am;
00156          } while (x <= 0.0);
00157          e=(1.0+y*y)*exp(am*log(x/am)-s*y);
00158       } while (ran1() > e);
00159    }
00160    return (x*R);
00161 }

double matvec::Gamma_cdf const double    g,
const double    alfa,
const double    theta
 

Definition at line 339 of file stat1.cpp.

References gammin().

00340 {
00341    double retval = 0.0;
00342    if (g <= 0.0 ) return retval;
00343    if (alfa > 0.0 ) {
00344       if (theta > 0.0 ) {
00345          retval = gammin(g/theta,alfa);
00346       }
00347       else {
00348          throw exception(" Gamma_cdf(): bad arg3, value > 0 expected");
00349       }
00350    }
00351    else {
00352       throw exception(" Gamma_cdf(): bad arg2: %f, value > 0 expected");
00353    }
00354    return retval;
00355 }

double matvec::gammin const double    x,
const double    a
 

Definition at line 225 of file stat1.cpp.

References exp(), gammln(), log(), Normal_cdf(), and sqrt().

Referenced by ChiSquare_cdf(), ChiSquare_inv(), Gamma_cdf(), and matvec::PoissonDist::sample().

00226 {
00227    //////////////////////////////////////////////////////////
00228    // ALGORITHM AS239 Appl. Statist. (1988) 37(3), GAMMAD()
00229    // return the incomplete Gamma function for arguments:
00230    //       x between 0 and +infinite
00231    //       p positive
00232    // REQUIREMENTS: Normal_cdf(), gammln()
00233    ////////////////////////////////////////////////////////
00234    double zero,one,two,three,nine,tol,oflo,xbig,plimit,elimit;
00235    double pn1,pn2,pn3,pn4,pn5,pn6,rn,arg,a,b,c,an;
00236    zero = 0.0; one = 1.0; two = 2.0; three = 3.0; nine = 9.0;
00237    tol = 1.0e-14; oflo = 1.0e+37; xbig = 1.0e+8;
00238    plimit = 1000.0; elimit = -88.0;
00239    double retval = zero;
00240    if (x < zero || p <= zero) throw exception(" gammin(): bad args");
00241    if (x == zero) return retval;
00242    if (p > plimit) {      // use a normal approximation
00243       pn1 = three*sqrt(p)*(pow(x/p,one/three) + one/(nine*p) - one);
00244       return Normal_cdf(pn1);
00245    }
00246    if (x > xbig) return one;   // x is extremely large compared to p
00247    if (x <= one || x < p) {    //  use Pearson's series expansion
00248       arg = p*log(x) - x - gammln(p + one);
00249       c = one;
00250       retval = one;
00251       a = p;
00252       do {
00253          a++;
00254          c *= x/a;
00255          retval += c;
00256       } while (c > tol);
00257       arg += log(retval);
00258       retval = zero;
00259       if (arg >= elimit) retval = exp(arg);
00260    }
00261    else {
00262       arg = p*log(x) - x - gammln(p);
00263       a = one - p;
00264       b = a + x + one;
00265       c = zero;
00266       pn1 = one;   pn2 = x;   pn3 = x + one;   pn4 = x*b;
00267       retval = pn3/pn4;
00268       for(;;) {
00269          a += one; b += two; c += one;
00270          an = a*c;
00271          pn5 = b*pn3 - an*pn1;
00272          pn6 = b*pn4 - an*pn2;
00273          if (fabs(pn6) > zero) {
00274             rn = pn5/pn6;
00275             if (fabs(retval - rn) <= tol*(rn < one ? rn: one)) break;
00276             retval = rn;
00277          }
00278          pn1 = pn3;   pn2 = pn4;   pn3 = pn5;   pn4 = pn6;
00279          if (fabs(pn5) >= oflo) { // re-scale terms, if terms are large
00280             pn1 /= oflo;  pn2 /= oflo;  pn3 /= oflo;  pn4 /= oflo;
00281          }
00282       }
00283       arg += log(retval);
00284       retval = one;
00285       if (arg >= elimit) retval = one - exp(arg);
00286    }
00287    return retval;
00288 }

double matvec::gammln const double    xx
 

Definition at line 208 of file stat1.cpp.

References log().

Referenced by Beta_cdf(), BinCoef(), matvec::BinomialDist::cdf(), matvec::GammaDist::cdf(), ChiSquare_cdf(), ChiSquare_inv(), factrl(), gammin(), matvec::NegativeBinomialDist::mgf(), matvec::BinomialDist::sample(), t_cdf(), and t_expected_value().

00209 {
00210    if (xx <= 0.0) throw exception(" gammln(): bad arg, value > 0 expected");
00211    double x,tmp,ser;
00212    static double cof[6]={76.18009173,-86.50532033,24.01409822,
00213                          -1.231739516,0.120858003e-2,-0.536382e-5};
00214    x = xx-1.0;
00215    tmp = x + 5.5;
00216    tmp -= (x+0.5)*log(tmp);
00217    ser=1.0;
00218    for (int j=0; j<=5; j++) {
00219       x += 1.0;
00220       ser += cof[j]/x;
00221    }
00222    return -tmp+log(ser*2.50662827465);
00223 }

double matvec::gasdev void   
 

Definition at line 103 of file stat1.cpp.

References log(), ran1(), and sqrt().

00104 {
00105    static int iset=0;
00106    static double gset;
00107    double fac,r,v1,v2;
00108    if  (iset == 0) {
00109       do {
00110          v1=2.0*ran1()-1.0;
00111          v2=2.0*ran1()-1.0;
00112          r=v1*v1+v2*v2;
00113       } while (r >= 1.0 || r == 0.0);
00114       fac=sqrt(-2.0*log(r)/r);
00115       gset=v1*fac;
00116       iset=1;
00117       return v2*fac;
00118    } else {
00119       iset=0;
00120       return gset;
00121    }
00122 }

double matvec::genbet const double    aa,
const double    bb
 

Definition at line 1135 of file stat2.cpp.

References genbet(), min, and ranf().

Referenced by genbet(), and matvec::BetaDist::sample().

01151                                     :317-322  (1978)
01152 //     (Algorithms BB and BC)
01153 // ***********************************************************************
01154 {
01155 #define expmax 89.0
01156 #define infnty 1.0E38
01157    static double olda = -1.0;
01158    static double oldb = -1.0;
01159    static double genbet,a,alpha,b,beta,delta,gamma,k1,k2,r,s,t,u1,u2,v,w,y,z;
01160    static long qsame;
01161 
01162    qsame = olda == aa && oldb == bb;
01163    if (qsame) goto S20;
01164    if (aa <= 0.0 || bb <= 0.0) throw exception("genbet(): bad args");
01165    olda = aa;
01166    oldb = bb;
01167 S20:
01168    if(!(std::min(aa,bb) > 1.0)) goto S100;
01169                      //  Alborithm BB Initialize
01170    if(qsame) goto S30;
01171    a = std::min(aa,bb);
01172    b = std::max(aa,bb);
01173    alpha = a+b;
01174    beta = sqrt((alpha-2.0)/(2.0*a*b-alpha));
01175    gamma = a+1.0/beta;
01176 S30:
01177 S40:
01178    u1 = ranf();   //  Step 1
01179    u2 = ranf();
01180    v = beta*log(u1/(1.0-u1));
01181    if(!(v > expmax)) goto S50;
01182    w = infnty;
01183    goto S60;
01184 S50:
01185    w = a*exp(v);
01186 S60:
01187    z = pow(u1,2.0)*u2;
01188    r = gamma*v-1.3862944;
01189    s = a+r-w;
01190        //  Step 2
01191    if(s+2.609438 >= 5.0*z) goto S70;
01192        //  Step 3
01193    t = log(z);
01194    if(s > t) goto S70;
01195          // Step 4
01196    if(r+alpha*log(alpha/(b+w)) < t) goto S40;
01197 S70:    //  Step 5
01198    if(!(aa == a)) goto S80;
01199    genbet = w/(b+w);
01200    goto S90;
01201 S80:
01202    genbet = b/(b+w);
01203 S90:
01204    goto S230;
01205 S100:   //  Algorithm BC Initialize
01206    if(qsame) goto S110;
01207    a = std::max(aa,bb);
01208    b = std::min(aa,bb);
01209    alpha = a+b;
01210    beta = 1.0/b;
01211    delta = 1.0+a-b;
01212    k1 = delta*(1.38889e-2+4.16667e-2*b)/(a*beta-0.777778);
01213    k2 = 0.25+(0.5+0.25/delta)*b;
01214 S110:
01215 S120:
01216    u1 = ranf();
01217        // Step 1
01218    u2 = ranf();
01219    if(u1 >= 0.5) goto S130;
01220         //Step 2
01221    y = u1*u2;
01222    z = u1*y;
01223    if(0.25*u2+z-y >= k1) goto S120;
01224    goto S170;
01225 S130:
01226       // Step 3
01227    z = pow(u1,2.0)*u2;
01228    if(!(z <= 0.25)) goto S160;
01229    v = beta*log(u1/(1.0-u1));
01230    if(!(v > expmax)) goto S140;
01231    w = infnty;
01232    goto S150;
01233 S140:
01234    w = a*exp(v);
01235 S150:
01236    goto S200;
01237 S160:
01238    if(z >= k2) goto S120;
01239 S170:
01240      // Step 4  Step 5
01241    v = beta*log(u1/(1.0-u1));
01242    if(!(v > expmax)) goto S180;
01243    w = infnty;
01244    goto S190;
01245 S180:
01246    w = a*exp(v);
01247 S190:
01248    if(alpha*(log(alpha/(b+w))+v)-1.3862944 < log(z)) goto S120;
01249 S200:
01250        // Step 6
01251    if(!(a == aa)) goto S210;
01252    genbet = w/(b+w);
01253    goto S220;
01254 S210:
01255    genbet = b/(b+w);
01256 S230:
01257 S220:
01258    return genbet;
01259 #undef expmax
01260 #undef infnty
01261 }

double matvec::genchi const int    df
 

Definition at line 487 of file stat2.cpp.

References genchi(), and sgamma().

Referenced by genchi(), genf(), gennch(), gennf(), matvec::tDist::sample(), matvec::ChiSquareDist::sample(), and matvec::Model::vce_gibbs_sampler().

00502 {
00503 
00504    if (df <= 0) throw exception(" genchi(): invalid arg");
00505    return (2.0*sgamma(static_cast<double>(df)/2.0));
00506 }

double matvec::genexp const double    av
 

Definition at line 508 of file stat2.cpp.

References genexp(), and sexpo().

Referenced by genexp().

00522                       :
00523 //               Ahrens, J.H. and Dieter, U.
00524 //               Computer Methods for Sampling From the
00525 //               Exponential and Normal Distributions.
00526 //               Comm. ACM, 15,10 (Oct. 1972), 873 - 882.
00527 //************************************************************
00528 
00529 {
00530 
00531     return ( sexpo()*av );
00532 }

double matvec::genf const int    dfn,
const int    dfd
 

Definition at line 534 of file stat2.cpp.

References genchi(), genf(), and warning().

Referenced by genf(), and matvec::FDist::sample().

00552 {
00553    if ( dfn <= 0.0 || dfd <= 0.0) throw exception(" genf(): invalid args");
00554    static double genfval = 0.0;
00555    static double xden,xnum;
00556    xnum = genchi(dfn)/dfn;
00557           //////  GENF = ( GENCHI(DFN)/DFN )/( GENCHI(DFD)/DFD ) ///////
00558    xden = genchi(dfd)/dfd;
00559    if ( xden <= 9.999999999998e-39*xnum) {
00560       warning("GENF - generated numbers would cause overflow,"
00561                  " df1 = %16.6E, df2 = %16.6E",xnum,xden);
00562       genfval = 1.0E38;
00563    }
00564    else {
00565       genfval = xnum/xden;
00566    }
00567    return genfval;
00568 }

double matvec::gengam const double    a,
const double    r
 

Definition at line 451 of file stat2.cpp.

References gengam(), and sgamma().

Referenced by gengam().

00468                       :
00469 //               (Case A >= 1.0)
00470 //               Ahrens, J.H. and Dieter, U.
00471 //               Generating Gamma Variates by a
00472 //               Modified Rejection Technique.
00473 //               Comm. ACM, 25,1 (Jan. 1982), 47 - 54.
00474 //     Algorithm GD
00475 //               (Case 0.0 <= A <= 1.0)
00476 //               Ahrens, J.H. and Dieter, U.
00477 //               Computer Methods for Sampling from Gamma,
00478 //               Beta, Poisson and Binomial Distributions.
00479 //               Computing, 12 (1974), 223-246/
00480 //     Adapted algorithm GS.
00481 //*****************************************************************
00482 
00483 {
00484     return ( sgamma(a)*r );
00485 }

double matvec::gennch const int    df,
const double    xnonc
 

Definition at line 570 of file stat2.cpp.

References genchi(), gennch(), gennor(), and snorm().

Referenced by gennch(), and gennf().

00589 {
00590    if ( df > 1 && xnonc >= 0.0) {
00591       return ( genchi(df-1)+pow(gennor(sqrt(xnonc),1.0),2.0));
00592    }
00593    else if (df == 1 && xnonc >= 0.0) {
00594       double x = snorm() + xnonc;
00595       return x*x;
00596    }
00597    else {
00598       throw exception(" gennch(): invalid arg");
00599    }
00600 }

double matvec::gennf const int    dfn,
const int    dfd,
const double    xnonc
 

Definition at line 602 of file stat2.cpp.

References genchi(), gennch(), gennf(), and warning().

Referenced by gennf(), and matvec::FDist::sample().

00622 {
00623    if (dfn <= 1 || dfd <= 0 || xnonc < 0.0) throw exception(" gennf(): invalid args");
00624    static double gennf,xden,xnum;
00625    xnum = gennch(dfn,xnonc)/dfn;
00626        /////   GENNF = ( GENNCH(DFN,XNONC)/DFN )/( GENCHI(DFD)/DFD ) /////
00627    xden = genchi(dfd)/dfd;
00628    if ( xden <= 9.999999999998e-39*xnum ) {
00629       warning("gennf(): overflow, df1= %16.6E, df2= %16.6E",xnum,xden);
00630       gennf = 1.0E38;
00631    }
00632    else {
00633       gennf = xnum/xden;
00634    }
00635    return gennf;
00636 }

double matvec::gennor const double    av,
const double    sd
 

Definition at line 428 of file stat2.cpp.

References gennor(), and snorm().

Referenced by gennch(), and gennor().

00441                       :
00442 //               Ahrens, J.H. and Dieter, U.
00443 //               Extensions of Forsythe's Method for Random
00444 //              Sampling from the Normal Distribution.
00445 //               Math. Comput., 27,124 (Oct. 1973), 927 - 937.
00446 //********************************************************************
00447 {
00448     return ( sd*snorm()+av );
00449 }

double matvec::genunf const double    low,
const double    high
 

Definition at line 638 of file stat2.cpp.

References genunf(), and ranf().

Referenced by genunf(), and matvec::UniformDist::sample().

00648 {
00649    if (low > high ) throw exception("genunf(): invalid arg");
00650    return ( low+(high-low)*ranf() );
00651 }

void matvec::getlambda double **    lambda,
const int    n
 

Definition at line 37 of file pedigree.cpp.

Referenced by matvec::Model::add_Ag(), matvec::GLMM::add_Ag(), matvec::Model::add_Ag_diag(), matvec::GLMM::add_Ag_old(), matvec::GLMM::add_AgSand(), matvec::Model::add_G_1_single_trait(), matvec::Population::add_Ga_inv(), matvec::Pedigree::ainv(), and matvec::GLMM::ainv().

00038 {
00039    if (lambda) {
00040       if (n == 3 ) {
00041          lambda[0][0] =  1.0; lambda[0][1] = -0.5; lambda[0][2] = -0.5;
00042          lambda[1][0] = -0.5; lambda[1][1] =  0.25; lambda[1][2] = 0.25;
00043          lambda[2][0] = -0.5; lambda[2][1] =  0.25; lambda[2][2] = 0.25;
00044       }
00045       else {
00046          throw exception("getlambda(a,n),n must be 3");
00047       }
00048    }
00049    else {
00050       throw exception("getlambda(l,n): l is NULL");
00051    }
00052 }

unsigned matvec::ginverse1 BG **    a,
const unsigned    n,
BG   lgdet,
int    mode,
const double    tol
 

Definition at line 112 of file ginverse1.cpp.

References ginverse1().

00114 {
00115    unsigned i,j,k,irank = n;
00116    BG sum;
00117    ///////////////////////////////////////////////////
00118    // column-by-column, we are doing
00119    // L (lower triangular) in place such that L*L'=A
00120    ///////////////////////////////////////////////////
00121    lgdet = 0.0;
00122    for (j=0; j<n; ++j) {  // for column j, L[j][j] is calculated first
00123       sum = a[j][j];
00124       for (k=0; k<j; ++k) sum -= a[j][k]*a[j][k];
00125                         //  for other elements L[i,j] in jth column, i=j+1 to n
00126       if (sum <= -tol) {
00127         throw exception("ginverse1(): matrix is non-psd");
00128       }
00129       if (fabs(sum) < tol) {
00130          irank--;
00131          for (i=j; i<n; i++) a[i][j]=0.0;
00132       }
00133       else {
00134          lgdet += log(sum);
00135          a[j][j]=sqrt(sum);
00136          for (i=j+1; i<n; i++) {
00137             sum = a[i][j];
00138             for (k=0; k<j; ++k) sum -= a[i][k]*a[j][k];
00139             a[i][j]=sum/a[j][j];
00140          }
00141       }
00142    }
00143    if (mode != 1) {
00144       if (mode ==2) {
00145         for(i=0;i<n-1;i++) { 
00146           //memset(&(a[i][i+1]),'\0',sizeof(BG)*(n-i-1));
00147           for(j=i+1;j<n;j++){
00148             a[i][j] = 0.0;
00149           }
00150         }
00151        }
00152        return irank;
00153    }
00154    //////////////////////////////////////////
00155    //  column-by-column,  we are doing 
00156    //   inv(L) in place (lower triangl)
00157    //////////////////////////////////////////
00158    for (j=0; j<n; j++) { 
00159       if (fabs(a[j][j]) < tol) {
00160          for (i=j; i<n; i++) a[i][j]=0.0;            
00161       }
00162       else {
00163          a[j][j] = 1./a[j][j];
00164          for (i=j+1; i<n; i++) {
00165             if (a[i][i] <= -tol)  throw exception("ginverse1(): matrix is non-psd");
00166             if (fabs(a[i][i]) < tol) {
00167                a[i][j]=0.0;
00168             }
00169             else {
00170                sum=0.0;
00171                for (k=j; k<i; k++) sum -= a[i][k]*a[k][j];                      
00172                a[i][j] = sum/a[i][i];
00173             }      // end of if-else block
00174          }         // end of for-loop
00175       }            // end of if-else block
00176    }               // end of for-loop
00177 
00178    //////////////////////////////////////
00179    //  column-by-column, we are doing 
00180    //  inv(L')*inv(L)= inv(a) in place
00181    //////////////////////////////////////
00182    for (j=0; j<n; j++) {
00183       for (i=j; i<n; i++) {
00184          for (sum=0.0,k=i; k<n; k++) sum += a[k][i]*a[k][j];    
00185          a[i][j]=sum;    a[j][i]=sum;
00186       }
00187    }
00188    return irank;
00189 }

unsigned matvec::ginverse1 double **    a,
const unsigned    n,
double &    lgdet,
int    mode,
const double    tol
 

Definition at line 38 of file ginverse1.cpp.

Referenced by matvec::Model::add_Ag(), matvec::GLMM::add_Ag(), matvec::Model::add_Ag_diag(), matvec::GLMM::add_Ag_old(), matvec::GLMM::add_AgSand(), matvec::Model::add_Ig(), matvec::Model::add_Ig_diag(), matvec::GLMM::add_IgSand(), matvec::doubleMatrix::ginv1(), matvec::BGMatrix::ginv1(), ginverse1(), matvec::Model::inverse_residual_var(), matvec::doubleMatrix::logdet(), matvec::BGMatrix::logdet(), matvec::doubleMatrix::sqrtm(), and matvec::BGMatrix::sqrtm().

00040 {
00041    unsigned i,j,k,irank = n;
00042    double sum;
00043    ///////////////////////////////////////////////////
00044    // column-by-column, we are doing
00045    // L (lower triangular) in place such that L*L'=A
00046    ///////////////////////////////////////////////////
00047    lgdet = 0.0;
00048    for (j=0; j<n; ++j) {  // for column j, L[j][j] is calculated first
00049       sum = a[j][j];
00050       for (k=0; k<j; ++k) sum -= a[j][k]*a[j][k];
00051                         //  for other elements L[i,j] in jth column, i=j+1 to n
00052       if (sum <= -tol) {
00053         throw exception("ginverse1(): matrix is non-psd");
00054       }
00055       if (std::fabs(sum) < tol) {
00056          irank--;
00057          for (i=j; i<n; i++) a[i][j]=0.0;
00058       }
00059       else {
00060          lgdet += std::log(sum);
00061          a[j][j]=std::sqrt(sum);
00062          for (i=j+1; i<n; i++) {
00063             sum = a[i][j];
00064             for (k=0; k<j; ++k) sum -= a[i][k]*a[j][k];
00065             a[i][j]=sum/a[j][j];
00066          }
00067       }
00068    }
00069    if (mode != 1) {
00070       if (mode ==2) {
00071          for(i=0;i<n-1;i++) memset(&(a[i][i+1]),'\0',sizeof(double)*(n-i-1));
00072        }
00073        return irank;
00074    }
00075    //////////////////////////////////////////
00076    //  column-by-column,  we are doing 
00077    //   inv(L) in place (lower triangl)
00078    //////////////////////////////////////////
00079    for (j=0; j<n; j++) { 
00080       if (std::fabs(a[j][j]) < tol) {
00081          for (i=j; i<n; i++) a[i][j]=0.0;            
00082       }
00083       else {
00084          a[j][j] = 1./a[j][j];
00085          for (i=j+1; i<n; i++) {
00086             if (a[i][i] <= -tol)  throw exception("ginverse1(): matrix is non-psd");
00087             if (std::fabs(a[i][i]) < tol) {
00088                a[i][j]=0.0;
00089             }
00090             else {
00091                sum=0.0;
00092                for (k=j; k<i; k++) sum -= a[i][k]*a[k][j];                      
00093                a[i][j] = sum/a[i][i];
00094             }      // end of if-else block
00095          }         // end of for-loop
00096       }            // end of if-else block
00097    }               // end of for-loop
00098 
00099    //////////////////////////////////////
00100    //  column-by-column, we are doing 
00101    //  inv(L')*inv(L)= inv(a) in place
00102    //////////////////////////////////////
00103    for (j=0; j<n; j++) {
00104       for (i=j; i<n; i++) {
00105          for (sum=0.0,k=i; k<n; k++) sum += a[k][i]*a[k][j];    
00106          a[i][j]=sum;    a[j][i]=sum;
00107       }
00108    }
00109    return irank;
00110 }

void ginverse2 double *    a,
const unsigned    n,
const double    tol
[static]
 

Definition at line 40 of file model_blup.cpp.

References lidx().

Referenced by matvec::Model::blup_pccg().

00043 {
00044    unsigned i,j,k,t,ii,jj;
00045    double sum,aii,ajj;
00046 
00047    ///////////////////////////////////////////////////
00048    // column-by-column, we are doing
00049    // L (lower triangular) in place such that L*L'=A
00050    ///////////////////////////////////////////////////
00051    for (j=0; j<n; j++) {  // for column j, L[j][j] is calculated first
00052       jj = lidx(j,j,n);
00053       sum = ajj = a[jj];
00054       for (k=0; k<j; ++k) {
00055         aii = a[lidx(j,k,n)];
00056         sum -= aii*aii;
00057       }
00058                         //  for other elements L[i,j] in jth column, i=j+1 to n
00059       if (sum <= -tol) {
00060          throw exception("ginverse2(): matrix is non-psd");
00061       }
00062       if (fabs(sum) < tol) {
00063          for (i=j; i<n; i++) { a[jj++]=0.0; }
00064       }
00065       else {
00066          a[jj]= ajj = std::sqrt(sum);
00067          for (i=j+1; i<n; i++) {
00068             sum = a[lidx(i,j,n)];
00069             for (k=0; k<j; ++k) {
00070                sum -= a[lidx(i,k,n)]*a[lidx(j,k,n)];
00071             }
00072             a[lidx(i,j,n)]=sum/ajj;
00073          }
00074       }
00075    }
00076 
00077    //////////////////////////////////////////
00078    //  column-by-column,  we are doing 
00079    //   inv(L) in place (lower triangl)
00080    //////////////////////////////////////////
00081    for (j=0; j<n; j++) {
00082       jj = lidx(j,j,n);
00083       if (a[jj] < tol) {
00084          for (i=j; i<n; i++) {
00085             a[jj++]=0.0;
00086          }         
00087       }
00088       else {
00089          a[jj] = 1./a[jj];
00090          for (i=j+1; i<n; i++) {
00091             aii = a[lidx(i,i,n)];
00092             jj++;
00093             if (aii <= -tol) {
00094                throw exception("ginverse2(): matrix is non-psd");
00095             }
00096             else if (fabs(aii) < tol) {
00097                a[jj]=0.0;
00098             }
00099             else {
00100                for (sum=0.0,k=j; k<i; ++k) sum -= a[lidx(i,k,n)]*a[lidx(k,j,n)];
00101                a[jj] = sum/aii;
00102             }      // end of if-else block
00103          }         // end of for-loop
00104       }            // end of if-else block
00105    }               // end of for-loop
00106 
00107    //////////////////////////////////////
00108    //  column-by-column, we are doing 
00109    //  inv(L')*inv(L)= inv(a) in place
00110    //////////////////////////////////////
00111    for (j=0; j<n; j++) {
00112      jj = lidx(j,j,n);
00113       for (i=j; i<n; i++) {
00114          ii = lidx(i,i,n);
00115          k = lidx(i,j,n);
00116          for (sum=0.0,t=i; t<n; ++t) sum += a[ii++]*a[k++];    
00117          a[jj++]=sum;
00118       }
00119    }
00120    return;
00121 }

void matvec::gs_ioc int *    ia,
int *    ja,
double *    a,
const double *    rhs,
double *    sol,
const int    n,
const double    relax,
const double    stopval,
const int    mxiter,
const double    tol,
unsigned &    cov
 

Definition at line 55 of file sparsematrix.cpp.

00086 {
00087    int j,row,col;
00088    double oldsol,newsol,local,diag, cmax,cval;
00089    Vector<double> adj_rhs(n);
00090    int niter = 0;
00091    cov = 0;
00092 
00093    std::cout << std::endl;
00094    do {                             // now iteration begins
00095       cmax = 0.0;
00096       for (row=1; row<=n; row++) adj_rhs[row-1] = rhs[row];
00097       for (row=1; row<=n; row++) {
00098          oldsol = sol[row];
00099          local = 0.0;
00100          diag = 0.0;             // subtract all the other solutions*count:
00101          for (j=ia[row]; j<=ia[row+1]-1; j++) {
00102             col = ja[j];
00103             if (col != row) {
00104                local += a[j]*sol[col];
00105             }
00106             else {
00107                diag = a[j];
00108             }
00109          }
00110          if (diag > tol) {
00111             newsol = (adj_rhs[row - 1] - local)/diag;   // new solution for GS
00112             cval = (newsol - oldsol)*relax;         // change vs old solution
00113             if (fabs(cval) > cmax) cmax=fabs(cval);
00114             newsol = sol[row] = oldsol + cval;      // new solution for SOR
00115             newsol = sol[row];
00116             for (j = ia[row]; j<=ia[row+1]-1; j++) {
00117                col = ja[j];
00118                adj_rhs[col - 1] -= a[j]*newsol;         // adjust the adj_rhs
00119             }
00120          }
00121          else if (diag > -tol) {
00122             throw exception(" zero-diagonal found in gs_ioc.c");
00123          }
00124          else {
00125             throw exception(" in gs_ioc(), matrix is not psd");
00126          }
00127       }
00128       niter += 1;
00129       if ( (niter % 10) ==0) {
00130          std::cout << " IOC: # of iter = " << niter << ", max_change = "
00131               << cmax << std::endl;
00132       }
00133    } while (niter <= mxiter && cmax > stopval);
00134    if (cmax <= stopval) cov=1;
00135 }

void matvec::gs_ioc int *    ia,
int *    ja,
BG   a,
const BG   rhs,
BG   sol,
const int    n,
const double    relax,
const double    stopval,
const int    mxiter,
const double    tol,
unsigned &    cov
 

Definition at line 55 of file sparsebgmatrix.cpp.

References matvec::BG::f.

Referenced by matvec::SparseMatrix::solve(), and matvec::SparseBGMatrix::solve().

00086 {
00087    int j,row,col;
00088    BG oldsol,newsol,local,diag, cmax,cval;
00089    Vector<BG> adj_rhs(n);
00090    int niter = 0;
00091    cov = 0;
00092 
00093    std::cout << std::endl;
00094    do {                             // now iteration begins
00095       cmax = 0.0;
00096       for (row=1; row<=n; row++) adj_rhs[row-1] = rhs[row];
00097       for (row=1; row<=n; row++) {
00098          oldsol = sol[row];
00099          local = 0.0;
00100          diag = 0.0;             // subtract all the other solutions*count:
00101          for (j=ia[row]; j<=ia[row+1]-1; j++) {
00102             col = ja[j];
00103             if (col != row) {
00104                local += a[j]*sol[col];
00105             }
00106             else {
00107                diag = a[j];
00108             }
00109          }
00110          if (diag > tol) {
00111             newsol = (adj_rhs[row - 1] - local)/diag;   // new solution for GS
00112             cval = (newsol - oldsol)*relax;         // change vs old solution
00113             if (fabs(cval) > cmax) cmax=fabs(cval);
00114             newsol = sol[row] = oldsol + cval;      // new solution for SOR
00115             newsol = sol[row];
00116             for (j = ia[row]; j<=ia[row+1]-1; j++) {
00117                col = ja[j];
00118                adj_rhs[col - 1] -= a[j]*newsol;         // adjust the adj_rhs
00119             }
00120          }
00121          else if (diag > -tol) {
00122             throw exception(" zero-diagonal found in gs_ioc.c");
00123          }
00124          else {
00125             throw exception(" in gs_ioc(), matrix is not psd");
00126          }
00127       }
00128       niter += 1;
00129       if ( (niter % 10) ==0) {
00130          std::cout << " IOC: # of iter = " << niter << ", max_change = "
00131               << cmax.f << std::endl;
00132       }
00133    } while (niter <= mxiter && cmax > stopval);
00134    if (cmax <= stopval) cov=1;
00135 }

template<class T>
Matrix<T> hadjoin const Vector< T > &    a,
const Matrix< T > &    b
[inline]
 

Definition at line 1048 of file matrix.h.

References matvec::Matrix< T >::num_cols(), matvec::Matrix< T >::num_rows(), and matvec::Vector< T >::size().

01049 {
01050    if (a.size() != b.num_rows()) throw exception("Matrix<T>::hadjoin(): size not conformable");
01051    Matrix<T> temp(a.size(),1 + b.num_cols());
01052    for (int i=0; i<a.size(); ++i) {
01053       temp[i][0] = a[i];
01054       memcpy(&(temp[i][1]),b[i],sizeof(T)*b.num_cols());
01055    }
01056    return temp;
01057 }

template<class T>
Matrix<T> hadjoin const Matrix< T > &    a,
const Vector< T > &    b
[inline]
 

Definition at line 1037 of file matrix.h.

References matvec::Matrix< T >::num_cols(), matvec::Matrix< T >::num_rows(), and matvec::Vector< T >::size().

01038 {
01039    if (a.num_rows() != b.size()) throw exception("Matrix<T>::hadjoin(): size not conformable");
01040    Matrix<T> temp(a.num_rows(),a.num_cols() + 1);
01041    for (int i=0; i<a.num_rows(); ++i) {
01042       memcpy(temp[i],a[i],sizeof(T)*a.num_cols());
01043       temp[i][a.num_cols()] = b[i];
01044    }
01045    return temp;
01046 }

template<class T>
Matrix<T> hadjoin const Matrix< T > &    a,
const Matrix< T > &    b
[inline]
 

Definition at line 1026 of file matrix.h.

References matvec::Matrix< T >::num_cols(), and matvec::Matrix< T >::num_rows().

Referenced by matvec::GLMM::AI_REML().

01027 {
01028    if (a.num_rows() != b.num_rows()) throw exception("Matrix<T>::hadjoin(): size not conformable");
01029    Matrix<T> temp(a.num_rows(),a.num_cols() + b.num_cols());
01030    for (int i=0; i<a.num_rows(); ++i) {
01031       memcpy(temp[i],a[i],sizeof(T)*a.num_cols());
01032       memcpy(&(temp[i][a.num_cols()]),b[i],sizeof(T)*b.num_cols());
01033    }
01034    return temp;
01035 }

void matvec::hsort_ija unsigned    n,
int *    ia,
int *    ja,
BG   a
[static]
 

this is a Heap sort, sorting (ia,ja) ascendingly.

ia(1...n), ja(1...n) ,a(1..n) where n is the actual number of non-zeros in A

Definition at line 1231 of file sparsebgmatrix.cpp.

01232 {
01233    unsigned iia, jja,i,j,l,ir;
01234    BG aa;
01235    if (n == 1) return;
01236    l = (n >> 1)+1;              /* l = n/2+1; */
01237    ir=n;
01238    for (;;) {
01239       if (l > 1) {
01240          l = l-1;  iia = ia[l];  jja = ja[l];  aa = a[l];
01241 
01242       }
01243       else {
01244          iia = ia[ir];  jja = ja[ir];  aa = a[ir];
01245          ia[ir] = ia[1]; ja[ir] = ja[1];  a[ir] = a[1];
01246          ir--;
01247          if(ir == 1) {
01248             ia[1] = iia;  ja[1] = jja;  a[1] = aa;
01249             return;
01250          }
01251       }
01252       i=l;
01253       j=l+l;
01254       while ( j <= ir) {
01255          if (j < ir) {
01256             if (ia[j]<ia[j+1]  || (ia[j]==ia[j+1] && ja[j] < ja[j+1])) j++;
01257          }
01258          if (iia < ia[j] || (iia == ia[j] && jja < ja[j])) {
01259             ia[i] = ia[j];  ja[i] = ja[j];  a[i] = a[j];
01260             i=j;
01261             j=j+j;
01262          }
01263          else {
01264             j=ir+1;
01265          }
01266       }
01267       ia[i] = iia;  ja[i] = jja;  a[i] = aa;
01268    }
01269 }

void hsort_ija unsigned    n,
int *    ia,
int *    ja,
double *    a
[static]
 

this is a Heap sort, sorting (ia,ja) ascendingly.

ia(1...n), ja(1...n) ,a(1..n) where n is the actual number of non-zeros in A

Definition at line 1642 of file glmm2.cpp.

Referenced by matvec::GLMM::build_hInv(), matvec::SparseMatrix::close(), and matvec::SparseBGMatrix::close().

01648  {
01649     unsigned iia, jja,i,j,l,ir;
01650     double aa;
01651     if (n == 1) return;
01652     l = (n >> 1)+1;             /* l = n/2+1; */
01653     ir=n;
01654     for (;;) {
01655        if (l > 1) {
01656           l = l-1;  iia = ia[l];  jja = ja[l];  aa = a[l];
01657 
01658        }
01659        else {
01660           iia = ia[ir];  jja = ja[ir];  aa = a[ir];
01661           ia[ir] = ia[1]; ja[ir] = ja[1];  a[ir] = a[1];
01662           ir--;
01663           if(ir == 1) {
01664              ia[1] = iia;  ja[1] = jja;  a[1] = aa;
01665              return;
01666           }
01667        }
01668        i=l;
01669        j=l+l;
01670        while ( j <= ir) {
01671           if (j < ir) {
01672              if (ia[j]<ia[j+1]  || (ia[j]==ia[j+1] && ja[j] < ja[j+1])) j++;
01673           }
01674           if (iia < ia[j] || (iia == ia[j] && jja < ja[j])) {
01675              ia[i] = ia[j];  ja[i] = ja[j];  a[i] = a[j];
01676              i=j;
01677              j=j+j;
01678           }
01679           else {
01680              j=ir+1;
01681           }
01682        }
01683        ia[i] = iia;  ja[i] = jja;  a[i] = aa;
01684     }
01685  }

long matvec::ignbin const long    n,
const double    pp
 

Definition at line 653 of file stat2.cpp.

References ignbin(), min, and ranf().

Referenced by ignbin(), matvec::BinomialDist::sample(), and matvec::Pedigree::sample().

00671                                   :
00672 //         Kachitvichyanukul, V. and Schmeiser, B. W.
00673 //         Binomial Random Variate Generation.
00674 //         Communications of the ACM, 31, 2
00675 //         (February, 1988) 216.
00676 //
00677 //     SUBROUTINE BTPEC(N,PP,ISEED,JX)
00678 //     BINOMIAL RANDOM VARIATE GENERATOR
00679 //     MEAN .LT. 30 -- INVERSE CDF
00680 //       MEAN .GE. 30 -- ALGORITHM BTPE:  ACCEPTANCE-REJECTION VIA
00681 //       FOUR REGION COMPOSITION.  THE FOUR REGIONS ARE A TRIANGLE
00682 //       (SYMMETRIC IN THE CENTER), A PAIR OF PARALLELOGRAMS (ABOVE
00683 //       THE TRIANGLE), AND EXPONENTIAL LEFT AND RIGHT TAILS.
00684 //     BTPE REFERS TO BINOMIAL-TRIANGLE-PARALLELOGRAM-EXPONENTIAL.
00685 //     BTPEC REFERS TO BTPE AND "COMBINED."  THUS BTPE IS THE
00686 //       RESEARCH AND BTPEC IS THE IMPLEMENTATION OF A COMPLETE
00687 //       USABLE ALGORITHM.
00688 //     REFERENCE:  VORATAS KACHITVICHYANUKUL AND BRUCE SCHMEISER,
00689 //       "BINOMIAL RANDOM VARIATE GENERATION,"
00690 //       COMMUNICATIONS OF THE ACM, FORTHCOMING
00691 //     WRITTEN:  SEPTEMBER 1980.
00692 //       LAST REVISED:  MAY 1985, JULY 1987
00693 //     REQUIRED SUBPROGRAM:  RAND() -- A UNIFORM (0,1) RANDOM NUMBER
00694 //                           GENERATOR
00695 //     ARGUMENTS
00696 //       N : NUMBER OF BERNOULLI TRIALS            (INPUT)
00697 //       PP : PROBABILITY OF SUCCESS IN EACH TRIAL (INPUT)
00698 //       ISEED:  RANDOM NUMBER SEED                (INPUT AND OUTPUT)
00699 //       JX:  RANDOMLY GENERATED OBSERVATION       (OUTPUT)
00700 //     VARIABLES
00701 //      PSAVE: VALUE OF PP FROM THE LAST CALL TO BTPEC
00702 //       NSAVE: VALUE OF N FROM THE LAST CALL TO BTPEC
00703 //       XNP:  VALUE OF THE MEAN FROM THE LAST CALL TO BTPEC
00704 //       P: PROBABILITY USED IN THE GENERATION PHASE OF BTPEC
00705 //       FFM: TEMPORARY VARIABLE EQUAL TO XNP + P
00706 //       M:  INTEGER VALUE OF THE CURRENT MODE
00707 //       FM:  FLOATING POINT VALUE OF THE CURRENT MODE
00708 //       XNPQ: TEMPORARY VARIABLE USED IN SETUP AND SQUEEZING STEPS
00709 //       P1:  AREA OF THE TRIANGLE
00710 //       C:  HEIGHT OF THE PARALLELOGRAMS
00711 //       XM:  CENTER OF THE TRIANGLE
00712 //       XL:  LEFT END OF THE TRIANGLE
00713 //       XR:  RIGHT END OF THE TRIANGLE
00714 //       AL:  TEMPORARY VARIABLE
00715 //       XLL:  RATE FOR THE LEFT EXPONENTIAL TAIL
00716 //       XLR:  RATE FOR THE RIGHT EXPONENTIAL TAIL
00717 //       P2:  AREA OF THE PARALLELOGRAMS
00718 //       P3:  AREA OF THE LEFT EXPONENTIAL TAIL
00719 //       P4:  AREA OF THE RIGHT EXPONENTIAL TAIL
00720 //       U:  A U(0,P4) RANDOM VARIATE USED FIRST TO SELECT ONE OF THE
00721 //           FOUR REGIONS AND THEN CONDITIONALLY TO GENERATE A VALUE
00722 //           FROM THE REGION
00723 //       V:  A U(0,1) RANDOM NUMBER USED TO GENERATE THE RANDOM VALUE
00724 //           (REGION 1) OR TRANSFORMED INTO THE VARIATE TO ACCEPT OR
00725 //           REJECT THE CANDIDATE VALUE
00726 //       IX:  INTEGER CANDIDATE VALUE
00727 //       X:  PRELIMINARY CONTINUOUS CANDIDATE VALUE IN REGION 2 LOGIC
00728 //           AND A FLOATING POINT IX IN THE ACCEPT/REJECT LOGIC
00729 //       K:  ABSOLUTE VALUE OF (IX-M)
00730 //       F:  THE HEIGHT OF THE SCALED DENSITY FUNCTION USED IN THE
00731 //           ACCEPT/REJECT DECISION WHEN BOTH M AND IX ARE SMALL
00732 //           ALSO USED IN THE INVERSE TRANSFORMATION
00733 //       R: THE RATIO P/Q
00734 //      G: CONSTANT USED IN CALCULATION OF PROBABILITY
00735 //       MP:  MODE PLUS ONE, THE LOWER INDEX FOR EXPLICIT CALCULATION
00736 //            OF F WHEN IX IS GREATER THAN M
00737 //       IX1:  CANDIDATE VALUE PLUS ONE, THE LOWER INDEX FOR EXPLICIT
00738 //             CALCULATION OF F WHEN IX IS LESS THAN M
00739 //       I:  INDEX FOR EXPLICIT CALCULATION OF F FOR BTPE
00740 //       AMAXP: MAXIMUM ERROR OF THE LOGARITHM OF NORMAL BOUND
00741 //       YNORM: LOGARITHM OF NORMAL BOUND
00742 //       ALV:  NATURAL LOGARITHM OF THE ACCEPT/REJECT VARIATE V
00743 //       X1,F1,Z,W,Z2,X2,F2, AND W2 ARE TEMPORARY VARIABLES TO BE
00744 //       USED IN THE FINAL ACCEPT/REJECT TEST
00745 //       QN: PROBABILITY OF NO SUCCESS IN N TRIALS
00746 //     REMARK
00747 //       IX AND JX COULD LOGICALLY BE THE SAME VARIABLE, WHICH WOULD
00748 //       SAVE A MEMORY POSITION AND A LINE OF CODE.  HOWEVER, SOME
00749 //       COMPILERS (E.G.,CDC MNF) OPTIMIZE BETTER WHEN THE ARGUMENTS
00750 //       ARE NOT INVOLVED.
00751 //     ISEED NEEDS TO BE DOUBLE PRECISION IF THE IMSL ROUTINE
00752 //     GGUBFS IS USED TO GENERATE UNIFORM RANDOM NUMBER, OTHERWISE
00753 //     TYPE OF ISEED SHOULD BE DICTATED BY THE UNIFORM GENERATOR
00754 //    *DETERMINE APPROPRIATE ALGORITHM AND WHETHER SETUP IS NECESSARY*
00755 //************************************************************************
00756 {
00757   if (pp < 0.0 || pp>1.0) throw exception("ignbin(): bad arg2, value between (0,1) expected");
00758    static double psave = -1.0;
00759    static long nsave = -1;
00760    static long ignbin,i,ix,ix1,k,m,mp,T1;
00761    static double al,alv,amaxp,c,f,f1,f2,ffm,fm,g,p,p1,p2,p3,p4,q,qn,r;
00762    static double u,v,w,w2,x,x1,x2,xl,xll,xlr,xm,xnp,xnpq,xr,ynorm,z,z2;
00763 
00764    if(pp != psave) goto S10;
00765    if(n != nsave) goto S20;
00766    if(xnp < 30.0) goto S150;
00767    goto S30;
00768 S10:  /////// SETUP, PERFORM ONLY WHEN PARAMETERS CHANGE //////
00769 
00770    psave = pp;
00771    p = std::min(psave,1.0-psave);
00772    q = 1.0-p;
00773 S20:
00774    xnp = n*p;
00775    nsave = n;
00776    if(xnp < 30.0) goto S140;
00777    ffm = xnp+p;
00778    m = long(ffm);
00779    fm = m;
00780    xnpq = xnp*q;
00781    p1 = (long) (2.195*sqrt(xnpq)-4.6*q)+0.5;
00782    xm = fm+0.5;
00783    xl = xm-p1;
00784    xr = xm+p1;
00785    c = 0.134+20.5/(15.3+fm);
00786    al = (ffm-xl)/(ffm-xl*p);
00787    xll = al*(1.0+0.5*al);
00788    al = (xr-ffm)/(xr*q);
00789    xlr = al*(1.0+0.5*al);
00790    p2 = p1*(1.0+c+c);
00791    p3 = p2+c/xll;
00792    p4 = p3+c/xlr;
00793 S30:   ////////  GENERATE VARIATE  ////////
00794 
00795    u = ranf()*p4;
00796    v = ranf();
00797 
00798    ///////  TRIANGULAR REGION  ///////
00799 
00800    if(u > p1) goto S40;
00801    ix = long(xm-p1*v+u);
00802    goto S170;
00803 S40:     /////  PARALLELOGRAM REGION  /////
00804    if(u > p2) goto S50;
00805    x = xl+(u-p1)/c;
00806    v = v*c+1.0-fabs(xm-x)/p1;
00807    if(v > 1.0 || v <= 0.0) goto S30;
00808    ix = long(x);
00809    goto S70;
00810 S50:       //////  LEFT TAIL  ///////
00811    if(u > p3) goto S60;
00812    ix = long(xl+log(v)/xll);
00813    if(ix < 0) goto S30;
00814    v *= ((u-p2)*xll);
00815    goto S70;
00816 S60:     ////  RIGHT TAIL  ////
00817    ix = long(xr-log(v)/xlr);
00818    if(ix > n) goto S30;
00819    v *= ((u-p3)*xlr);
00820 S70:   //// DETERMINE APPROPRIATE WAY TO PERFORM ACCEPT/REJECT TEST  ////
00821    k = abs((int)(ix-m));
00822    if(k > 20 && k < xnpq/2-1) goto S130;
00823 
00824      ///////  EXPLICIT EVALUATION   //////
00825 
00826    f = 1.0;
00827    r = p/q;
00828    g = (n+1)*r;
00829    T1 = m-ix;
00830    if(T1 < 0) {
00831       goto S80;
00832    }
00833    else if(T1 == 0) {
00834       goto S120;
00835    }
00836    else {
00837       goto S100;
00838    }
00839 S80:
00840    mp = m+1;
00841    for(i=mp; i<=ix; i++) f *= (g/i-r);
00842    goto S120;
00843 S100:
00844    ix1 = ix+1;
00845    for(i=ix1; i<=m; i++) f /= (g/i-r);
00846 S120:
00847    if(v <= f) goto S170;
00848    goto S30;
00849 S130:   /////   SQUEEZING USING UPPER AND LOWER BOUNDS ON ALOG(F(X))  ////
00850    amaxp = k/xnpq*((k*(k/3.0+0.625)+0.1666666666666)/xnpq+0.5);
00851    ynorm = -(k*k/(2.0*xnpq));
00852    alv = log(v);
00853    if(alv < ynorm-amaxp) goto S170;
00854    if(alv > ynorm+amaxp) goto S30;
00855     ///////////////////////////////////////////////////
00856     //  STIRLING'S FORMULA TO MACHINE ACCURACY FOR
00857     // THE FINAL ACCEPTANCE/REJECTION TEST
00858     ////////////////////////////////////////////////////
00859    x1 = ix+1.0;
00860    f1 = fm+1.0;
00861    z = n+1.0-fm;
00862    w = n-ix+1.0;
00863    z2 = z*z;
00864    x2 = x1*x1;
00865    f2 = f1*f1;
00866    w2 = w*w;
00867    if (alv <= xm*log(f1/x1)+(n-m+0.5)*log(z/w)+(ix-m)*log(w*p/(x1*q))+(13860.0-
00868      (462.0-(132.0-(99.0-140.0/f2)/f2)/f2)/f2)/f1/166320.0+(13860.0-(462.0-
00869       (132.0-(99.0-140.0/z2)/z2)/z2)/z2)/z/166320.0+(13860.0-(462.0-(132.0-
00870       (99.0-140.0/x2)/x2)/x2)/x2)/x1/166320.0+(13860.0-(462.0-(132.0-(99.0
00871       -140.0/w2)/w2)/w2)/w2)/w/166320.0) goto S170;
00872    goto S30;
00873 S140:   ////  INVERSE CDF LOGIC FOR MEAN LESS THAN 30 ////
00874    qn = pow(q,(double)n);
00875    r = p/q;
00876    g = r*(n+1);
00877 S150:
00878    ix = 0;
00879    f = qn;
00880    u = ranf();
00881 S160:
00882    if(u < f) goto S170;
00883    if(ix > 110) goto S150;
00884    u -= f;
00885    ix += 1;
00886    f *= (g/ix-r);
00887    goto S160;
00888 S170:
00889    if(psave > 0.5) ix = n-ix;
00890    ignbin = ix;
00891    return ignbin;
00892 }

long matvec::ignpoi const double    mu
 

Definition at line 894 of file stat2.cpp.

References ignpoi(), min, and ranf().

Referenced by ignpoi(), and matvec::PoissonDist::sample().

00895 {
00896    /////////////////////////////////////////////////////////////////////////
00897    // all labels stattements have been replaced with non-labeled statements
00898    // T Wang, 4/27/94
00899    /////////////////////////////////////////////////////////////////////////
00900    static double a0 = -0.5;
00901    static double a1 = 0.3333333;
00902    static double a2 = -0.2500068;
00903    static double a3 = 0.2000118;
00904    static double a4 = -0.1661269;
00905    static double a5 = 0.1421878;
00906    static double a6 = -0.1384794;
00907    static double a7 = 0.125006;
00908    static double muold = 0.0;
00909    static double muprev = 0.0;
00910    static double fact[10] = {
00911     1.0,1.0,2.0,6.0,24.0,120.0,720.0,5040.0,40320.0,362880.0
00912    };
00913    static long ignpoi,j,k,kflag,l,m;
00914    static double b1,b2,c,c0,c1,c2,c3,d,del,difmuk,e,fk,fx,fy,g;
00915    static double omega,p,p0,px,py,q,s,t,u,v,x,xx,pp[35];
00916    int wflag;    // added by T. Wang
00917 
00918    if (mu != muprev) {
00919       if (mu < 10.0) {
00920          //   C A S E  B. (START NEW TABLE AND CALCULATE P0 IF NECESSARY)
00921          muprev = 0.0;
00922          if (mu != muold) {
00923             muold = mu;
00924             m = std::max(1L,(long) (mu));
00925             l = 0;
00926             p = exp(-mu);
00927             q = p0 = p;
00928          }
00929          //  STEP U. UNIFORM SAMPLE FOR INVERSION METHOD
00930          for (;;) {
00931             u = ranf();
00932             ignpoi = 0;
00933             if (u <= p0) return ignpoi;
00934             //////////////////////////////////////////////////////////
00935             // STEP T. TABLE COMPARISON UNTIL THE END PP(L) OF THE
00936             //         PP-TABLE OF CUMULATIVE POISSON PROBABILITIES
00937             //         (0.458=PP(9) FOR MU=10)
00938             //////////////////////////////////////////////////////////
00939             if (l == 0) {
00940                ////////////////////////////////////////////////////////
00941                //  STEP C. CREATION OF NEW POISSON PROBABILITIES P
00942                //          AND THEIR CUMULATIVES Q=PP(K)
00943                /////////////////////////////////////////////////////////
00944                l += 1;
00945                for (k=l; k<=35; k++) {
00946                   p = p*mu/(double)k;
00947                   q += p;
00948                   *(pp+k-1) = q;
00949                   if(u <= q) {l = ignpoi = k; return ignpoi;}
00950                }
00951                l = 35;
00952             }
00953             else {
00954                j = 1;
00955                if (u > 0.458) j = std::min(l,m);
00956                for (k=j; k<=l; k++) {
00957                   if (u <= *(pp+k-1)) {ignpoi = k; return ignpoi;}
00958                }
00959                if (l != 35) {
00960                   l += 1;
00961                   for (k=l; k<=35; k++) {
00962                      p = p*mu/(double)k;
00963                      q += p;
00964                      *(pp+k-1) = q;
00965                      if(u <= q) {l = ignpoi = k; return ignpoi;}
00966                   }
00967                   l = 35;
00968                }
00969             }
00970          }
00971       }
00972       else {
00973          ////  C A S E  A. (RECALCULATION OF S,D,L IF MU HAS CHANGED) ////
00974          muprev = mu;
00975          s = sqrt(mu);
00976          d = 6.0*mu*mu;
00977           /////////////////////////////////////////////////////////////
00978           //   THE POISSON PROBABILITIES PK EXCEED THE DISCRETE NORMAL
00979          //      PROBABILITIES FK WHENEVER K >= M(MU). L=IFIX(MU-1.1484)
00980          //      IS AN UPPER BOUND TO M(MU) FOR ALL MU >= 10 .
00981          ///////////////////////////////////////////////////////////////
00982          l = (long) (mu-1.1484);
00983       }
00984    }
00985    ////   STEP N. NORMAL SAMPLE - SNORM(IR) FOR STANDARD NORMAL DEVIATE ///
00986    g = mu+s*snorm();
00987    if (g >= 0.0) {
00988       ignpoi = (long) (g);
00989           //  STEP I. IMMEDIATE ACCEPTANCE IF IGNPOI IS LARGE ENOUGH  //
00990       if (ignpoi >= l) return ignpoi;
00991           // STEP S. SQUEEZE ACCEPTANCE - SUNIF(IR) FOR (0,1)-SAMPLE U  //
00992       fk = (double)ignpoi;
00993       difmuk = mu-fk;
00994       u = ranf();
00995       if (d*u >= difmuk*difmuk*difmuk) return ignpoi;
00996    }
00997    /////////////////////////////////////////////////////////////////////
00998    //  STEP P. PREPARATIONS FOR STEPS Q AND H.
00999    //          (RECALCULATIONS OF PARAMETERS IF NECESSARY)
01000    //          .3989423=(2*PI)**(-.5)  .416667E-1=1./24.  .1428571=1./7.
01001    //          THE QUANTITIES B1, B2, C3, C2, C1, C0 ARE FOR THE HERMITE
01002    //          APPROXIMATIONS TO THE DISCRETE NORMAL PROBABILITIES FK.
01003    //          C=.1069/MU GUARANTEES MAJORIZATION BY THE 'HAT'-FUNCTION.
01004    ///////////////////////////////////////////////////////////////////////
01005    if (mu != muold) {
01006       muold = mu;
01007       omega = 0.3989423/s;
01008       b1 = 4.166667E-2/mu;
01009       b2 = 0.3*b1*b1;
01010       c3 = 0.1428571*b1*b2;
01011       c2 = b2-15.0*c3;
01012       c1 = b1-6.0*b2+45.0*c3;
01013       c0 = 1.0-b1+3.0*b2-15.0*c3;
01014       c = 0.1069/mu;
01015    }
01016    wflag = -1;
01017    if (g >= 0.0) {  // 'SUBROUTINE' F IS CALLED (KFLAG=0 FOR CORRECT RETURN)
01018       kflag = 0;
01019       wflag = 0;
01020    }
01021    else {
01022       wflag = 1;
01023    }
01024 
01025    for (;;) {          //  STEP Q. QUOTIENT ACCEPTANCE (RARE CASE)
01026       if ( wflag > 0) {
01027          do {
01028             ////////////////////////////////////////////////////////////////
01029             //  STEP E. EXPONENTIAL SAMPLE - SEXPO(IR) FOR STANDARD EXPONENTIAL
01030             //          DEVIATE E AND SAMPLE T FROM THE LAPLACE 'HAT'
01031             //          (IF T <= -.6744 THEN PK < FK FOR ALL MU >= 10.)
01032             ////////////////////////////////////////////////////////////////
01033             e = sexpo();
01034             u = ranf();
01035             u += (u-1.0);
01036             t = 1.8+fsign(e,u);
01037          } while (t <= -0.6744);
01038          ignpoi = (long) (mu+s*t);
01039          fk = (double)ignpoi;
01040          difmuk = mu-fk;
01041          //  'SUBROUTINE' F IS CALLED (KFLAG=1 FOR CORRECT RETURN)
01042          kflag = 1;
01043       }
01044       else {
01045          wflag = 1;
01046       }
01047       for (;;) {
01048          if (ignpoi < 10) {
01049             px = -mu;
01050             py = pow(mu,(double)ignpoi)/ *(fact+ignpoi);
01051             x = (0.5-difmuk)/s;
01052             xx = x*x;
01053             fx = -0.5*xx;
01054             fy = omega*(((c3*xx+c2)*xx+c1)*xx+c0);
01055             if (kflag <= 0) {
01056                if(fy-u*fy <= py*exp(px-fx)) return ignpoi;
01057                break;
01058             }
01059          }
01060          else {
01061             ////////////////////////////////////////////////////////////////
01062             //        CASE IGNPOI .GE. 10 USES POLYNOMIAL APPROXIMATION
01063             //        A0-A7 FOR ACCURACY WHEN ADVISABLE
01064             //        .8333333E-1=1./12.  .3989423=(2*PI)**(-.5)
01065             ////////////////////////////////////////////////////////////////
01066             del = 8.333333E-2/fk;
01067             del -= (4.8*del*del*del);
01068             v = difmuk/fk;
01069             if (fabs(v) > 0.25) {
01070                px = fk*log(1.0+v)-difmuk-del;
01071             }
01072             else {
01073                px =
01074                 fk*v*v*(((((((a7*v+a6)*v+a5)*v+a4)*v+a3)*v+a2)*v+a1)*v+a0)-del;
01075             }
01076             py = 0.3989423/sqrt(fk);
01077              x = (0.5-difmuk)/s;
01078             xx = x*x;
01079             fx = -0.5*xx;
01080             fy = omega*(((c3*xx+c2)*xx+c1)*xx+c0);
01081             if (kflag <= 0) {
01082                if(fy-u*fy <= py*exp(px-fx)) return ignpoi;
01083                break;
01084             }
01085          }
01086                // STEP H. HAT ACCEPTANCE (E IS REPEATED ON REJECTION)
01087          if  (c*fabs(u) > py*exp(px+e)-fy*exp(fx+e)) {break;}
01088          else                                        {return ignpoi;}
01089       }
01090    }
01091 }

long matvec::ignuin const long    low,
const long    high
 

Definition at line 1093 of file stat2.cpp.

References ignuin().

Referenced by ignuin(), and matvec::DiscreteUniformDist::sample().

01109 {
01110 #define maxnum 2147483647L
01111    if (low > high) throw exception(" ignu(low,high): low > hig");
01112 //    long random();
01113    static long ignuin,ign,maxnow,range,ranp1;
01114    range = high-low;
01115    if (range > maxnum) throw exception(" ignuin(low,high): high too large");
01116    if (low == high) {
01117       ignuin = low;
01118       return ignuin;
01119    }
01120    ////////////////////////////////////////////////////////////////////
01121    //  Number to be generated should be in range 0..RANGE
01122    //  Set MAXNOW so that the number of integers in 0..MAXNOW is an
01123    //  integral multiple of the number in 0..RANGE
01124    ////////////////////////////////////////////////////////////////////
01125    ranp1 = range+1;
01126    maxnow = maxnum/ranp1*ranp1;
01127 S40:   //   ign = ignlgi()-1;
01128    ign = rand();
01129    if(ign > maxnow) goto S40;
01130    ignuin = low+ign%ranp1;
01131    return ignuin;
01132 #undef maxnum
01133 }

void initial_BGarray BG   v,
unsigned    n,
double    a
[inline]
 

Definition at line 121 of file bg.h.

Referenced by matvec::SparseBGMatrix::mv(), matvec::SparseBGMatrix::resize(), matvec::SparseBGMatrix::solve(), matvec::SparseBGMatrix::solvrow(), matvec::SparseBGMatrix::SparseBGMatrix(), and sweep().

00121                                                            {
00122   for (unsigned i = 0; i<n; i++) {
00123      v[i] = a;
00124   }
00125 }

int matvec::inva22 Matrix< double > &    a22 [static]
 

Definition at line 638 of file pop_mblup.cpp.

References matvec::Session::epsilon, and SESSION.

Referenced by matvec::Population::add_Gv_inv(), and matvec::Population::relv_inv().

00639 {
00640    double tem,det;
00641    det = (a22[0][0]*a22[1][1] - a22[0][1]*a22[1][0]);
00642    if (fabs(det) < SESSION.epsilon) return 0;
00643    det = 1.0/det;
00644    tem=a22[0][0];
00645    a22[0][0] = det*a22[1][1];
00646    a22[1][1] = det*tem;
00647    a22[0][1] *= -det;
00648    a22[1][0] *= -det;
00649    return 1;
00650 }

doubleMatrix * matvec::KP Data   D,
doubleMatrix   SKM,
const std::string &    lpl,
const std::string &    censor
 

Definition at line 832 of file glmm2.cpp.

References matvec::Data::datasheet, matvec::Data::field_index(), matvec::Data::num_rows(), matvec::Matrix< double >::resize(), matvec::Matrix< double >::sortby(), matvec::Matrix< double >::submat(), and warning().

00832                                                                                               {
00833 
00834    // 0=censored 1=uncensored;
00835    doubleMatrix ret,*retpt;
00836    doubleMatrix SKM;
00837    int ny;
00838    ny=D->num_rows();
00839    doubleMatrix lpl_mat;
00840    lpl_mat.resize(ny,2);
00841    int lpl_col;
00842    if(-1 ==(lpl_col=D->field_index(lpl))) {
00843      warning("KP: Can't find lpl %s \n",lpl.c_str());
00844      return(NULL);
00845    }
00846    for(int i=0;i<ny;i++){
00847      lpl_mat[i][0]=D->datasheet[lpl_col][i].double_val();
00848    }
00849    int status_col;
00850    if(-1 ==(status_col=D->field_index(censor))) {
00851      warning("KP: Can't find status %s \n",censor.c_str());
00852      return(NULL);
00853    };
00854    for(int i=0;i<ny;i++){
00855      lpl_mat[i][1]=D->datasheet[status_col][i].double_val();
00856      // std::cout << i << " " << lpl_mat[i][0] << " " << lpl_mat[i][1] << endl;
00857    }
00858    lpl_mat.sortby(Matrix<double>::COLUMN,0);
00859    // std::cout << "\n  Input doubleMatrix col 1 = time  col 2: 0=censored 1=uncensored " << lpl_mat;
00860    int j;
00861    float numdead;
00862    float numcensored;
00863    float lagdead;
00864    float lagcensored;
00865    doubleMatrix skm_dead;
00866    skm_dead.resize(ny,3);
00867    j=0;
00868    lagdead=0.;
00869    lagcensored=0.;
00870    for(int i=0;i<ny;i++){
00871      if(i < ny-1 && lpl_mat[i][0] == lpl_mat[i+1][0]){
00872        if(lpl_mat[i][1]==1){
00873         lagdead++;
00874        }
00875        if(lpl_mat[i][1]==0){
00876         lagcensored++;
00877        }
00878      }
00879      else{
00880        if(lpl_mat[i][1]==1){
00881         numdead=1.+lagdead;
00882         numcensored=lagcensored;
00883         lagdead=0.;
00884         lagcensored=0.;
00885         skm_dead[j][0]=lpl_mat[i][0];
00886         skm_dead[j][1]=numdead;
00887         skm_dead[j][2]=numcensored;
00888         j++;
00889        }
00890        if(lpl_mat[i][1]==0.){
00891         numdead=lagdead;
00892         numcensored=1.+lagcensored;
00893         lagdead=0.;
00894         lagcensored=0.;
00895         skm_dead[j][0]=lpl_mat[i][0];
00896         skm_dead[j][1]=numdead;
00897         skm_dead[j][2]=numcensored;
00898         j++;
00899        }
00900      }
00901    }
00902    //std::cout << "\n SKM dead vector = " << skm_dead;
00903    skm_dead=skm_dead.submat(0,0,j,3);
00904    //std::cout << "\n SKM dead vector = " << skm_dead;
00905 
00906 
00907    //doubleMatrix SKM;
00908    SKM.resize(j+1,3);
00909    float tot_alive;
00910    tot_alive=ny;
00911    int k;
00912    k=0;
00913    for(int i=0;i<j+1;i++){
00914      if(i == 0){
00915        SKM[k][0]=0.;
00916        SKM[k][1]=1.;
00917        k++;
00918      }
00919      if(i > 0 && skm_dead[i-1][1] != 0.){
00920        SKM[k][0]=skm_dead[i-1][0];
00921        SKM[k][1]=SKM[k-1][1]*(1.-(skm_dead[i-1][1]/tot_alive));
00922        SKM[k-1][2]=SKM[k][1]/SKM[k-1][1];
00923        SKM[k-1][2]/=SKM[k][0]-SKM[k-1][0];
00924        tot_alive=tot_alive-(skm_dead[i-1][1]+skm_dead[i-1][2]);
00925        k++;
00926      }
00927      if(i > 0 && skm_dead[i-1][1] == 0.){
00928        tot_alive=tot_alive-(skm_dead[i-1][1]+skm_dead[i-1][2]);
00929      }
00930    }
00931    SKM[k-1][2]=SKM[k-2][2];
00932    //for(int ii=0;ii<k;ii++) cout << SKM[ii][0] << " " << SKM[ii][1] << " " << SKM[ii][2] <<endl;
00933    SKM_ans=SKM.submat(0,0,k,3);
00934    return(&SKM_ans);
00935 
00936 
00937  }

unsigned lidx const unsigned    i,
const unsigned    j,
const unsigned    n
[static]
 

Definition at line 33 of file model_blup.cpp.

Referenced by matvec::Model::add_Ag_diag(), matvec::Model::add_Ig_diag(), matvec::Model::compute_rhs_diag_mme(), ginverse2(), and preconditioning().

00036 {
00037    return ((n+n-j-1)*j+i+i)/2;
00038 }

void matvec::linmin double *    p,
double *    xi,
int    n,
double &    fret
[static]
 

Definition at line 124 of file min_nr_powell.cpp.

References brent(), f1dim(), mnbrak(), ncom, pcom, TOL, and xicom.

Referenced by nr_powell().

00125 {
00126    int j;
00127    double xx,xmin,fx,fb,fa,bx,ax;
00128 
00129    ncom = n;
00130    
00131    if(n>0){
00132      pcom = new double [n];
00133    }
00134    else {
00135      pcom = 0;
00136    }
00137    if(n>0){
00138      xicom = new double [n];
00139    }
00140    else {
00141      xicom = 0;
00142    }
00143    for (j=0; j<n; j++) {
00144       pcom[j] = p[j];
00145       xicom[j] = xi[j];
00146    }
00147    ax=0.0;
00148    xx=1.0;
00149    bx=0.0; fa=0.0; fx=0.0; fb=0.0; xmin=0.0;
00150    mnbrak(ax,xx,bx,fa,fx,fb,f1dim);
00151    fret = brent(ax,xx,bx,f1dim,TOL,xmin);
00152    for (j=0; j<n; j++) {
00153       xi[j] *= xmin;
00154       p[j] += xi[j];
00155    }
00156    if(xicom){
00157      delete [] xicom;
00158      xicom=0;
00159    }
00160    if(pcom){
00161      delete [] pcom;
00162      pcom=0;
00163    }
00164 }

template<class T>
Matrix<T> log const Matrix< T > &    a
 

Definition at line 1110 of file matrix.h.

References matvec::Matrix< T >::apply().

01110 { return a.apply(std::log); }

Field log Field   a
 

Definition at line 770 of file field.cpp.

References matvec::Field::map().

00770 {return a.map(std::log);}

BG log const BG   a
 

Definition at line 136 of file bg.cpp.

References matvec::BG::D, matvec::BG::d, matvec::BG::f, and matvec::Matrix< double >::transpose().

Referenced by Beta_cdf(), betain(), matvec::GLMM::Build_SinMat(), ChiSquare_cdf(), ChiSquare_inv(), matvec::SparseBGMatrix::factor(), gamdev(), gammin(), gammln(), gasdev(), matvec::SparseBGMatrix::logdet(), Normal_inv(), sgamma(), t_cdf(), and t_expected_value().

00136                      {
00137     BG r; 
00138     r.f = std::log(a.f);
00139     r.d = 1.0/a.f*a.d;
00140     r.D = 1.0/a.f*a.D - 1.0/(a.f*a.f) * a.d*a.d.transpose();
00141     return r;
00142   }

template<class T>
Matrix<T> log10 const Matrix< T > &    a
 

Definition at line 1111 of file matrix.h.

References matvec::Matrix< T >::apply().

01111 { return a.apply(std::log10); }

Field log10 Field   a
 

Definition at line 771 of file field.cpp.

References matvec::Field::map().

00771 {return a.map(std::log10);}

void matvec::lubksb double **    a,
int    n,
int *    indx,
double *    b
 

Definition at line 389 of file ginverse1.cpp.

References lubksb().

00390 {
00391    int i,j;
00392    int ip, ii = -1;
00393    double sum;
00394 
00395    for (i=0;i<n;i++) {
00396       ip = indx[i];
00397       sum = b[ip];
00398       b[ip] = b[i];
00399       if (ii>=0) {
00400          for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
00401       }
00402       else if (sum) {
00403          ii=i;
00404       }
00405       b[i] = sum;
00406    }
00407    for (i=n-1; i>=0; i--) {
00408       sum = b[i];
00409       for (j=i+1;j<n;j++) sum -= a[i][j]*b[j];
00410       b[i] = sum/a[i][i];
00411    }
00412 }

void matvec::lubksb BG **    a,
int    n,
int *    indx,
BG   b
 

Definition at line 414 of file ginverse1.cpp.

References lubksb().

Referenced by matvec::doubleMatrix::inv(), matvec::BGMatrix::inv(), matvec::doubleMatrix::lu_solve(), matvec::BGMatrix::lu_solve(), and lubksb().

00415 {
00416    int i,j;
00417    int ip, ii = -1;
00418    BG sum;
00419 
00420    for (i=0;i<n;i++) {
00421       ip = indx[i];
00422       sum = b[ip];
00423       b[ip] = b[i];
00424       if (ii>=0) {
00425          for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
00426       }
00427       else if (sum!=0.0) {
00428          ii=i;
00429       }
00430       b[i] = sum;
00431    }
00432    for (i=n-1; i>=0; i--) {
00433       sum = b[i];
00434       for (j=i+1;j<n;j++) sum -= a[i][j]*b[j];
00435       b[i] = sum/a[i][i];
00436    }
00437 }

void matvec::ludcmp double **    a,
int    n,
int *    indx,
int &    d,
double    tol
 

Definition at line 291 of file ginverse1.cpp.

References ludcmp().

00292 {
00293    int i,j,k,imax;
00294    double big,dum,sum,temp;
00295    double *vv = new double [n];
00296    d=1;
00297    for (i=0;i<n;i++) {
00298       big = 0.0;
00299       for (j=0;j<n;j++) if ((temp=std::fabs(a[i][j])) > big) big=temp;
00300       if (std::fabs(big) < tol) throw exception("ludcmp(): Singular matrix");
00301       vv[i] = 1.0/big;
00302    }
00303    for (j=0;j<n;j++) {
00304       for (i=0;i<j;i++) {
00305          sum = a[i][j];
00306          for (k=0;k<i;k++) sum -= a[i][k]*a[k][j];
00307          a[i][j]=sum;
00308       }
00309       big=0.0;
00310       imax = j;
00311       for (i=j;i<n;i++) {
00312          sum = a[i][j];
00313          for (k=0;k<j;k++) sum -= a[i][k]*a[k][j];
00314          a[i][j] = sum;
00315          if ( (dum = vv[i]*std::fabs(sum)) >= big) {
00316             big = dum;
00317             imax = i;
00318          }
00319       }
00320       if (j != imax) {
00321          for (k = 0; k<n; k++) {
00322             dum = a[imax][k];
00323             a[imax][k] = a[j][k];
00324             a[j][k] = dum;
00325          }
00326          d = -d;
00327          vv[imax] = vv[j];
00328       }
00329       indx[j] = imax;
00330       if (std::fabs(a[j][j]) < tol) throw exception("ludcmp(): Singular matrix");
00331       dum = 1.0/(a[j][j]);
00332       for (i=j+1;i<n;i++) a[i][j] *= dum;
00333    }
00334    if(vv){
00335      delete []  vv;
00336      vv=0;
00337    }
00338 }

void matvec::ludcmp BG **    a,
int    n,
int *    indx,
int &    d,
double    tol
 

Definition at line 340 of file ginverse1.cpp.

References ludcmp().

Referenced by matvec::doubleMatrix::det(), matvec::BGMatrix::det(), matvec::doubleMatrix::inv(), matvec::BGMatrix::inv(), matvec::doubleMatrix::lu_solve(), matvec::BGMatrix::lu_solve(), and ludcmp().

00341 {
00342    int i,j,k,imax;
00343    BG big,dum,sum,temp;
00344    BG *vv = new BG [n];
00345    d=1;
00346    for (i=0;i<n;i++) {
00347       big = 0.0;
00348       for (j=0;j<n;j++) if ((temp=fabs(a[i][j])) > big) big=temp;
00349       if (fabs(big) < tol) throw exception("ludcmp(): Singular matrix");
00350       vv[i] = 1.0/big;
00351    }
00352    for (j=0;j<n;j++) {
00353       for (i=0;i<j;i++) {
00354          sum = a[i][j];
00355          for (k=0;k<i;k++) sum -= a[i][k]*a[k][j];
00356          a[i][j]=sum;
00357       }
00358       big=0.0;
00359       imax = j;
00360       for (i=j;i<n;i++) {
00361          sum = a[i][j];
00362          for (k=0;k<j;k++) sum -= a[i][k]*a[k][j];
00363          a[i][j] = sum;
00364          if ( (dum = vv[i]*fabs(sum)) >= big) {
00365             big = dum;
00366             imax = i;
00367          }
00368       }
00369       if (j != imax) {
00370          for (k = 0; k<n; k++) {
00371             dum = a[imax][k];
00372             a[imax][k] = a[j][k];
00373             a[j][k] = dum;
00374          }
00375          d = -d;
00376          vv[imax] = vv[j];
00377       }
00378       indx[j] = imax;
00379       if (fabs(a[j][j]) < tol) throw exception("ludcmp(): Singular matrix");
00380       dum = 1.0/(a[j][j]);
00381       for (i=j+1;i<n;i++) a[i][j] *= dum;
00382    }
00383    if(vv){
00384      delete []  vv;
00385      vv=0;
00386    }
00387 }

void matvec::mnbrak double &    ax,
double &    bx,
double &    cx,
double &    fa,
double &    fb,
double &    fc,
double(*    func)(double)
[static]
 

Definition at line 182 of file min_nr_powell.cpp.

References GLIMIT, GOLD, MAX, SHFT, SIGN, and TINY.

Referenced by linmin().

00184 {
00185    double ulim,u,r,q,fu,dum;
00186 
00187    fa=(*func)(ax);
00188    fb=(*func)(bx);
00189    if (fb > fa) {
00190       SHFT(dum,ax,bx,dum)
00191       SHFT(dum,fb,fa,dum)
00192    }
00193    cx = bx+GOLD*(bx-ax);
00194    fc = (*func)(cx);
00195    while (fb > fc) {
00196       r=(bx-ax)*(fb-fc);
00197       q=(bx-cx)*(fb-fa);
00198       u=(bx)-((bx-cx)*q-(bx-ax)*r)/
00199          (2.0*SIGN(MAX(fabs(q-r),TINY),q-r));
00200       ulim = bx+GLIMIT*(cx-bx);
00201       if ((bx-u)*(u-cx) > 0.0) {
00202          fu=(*func)(u);
00203          if (fu < fc) {
00204             ax = bx;
00205             bx = u;
00206             fa = fb;
00207             fb = fu;
00208             return;
00209          } else if (fu > fb) {
00210             cx = u;
00211             fc = fu;
00212             return;
00213          }
00214          u= cx +GOLD*(cx-bx);
00215          fu=(*func)(u);
00216       } else if ((cx-u)*(u-ulim) > 0.0) {
00217          fu=(*func)(u);
00218          if (fu < fc) {
00219             SHFT(bx,cx,u,cx+GOLD*(cx-bx))
00220             SHFT(fb,fc,fu,(*func)(u))
00221          }
00222       } else if ((u-ulim)*(ulim-cx) >= 0.0) {
00223          u=ulim;
00224          fu=(*func)(u);
00225       } else {
00226          u=(cx)+GOLD*(cx-bx);
00227          fu=(*func)(u);
00228       }
00229       SHFT(ax,bx,cx,u)
00230       SHFT(fa,fb,fc,fu)
00231    }
00232 }

void nest_xaction std::string &    s [static]
 

Definition at line 2137 of file model.cpp.

References nest_xaction().

Referenced by matvec::Model::build_model_term(), and nest_xaction().

02138 {
02139    int i,j;
02140    int n = s.size();
02141    for (i=0; i<n; i++) {
02142       if ( s[i] == '(' ) s[i] = '*';
02143       if ( s[i] == ')' ) s[i] = ' ';
02144    }
02145 
02146       // replace space around * with *
02147    i = 0;
02148    while (i<n) {
02149       if (s[i] == '*' ) {
02150          j = i;
02151          while (s[--j] == ' ') s[j]='*';
02152          while (s[++i] == ' ') s[i]='*';
02153       }
02154       i++;
02155    }
02156 }

Chromosome* new_Chromosome_vec const unsigned    n
 

Genome * matvec::new_Genome_vec const unsigned    m,
GeneticDist   D = 0
 

Definition at line 26 of file genome.cpp.

References check_ptr(), and matvec::Genome::remodel().

Referenced by matvec::Population::build_pop_gamete().

00027 {
00028    Genome *T = 0;
00029    if (m) {
00030       T = new Genome[m];
00031       check_ptr(T);
00032    }
00033    if (D) {
00034       for (unsigned i=0; i<m; i++) T[i].remodel(D);
00035    }
00036    return T;
00037 }

HashNode* new_HashNode_vec const unsigned    n,
const size_t    s
 

RNormal* new_n_posterior_vec const int    m
 

NuFamily* new_NuFamily_vec const unsigned    m
 

long matvec::next_prime long    n
 

Definition at line 103 of file hashtable.cpp.

Referenced by matvec::HashTable::insert(), matvec::SparseMatrix::resize(), matvec::SparseBGMatrix::resize(), matvec::HashTable::resize(), matvec::SparseBGMatrix::SparseBGMatrix(), and matvec::SparseMatrix::SparseMatrix().

00104 {
00105    long prime;                  // tentative prime
00106    long factor;                 // prime factor
00107    int is_prime;                // 'prime' is a prime number
00108 
00109    n = (n < 0L) ? -n : n;       // be safe -- force n positive
00110    prime = 2L*(n/2L) + 1L;      // next odd number
00111    is_prime = (prime <= 3L);
00112    while (!is_prime) {
00113       factor = 5L;
00114       is_prime = (prime % 2L) && (prime % 3L);
00115       while (is_prime && (factor*factor <= prime)) {
00116          if ((prime % factor) == 0L)
00117             is_prime = 0;
00118          else if ((prime % (factor + 2L)) == 0L)
00119             is_prime = 0;
00120          else               // factor+4 is divisible by 3 (every 3rd odd is)
00121             factor += 6L;
00122       }
00123       if (!is_prime) prime += 2L;
00124    }
00125    return (prime);
00126 }

int matvec::nonsymm_eigen const int    job,
double **    A,
const int    n,
double *    rr,
double *    ri,
double **    vr,
double **    vi,
const int    maxiter = 50
 

Definition at line 590 of file nonsymm_eigen.cpp.

References balance(), elemhess(), nonsymm_eigen_core(), sqrt(), and unbalance().

Referenced by matvec::doubleMatrix::eigen().

00592 {    
00593    //////////////////////////////////////////////////////////////////////////
00594    //  This nonsym_eigen() works for eigenvalue/vector analysis
00595    //         for real general square matrix A
00596    //         A will be destroyed
00597    //         rr,ri are vectors containing eigenvalues
00598    //         vr,vi are  matrice containing eigenvectors
00599    //         maxiter is  max. no. of iterations to converge, usually 30
00600    //         note that A*v(:,j) = v(:,j)*r(j) where v(:,j) means jth column
00601    //   job = 0, no eigenvectors are needed
00602    //         1, eigenvectors are also needed
00603    //
00604    //  Algorithm: Handbook for Automatic Computation, vol 2
00605    //             by Wilkinson and Reinsch, 1971
00606    //             most of source codes were taken from a public domain
00607    //             solftware called MATCALC.
00608    //  Credits:   goes to the authors of MATCALC
00609    //
00610    //  return     -2 out of memory
00611    //             -1 not converged
00612    //              0 real    eigenvalues/vectors
00613    //              1 complex eigenvalues/vectors
00614    //
00615    //  Tianlin Wang at University of Illinois
00616    //  Thu May  6 15:22:31 CDT 1993
00617    //////////////////////////////////////////////////////////////////////////
00618 
00619    double base  = 2.0;           // base of floating point arithmetic
00620    double digits = 53.0;          // no. of digits to the base in the fraction
00621    double eps  = pow(base,1.0-digits);
00622    double tiny =  sqrt(eps);
00623    int low,hi,i,k=0;
00624    double *w;
00625    if(n>0){
00626      w = new double [n];
00627    }
00628    else {
00629      w = 0;
00630    }
00631    if (!w) return -2;
00632    balance(A,n,&low,&hi,w,base);
00633    elemhess(job,A,n,low,hi,vr,vi);
00634    if (-1 == nonsymm_eigen_core(job,A,n,low,hi,rr,ri,vr,vi,eps,maxiter)) {
00635       k = -1;
00636    }
00637    else {
00638       if (job) unbalance(n,vr,vi,low,hi,w);
00639       for (i=0; i<n; i++) if (fabs(ri[i]) > tiny) {k = 1; break;}
00640    }
00641    if(w){
00642      delete [] w;
00643      w=0;
00644    }
00645    return k;
00646 }

int nonsymm_eigen_core const int    job,
double **    mat,
const int    n,
const int    low,
const int    hi,
double *    valr,
double *    vali,
double **    vr,
double **    vi,
const double    eps,
const int    maxiter
 

Definition at line 262 of file nonsymm_eigen.cpp.

References min, and sqrt().

Referenced by nonsymm_eigen().

00265 {
00266    ////////////////////////////////////////////////////////////////
00267    // Calculate eigenvalues and eigenvectors of a real upper
00268    // Hessenberg matrix
00269    // Return 1 if converges successfully and 0 otherwise
00270    /////////////////////////////////////////////////////////////////
00271    std::complex<double> v;
00272    double p,q,r,s,t,w,x,y,z,ra,sa,norm;
00273    int niter,en,i,j,k,l,m;
00274 
00275    for (i=0; i<n; i++) {
00276       valr[i]=0.0;
00277       vali[i]=0.0;
00278    }
00279       /* store isolated roots and calculate norm */
00280    for (norm = 0,i = 0; i < n; i++) {
00281       for (j = std::max(0,i-1); j < n; j++) {
00282          norm += fabs(mat[i][j]);
00283       }
00284       if (i < low || i > hi) valr[i] = mat[i][i];
00285    }
00286    t = 0;
00287    en = hi;
00288 
00289    while (en >= low) {
00290       niter = 0;
00291       for (;;) {
00292 
00293        /* look for single small subdiagonal element */
00294 
00295          for (l = en; l > low; l--) {
00296             s = fabs(mat[l-1][l-1]) + fabs(mat[l][l]);
00297             if (s == 0) s = norm;
00298             if (fabs(mat[l][l-1]) <= eps * s) break;
00299          }
00300 
00301          /* form shift */
00302 
00303          x = mat[en][en];
00304 
00305          if (l == en) {             /* one root found */
00306             valr[en] = x + t;
00307             if (job) mat[en][en] = x + t;
00308             en--;
00309             break;
00310          }
00311 
00312          y = mat[en-1][en-1];
00313          w = mat[en][en-1] * mat[en-1][en];
00314 
00315          if (l == en - 1) {                /* two roots found */
00316             p = (y - x) / 2;
00317             q = p * p + w;
00318             z = sqrt(fabs(q));
00319             x += t;
00320             if (job) {
00321                mat[en][en] = x;
00322                mat[en-1][en-1] = y + t;
00323             }
00324             if (q < 0) {                /* complex pair */
00325                valr[en-1] = x+p;
00326                vali[en-1] = z;
00327                valr[en] = x+p;
00328                vali[en] = -z;
00329             }
00330             else {                      /* real pair */
00331                z = (p < 0) ? p - z : p + z;
00332                valr[en-1] = x + z;
00333                valr[en] = (z == 0) ? x + z : x - w / z;
00334                if (job) {
00335                   x = mat[en][en-1];
00336                   s = fabs(x) + fabs(z);
00337                   p = x / s;
00338                   q = z / s;
00339                   r = sqrt(p*p+q*q);
00340                   p /= r;
00341                   q /= r;
00342                   for (j = en - 1; j < n; j++) {
00343                      z = mat[en-1][j];
00344                      mat[en-1][j] = q * z + p *
00345                      mat[en][j];
00346                      mat[en][j] = q * mat[en][j] - p*z;
00347                   }
00348                   for (i = 0; i <= en; i++) {
00349                      z = mat[i][en-1];
00350                      mat[i][en-1] = q * z + p * mat[i][en];
00351                      mat[i][en] = q * mat[i][en] - p*z;
00352                   }
00353                   for (i = low; i <= hi; i++) {
00354                      z = vr[i][en-1];
00355                      vr[i][en-1] = q*z + p*vr[i][en];
00356                      vr[i][en] = q*vr[i][en] - p*z;
00357                   }
00358                }
00359             }
00360             en -= 2;
00361             break;
00362          }
00363          if (niter == maxiter) return(-1);
00364          if (niter != 0 && niter % 10 == 0) {
00365             t += x;
00366             for (i = low; i <= en; i++) mat[i][i] -= x;
00367             s = fabs(mat[en][en-1]) + fabs(mat[en-1][en-2]);
00368             x = y = 0.75 * s;
00369             w = -0.4375 * s * s;
00370          }
00371          niter++;
00372            /* look for two consecutive small subdiagonal elements */
00373          for (m = en - 2; m >= l; m--) {
00374             z = mat[m][m];
00375             r = x - z;
00376             s = y - z;
00377             p = (r * s - w) / mat[m+1][m] + mat[m][m+1];
00378             q = mat[m+1][m+1] - z - r - s;
00379             r = mat[m+2][m+1];
00380             s = fabs(p) + fabs(q) + fabs(r);
00381             p /= s;
00382             q /= s;
00383             r /= s;
00384             if (m == l || fabs(mat[m][m-1]) * (fabs(q)+fabs(r)) <=
00385                 eps * (fabs(mat[m-1][m-1]) + fabs(z) +
00386                 fabs(mat[m+1][m+1])) * fabs(p)) break;
00387          }
00388          for (i = m + 2; i <= en; i++) mat[i][i-2] = 0;
00389          for (i = m + 3; i <= en; i++) mat[i][i-3] = 0;
00390              /* double QR step involving rows l to en and columns m to en */
00391          for (k = m; k < en; k++) {
00392             if (k != m) {
00393                p = mat[k][k-1];
00394                q = mat[k+1][k-1];
00395                r = (k == en - 1) ? 0 : mat[k+2][k-1];
00396                if ((x = fabs(p) + fabs(q) + fabs(r)) == 0) continue;
00397                p /= x;
00398                q /= x;
00399                r /= x;
00400             }
00401             s = sqrt(p*p+q*q+r*r);
00402             if (p < 0) s = -s;
00403             if (k != m) {
00404                mat[k][k-1] = -s * x;
00405             }
00406             else if (l != m) {
00407                mat[k][k-1] = -mat[k][k-1];
00408             }
00409             p += s;
00410             x = p / s;
00411             y = q / s;
00412             z = r / s;
00413             q /= p;
00414             r /= p;
00415                 /* row modification */
00416             for (j = k; j <= (!job ? en : n-1); j++){
00417                p = mat[k][j] + q * mat[k+1][j];
00418                if (k != en - 1) {
00419                   p += r * mat[k+2][j];
00420                   mat[k+2][j] -= p * z;
00421                }
00422                mat[k+1][j] -= p * y;
00423                mat[k][j] -= p * x;
00424             }
00425             j = std::min(en,k+3);
00426               /* column modification */
00427             for (i = (!job ? l : 0); i <= j; i++) {
00428                p = x * mat[i][k] + y * mat[i][k+1];
00429                if (k != en - 1) {
00430                   p += z * mat[i][k+2];
00431                   mat[i][k+2] -= p*r;
00432                }
00433                mat[i][k+1] -= p*q;
00434                mat[i][k] -= p;
00435             }
00436             if (job) {             /* accumulate transformations */
00437                for (i = low; i <= hi; i++) {
00438                   p = x * vr[i][k] + y * vr[i][k+1];
00439                   if (k != en - 1) {
00440                      p += z * vr[i][k+2];
00441                      vr[i][k+2] -= p*r;
00442                   }
00443                   vr[i][k+1] -= p*q;
00444                   vr[i][k] -= p;
00445                }
00446             }
00447          }
00448       }
00449    }
00450 
00451    if (!job) return(0);
00452    if (norm != 0) {
00453        /* back substitute to find vectors of upper triangular form */
00454       for (en = n-1; en >= 0; en--) {
00455          p = valr[en];
00456          if ((q = vali[en]) < 0) {            /* complex vector */
00457             m = en - 1;
00458             if (fabs(mat[en][en-1]) > fabs(mat[en-1][en])) {
00459                mat[en-1][en-1] = q / mat[en][en-1];
00460                mat[en-1][en] = (p - mat[en][en]) /
00461                      mat[en][en-1];
00462             }
00463             else {
00464                v = std::polar(0.0,-mat[en-1][en])/std::polar(mat[en-1][en-1]-p,q);
00465                mat[en-1][en-1] = v.real();
00466                mat[en-1][en] = v.imag();
00467             }
00468             mat[en][en-1] = 0;
00469             mat[en][en] = 1;
00470             for (i = en - 2; i >= 0; i--) {
00471                w = mat[i][i] - p;
00472                ra = 0;
00473                sa = mat[i][en];
00474                for (j = m; j < en; j++) {
00475                   ra += mat[i][j] * mat[j][en-1];
00476                   sa += mat[i][j] * mat[j][en];
00477                }
00478                if (vali[i] < 0) {
00479                   z = w;
00480                   r = ra;
00481                   s = sa;
00482                }
00483                else {
00484                   m = i;
00485                   if (vali[i] == 0) {
00486                      v = std::polar(-ra,-sa)/std::polar(w,q);
00487                      mat[i][en-1] = v.real();
00488                      mat[i][en] = v.imag();
00489                   }
00490                   else {                      /* solve complex equations */
00491                      x = mat[i][i+1];
00492                      y = mat[i+1][i];
00493                      v = std::complex<double>((valr[i]- p)*(valr[i]-p) + vali[i]*vali[i] - q*q,
00494                                                (valr[i] - p)*2*q );
00495 
00496                      if ((fabs(v.real()) + fabs(v.imag())) == 0) {
00497                         v = std::complex<double>(eps * norm * (fabs(w) +
00498                                 fabs(q) + fabs(x) + fabs(y) + fabs(z)), v.imag());
00499                      }
00500                      v = std::polar(x*r-z*ra+q*sa,x*s-z*sa-q*ra) / v;
00501                      mat[i][en-1] = v.real();
00502                      mat[i][en] = v.imag();
00503                      if (fabs(x) > fabs(z) + fabs(q)) {
00504                         mat[i+1][en-1] =
00505                              (-ra - w * mat[i][en-1] +
00506                              q * mat[i][en]) / x;
00507                         mat[i+1][en] = (-sa - w * mat[i][en] -
00508                              q * mat[i][en-1]) / x;
00509                      }
00510                      else {
00511                         v = std::polar(-r-y*mat[i][en-1],
00512                              -s-y*mat[i][en]) / std::polar(z,q);
00513                         mat[i+1][en-1] = v.real();
00514                         mat[i+1][en] = v.imag();
00515                      }
00516                   }
00517                }
00518             }
00519          }
00520          else if (q == 0) {                             /* real vector */
00521             m = en;
00522             mat[en][en] = 1;
00523             for (i = en - 1; i >= 0; i--) {
00524                w = mat[i][i] - p;
00525                r = mat[i][en];
00526                for (j = m; j < en; j++) {
00527                   r += mat[i][j] * mat[j][en];
00528                }
00529                if (vali[i] < 0) {
00530                   z = w;
00531                   s = r;
00532                }
00533                else {
00534                   m = i;
00535                   if (vali[i] == 0) {
00536                      if ((t = w) == 0) t = eps * norm;
00537                      mat[i][en] = -r / t;
00538                   }
00539                   else {            /* solve real equations */
00540                      x = mat[i][i+1];
00541                      y = mat[i+1][i];
00542                      q = (valr[i] - p) * (valr[i] - p) + vali[i]*vali[i];
00543                      t = (x * s - z * r) / q;
00544                      mat[i][en] = t;
00545                      if (fabs(x) <= fabs(z)) {
00546                         mat[i+1][en] = (-s - y * t) / z;
00547                      }
00548                      else {
00549                         mat[i+1][en] = (-r - w * t) / x;
00550                      }
00551                   }
00552                }
00553             }
00554          }
00555       }
00556              /* vectors of isolated roots */
00557       for (i = 0; i < n; i++) {
00558          if (i < low || i > hi) {
00559             for (j = i; j < n; j++) {
00560                vr[i][j] = mat[i][j];
00561             }
00562          }
00563       }
00564        /* multiply by transformation matrix */
00565 
00566       for (j = n-1; j >= low; j--) {
00567          m = std::min(j,hi);
00568          for (i = low; i <= hi; i++) {
00569             for (z = 0,k = low; k <= m; k++) {
00570                z += vr[i][k] * mat[k][j];
00571             }
00572             vr[i][j] = z;
00573          }
00574       }
00575    }
00576     /* rearrange complex eigenvectors */
00577    for (j = 0; j < n; j++) {
00578       if (vali[j] != 0) {
00579          for (i = 0; i < n; i++) {
00580             vi[i][j] = vr[i][j+1];
00581             vr[i][j+1] = vr[i][j];
00582             vi[i][j+1] = -vi[i][j];
00583          }
00584          j++;
00585       }
00586    }
00587    return(0);
00588 }

double matvec::Normal_cdf const double    x
 

Definition at line 163 of file stat1.cpp.

References erf(), and sqrt().

Referenced by matvec::NormalDist::cdf(), gammin(), t_cdf(), and matvec::LogNormalDist::variance().

00164 {
00165    // ******************************************************************
00166    // return the cumulative distribution value for a standard normal
00167    // *****************************************************************
00168    double pr = 0.5;
00169    if (x == 0.0) return pr;
00170    register double c = 1.0/sqrt(2.0);      // c = 1/sqrt(2)
00171    if (x <0.0) { pr = 0.5 - 0.5*erf(-x*c);}
00172    else        { pr = 0.5 + 0.5*erf(x*c);}
00173    return pr;
00174 }

double matvec::Normal_inv const double    p
 

Definition at line 571 of file stat1.cpp.

References log(), and sqrt().

Referenced by ChiSquare_inv(), matvec::LogNormalDist::inv(), and matvec::NormalDist::inv().

00572 {
00573    //////////////////////////////////////////////////////////////////
00574    // ALGORITHM AS241  APPL. STATIST. (1988) VOL. 37, NO. 3
00575    //
00576    // Produces the standard normal deviate Z corresponding to a given
00577    // lower tail area of P; Z is accurate to about 1 part in 10**16.
00578    //////////////////////////////////////////////////////////////////
00579    if (p <= 0.0 || p >= 1.0 ) throw exception(" Normal_inv(): bad arg, value in (0,1) expected");
00580    if (p == 0.5) return 0.0;
00581    double *a = new double [8];
00582    double *b = new double [8];
00583    double *c = new double [8];
00584    double *d = new double [8];
00585    double *e = new double [8];
00586    double *f = new double [8];
00587    a[0] = 3.3871328727963666080e+0;   a[1] = 1.3314166789178437745e+2;
00588    a[2] = 1.9715909503065514427e+3;   a[3] = 1.3731693765509461125e+4;
00589    a[4] = 4.5921953931549871457e+4;   a[5] = 6.7265770927008700853e+4;
00590    a[6] = 3.3430575583588128105e+4;   a[7] = 2.5090809287301226727e+3;
00591 
00592    b[0] = 0.0;                        b[1] = 4.2313330701600911252e+1;
00593    b[2] = 6.8718700749205790830e+2;   b[3] = 5.3941960214247511077e+3;
00594    b[4] = 2.1213794301586595867e+4;   b[5] = 3.9307895800092710610e+4;
00595    b[6] =  2.8729085735721942674e+4;  b[7] = 5.2264952788528545610e+3;
00596 
00597    c[0] = 1.42343711074968357734e+0;  c[1] = 4.63033784615654529590e+0;
00598    c[2] = 5.76949722146069140550e+0;  c[3] = 3.64784832476320460504e+0;
00599    c[4] = 1.27045825245236838258e+0;  c[5] = 2.41780725177450611770e-1;
00600    c[6] = 2.27238449892691845833e-2;  c[7] = 7.74545014278341407640e-4;
00601 
00602    d[0] = 0.0;                        d[1] = 2.05319162663775882187e+0;
00603    d[2] = 1.67638483018380384940e+0;  d[3] = 6.89767334985100004550e-1;
00604    d[4] = 1.48103976427480074590e-1;  d[5] = 1.51986665636164571966e-2;
00605    d[6] = 5.47593808499534494600e-4;  d[7] = 1.05075007164441684324e-9;
00606 
00607    e[0] = 6.65790464350110377720e+0;  e[1] = 5.46378491116411436990e+0;
00608    e[2] = 1.78482653991729133580e+0;  e[3] = 2.96560571828504891230e-1;
00609    e[4] = 2.65321895265761230930e-2;  e[5] = 1.24266094738807843860e-3;
00610    e[6] = 2.71155556874348757815e-5;  e[7] = 2.01033439929228813265e-7;
00611 
00612    f[0] = 0.0;                        f[1] = 5.99832206555887937690e-1;
00613    f[2] = 1.36929880922735805310e-1;  f[3] = 1.48753612908506148525e-2;
00614    f[4] = 7.86869131145613259100e-4;  f[5] = 1.84631831751005468180e-5;
00615    f[6] = 1.42151175831644588870e-7;  f[7] = 2.04426310338993978564e-15;
00616 
00617    double split1 = 0.425;
00618    double split2 = 5.0;
00619    double const1 = 0.180625;
00620    double const2 = 1.6;
00621 
00622    double q,r,w1,w2,ppnd16;
00623    q = p - 0.5;
00624    if (fabs(q) <= split1) {
00625       r = const1 - q*q;
00626       w1 = ((((((a[7]*r+a[6])*r+a[5])*r+a[4])*r+a[3])*r+a[2])*r+a[1])*r+a[0];
00627       w2 = ((((((b[7]*r+b[6])*r+b[5])*r+b[4])*r+b[3])*r+b[2])*r+b[1])*r+1.0;
00628       ppnd16 = q*w1/w2;
00629    }
00630    else {
00631       if (q < 0.0) {r = p;}
00632       else         {r = 1.0-p;}
00633       if (r <= 0.0) {
00634         if(a){
00635           delete [] a;  
00636           a=0;
00637         }
00638         if(b){
00639           delete [] b;  
00640           b=0;
00641         }
00642         if(c){
00643           delete [] c;
00644           c=0;
00645         }
00646         if(d){
00647           delete [] d;  
00648           d=0;
00649         }
00650         if(e){
00651           delete [] e;  
00652           e=0;
00653         }
00654         if(f){
00655           delete [] f;
00656           f=0;
00657         }
00658          return 0.0;
00659       }
00660       r = sqrt(-log(r));
00661       if (r <= split2) {
00662          r -= const2;
00663          w1= ((((((c[7]*r+c[6])*r+c[5])*r+c[4])*r+c[3])*r+c[2])*r+c[1])*r+c[0];
00664          w2= ((((((d[7]*r+d[6])*r+d[5])*r+d[4])*r+d[3])*r+d[2])*r+d[1])*r+1.0;
00665       }
00666       else {
00667          r -= split2;
00668          w1= ((((((e[7]*r+e[6])*r+e[5])*r+e[4])*r+e[3])*r+e[2])*r+e[1])*r+e[0];
00669          w2= ((((((f[7]*r+f[6])*r+f[5])*r+f[4])*r+f[3])*r+f[2])*r+f[1])*r+1.0;
00670       }
00671       ppnd16 = w1/w2;
00672       if (q< 0.0) ppnd16 = -ppnd16;
00673    }
00674    if(a){
00675      delete [] a;  
00676      a=0;
00677    }
00678    if(b){
00679      delete [] b;  
00680      b=0;
00681    }
00682    if(c){
00683      delete [] c;
00684      c=0;
00685    }
00686    if(d){
00687      delete [] d;  
00688      d=0;
00689    }
00690    if(e){
00691      delete [] e;  
00692      e=0;
00693    }
00694    if(f){
00695      delete [] f;
00696      f=0;
00697    }
00698    return ppnd16;
00699 }

void matvec::normal_loginClasses matvec::Observe   observe
 

Definition at line 1749 of file glmm1.cpp.

References matvec::Observe::estimate, matvec::doubleMatrix::ginv1(), matvec::Observe::H, matvec::Observe::like_val, matvec::doubleMatrix::logdet(), matvec::doubleMatrix::mat_exp(), matvec::doubleMatrix::mat_exp_der(), matvec::doubleMatrix::mat_log(), matvec::Observe::numtrait, matvec::Observe::nvc, matvec::Observe::pattern, matvec::doubleMatrix::quadratic(), matvec::Observe::rec, matvec::Observe::Resid_Sin_Mat, matvec::Observe::Resid_Var, matvec::Observe::resid_var, matvec::Observe::residual, matvec::Vector< T >::resize(), matvec::Matrix< double >::resize(), matvec::Observe::rpy, matvec::Observe::rve, matvec::Matrix< double >::transpose(), matvec::Observe::varcomp, and matvec::Observe::y.

Referenced by matvec::GLMM::normalLog().

01749                                                  {
01750         int numtrait=observe.numtrait;
01751         
01752         doubleMatrix H,variance;
01753         doubleMatrix Alog;
01754         doubleMatrix P,R;
01755         Vector<double> D;
01756         // double final=0;
01757         //if(observe.param) final=*((double *) observe.param[0]);
01758         int k,t1,t2;
01759         if(observe.rec < 0) {
01760                 if(observe.nvc) { 
01761                         if(observe.nvc != (numtrait*(numtrait+1))/2) {
01762                                 throw matvec::exception("Error: Should be normal(,%d)");
01763                                 return;
01764                         } 
01765                         
01766                         
01767                         //cout <<  variance << observe.resid_var[0];
01768                         *observe.resid_var=observe.resid_var[0];
01769                         Alog=(observe.resid_var[0]).mat_log();
01770                         for(k=0,t1=0;t1<numtrait;t1++) 
01771                                 for(t2=t1;t2<numtrait;t2++,k++) 
01772                                         observe.varcomp[0][k]=Alog[t1][t2];
01773                 }
01774                 return;
01775         }
01776         Vector<double> mean,y;
01777         Alog.resize(numtrait,numtrait);
01778         
01779         Vector<doubleMatrix> part(observe.nvc);
01780         for(k=0;k<observe.nvc;k++) part[k].resize(numtrait,numtrait);
01781         if(observe.nvc) {
01782                 
01783         for(k=0,t1=0;t1<numtrait;t1++) 
01784                         for(t2=t1;t2<numtrait;t2++,k++) {
01785                                 Alog[t1][t2]=observe.varcomp[0][k];
01786                                 Alog[t2][t1]=observe.varcomp[0][k];
01787                         }
01788                                 variance=Alog.mat_exp();
01789                 //cout << variance;
01790                 Alog=variance.mat_log();
01791                 
01792                 for(k=0,t1=0;t1<numtrait;t1++) 
01793                         for(t2=t1;t2<numtrait;t2++,k++) 
01794                                 observe.varcomp[0][k]=Alog[t1][t2];
01795                 *observe.resid_var=variance;
01796                 variance.mat_exp_der(part);
01797                 doubleMatrix Atmp;
01798                 /*
01799                  for(int ii=0,t1=0;t1 <numtrait;t1++)  
01800                  for(t2=t1;t2<numtrait;t2++,ii++) {
01801                          Atmp=Alog;
01802                          Atmp[t1][t2]+=.0001;
01803                          Atmp[t2][t1]=Atmp[t1][t2];
01804                          Atmp=(Atmp.mat_exp()-Alog.mat_exp())/.0001;
01805                          cout << ii << part[ii]<< endl;
01806                          cout << ii << Atmp << endl;
01807                  }
01808                  */
01809                 
01810         }
01811         else{
01812                 variance=observe.resid_var[0];
01813         }
01814         //cout << "\n Variance " << variance << *observe.resid_var;
01815         doubleMatrix Miss;
01816         Miss.resize(numtrait,numtrait);
01817         //cout << "\n Pattern" ;
01818         for(t1=0;t1<numtrait;t1++) {
01819                 //cout <<' ' <<observe.pattern[t1];
01820                 if(observe.pattern[t1]=='1') Miss[t1][t1]=1;
01821         }
01822         //Miss.identity();
01823         
01824         //static double mean,H,variance,scale,y;
01825         y=observe.y;
01826         y=Miss*y;
01827         //cout << "\n y\n" << y;
01828         // if(observe.estimate[0] < -5.) observe.estimate[0]=-5.;
01829         //  if(observe.estimate[0] > 5.) observe.estimate[0]=5.;
01830         mean=Miss*observe.estimate;
01831         
01832         variance=*observe.resid_var;
01833         variance=Miss*variance*Miss;
01834         //cout << "\n Variance \n" << variance; 
01835         observe.Resid_Var=variance;
01836         variance=variance.ginv1();
01837         //cout << "\n Variance inv \n" << variance; 
01838         observe.like_val=.5*(variance.logdet()-variance.quadratic((y-mean),(y-mean))) ;
01839         
01840         
01841         observe.H=Miss;
01842         H=observe.H;
01843         //observe.Hinv[0][0]=1./H;
01844         observe.rve=H.transpose()*variance*H;
01845         
01846         observe.rpy=H.transpose()*variance*y;
01847         //cout <<"\n rve rpy "<<observe.rve << " " << observe.rpy<< " H "<< H << "pattern" << observe.pattern <<" y"<<  y <<" var" << variance;
01848         observe.residual=y-mean;
01849         if(observe.nvc) {
01850                 for(k=0,t1=0;t1<numtrait;t1++)
01851                         for(t2=t1;t2<numtrait;t2++,k++) {
01852                                 
01853                                 observe.Resid_Sin_Mat[k]=variance*Miss*part[k]*Miss*variance;
01854                                 //      cout << k << " " << part[k] << observe.Resid_Sin_Mat[k] << endl;
01855                         }
01856                                 
01857         }
01858 }

double matvec::nr_powell Data   D,
Model   M,
Vector< double > &    p,
Matrix< double > &    xi,
int    nvc,
int &    iter,
double    ftol
 

Definition at line 63 of file min_nr_powell.cpp.

References matvec::Vector< T >::begin(), funk(), linmin(), nfunk, SQR, and warning().

Referenced by matvec::Model::vce_dfreml().

00065 {
00066    myD = D;
00067    myM = M;
00068    nfunk = 0;
00069 
00070    int i,ibig,j;
00071    double t,fptt,fp,del,fret;
00072    int maxiter = iter;
00073 
00074    Vector<double> pt(n);
00075    Vector<double> ptt(n);
00076    Vector<double> xit(n);
00077 
00078    fret = funk(p);
00079    for (j=0; j<n; j++) pt[j]=p[j];
00080    for (iter=1; ;iter++) {
00081       fp = fret;
00082       ibig = 0;
00083       del = 0.0;
00084       for (i=0; i<n; i++) {
00085          for (j=0; j<n; j++) xit[j] = xi[j][i];
00086          fptt = fret;
00087          linmin(p.begin(),xit.begin(),n,fret);
00088          if (fabs(fptt-fret) > del) {
00089             del = fabs(fptt-fret);
00090             ibig = i;
00091          }
00092       }
00093       if (2.0*fabs(fp-fret) <= ftol*(fabs(fp)+fabs(fret))) {
00094          iter = nfunk;
00095          return fret;
00096       }
00097       if (iter == maxiter) {
00098          warning(" Too many iterations in routine POWELL\n"
00099                "%d out of %d",iter,maxiter);
00100          return fret;
00101       }
00102       for (j=0; j<n; j++) {
00103          ptt[j] = 2.0*p[j] - pt[j];
00104          xit[j] = p[j] - pt[j];
00105          pt[j] = p[j];
00106       }
00107       fptt = funk(ptt);
00108       if (fptt < fp) {
00109          t = 2.0*(fp-2.0*fret+fptt)*SQR(fp-fret-del)-del*SQR(fp-fptt);
00110          if (t < 0.0) {
00111             linmin(p.begin(),xit.begin(),n,fret);
00112             for (j=0; j<n; j++) xi[j][ibig] = xit[j];
00113          }
00114       }
00115    }
00116 }

SparseMatrix operator * const double    s,
const SparseMatrix   A
 

Definition at line 499 of file sparsematrix.cpp.

References operator *().

00500 {return operator*(A,s);}

SparseMatrix operator * const SparseMatrix   A,
const double    s
 

Multiply SparseMatrix object by s.

Definition at line 475 of file sparsematrix.cpp.

References matvec::SparseMatrix::aav, matvec::SparseMatrix::dim, matvec::SparseMatrix::hash_srt, matvec::SparseMatrix::hsize, matvec::SparseMatrix::iiv, matvec::SparseMatrix::jjv, matvec::SparseMatrix::max_nz, matvec::SparseMatrix::nonzero, and matvec::SparseMatrix::srt_hash.

00476 {
00477    if ( A.dim == 0 ) throw exception("SparseMatrix is empty");
00478 
00479    unsigned new_dim = A.dim;
00480    unsigned new_nz = A.nonzero;
00481    SparseMatrix B(new_dim,A.max_nz);
00482    B.nonzero = new_nz;
00483    memcpy(B.iiv,A.iiv,sizeof(int)*A.hsize);
00484    memcpy(B.jjv,A.jjv,sizeof(int)*A.hsize);
00485    if(A.hsize){
00486      memcpy(B.hash_srt,A.hash_srt,sizeof(int)*(A.hsize+1));
00487      memcpy(B.srt_hash,A.srt_hash,sizeof(int)*(A.hsize+1));
00488    }
00489    double *A_pt = A.aav;
00490    double *New_pt = B.aav;
00491    for (unsigned i=0; i<A.hsize; i++) {
00492      if (A.iiv[i] > 0) {
00493        New_pt[i] = A_pt[i]*s;
00494      }
00495    }
00496    return B;
00497 }

Vector<double> operator * const SparseMatrix   A,
const Vector< double > &    v
 

SparseMatrix times vector operation.

Definition at line 395 of file sparsematrix.cpp.

References matvec::SparseMatrix::aav, matvec::Vector< T >::begin(), matvec::SparseMatrix::dim, matvec::SparseMatrix::iiv, matvec::SparseMatrix::jjv, matvec::SparseMatrix::nonzero, matvec::Vector< T >::reserve(), matvec::Vector< T >::size(), and matvec::SparseMatrix::srt_hash.

00396 {
00397   if ( A.dim == 0) throw exception("SparseMatrix is empty");
00398   if (A.dim != v.size()) throw exception("SparseMatrix*Vector<double>, size not conformable");
00399   Vector<double> vec_out;
00400   int n = A.dim;
00401   int *A_ia = A.iiv-1;   /* offset by 1 */
00402   int *A_ja = A.jjv-1;
00403   double *A_a = A.aav-1;
00404   double *v_ve = v.begin()-1;
00405   double tmp,x;
00406   unsigned k,i,j;
00407 
00408   vec_out.reserve(n);
00409   double *out = vec_out.begin();
00410 
00411   out--; // offset by 1
00412   int ksrt=0;       //SDK
00413   for(ksrt=0;ksrt<A.nonzero;ksrt++) { //SDK
00414     k=A.srt_hash[ksrt];  //SDK
00415   //  for (k=1; k<=A.hsize; k++) {  //SDK
00416     if (i = A_ia[k]) {
00417         j = A_ja[k];
00418         x = A_a[k];
00419         out[i] = out[i] + x*v_ve[j];
00420         if (j>i) {
00421           out[j] = out[j] + x*v_ve[i];
00422         }
00423     }
00424   }
00425   return vec_out;
00426 }

SparseBGMatrix operator * const BG    s,
const SparseBGMatrix   A
 

Definition at line 503 of file sparsebgmatrix.cpp.

References operator *().

00504 {return operator*(A,s);}

SparseBGMatrix operator * const SparseBGMatrix   A,
const BG    s
 

Multiply SparseBGMatrix object by s.

Definition at line 479 of file sparsebgmatrix.cpp.

References matvec::SparseBGMatrix::aav, matvec::SparseBGMatrix::dim, matvec::SparseBGMatrix::hash_srt, matvec::SparseBGMatrix::hsize, matvec::SparseBGMatrix::iiv, matvec::SparseBGMatrix::jjv, matvec::SparseBGMatrix::max_nz, matvec::SparseBGMatrix::nonzero, and matvec::SparseBGMatrix::srt_hash.

00480 {
00481    if ( A.dim == 0 ) throw exception("SparseBGMatrix is empty");
00482 
00483    unsigned new_dim = A.dim;
00484    unsigned new_nz = A.nonzero;
00485    SparseBGMatrix B(new_dim,A.max_nz);
00486    B.nonzero = new_nz;
00487    memcpy(B.iiv,A.iiv,sizeof(int)*A.hsize);
00488    memcpy(B.jjv,A.jjv,sizeof(int)*A.hsize);
00489    if(A.hsize){
00490      memcpy(B.hash_srt,A.hash_srt,sizeof(int)*(A.hsize+1));
00491      memcpy(B.srt_hash,A.srt_hash,sizeof(int)*(A.hsize+1));
00492    }
00493    BG *A_pt = A.aav;
00494    BG *New_pt = B.aav;
00495    for (unsigned i=0; i<A.hsize; i++) {
00496      if (A.iiv[i] > 0) {
00497        New_pt[i] = A_pt[i]*s;
00498      }
00499    }
00500    return B;
00501 }

Vector<BG> operator * const SparseBGMatrix   A,
const Vector< BG > &    v
 

SparseBGMatrix times vector operation.

Definition at line 398 of file sparsebgmatrix.cpp.

References matvec::SparseBGMatrix::aav, matvec::Vector< T >::begin(), matvec::SparseBGMatrix::dim, matvec::SparseBGMatrix::iiv, matvec::SparseBGMatrix::jjv, matvec::SparseBGMatrix::nonzero, matvec::Vector< T >::reserve(), matvec::Vector< T >::size(), and matvec::SparseBGMatrix::srt_hash.

00399 {
00400   if ( A.dim == 0) throw exception("SparseBGMatrix is empty");
00401   if (A.dim != v.size()) throw exception("SparseBGMatrix*Vector<BG>, size not conformable");
00402   Vector<BG> vec_out;
00403   int n = A.dim;
00404   int *A_ia = A.iiv-1;   /* offset by 1 */
00405   int *A_ja = A.jjv-1;
00406   BG *A_a = A.aav-1;
00407   BG *v_ve = v.begin()-1;
00408   BG tmp,x;
00409   unsigned k,i,j;
00410 
00411   vec_out.reserve(n);
00412   BG *out = vec_out.begin();
00413 
00414   out--; // offset by 1
00415   int ksrt=0;       //SDK
00416   for(ksrt=0;ksrt<A.nonzero;ksrt++) { //SDK
00417     k=A.srt_hash[ksrt];  //SDK
00418   //  for (k=1; k<=A.hsize; k++) {  //SDK
00419     if (i = A_ia[k]) {
00420         j = A_ja[k];
00421         x = A_a[k];
00422         out[i] = out[i] + x*v_ve[j];
00423         if (j>i) {
00424           out[j] = out[j] + x*v_ve[i];
00425         }
00426     }
00427   }
00428   return vec_out;
00429 }

template<class T>
Matrix<T> operator * const T &    a,
const Matrix< T > &    b
 

Definition at line 1123 of file matrix.h.

01123 { return b * a; }

template<class T>
Vector<T> operator * const Vector< T > &    a,
const Matrix< T > &    b
[inline]
 

Definition at line 1012 of file matrix.h.

References matvec::Matrix< T >::begin(), matvec::Matrix< T >::num_cols(), matvec::Matrix< T >::num_rows(), and matvec::Vector< T >::size().

01013 {
01014    if (a.size() != b.num_rows()) throw exception("Matrix<T>::operator*: size not conformable");
01015    Vector<T> temp(b.num_cols());
01016    T** me = b.begin();
01017    T x;
01018    for (int i,j=0; j<b.num_cols(); ++j) {
01019       x = T();
01020       for (i=0; i<b.num_rows(); ++i)  x += a[i] * me[i][j];
01021       temp[j] = x;
01022    }
01023    return temp;
01024 }

doubleMatrix matvec::operator * Vector< double > &    a,
Vector< double > &    b
 

Definition at line 1084 of file glmm2.cpp.

References matvec::Vector< T >::size().

01085  {
01086    int row,col;
01087    row=a.size();
01088    col=b.size();
01089    doubleMatrix temp(row,col);
01090    for(int i=0;i<row;i++) {
01091      for(int j=0;j<col;j++) {
01092        temp[i][j]=a[i]*b[j];
01093      }
01094    }
01095    return temp;
01096  }

Field operator * const double    s,
const Field   B
 

Definition at line 632 of file field.cpp.

References matvec::Field::col_struct, matvec::Field::dat_vec, matvec::DataNode::double_val(), matvec::DataNode::missing, matvec::Field::ne, matvec::FieldStruct::type(), and warning().

00633 {
00634    if (B.col_struct.type()=='S') {
00635       warning("s * Field: failed for string column");
00636       return Field();
00637    }
00638    DataNode *temp; 
00639    if(B.ne>0){
00640      temp = new DataNode [B.ne];
00641    }
00642    else {
00643      temp = 0;
00644    }
00645    for (unsigned i=0; i<B.ne; i++) {
00646       if (B.dat_vec[i].missing) {
00647          temp[i].missing = 1;
00648       }
00649       else {
00650          temp[i].double_val( s * B.dat_vec[i].double_val());
00651       }
00652    }
00653    return Field(B.ne,temp,B.col_struct);
00654 }

Field operator * const Field   A,
const double    s
 

Definition at line 536 of file field.cpp.

References matvec::Field::col_struct, matvec::Field::dat_vec, matvec::DataNode::double_val(), matvec::DataNode::missing, matvec::Field::ne, matvec::FieldStruct::type(), and warning().

00537 {
00538    if (A.col_struct.type()=='S') {
00539       warning("Field * s: failed for string column");
00540       return Field();
00541    }
00542    DataNode *temp; 
00543    if(A.ne>0){
00544      temp = new DataNode [A.ne];
00545    }
00546    else {
00547      temp = 0;
00548    }
00549    for (unsigned i=0; i<A.ne; i++) {
00550       if (A.dat_vec[i].missing) {
00551          temp[i].missing = 1;
00552       }
00553       else {
00554          temp[i].double_val(A.dat_vec[i].double_val() * s);
00555       }
00556    }
00557    return Field(A.ne,temp,A.col_struct);
00558 }

Field operator * const Field   A,
const Field   B
 

Definition at line 428 of file field.cpp.

References matvec::Field::col_struct, matvec::Field::dat_vec, matvec::DataNode::double_val(), matvec::DataNode::missing, matvec::Field::ne, matvec::FieldStruct::type(), and warning().

00429 {
00430    unsigned n = A.ne;
00431    if (n != B.ne) {
00432       warning("Col * Col: size not conformable");
00433       return Field();
00434    }
00435    if (A.col_struct.type()=='S' || B.col_struct.type()=='S') {
00436       warning("Col * Field: failed for string column");
00437       return Field();
00438    }
00439    DataNode *temp; 
00440    if(n>0){
00441      temp = new DataNode [n];
00442    }
00443    else {
00444      temp = 0;
00445    }
00446    for (unsigned i=0; i<n; i++) {
00447       if (A.dat_vec[i].missing || B.dat_vec[i].missing) {
00448          temp[i].missing = 1;
00449       }
00450       else {
00451          temp[i].double_val(A.dat_vec[i].double_val()*
00452                             B.dat_vec[i].double_val());
00453       }
00454    }
00455    return Field(n,temp,A.col_struct);
00456 }

BG operator * const BG   a,
double    b
 

Definition at line 189 of file bg.cpp.

00189                                      {
00190     BG r = b * a;
00191     return r;
00192   }

BG operator * double    a,
const BG   b
 

Definition at line 168 of file bg.cpp.

References matvec::BG::D, matvec::BG::d, and matvec::BG::f.

00168                                      {
00169     BG r;
00170     r.f = a*b.f;
00171     r.d = a*b.d;
00172     r.D = a*b.D;
00173     return r;
00174   }

BG operator * const BG   a,
const BG   b
 

Definition at line 99 of file bg.cpp.

References matvec::BG::D, matvec::BG::d, matvec::BG::f, and matvec::Matrix< double >::transpose().

Referenced by operator *().

00099                                         {
00100     BG r;
00101     r.f = a.f * b.f;
00102     r.d = a.f*b.d + a.d*b.f;
00103     r.D = a.f*b.D + a.d*b.d.transpose() + b.d*a.d.transpose() + a.D*b.f;
00104     return r;
00105   }

BG& operator *= BG   a,
double    b
 

Definition at line 229 of file bg.cpp.

00229                                  {
00230     a = a * b;
00231     return a;
00232   }
<