WMAP_5yr_tetbeebbeb_pixlike.cc

Go to the documentation of this file.
00001 /****************************************************************
00002  *   This file is part of the C++ port of the Fortran likelihood
00003  *   code for the WMAP 5yr release provided by the WMAP team
00004  *   at http://lambda.gsfc.nasa.gov/ .
00005  *
00006  *   The code was ported by Georg Robbers for easier
00007  *   interfacing with cmbeasy (http://www.cmbeasy.org).
00008  *   Bugs in this port should be reported to the
00009  *   cmbeasy authors (bugs@cmbeasy.org).
00010  ****************************************************************/
00011 #include "WMAP_5yr_tetbeebbeb_pixlike.h"
00012 #include "WMAP_5yr_util.h"
00013 #include "WMAP_5yr_options.h"
00014 
00015 #include "read_archive_map.h"
00016 #include "read_fits.h"
00017 
00018 #include "gsl/gsl_linalg.h"
00019 #include "gsl/gsl_blas.h"
00020 #include "gsl/gsl_cblas.h"
00021 
00022 #include <fstream>
00023 #include <iostream>
00024 
00025 // ===========================================================================
00026 namespace  wmap_tetbeebbeb_lowl
00027 {
00028 
00029 // This code calculates the likelihood function of Q/U
00030 // polarization for TE/TB/EE/BB/EB signal plus noise.
00031 //
00032 // Use combined 3yr data.
00033 // - Combined Ninv and weighted maps have been generated by
00034 // maps/weight_degrade_coadd_p2.f90
00035 //
00036 // - NinvPlNinv has been generated by
00037 // ninv/generate_ninvplninv_r3_p2.f90
00038 //
00039 // E. Komatsu, October 4, 2005
00040 // Incorporated into likelihood code October 7th RB.
00041 // BB has been added, November 6th EK.
00042 //
00043 // - Implementation of David's likelihood
00044 // Ek, December 16, 2005
00045 //
00046 // << MODIFICATION HISTORY AFTER RELEASE on March 16, 2006 >>
00047 //
00048 // E. Komatsu, June 18, 2006
00049 // -- Use alm^tt from the released 3yr ILC map (wmap_ilc_3yr_v2.fits)
00050 // -- Marginalization over the polarized foreground
00051 //
00052 // E. Komatsu, February 3, 2007
00053 // -- 5-yr version
00054 // -- Use double-precision LAPACK
00055 //
00056 // E. Komatsu, December 25, 2007
00057 // -- TB & EB have been added as optional
00058 // -- Foreground marginalization done to N^{-1} outside of this code
00059 //
00060 // ===========================================================================
00061 
00062 int  nsmax,nlmax,mp;
00063 long int np;
00064 #ifdef OPTIMIZE
00065 real8_1d xxx, yyy;
00066 real4_2d ninvplninv2;
00067 real4_3d ninvplninv3;
00068 real8_3d ninvplninv2_double;
00069 real8_3d ninvplninv3_double;
00070 #else
00071 real4_3d ninvplninv1;
00072 real4_3d ninvplninv2;
00073 real8_3d ninvplninv1_double;
00074 real8_3d ninvplninv2_double;
00075 #endif
00076 
00077 Array1D<int>  ngood;
00078 Matrix<REAL_8>  Dp0;
00079 Array1D<REAL_8>  m_r3,w_r3,p_r3,f_r3,zzz;
00080 Matrix<COMPLEX>  alm_tt,NinvYe, NinvYb;
00081 Array1D<double>  wl;
00082 //static const REAL sig_temp = 0.01;
00083 static const REAL sig_temp = 0.0; // FG marginalization done outside of this code
00084 
00085 
00086 
00087 //===========================================
00088 void  tetbeebbeb_lowl_like_setup()
00089 {
00090   using namespace std;
00091   //===========================================
00092   using namespace wmap_util;
00093   string WMAP_data_dir=WMAP_OPTIONS::self()->WMAP_data_dir();
00094 
00095   //string(LEN=2)  rlz;
00096   static const int ires = 3;
00097   real_1d  T,N,Mask_R3;
00098   //string(len=3)  da(10),sband;
00099   int  ReadStatus;
00100   string qfile,ufile,maskfile,filename[13], tetbeebbebdir;
00101 
00102   int  nsmax,nlmax,l,ip,jp,m,stat,i,j;
00103   unsigned int k;
00104   real_2d NinvQUr3;
00105 
00106 #ifdef TIMING
00107   wmap_timing_start( "tetbeebbeb_lowl_like_setup" );
00108 #endif
00109 
00110   //------------------------------
00111   // Set initial parameters
00112   //-----------------------------
00113 
00114   //X   da=(/\"K1\",\"Ka\",\"Q1\",\"Q2\",\"V1\",\"V2\",\"W1\",\"W2\",\"W3\",\"W4\"/)
00115   //X   da(2)=\"Ka1\";
00116 
00117 
00118   nsmax = (int)pow( 2.,ires );
00119   nlmax = 3*nsmax-1; // using Nyquist sampling rather than 2*nsmax
00120   np = 12*nsmax*nsmax;
00121 
00122   WMAP_OPTIONS* options = WMAP_OPTIONS::self();
00123 
00124   if ( options->using_gibbs_pol_cleaning() )  {
00125     tetbeebbebdir = WMAP_data_dir+"lowlP/gibbs/";
00126     filename[0]=tetbeebbebdir+"gibbs_masked_ee_ninvplninv_qu_r3_5yr.fits";
00127     filename[1]=tetbeebbebdir+"gibbs_masked_bb_ninvplninv_qu_r3_5yr.fits";
00128     filename[10]=tetbeebbebdir+"tbeb/gibbs_masked_eb_ninvplninv_qu_r3_5yr.fits";
00129     filename[11]=tetbeebbebdir+"tbeb/gibbs_masked_be_ninvplninv_qu_r3_5yr.fits";
00130 
00131     filename[2]=tetbeebbebdir+"gibbs_masked_ninv_qu_r3_5yr.fits";
00132     filename[3]=tetbeebbebdir+"gibbs_wt_r3_5yr.map_q";
00133     filename[4]=tetbeebbebdir+"gibbs_wt_r3_5yr.map_u";
00134     filename[6]=tetbeebbebdir+"gibbs_masked_ninvy_e_qu_r3_5yr.fits";
00135     filename[12]=tetbeebbebdir+"tbeb/gibbs_masked_ninvy_b_qu_r3_5yr.fits";
00136   } else {
00137     tetbeebbebdir = WMAP_data_dir+"lowlP/std/";
00138     filename[0]=tetbeebbebdir+"masked_ee_ninvplninv_qu_r3_corrected_5yr.KaQV.fits";
00139     filename[1]=tetbeebbebdir+"masked_bb_ninvplninv_qu_r3_corrected_5yr.KaQV.fits";
00140     filename[10]=tetbeebbebdir+"tbeb/masked_eb_ninvplninv_qu_r3_corrected_5yr.KaQV.fits";
00141     filename[11]=tetbeebbebdir+"tbeb/masked_be_ninvplninv_qu_r3_corrected_5yr.KaQV.fits";
00142 
00143     filename[2]=tetbeebbebdir+"masked_ninv_qu_r3_corrected_5yr.KaQV.fits";
00144     filename[3]=tetbeebbebdir+"wt_r3_5yr.KaQV.map_q";
00145     filename[4]=tetbeebbebdir+"wt_r3_5yr.KaQV.map_u";
00146     filename[6]=tetbeebbebdir+"masked_ninvy_e_qu_r3_corrected_5yr.KaQV.fits";
00147     filename[12]=tetbeebbebdir+"tbeb/masked_ninvy_b_qu_r3_corrected_5yr.KaQV.fits";
00148   }
00149   filename[5]=WMAP_data_dir+"lowlP/alm_tt_fs_r9_ilc_nopixwin_5yr.dat";
00150   filename[9]=WMAP_data_dir+"healpix_data/pixel_window_n0008.txt";
00151 
00152   tetbeebbebdir = WMAP_data_dir+"lowlP/std/";
00153 
00154     //------------------------------
00155     // read in res3 mask
00156     //------------------------------
00157 
00158   N.resize( Range( 0, np-1 ) );
00159 
00160   maskfile=WMAP_data_dir+"lowlP/mask_r3_p06_jarosik.fits";
00161   ifstream testStream( maskfile.c_str() );
00162   if (!testStream.good())
00163   {
00164     cout << "tetbeebbeb maskfile not found " << maskfile << endl;
00165   }
00166   Mask_R3.resize(np);
00167   T.resize(np);
00168   long int dum;
00169   Read_Archive_Map(maskfile, T, Mask_R3,dum,ReadStatus);
00170   if (ReadStatus!=0)
00171   {
00172     cout << "unable to read in mask" << maskfile << endl;
00173     exit( -1 );
00174   }
00175 
00176   ngood.resize( np );
00177   mp = 0;
00178   for (ip=0; ip<= np-1; ++ip)
00179   {
00180     if (Mask_R3(ip)!=0)
00181     {
00182       ngood(mp) = ip;
00183       mp = mp + 1;
00184     }
00185   }
00186 
00187   //------------------------------
00188   // read in N^{-1}P_l N^{-1} at res3
00189   //------------------------------
00190 
00191   string errorString;
00192   try
00193   {
00194 
00195 #ifdef OPTIMIZE
00196     errorString="ninvplninv2";
00197     ninvplninv2.resize(Range(1,mp*(2*mp+1)), Range(1,4*(nlmax-1)));
00198 
00199     errorString="ninvplninv3";
00200 
00201    Read_FITS_REAL_3D (filename[0], ninvplninv3, stat);
00202    if (stat  !=  0)  {
00203      cout <<  "Error " << stat << " while reading " << filename[0] << endl ;
00204      exit(-1);
00205    }
00206 
00207 
00208 
00209    k = 1;
00210    for (i = 0; i <= 2*mp-1; ++i ) {
00211      for (j = i; j <= 2*mp-1; ++j ) {
00212        ip = ngood(i%mp) + np*(i/mp);
00213        jp = ngood(j%mp) + np*(j/mp);
00214 
00215        for (int l=1; l <=nlmax-1; ++l) ninvplninv2(k,l) = ninvplninv3(l+1,ip,jp);
00216        k = k + 1;
00217      }
00218    }
00219 
00220   Read_FITS_REAL_3D (filename[1], ninvplninv3, stat);
00221   if (stat  !=  0)  {
00222     cout << "Error " << stat << " while reading " << filename[1] << endl;
00223     exit(-1);
00224   }
00225 
00226   k = 1;
00227   for (i = 0; i <= 2*mp-1; ++i ) {
00228       for (j = i; j <= 2*mp-1; ++j ) {
00229           ip = ngood((i%mp)) + np*(i/mp);
00230           jp = ngood((j%mp)) + np*(j/mp);
00231 
00232           for (int l=nlmax; l <=2*(nlmax-1); ++l) ninvplninv2(k,l) = ninvplninv3(l-nlmax+2,ip,jp);
00233           k = k + 1;
00234       }
00235   }
00236 
00237   Read_FITS_REAL_3D (filename[10], ninvplninv3, stat);
00238   if (stat  !=  0)  {
00239     cout << "Error " << stat << " while reading " << filename[10] << endl;
00240     exit(-1);
00241   }
00242 
00243   k = 1;
00244   for (i = 0; i <= 2*mp-1; ++i ) {
00245       for (j = i; j <= 2*mp-1; ++j ) {
00246           ip = ngood((i%mp)) + np*(i/mp);
00247           jp = ngood((j%mp)) + np*(j/mp);
00248 
00249           for (int l=2*nlmax-1; l <=3*(nlmax-1); ++l) ninvplninv2(k,l) = ninvplninv3(l-(2*nlmax-1)+2,ip,jp);
00250           k = k + 1;
00251       }
00252   }
00253 
00254   Read_FITS_REAL_3D (filename[11], ninvplninv3, stat);
00255   if (stat  !=  0)  {
00256     cout << "Error " << stat << " while reading " << filename[1] << endl;
00257     exit(-1);
00258   }
00259 
00260   k = 1;
00261   for (i = 0; i <= 2*mp-1; ++i ) {
00262       for (j = i; j <= 2*mp-1; ++j ) {
00263           ip = ngood((i%mp)) + np*(i/mp);
00264           jp = ngood((j%mp)) + np*(j/mp);
00265 
00266           for (int l=3*nlmax-2; l <=4*(nlmax-1); ++l) ninvplninv2(k,l) = ninvplninv3(l-(3*nlmax-2)+2,ip,jp);
00267           k = k + 1;
00268       }
00269   }
00270 
00271   ninvplninv3.free();
00272 
00273   xxx.resize(Range(1,4*(nlmax-1)));
00274   yyy.resize(Range(1,mp*(2*mp+1)));
00275 
00276 #else
00277 
00278     errorString = "ninvplninv";
00279 
00280     Read_FITS_REAL_3D (filename[0], ninvplninv1, stat);
00281     if (stat  !=  0)
00282     {
00283       cout <<  "Error " << stat << " while reading " << filename[0] << endl ;
00284       exit(-1);
00285     }
00286 
00287     Read_FITS_REAL_3D(filename[1], ninvplninv2, stat);
00288     if (stat  !=  0)
00289     {
00290       cout <<  "Error " << stat << " while reading " << filename[1] << endl;
00291       exit( -1 );
00292     }
00293 
00294     Read_FITS_REAL_3D(filename[10], ninvplninv3, stat);
00295     if (stat  !=  0)
00296     {
00297       cout <<  "Error " << stat << " while reading " << filename[10] << endl;
00298       exit( -1 );
00299     }
00300 
00301     Read_FITS_REAL_3D(filename[11], ninvplninv4, stat);
00302     if (stat  !=  0)
00303     {
00304       cout <<  "Error " << stat << " while reading " << filename[11] << endl;
00305       exit( -1 );
00306     }
00307 #endif
00308 
00309     //------------------------------
00310     // read in N^{-1} at res3
00311     //------------------------------
00312     errorString = "NinvQUr3";
00313 
00314     Read_FITS_REAL_2D (filename[2], NinvQUr3, stat);
00315     if (stat  !=  0)
00316     {
00317       cout <<  "Error " << stat << " while reading " << filename[2] << endl ;
00318       exit(-1);
00319     }
00320 
00321     //------------------------------
00322     // read in maps at res3
00323     //------------------------------
00324 
00325     errorString = "w_r3, m_r3, p_r3 or zzz";
00326     w_r3.resize(2*mp);
00327     m_r3.resize(2*mp);
00328     p_r3.resize(2*mp);
00329     zzz.resize(2*mp);
00330     qfile=filename[3];
00331     Read_Archive_Map(qfile,T,N,np,ReadStatus);
00332     if (ReadStatus!=0)
00333     {
00334       cout << "unable to read in " << qfile << endl;
00335       exit( -1 );
00336     }
00337 
00338     for (ip=0; ip<= mp-1; ++ip)
00339     {
00340       w_r3(ip) = T(ngood(ip))*Mask_R3(ngood(ip));
00341     }
00342 
00343     ufile=filename[4];
00344     T.resize(np);
00345     N.resize(np);
00346     Read_Archive_Map(ufile,T,N,np,ReadStatus);
00347     if (ReadStatus!=0)
00348     {
00349       cout << "unable to read in " << ufile << endl;
00350       exit( -1 );
00351     }
00352 
00353     for (ip=0; ip<= mp-1; ++ip)
00354     {
00355       w_r3(mp+ip) = T(ngood(ip))*Mask_R3(ngood(ip));
00356     }
00357 
00358     Dp0.resize(2*mp,2*mp);
00359 
00360     for (ip=0; ip<= mp-1; ++ip)
00361     {
00362       for (jp=0; jp<= mp-1; ++jp)
00363       {
00364         Dp0(ip,jp) = NinvQUr3(ngood(ip),ngood(jp));
00365         Dp0(ip,mp+jp) = NinvQUr3(ngood(ip),np+ngood(jp));
00366         Dp0(mp+ip,jp) = NinvQUr3(np+ngood(ip),ngood(jp));
00367         Dp0(mp+ip,mp+jp) = NinvQUr3(np+ngood(ip),np+ngood(jp));
00368       }
00369     }
00370 
00371   //------------------------------
00372   // read in FG templates at res3 [NO LONGER USED: 12/25/07, EK]
00373   //------------------------------
00374 
00375   errorString="f_r3";
00376   f_r3.resize(Range(0,2*mp-1));
00377 
00378   f_r3 = 0.0;
00379   if (sig_temp != 0) {
00380     cout <<  "Using foreground templates!!!" << endl;
00381     cout <<  "----------------------------------------------------------------------" << endl;
00382     exit(-1);
00383 
00384     qfile=filename[7];
00385     Read_Archive_Map(qfile,T,N,np,ReadStatus);
00386     if (ReadStatus!=0) {
00387       cout << "unable to read in " << qfile << endl;
00388       exit(-1);
00389     }
00390 
00391     for (ip=0; ip<= mp-1; ++ip) {
00392       f_r3(ip) = T(ngood(ip))*Mask_R3(ngood(ip));
00393     }
00394 
00395     ufile=filename[8];
00396     Read_Archive_Map(ufile,T,N,np,ReadStatus);
00397     if (ReadStatus!=0)  {
00398       cout << "unable to read in " << ufile << endl;
00399       exit(-1);
00400     }
00401 
00402     for (ip=0; ip<= mp-1; ++ip) {
00403       f_r3(mp+ip) = T(ngood(ip))*Mask_R3(ngood(ip));
00404     }
00405   }
00406 
00407   //------------------------------
00408   // read in alm_tt
00409   //------------------------------
00410   errorString = "alm_tt";
00411   alm_tt.resize( nlmax+1, nlmax+1 );
00412   ifstream lun( filename[5].c_str() );
00413   for (l=0; l<= nlmax; ++l)
00414   {
00415     for (m=0; m<= l; ++m)
00416     {
00417       lun >> alm_tt(l,m);
00418     }
00419   }
00420   lun.close();
00421 
00422   //------------------------------
00423   // read in Ninv Y
00424   //------------------------------
00425   errorString = "NinvYe";
00426   NinvYe.resize(1536,300);
00427 
00428   Read_FITS_Complex_2D_LM (filename[6], NinvYe, stat, 0 /* IndFmt, was 2*/);
00429   if (stat  !=  0)
00430   {
00431     cout <<  "Error " << stat << " while reading " << filename[6] << endl;
00432     exit( -1 );
00433   }
00434   NinvYe.reindexSelf(0, 1); // instead of the IndFmt=2 above
00435 
00436   errorString = "NinvYb";
00437   NinvYb.resize(1536,300);
00438 
00439   Read_FITS_Complex_2D_LM (filename[12], NinvYb, stat, 0 /* IndFmt, was 2*/);
00440   if (stat  !=  0)
00441   {
00442     cout <<  "Error " << stat << " while reading " << filename[12] << endl;
00443     exit( -1 );
00444   }
00445   NinvYb.reindexSelf(0, 1); // instead of the IndFmt=2 above
00446 
00447   //
00448   // read in pixel window
00449   //
00450   wl.resize( Range(0, nlmax ) );
00451 
00452   ifstream wlfile(filename[9].c_str());
00453   for ( int i = 0; i <= nlmax; ++i )
00454       wlfile >> wl(i);
00455   }
00456   catch ( std::bad_alloc )
00457   {
00458     cout << "Memory Allocation error for: " << errorString << endl;
00459     exit( -1 );
00460   }
00461 
00462 #ifdef TIMING
00463   wmap_timing_end();
00464 #endif
00465 
00466 } //  tetbeebbeb_lowl_like_setup
00467 
00468 
00469 unsigned int tetbeebbeb_pixlike_dof()
00470 {
00471   return 2*mp;
00472 }
00473 
00474 //===========================================
00475 void  tetbeebbeb_lowl_likelihood(int nlmaxin, real8_1d& Clttin, real8_1d& Cltein, real8_1d& Cltbin,
00476                                  real8_1d& Cleein, real8_1d& Clbbin, real8_1d& Clebin,
00477                                  REAL_8& chisq_r3, REAL_8& lndet)
00478 {
00479 
00480   //=======================================
00481   using namespace wmap_util;
00482   using namespace std;
00483 
00484   const double teeebb_pixlike_lndet_offset= WMAP_OPTIONS::self()->teeebb_pixlike_lndet_offset();
00485 #ifndef OPTIMIZE
00486   Matrix<REAL_8> NinvSNinv;
00487   Matrix<REAL_8> CQU;
00488 #endif
00489   Matrix<REAL_8> Dp;
00490   Array1D<REAL_8> Clee,Clbb,Cltt,Clte, Cltb, Cleb;
00491   int  ip,jp,nlmax,info,l,m,i,k;
00492   REAL Omega_pix;
00493 
00494 #ifdef TIMING
00495   wmap_timing_start( "tetbeebbeb_lowl_likelihood" );
00496 #endif
00497 
00498   nlmax = 23;
00499   Omega_pix = 3.14159/(3.*8.*8.);
00500   if (nlmaxin != nlmax)
00501   {
00502     cout << "need nlmax" << nlmax << ", in tetbeebbeb likelihood, currently" << nlmaxin;
00503     exit(0);
00504   }
00505 
00506   Cltt.resize(Range(2,nlmax));
00507   Clte.resize(Range(2,nlmax));
00508   Clee.resize(Range(2,nlmax));
00509   Clbb.resize(Range(2,nlmax));
00510   Cltb.resize(Range(2,nlmax));
00511   Cleb.resize(Range(2,nlmax));
00512   for (l=2; l<= nlmax; ++l)
00513   {
00514     double wl2 = wl(l)*wl(l);
00515     Cltt(l) = Clttin(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00516     Clte(l) = Cltein(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00517     Clee(l) = Cleein(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00518     Clbb(l) = Clbbin(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00519     Cltb(l) = Cltbin(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00520     Cleb(l) = Clebin(l)/double(l*(l+1))*(2.*3.14159)*1.e-6*wl2;
00521   }
00522 
00523   //------------------------------
00524   // compute [N^{-1}S N^{-1} + N^{-1}]^{-1}
00525   //------------------------------
00526 
00527 
00528   lndet = 0.;
00529 
00530 #ifdef OPTIMIZE
00531   Dp.resize(Range(0,2*mp-1),Range(0,2*mp-1));
00532 
00534 
00535   for (l = 2; l <= nlmax; ++l ) {
00536     xxx(l-1) = Clee(l)-Clte(l)*Clte(l)/Cltt(l);
00537     xxx(l+nlmax-2) = Clbb(l)-Cltb(l)*Cltb(l)/Cltt(l);
00538     xxx(l+2*nlmax-3) = Cleb(l)-Clte(l)*Cltb(l)/Cltt(l);
00539     xxx(l+3*nlmax-4) = Cleb(l)-Cltb(l)*Clte(l)/Cltt(l);
00540   }
00541 
00542 //X call DGEMV( 'N', mp*(2*mp+1), 2*(nlmax-1), 1.d0, DBLE(ninvplninv2), &
00543 //X              mp*(2*mp+1), xxx, 1, 0.d0, yyy, 1 )
00544   yyy = 0.;
00545   for (unsigned int i = 0; i < ninvplninv2.rows(); ++i)
00546       for (unsigned int j = 0; j < ninvplninv2.cols(); ++j)
00547           *(yyy.data()+i) += double(ninvplninv2(i+1,j+1))*(*(xxx.data()+j));
00548 
00549   k = 1;
00550   for (ip = 0; ip <= 2*mp-1; ++ip ) {
00551       for (jp = ip; jp <= 2*mp-1; ++jp ) {
00552           Dp(ip,jp) = Dp0(ip,jp) + yyy(k);
00553               k = k + 1;
00554       }
00555   }
00556 
00557 #else
00558   //
00559   // fill only the upper triangular part of N^{-1}SN^{-1}.
00560   // add the foreground error term.
00561 
00562   NinvSNinv.resize( 2*mp, 2*mp);
00563   NinvSNinv = 0.;
00564 
00565   double* eetefact = new double[nlmax+1];
00566   double* bbtbfact = new double[nlmax+1];
00567   double* ebtefact = new double[nlmax+1];
00568   double* ebtbfact = new double[nlmax+1];
00569   for ( int i = 2; i <= nlmax; ++i ) {
00570     eetefact[i] = (Clee(i)- Clte(i)*Clte(i)/Cltt(i));
00571     bbtbfact[i] = (Clbb(i)- Cltb(i)*Cltb(i)/Cltt(i));
00572     ebtefact[i] = (Cleb(i)- Clte(i)*Cltb(i)/Cltt(i));
00573     ebtbfact[i] = (Cleb(i)- Cltb(i)*Clte(i)/Cltt(i));
00574   }
00575   gsl_vector_view eetevw = gsl_vector_view_array( eetefact+2, nlmax-1 );
00576   gsl_vector_ *eetev = &eetevw.vector;
00577   gsl_vector_view bbtbvw = gsl_vector_view_array( bbtbfact+2, nlmax-1 );
00578   gsl_vector_ *bbtbv = &bbtbvw.vector;
00579   gsl_vector_view ebtevw = gsl_vector_view_array( ebtefact+2, nlmax-1 );
00580   gsl_vector_ *ebtev = &ebtevw.vector;
00581   gsl_vector_view ebtbvw = gsl_vector_view_array( ebtbfact+2, nlmax-1 );
00582   gsl_vector_ *ebtbv = &ebtbvw.vector;
00583 
00584   //gsl_vector_view bbvw = gsl_vector_view_array( Clbb.data(), Clbb.size() );
00585   //gsl_vector * bbv = &bbvw.vector;
00586 
00587   gsl_vector_view v1w, v2w, v3w, v4w;
00588   gsl_vector      *v1, *v2, *v3, *v4;
00589 
00590 
00591 
00592   for (ip=0; ip<= mp-1; ++ip)
00593   {
00594     double ddot1, ddot2, ddot3, ddot4;
00595     for (jp=ip; jp<= mp-1; ++jp)
00596     {
00597       v1w = gsl_vector_view_array_with_stride ( &ninvplninv1_double(2,ngood( ip ), ngood( jp )),
00598                                                     ninvplninv1_double.stride(firstDim), nlmax-1 );
00599       v2w = gsl_vector_view_array_with_stride ( &ninvplninv2_double(2,ngood( ip ), ngood( jp ) ),
00600                                                     ninvplninv2_double.stride(firstDim), nlmax-1 );
00601       v3w = gsl_vector_view_array_with_stride ( &ninvplninv3_double(2,ngood( ip ), ngood( jp ) ),
00602                                                     ninvplninv3_double.stride(firstDim), nlmax-1 );
00603       v4w = gsl_vector_view_array_with_stride ( &ninvplninv4_double(2,ngood( ip ), ngood( jp ) ),
00604                                                     ninvplninv4_double.stride(firstDim), nlmax-1 );
00605       v1 = &v1w.vector;
00606       v2 = &v2w.vector;
00607       v3 = &v3w.vector;
00608       v4 = &v4w.vector;
00609       gsl_blas_ddot( eetevw, v1, &ddot1 );
00610       gsl_blas_ddot( bbtbvw, v2, &ddot2 );
00611       gsl_blas_ddot( ebtevw, v3, &ddot3 );
00612       gsl_blas_ddot( ebtbvw, v4, &ddot4 );
00613       NinvSNinv(ip,jp) = ddot1+ddot2+ddot3+ddot4;
00614 
00615       v1w = gsl_vector_view_array_with_stride ( &ninvplninv1_double(2,ngood( ip ),( int )np+ngood( jp )),
00616                                                     ninvplninv1_double.stride(firstDim), nlmax-1 );
00617       v2w = gsl_vector_view_array_with_stride ( &ninvplninv2_double( 2,ngood( ip ),( int )np+ ngood( jp ) ),
00618                                                     ninvplninv2_double.stride(firstDim), nlmax-1 );
00619       v3w = gsl_vector_view_array_with_stride ( &ninvplninv3_double( 2,ngood( ip ),( int )np+ ngood( jp ) ),
00620                                                     ninvplninv3_double.stride(firstDim), nlmax-1 );
00621       v4w = gsl_vector_view_array_with_stride ( &ninvplninv4_double( 2,ngood( ip ),( int )np+ ngood( jp ) ),
00622                                                     ninvplninv4_double.stride(firstDim), nlmax-1 );
00623       v1 = &v1w.vector;
00624       v2 = &v2w.vector;
00625       v3 = &v3w.vector;
00626       v4 = &v4w.vector;
00627       gsl_blas_ddot( eetevw, v1, &ddot1 );
00628       gsl_blas_ddot( bbtbvw, v2, &ddot2 );
00629       gsl_blas_ddot( ebtevw, v3, &ddot3 );
00630       gsl_blas_ddot( ebtbvw, v4, &ddot4 );
00631       NinvSNinv(ip,mp+jp) = ddot1+ddot2+ddot3+ddot4;
00632 
00633       v1w=gsl_vector_view_array_with_stride(&ninvplninv1_double(2,(int)np+ngood(ip),(int)np+ngood(jp)),
00634                                             ninvplninv1_double.stride(firstDim), nlmax-1 );
00635       v2w=gsl_vector_view_array_with_stride(&ninvplninv2_double(2,(int)np+ngood(ip),(int)np+ngood(jp)),
00636                                             ninvplninv2_double.stride(firstDim), nlmax-1 );
00637       v3w=gsl_vector_view_array_with_stride(&ninvplninv3(2,(int)np+ngood(ip),(int)np+ngood(jp)),
00638                                             ninvplninv3.stride(firstDim), nlmax-1 );
00639       v4w=gsl_vector_view_array_with_stride(&ninvplninv4(2,(int)np+ngood(ip),(int)np+ngood(jp)),
00640                                             ninvplninv4.stride(firstDim), nlmax-1 );
00641       v1 = &v1w.vector;
00642       v2 = &v2w.vector;
00643       v3 = &v3w.vector;
00644       v4 = &v4w.vector;
00645       gsl_blas_ddot( eetevw, v1, &ddot1 );
00646       gsl_blas_ddot( bbtbvw, v2, &ddot2 );
00647       gsl_blas_ddot( ebtevw, v3, &ddot3 );
00648       gsl_blas_ddot( ebtbvw, v4, &ddot4 );
00649       NinvSNinv(mp+ip,mp+jp) = ddot1+ddot2+ddot3+ddot4;
00650     }
00651 
00652     for (jp=0; jp<= ip-1; ++jp)
00653     {
00654       v1w = gsl_vector_view_array_with_stride ( &ninvplninv1_double(2,ngood( ip ),( int )np+ngood( jp )),
00655                                                  ninvplninv1_double.stride(firstDim), nlmax-1 );
00656       v2w = gsl_vector_view_array_with_stride ( &ninvplninv2_double(2,ngood( ip ),( int )np+ngood( jp )),
00657                                                 ninvplninv2_double.stride(firstDim), nlmax-1 );
00658       v3w = gsl_vector_view_array_with_stride ( &ninvplninv3_double( 2,ngood( ip ),( int )np+ ngood( jp ) ),
00659                                                 ninvplninv3_double.stride(firstDim), nlmax-1 );
00660       v4w = gsl_vector_view_array_with_stride ( &ninvplninv4_double( 2,ngood( ip ),( int )np+ ngood( jp ) ),
00661                                                 ninvplninv4_double.stride(firstDim), nlmax-1 );
00662 
00663       v1 = &v1w.vector;
00664       v2 = &v2w.vector;
00665       v3 = &v3w.vector;
00666       v4 = &v4w.vector;
00667       gsl_blas_ddot( eetevw, v1, &ddot1 );
00668       gsl_blas_ddot( bbtbvw, v2, &ddot2 );
00669       gsl_blas_ddot( ebtevw, v3, &ddot3 );
00670       gsl_blas_ddot( ebtbvw, v4, &ddot4 );
00671       NinvSNinv(ip,mp+jp) += ddot1+ddot2+ddot3+ddot4;
00672     }
00673   }
00674   delete[] eetefact;
00675   delete[] bbtbfact;
00676   delete[] ebtefact;
00677   delete[] ebtbfact;
00678 
00679   Dp = Dp0 +NinvSNinv ;
00680 
00681   NinvSNinv.free();
00682 #endif
00683 
00684 #ifdef TIMING
00685   wmap_timing_checkpoint( "finished Dp" );
00686 #endif
00687 
00688   gsl_matrix_view DpView = gsl_matrix_view_array( Dp.data(), 2*mp, 2*mp );
00689   gsl_matrix* gslDp = &DpView.matrix;
00690 
00691 
00692 
00693   info = gsl_matrix_transpose( gslDp ); // gsl decomposes into a lower triang. matrix
00694   info = gsl_linalg_cholesky_decomp( gslDp );
00695 
00696   if (info != 0)
00697   {
00698     //wmap_likelihood_error( "tetbeebbeb: bad dpotrf", info );
00699     //chisq_r3 = 0.0;
00700     //lndet = 0.0;
00701     chisq_r3 = 1e10;
00702     lndet = 0.0;
00703     cout <<  "dpotrf failed, info = " << info << endl ;
00704 #ifdef TIMING
00705     wmap_timing_end();
00706 #endif
00707      ofstream lun;
00708      lun.open("debug.txt");
00709      for (l = 2; l <= 23; ++l ) {
00710        lun << l << " " << Cltt(l) << " " << Clte(l) << " " << Cltb(l) << " "
00711            << Clee(l) << " " << Clbb(l) << " " << Cleb(l) << endl;
00712        cout << Cltt(l) << " " << Clte(l) << " " << Cltb(l) << " " << Clee(l)
00713             << " " << Clbb(l) << " " << Cleb(l) << endl;
00714      }
00715      lun.close();
00716      wmap_likelihood_error( "tetbeebbeb: bad dpotrf", info );
00717     // exit(-1);
00718     return;
00719   }
00720 
00721 #ifdef TIMING
00722   wmap_timing_checkpoint( "finished dpotrf" );
00723 #endif
00724 
00725   for (ip=0; ip< 2*mp; ++ip)
00726   {
00727     lndet = lndet + 2.*log( gsl_matrix_get( gslDp, ip, ip ) );
00728   }
00729 
00730 
00731 #ifndef OPTIMIZE
00732   cholesky_invert( gslDp);
00733 #endif
00734 
00735 #ifdef TIMING
00736   wmap_timing_checkpoint( "finished dpotri" );
00737 #endif
00738 
00739   if (info != 0)
00740   {
00741     wmap_likelihood_error( "tetbeebbeb: bad dpotri", info );
00742     chisq_r3 = 0.0;
00743     lndet = 0.0;
00744     return;
00745 
00746   }
00747 
00748 #ifndef OPTIMIZE
00749   gsl_matrix *  gslCQU = gslDp ;
00750   Dp.free();
00751 #endif
00752 
00753 
00754   //-----------------------------------
00755   // calculate the predicted QU at res3
00756   //-----------------------------------
00757   p_r3 = 0.;
00759   for (ip=0; ip<= mp-1; ++ip)
00760   {
00761     i = 3;
00762     for (l=2; l<= 23; ++l)
00763     {
00764       i = i+1;
00765       double tmpe = ( alm_tt(l,0)*NinvYe(ngood(ip),i) ).real();
00766       double tmpb = ( alm_tt(l,0)*NinvYb(ngood(ip),i) ).real();
00767       p_r3(ip)    = p_r3(ip)
00768                    + Clte(l)/Cltt(l)*wl(l)*tmpe
00769                    + Cltb(l)/Cltt(l)*wl(l)*tmpb;
00770       tmpe =( alm_tt(l,0).real()*NinvYe(ngood(ip)+np,i) ).real();
00771       tmpb =( alm_tt(l,0).real()*NinvYb(ngood(ip)+np,i) ).real();
00772       p_r3(ip+mp) = p_r3(ip+mp)
00773                     + Clte(l)/Cltt(l)*wl(l)*tmpe
00774                     + Cltb(l)/Cltt(l)*wl(l)*tmpb;
00775       for (m=1; m<= l; ++m)
00776       {
00777         i = i+1;
00778         tmpe = conj(alm_tt(l,m)*NinvYe(ngood(ip),i)).real();
00779         double tmpke =  ( alm_tt(l,m)*NinvYe(ngood(ip),i) ).real();
00780         tmpb = conj(alm_tt(l,m)*NinvYb(ngood(ip),i)).real();
00781         double tmpkb =  ( alm_tt(l,m)*NinvYb(ngood(ip),i) ).real();
00782         p_r3(ip) = p_r3(ip)
00783                    + Clte(l)/Cltt(l)*wl(l)*( tmpke + tmpe )
00784                    + Cltb(l)/Cltt(l)*wl(l)*( tmpkb + tmpb );
00785         tmpe = conj(alm_tt(l,m)*NinvYe(ngood(ip)+np,i)).real();
00786         tmpke = (alm_tt(l,m)*NinvYe(ngood(ip)+np,i)).real();
00787         tmpb = conj(alm_tt(l,m)*NinvYb(ngood(ip)+np,i)).real();
00788         tmpkb = (alm_tt(l,m)*NinvYb(ngood(ip)+np,i)).real();
00789         p_r3(ip+mp) = p_r3(ip+mp)
00790                       + Clte(l)/Cltt(l)*wl(l)*(tmpke+tmpe)
00791                       + Cltb(l)/Cltt(l)*wl(l)*(tmpkb+tmpb);
00792       }
00793     }
00794   }
00795 
00796 #ifdef OPTIMIZE
00797   m_r3 = w_r3-p_r3;
00798 //X   call DPOTRS( "U", 2*mp, 1, Dp, 2*mp, m_r3, 2*mp, info );
00799   gsl_vector_view m_r3view = gsl_vector_view_array(m_r3.data(), m_r3.size());
00800   gsl_linalg_cholesky_svx(gslDp, &m_r3view.vector);
00801   //chisq_r3 = DDOT(2*mp,m_r3,1,w_r3-p_r3,1) 
00802   zzz = w_r3-p_r3;
00803 //X   chisq_r3 = sum( m_r3*zzz );
00804   int istart = m_r3.firstIndex();
00805   int iend = istart + m_r3.size();
00806   chisq_r3 = 0;
00807   for (int i=istart; i < iend; ++i) chisq_r3+=m_r3(i)*zzz(i);
00808 #else
00809 
00810   gsl_vector *x = gsl_vector_alloc( 2*mp );
00811   gsl_vector *y = gsl_vector_alloc( 2*mp );
00812   for ( i = 0; i < 2*mp; ++i )
00813   {
00814     gsl_vector_set(x, i, w_r3(i)-p_r3(i));
00815     gsl_vector_set(y, i, m_r3(i));
00816   }
00817 
00818   int status = gsl_blas_dsymv( CblasUpper, 1., gslCQU, x, 0., y );
00819 
00820   status = gsl_blas_ddot(x,y,&chisq_r3);
00821 
00822   CQU.free();
00823   gsl_vector_free(x);
00824   gsl_vector_free(y);
00825 #endif
00826 
00827   chisq_r3 = chisq_r3/2.;
00828   lndet = (lndet - teeebb_pixlike_lndet_offset)/2.0;
00829 
00830 #ifdef TIMING
00831   wmap_timing_end();
00832 #endif
00833 
00834 } //  TETBEEBBEB_LOWL_LIKELIHOOD
00835 
00836 
00837 /* doing the cholesky inversion by hand along the lines of LAPACK */
00838 void cholesky_invert( gsl_matrix* A)
00839 {
00840 
00841   int N = A->size1-1;
00842   for ( int J = N; J >= 0;  --J )
00843   {
00844     gsl_matrix_set(A, J, J, 1. / gsl_matrix_get(A,  J, J ) );
00845     double AJJ = -gsl_matrix_get( A,  J, J );
00846 
00847     if (  J<N )
00848     {
00849 
00850       //Compute elements j+1:n of j-th column.
00851       gsl_matrix_view subA = gsl_matrix_submatrix( A, J+1, J, N-J, N-J );
00852       gsl_vector_view v = gsl_matrix_column ( &subA.matrix, 0 );
00853       subA = gsl_matrix_submatrix( A, J+1, J+1, N-J, N-J );
00854 
00855       gsl_blas_dtrmv ( CblasLower, CblasNoTrans, CblasNonUnit, &subA.matrix, &v.vector ) ;
00856       gsl_blas_dscal ( AJJ, &v.vector );
00857     }
00858   }
00859 
00860 
00861   //X  Compute the product L' * L.
00862   N = A->size1-1;
00863 
00864   double* matrixData = A->data;
00865   unsigned int tda = A->tda;
00866 
00867   for ( int i = 0; i <= N;  ++i )
00868     for ( int j = 0; j <= i;  ++j )
00869     {
00870       double res=0;
00871       for ( int k = i; k <= N;  ++k )
00872       {
00873         res += matrixData[ k*tda+i ] * matrixData[ k*tda+j ];
00874       }
00875       matrixData[j*tda+i ]=res;
00876     }
00877 }
00878 
00879 
00880 } // end namespace  WMAP_tetbeebbeb_lowl

Generated on Mon May 5 17:48:34 2008 for cmbeasy by  doxygen 1.4.6