comparison hmm/hmm.c @ 44:00603b8a940f

* Add direct support for ATLAS version of CLAPACK
author cannam
date Wed, 13 Feb 2008 12:49:47 +0000
parents a251fb0de594
children 03abd5957853
comparison
equal deleted inserted replaced
43:b4921bfd2aea 44:00603b8a940f
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <math.h> 11 #include <math.h>
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <float.h> 13 #include <float.h>
14 #include <time.h> /* to seed random number generator */ 14 #include <time.h> /* to seed random number generator */
15
15 #include <clapack.h> /* LAPACK for matrix inversion */ 16 #include <clapack.h> /* LAPACK for matrix inversion */
17
18 #ifdef ATLAS_ORDER
19 #define HAVE_ATLAS 1
20 #endif
21
22 #ifdef HAVE_ATLAS
23 // Using ATLAS C interface to LAPACK
24 #define dgetrf_(m, n, a, lda, ipiv, info) \
25 clapack_dgetrf(CblasColMajor, *m, *n, a, *lda, ipiv)
26 #define dgetri_(n, a, lda, ipiv, work, lwork, info) \
27 clapack_dgetri(CblasColMajor, *n, a, *lda, ipiv)
28 #endif
29
16 #ifdef _MAC_OS_X 30 #ifdef _MAC_OS_X
17 #include <vecLib/cblas.h> 31 #include <vecLib/cblas.h>
18 #else 32 #else
19 #include <cblas.h> /* BLAS for matrix multiplication */ 33 #include <cblas.h> /* BLAS for matrix multiplication */
20 #endif 34 #endif
669 int i, j; 683 int i, j;
670 for(j=0; j < L; j++) 684 for(j=0; j < L; j++)
671 for (i=0; i < L; i++) 685 for (i=0; i < L; i++)
672 a[j*L+i] = cov[i][j]; 686 a[j*L+i] = cov[i][j];
673 687
674 long M = (long) L; 688 int M = (int) L;
675 long* ipiv = (long*) malloc(L*L*sizeof(int)); 689 int* ipiv = (int *) malloc(L*L*sizeof(int));
676 long ret; 690 int ret;
677 691
678 /* LU decomposition */ 692 /* LU decomposition */
679 ret = dgetrf_(&M, &M, a, &M, ipiv, &ret); /* ret should be zero, negative if cov is singular */ 693 ret = dgetrf_(&M, &M, a, &M, ipiv, &ret); /* ret should be zero, negative if cov is singular */
680 if (ret < 0) 694 if (ret < 0)
681 { 695 {
698 if (det < 0) 712 if (det < 0)
699 det = -det; 713 det = -det;
700 *detcov = det; 714 *detcov = det;
701 715
702 /* allocate required working storage */ 716 /* allocate required working storage */
703 long lwork = -1; 717 #ifndef HAVE_ATLAS
704 double lwbest; 718 int lwork = -1;
719 double lwbest = 0.0;
705 dgetri_(&M, a, &M, ipiv, &lwbest, &lwork, &ret); 720 dgetri_(&M, a, &M, ipiv, &lwbest, &lwork, &ret);
706 lwork = (long) lwbest; 721 lwork = (int) lwbest;
707 double* work = (double*) malloc(lwork*sizeof(double)); 722 double* work = (double*) malloc(lwork*sizeof(double));
723 #endif
708 724
709 /* find inverse */ 725 /* find inverse */
710 dgetri_(&M, a, &M, ipiv, work, &lwork, &ret); 726 dgetri_(&M, a, &M, ipiv, work, &lwork, &ret);
711 727
712 for(j=0; j < L; j++) 728 for(j=0; j < L; j++)
713 for (i=0; i < L; i++) 729 for (i=0; i < L; i++)
714 icov[i][j] = a[j*L+i]; 730 icov[i][j] = a[j*L+i];
715 731
732 #ifndef HAVE_ATLAS
716 free(work); 733 free(work);
734 #endif
717 free(a); 735 free(a);
718 } 736 }
719 737
720 /* probability of multivariate Gaussian given mean, inverse and determinant of covariance */ 738 /* probability of multivariate Gaussian given mean, inverse and determinant of covariance */
721 double gauss(double* x, int L, double* mu, double** icov, double detcov, double* y, double* z) 739 double gauss(double* x, int L, double* mu, double** icov, double detcov, double* y, double* z)