Example #1
0
void matrixDMConstraintWeights(pulsar *psr){
		int i,j,k;
		int nobs=0;
		int nfit=psr->dmoffsDMnum+psr->dmoffsCMnum;
		int nDM=psr->dmoffsDMnum;

		double x;


		if (psr->param[param_dmmodel].fitFlag[0]==1)
		  {
		    logdbg("Getting DM constraints for %s",psr->name);
		    // find out how many obs we have.
		    for(i=0; i < psr->nobs; i++){
		      if (psr->obsn[i].deleted==0)
			{
			  char okay=1;
			  /* Check for START and FINISH flags */
			  if (psr->param[param_start].paramSet[0]==1 && psr->param[param_start].fitFlag[0]==1 &&
			      (psr->param[param_start].val[0] > psr->obsn[i].sat))
			    okay=0;
			  if (psr->param[param_finish].paramSet[0]==1 && psr->param[param_finish].fitFlag[0]==1 &&
			      psr->param[param_finish].val[0] < psr->obsn[i].sat)
			    okay=0;
			  if (okay==1)
			    {
			      nobs++;
			    }
			}
		    }
		// originally was sizeof(double*)*nobs.
		double ** designMatrix=(double**)malloc_uinv(nobs);
		double *e=(double*)malloc(sizeof(double)*nobs);

		nobs=0;
		//		printf("c0: %d %s\n",psr->nobs,psr->name);
		for(i=0; i < psr->nobs; i++){
		  // Check for "ok" and start/finish coppied from dofit.
		  // Needs to be the same so that the weighting is the same as the fit,
		  // but in practice probably doesn't matter.
		  //		  printf("c1 checking: %d %d %s\n",psr->obsn[i].deleted,psr->nobs,psr->name);
		  if (psr->obsn[i].deleted==0)
		    {
		      char okay=1;
		      
		      /* Check for START and FINISH flags */
		      if (psr->param[param_start].paramSet[0]==1 && psr->param[param_start].fitFlag[0]==1 &&
			  (psr->param[param_start].val[0] > psr->obsn[i].sat))
			okay=0;
		      if (psr->param[param_finish].paramSet[0]==1 && psr->param[param_finish].fitFlag[0]==1 &&
			  psr->param[param_finish].val[0] < psr->obsn[i].sat)
			okay=0;

		      //		  printf("c2 checking: %d\n",okay);
		      if (okay==1)
			{
			  x   = (double)(psr->obsn[i].bbat-psr->param[param_pepoch].val[0]);
			  double sig=psr->obsn[i].toaErr*1e-6;
			  double dmf = 1.0/(DM_CONST*powl(psr->obsn[i].freqSSB/1.0e6,2));
			  for (k=0; k < nfit;k++){
			    
			    if(k<nDM)
			      designMatrix[nobs][k]=dmf*getParamDeriv(psr,i,x,param_dmmodel,k)/sig;
			    else
			      designMatrix[nobs][k]=getParamDeriv(psr,i,x,param_dmmodel,k)/sig;
			  }
			  nobs++;
			  
			}
		    }
		} 
		printf("Calling TKleastSquares %d %d\n",nobs,nfit); fflush(stdout);
		TKleastSquares(NULL,NULL,designMatrix,designMatrix,nobs,nfit,1e-20,0,NULL,e,NULL);
		printf("Returning from calling TKleastSquares\n"); fflush(stdout);
		double sum_wDM=0;
		double sum_wCM=0;
		for (i=0;i<nfit;i++)
		  {
		    double sum=0.0;
		    if(i < nDM){
		      psr->dmoffsDM_weight[i]=1.0/e[i]/e[i];
		      printf("constraints.C here: %d %g\n",i,e[i]);
		      sum_wDM+=psr->dmoffsDM_weight[i];
		    } else {
		      psr->dmoffsCM_weight[i-nDM]=1.0/e[i]/e[i];
		      sum_wCM+=psr->dmoffsCM_weight[i-nDM];
		    }
		    
		    
		    
		  }
		//normalise the weights
		for (i=0;i<psr->dmoffsDMnum;i++)
		  psr->dmoffsDM_weight[i]/=sum_wDM;
		for (i=0;i<psr->dmoffsCMnum;i++)
		  psr->dmoffsCM_weight[i]/=sum_wCM;
		
		// free everything .
		free_uinv(designMatrix);
		free(e);
		  }
}
Example #2
0
/**
 * Do the least squares using QR decomposition
 */
double accel_lsq_qr(double** A, double* data, double* oparam, int ndata, int nparam, double** Ocvm){
    int nhrs=1;
    int nwork = -1;
    int info=0;
    int i,j;
    double iwork;

    // workspace query - works out optimal size for work array
    F77_dgels("T", &nparam, &ndata, &nhrs, A[0], &nparam, data, &ndata, &iwork, &nwork, &info);
    nwork=(int)iwork;
    logdbg("nwork = %d (%lf)",nwork,iwork);
    double* work = static_cast<double*>(malloc(sizeof(double)*nwork));
    logdbg("accel_lsq_qr ndata=%d nparam=%d",ndata,nparam);

    // as usual we prentend that the matrix is transposed to deal with C->fortran conversion
    // degls does a least-squares using QR decomposition. Fast and robust.
    F77_dgels("T", &nparam, &ndata, &nhrs, A[0], &nparam, data, &ndata, work, &nwork, &info);

    free(work);
    // if info is not zero then the fit failed.
    if(info!=0){
        logerr("Error in lapack DEGLS. INFO=%d See full logs for explanation.",info);
        logmsg("");
        logmsg("From: http://www.netlib.org/lapack/explore-html/d8/dde/dgels_8f.html");
        logmsg("INFO is INTEGER");
        logmsg("  = 0:  successful exit");
        logmsg("  < 0:  if INFO = -i, the i-th argument had an illegal value");
        logmsg("  > 0:  if INFO =  i, the i-th diagonal element of the");
        logmsg("        triangular factor of A is zero, so that A does not have");
        logmsg("        full rank; the least squares solution could not be");
        logmsg("        computed.");
        logmsg("");
        if(info > 0){
            logerr("It appears that you are fitting for a 'bad' parameter - E.g A jump on a non-existant flag.");
            logmsg(" TEMPO2 will NOT attempt to deal with this!");
            logmsg("Cannot continue. Abort fit.");
            return -1;
        } else {
            logmsg("Cannot continue. Abort fit.");
            return -1;
        }
    }
    assert(info==0);

    if(oparam!=NULL){
        // copy out the output parameters, which are written into the "data" array.
        memcpy(oparam,data,sizeof(double)*nparam);
    }
    double chisq=0;
    for( i = nparam; i < ndata; i++ ) chisq += data[i] * data[i];
    if (Ocvm != NULL){

        int n=nparam;


        // packed triangular matrix.
        double* _t=(double*)malloc(sizeof(double)*(n*(n+1))/2);

        // This code taken from the LAPACK documentation
        // to pack a triangular matrix.

        // we want the upper triangular matrix part of A.
        //
        // pack upper triangle like (r,c)
        // (1,1) (1,2) (2,2)
        //  i+(2n-j)(j-1)/2
        int jc=0;
        for (j=0;j<n;j++){ // cols
            for (i=0; i <=j; i++) { // rows
                _t[jc] = A[i][j]; // A came from fortran, so is in [col][row] ordering
                // BUT - we have transposed A, so we have to un-transpose it
                ++jc;
            }
        }

        logdbg("Inverting...");
        F77_dtptri("U","N",&n,_t,&i);
        if(i!=0){
            logerr("Error in lapack DTPTRI. INFO=%d",i);
            logmsg("From: http://www.netlib.org/lapack/explore-html/d8/d05/dtptri_8f.html");
            logmsg("INFO is INTEGER");
            logmsg("  = 0:  successful exit");
            logmsg("  < 0:  if INFO = -i, the i-th argument had an illegal value");
            logmsg("  > 0:  if INFO = i, A(i,i) is exactly zero.  The triangular");
            logmsg("  matrix is singular and its inverse can not be computed.");
            logmsg("Cannot continue - abort fit");
            return -1;
        }

        double **Rinv = malloc_uinv(n);

        for (j=0;j<n;j++){ // cols
            for (i=0;i<n;i++){ //rows
                Ocvm[i][j]=0;
            }
        }
        // Unpack the triangular matrix using reverse of above
        // We will put it in fortran, so continue to use [col][row] order
        jc=0;
        for (j=0;j<n;j++){ // cols
            for (i=0; i <=j; i++) { // rows
                Rinv[j][i] = _t[jc];
                Ocvm[j][i] = _t[jc];
                ++jc;
            }
        }

        free(_t);

        double a=chisq/(double)(ndata-nparam);
        // (X^T X)^-1 = Rinv.Rinv^T gives parameter covariance matrix
        // Note that Ocvm is input and output
        // and that covar matrix will be transposed, but it is
        // symetric so it doesn't matter!
        F77_dtrmm(  "R",  "U",    "T",  "N", &n, &n,    &a, *Rinv,  &n, *Ocvm, &n);
        // DTRMM ( SIDE, UPLO, TRANSA, DIAG,  M,  N, ALPHA,  A   , LDA,     B, LDB )

        if(debugFlag){
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    logdbg("COVAR %d %d %lg",i,j,Ocvm[i][j]);
                }
            }
        }

        free_uinv(Rinv);
    }


    return chisq;
}