Skip to content
Snippets Groups Projects
miscmaths.cc 68.2 KiB
Newer Older
Matthew Webster's avatar
Matthew Webster committed
    exit(-1);
  }
  for (int r = 1; r <= mat1.Nrows(); r++) {
    for (int c =1; c <= mat1.Ncols(); c++) {
		mat1(r,c) = mat1(r,c) * mat2(r,c);
    }
  }
}

Matthew Webster's avatar
Matthew Webster committed
//Deprecate?
Tim Behrens's avatar
Tim Behrens committed
ReturnMatrix vox_to_vox(const ColumnVector& xyz1,const ColumnVector& dims1,const ColumnVector& dims2,const Matrix& xfm){
  ColumnVector xyz1_mm(4),xyz2_mm,xyz2(3);
  xyz1_mm<<xyz1(1)*dims1(1)<<xyz1(2)*dims1(2)<<xyz1(3)*dims1(3)<<1;
  xyz2_mm=xfm*xyz1_mm;
  xyz2_mm=xyz2_mm/xyz2_mm(4);
  xyz2<<xyz2_mm(1)/dims2(1)<<xyz2_mm(2)/dims2(2)<<xyz2_mm(3)/dims2(3);
  xyz2.Release();
  return xyz2;
}

Matthew Webster's avatar
Matthew Webster committed
//Deprecate?
Tim Behrens's avatar
Tim Behrens committed
ReturnMatrix mni_to_imgvox(const ColumnVector& mni,const ColumnVector& mni_origin,const Matrix& mni2img, const ColumnVector& img_dims){
  ColumnVector mni_new_origin(4),img_mm;//homogeneous
  ColumnVector img_vox(3);
  mni_new_origin<<mni(1)+mni_origin(1)<<mni(2)+mni_origin(2)<<mni(3)+mni_origin(3)<<1;
  img_mm=mni2img*mni_new_origin;
  img_vox<<img_mm(1)/img_dims(1)<<img_mm(2)/img_dims(2)<<img_mm(3)/img_dims(3);
  img_vox.Release();
  return img_vox;
}

Matthew Webster's avatar
Matthew Webster committed
void remmean_econ(Matrix& mat, const int dim)
{
	Matrix matmean;
	remmean(mat, matmean, dim);
Matthew Webster's avatar
Matthew Webster committed
void remmean(Matrix& mat, Matrix& matmean, const int dim)
{
	matmean = mean(mat,dim);
  	if (dim == 1){
 		for (int mr=1; mr<=mat.Nrows(); mr++)
			mat.Row(mr) -= matmean.AsRow();
  	}
  	else{
		for (int mc=1; mc<=mat.Ncols(); mc++)
			mat.Column(mc) -= matmean.AsColumn(); 
  	}
}
Christian Beckmann's avatar
Christian Beckmann committed

void remmean(const Matrix& mat, Matrix& demeanedmat, Matrix& Mean,  const int dim)
{ 
Matthew Webster's avatar
Matthew Webster committed
	demeanedmat = mat;
	remmean(demeanedmat,Mean, dim);
}
Matthew Webster's avatar
Matthew Webster committed
ReturnMatrix remmean(const Matrix& mat, const int dim)
{ 
  Matrix res = mat;
  remmean_econ(res,dim);
  res.Release();
  return res;
ReturnMatrix oldcov(const Matrix& mat, const int norm)
Christian Beckmann's avatar
Christian Beckmann committed
{ 
  SymmetricMatrix res;
  Matrix tmp;
  int N;
  tmp=remmean(mat);
  if (norm == 1) {N = mat.Nrows();}
  else {N = mat.Nrows()-1;}  
  res << tmp.t()*tmp;
  res = res/N;

  res.Release();
  return res; 
}

ReturnMatrix cov(const Matrix& data, const bool sampleCovariance, int econ) 
{
  //This assumes vectors are stored using column order in data
Christian Beckmann's avatar
Christian Beckmann committed
  SymmetricMatrix res;
  res << zeros(data.Ncols(),data.Ncols()); 
  Matrix meanM(mean(data));
  int N=data.Nrows();
  if (sampleCovariance && N>1) 
    N--;
  if ( econ < 1 )
    econ=data.Nrows();
  for(int startRow=1; startRow <= data.Nrows(); startRow+=econ) {
    Matrix suba=data.SubMatrix(startRow,Min(startRow+econ-1,data.Nrows()),1,data.Ncols());
    for (int row=1; row <= suba.Nrows(); row++) 
      suba.Row(row)-=meanM;
    res << res + suba.t()*suba/N;
  }
  res.Release();
  return res; 
}
ReturnMatrix cov_r(const Matrix& data, const bool sampleCovariance, int econ) 
{
  //This assumes vectors are stored using row order in data
  SymmetricMatrix res;
  res << zeros(data.Nrows(),data.Nrows()); 
  Matrix meanM(mean(data,2));
  int N=data.Ncols();
  if (sampleCovariance && N>1) 
    N--;
  if ( econ < 1 )
    econ=data.Ncols();
  for(int startCol=1; startCol <= data.Ncols(); startCol+=econ) {
    Matrix suba=data.SubMatrix(1,data.Nrows(),startCol,Min(startCol+econ-1,data.Ncols()));
    for (int col=1; col <= suba.Ncols(); col++) 
       suba.Column(col)-=meanM;
    res << res + suba*suba.t()/N;
  }
Christian Beckmann's avatar
Christian Beckmann committed
  res.Release();
  return res; 
}

ReturnMatrix cov_r(const Matrix& data, const Matrix& weights2, int econ) 
{
  //This assumes vectors are stored using row order in data, weights are a single "row". No bool flag as biased vs unbiased isn't relevant here
  RowVector weights=((weights2/weights2.Sum()).AsRow());
  SymmetricMatrix res;
  res << zeros(data.Nrows(),data.Nrows()); 
  Matrix meanM(mean(data,weights,2));
Matthew Webster's avatar
Matthew Webster committed
  double N=(1-weights.SumSquare());//As weights.Sum() is equal to 1
  if ( econ < 1 )
    econ=data.Ncols();
  for(int startCol=1; startCol <= data.Ncols(); startCol+=econ) {
    Matrix suba=data.SubMatrix(1,data.Nrows(),startCol,Min(startCol+econ-1,data.Ncols()));
    for (int col=1; col <= suba.Ncols(); col++) {
       suba.Column(col)-=meanM;
       suba.Column(col)*=sqrt(weights(startCol+col-1));
    }
    res << res + suba*suba.t()/N;
  }
Matthew Webster's avatar
Matthew Webster committed
  res.Release();
  return res; 
}




ReturnMatrix corrcoef(const Matrix& mat, const bool norm)
Matthew Webster's avatar
Matthew Webster committed
{ 
  	SymmetricMatrix res;
	res = cov(mat,norm);
	Matrix D;
	D=diag(res);
	D=sqrt(D*D.t());
	res << SD(res,D);
	res.Release();
	return res;
}

ReturnMatrix flipud(const Matrix& mat)
{
  Matrix rmat(mat.Nrows(),mat.Ncols());
  for(int j=1;j<=mat.Ncols();j++)
    for(int i=1;i<=mat.Nrows();i++)
      rmat(i,j)=mat(mat.Nrows()-i+1,j);
  rmat.Release();
  return rmat;
}

ReturnMatrix fliplr(const Matrix& mat)
{
  Matrix rmat(mat.Nrows(),mat.Ncols());
  for(int j=1;j<=mat.Ncols();j++)
    for(int i=1;i<=mat.Nrows();i++)
      rmat(i,j)=mat(i,mat.Ncols()-j+1);
  rmat.Release();
  return rmat;
}


Christian Beckmann's avatar
Christian Beckmann committed
void symm_orth(Matrix &Mat)
{
  SymmetricMatrix Metric;
  Metric << Mat.t()*Mat;
  Metric = Metric.i();
  Matrix tmpE;
  DiagonalMatrix tmpD;
  EigenValues(Metric,tmpD,tmpE);
  Mat = Mat * tmpE * sqrt(abs(tmpD)) * tmpE.t();
}

void powerspectrum(const Matrix &Mat1, Matrix &Result, bool useLog)
  //calculates the powerspectrum for every column of Mat1
{
  Matrix res;
  for(int ctr=1; ctr <= Mat1.Ncols(); ctr++)
    {
      ColumnVector tmpCol;
      tmpCol=Mat1.Column(ctr);
      ColumnVector FtmpCol_real;
      ColumnVector FtmpCol_imag;
      ColumnVector tmpPow;
      
      RealFFT(tmpCol,FtmpCol_real,FtmpCol_imag);
Matthew Webster's avatar
Matthew Webster committed
	  pow(FtmpCol_real,2);
      pow(FtmpCol_imag,2);

      tmpPow = FtmpCol_real+FtmpCol_imag;
Christian Beckmann's avatar
Christian Beckmann committed
      tmpPow = tmpPow.Rows(2,tmpPow.Nrows());
Matthew Webster's avatar
Matthew Webster committed
      if(useLog){log(tmpPow);}
Christian Beckmann's avatar
Christian Beckmann committed
      if(res.Storage()==0){res= tmpPow;}else{res|=tmpPow;}
    }
    Result=res;
}


Tim Behrens's avatar
Tim Behrens committed
void element_mod_n(Matrix& Mat,double n)
{
 //represent each element in modulo n (useful for wrapping phases (n=2*M_PI))

  double tmp;
  for( int j=1;j<=Mat.Ncols();j++){
Tim Behrens's avatar
Tim Behrens committed
    for( int i=1;i<=Mat.Nrows();i++){
Tim Behrens's avatar
Tim Behrens committed
      while( !( (Mat(i,j)>0) && (Mat(i,j)<n) ) ){ 
Tim Behrens's avatar
Tim Behrens committed
	tmp = ( Mat(i,j) - rounddouble(Mat(i,j)/n)*n );
Christian Beckmann's avatar
Christian Beckmann committed
int nextpow2(int n)
{
  return (int)pow(2,ceil(log(float(n))/log(float(2))));
Christian Beckmann's avatar
Christian Beckmann committed
}

void xcorr(const Matrix& p_ts, Matrix& ret, int lag, int p_zeropad)
{
  Tracer tr("MISCMATHS::xcorr");

  int sizeTS = p_ts.Nrows();
  int numTS = p_ts.Ncols();
  
  if(p_zeropad == 0)
    p_zeropad = sizeTS;
  if(lag == 0)
    lag = sizeTS;

  ColumnVector x(p_zeropad);
  x = 0;
  ColumnVector fft_real;
  ColumnVector fft_im;
  ColumnVector dummy(p_zeropad);
  ColumnVector dummy2;
  dummy = 0;
  ColumnVector realifft(p_zeropad);
  ret.ReSize(lag,numTS);
  ret = 0;

  for(int i = 1; i <= numTS; i++)
    {
      x.Rows(1,sizeTS) = p_ts.Column(i);
      FFT(x, dummy, fft_real, fft_im);
      
      for(int j = 1; j <= p_zeropad; j++)
	{
	  // (x+iy)(x-iy) = x^2 + y^2
	  fft_real(j) = fft_real(j)*fft_real(j) + fft_im(j)*fft_im(j);
	  fft_im(j) = 0;
	}
      
      FFTI(fft_real, fft_im, realifft, dummy2);
      
      float varx = var(x.Rows(1,sizeTS)).AsScalar();
      ret.Column(i) = realifft.Rows(1,lag);
      
Christian Beckmann's avatar
Christian Beckmann committed
	{
	  // Correction to make autocorr unbiased and normalised
	  ret(j,i) = ret(j,i)/((sizeTS-j)*varx);
Christian Beckmann's avatar
Christian Beckmann committed
	}  
    }
}

ReturnMatrix xcorr(const Matrix& p_ts, int lag, int p_zeropad )
{
  Matrix r;
  
  xcorr(p_ts,r,lag,p_zeropad);
  r.Release();
  return r;
}

void detrend(Matrix& p_ts, int p_level)
{
  Tracer trace("MISCMATHS::detrend");

  int sizeTS = p_ts.Nrows();     

  // p_ts = b*a + e (OLS regression)
  // e is detrended data
  Matrix a(sizeTS, p_level+1);  

  // Create a
  for(int t = 1; t <= sizeTS; t++)
    {
      for(int l = 0; l <= p_level; l++)
Christian Beckmann's avatar
Christian Beckmann committed
    }
          
  // Form residual forming matrix R:
  Matrix R = IdentityMatrix(sizeTS)-a*pinv(a);
Christian Beckmann's avatar
Christian Beckmann committed
    {
Christian Beckmann's avatar
Christian Beckmann committed
    }      
}



ReturnMatrix read_vest(string p_fname)
{
  ifstream in;
  in.open(p_fname.c_str(), ios::in);
  
  if(!in) throw Exception(string("Unable to open "+p_fname).c_str());
Christian Beckmann's avatar
Christian Beckmann committed
  
  int numWaves = 0;
  int numPoints = 0;
  
  string str;
  
  while(true)
    {
      if(!in.good()) throw Exception(string(p_fname+" is not a valid vest file").c_str());	
Christian Beckmann's avatar
Christian Beckmann committed
      in >> str;
      if(str == "/Matrix")
	break;
      else if(str == "/NumWaves")
	{
	  in >> numWaves;
	}
      else if(str == "/NumPoints" || str == "/NumContrasts")
	{
	  in >> numPoints;
	}
    }
  
  Matrix p_mat(numPoints, numWaves);
  
  for(int i = 1; i <= numPoints; i++)
    {
      for(int j = 1; j <= numWaves; j++)    
	{
	  if (!in.eof()) in >> ws >> p_mat(i,j) >> ws;
	  else throw Exception(string(p_fname+" has insufficient data points").c_str());
Christian Beckmann's avatar
Christian Beckmann committed
	}
    }
  
  in.close();

  p_mat.Release();
  return p_mat;
}

Tim Behrens's avatar
Tim Behrens committed
void ols(const Matrix& data,const Matrix& des,const Matrix& tc, Matrix& cope,Matrix& varcope){
Stephen Smith's avatar
Stephen Smith committed
  // ols
  // data is t x v
  // des is t x ev (design matrix)
  // tc is cons x ev (contrast matrix)
  // cope and varcope will be cons x v
  // but will be resized if they are wrong
  // hence may be passed in uninitialised
  // TB 2004
  if(data.Nrows() != des.Nrows()){
    cerr <<"MISCMATHS::ols - data and design have different number of time points"<<endl;
    exit(-1);
  }
  if(des.Ncols() != tc.Ncols()){
    cerr <<"MISCMATHS::ols - design and contrast matrix have different number of EVs"<<endl;
    exit(-1);
  }  
  Matrix pdes = pinv(des);
  Matrix prevar=diag(tc*pdes*pdes.t()*tc.t());
  Matrix R=IdentityMatrix(des.Nrows())-des*pdes;
Stephen Smith's avatar
Stephen Smith committed
  float tR=R.Trace();
  Matrix pe=pdes*data;
  cope=tc*pe;
  Matrix res=data-des*pe;
  Matrix sigsq=sum(SP(res,res))/tR;
  varcope=prevar*sigsq;
  
  
}

Matthew Webster's avatar
Matthew Webster committed
float ols_dof(const Matrix& des){
  if ( des.Nrows() > 4000 ) //Use the simple version as huge designs require too much RAM in the full calculation
    return des.Nrows() - des.Ncols();
  try {
Tim Behrens's avatar
Tim Behrens committed
  Matrix pdes = pinv(des);
  Matrix R=IdentityMatrix(des.Nrows())-des*pdes;
  return R.Trace();}
  catch (...) {
    cerr << "ols_dof: Error in determining the trace, resorting to basic calculation" << endl;
  }
  return des.Nrows() - des.Ncols();
int conjgrad(ColumnVector& x, const Matrix& A, const ColumnVector& b, int maxit,
	     float reltol)
{
  // solves:  A * x = b    (for x)
  // implementation of algorithm in Golub and Van Loan (3rd ed, page 527)
  ColumnVector rk1, rk2, pk, apk;
  double betak, alphak, rk1rk1=0, rk2rk2, r00=0;
  int k=0;
  rk1 = b - A*x;  // a *big* calculation
  for (int n=1; n<=maxit; n++) {
    k++;
    if (k==1) {
      pk = rk1;
      rk1rk1 = (rk1.t() * rk1).AsScalar();
    } else {
      rk2rk2 = rk1rk1;  // from before
      rk1rk1 = (rk1.t() * rk1).AsScalar();
      if (rk2rk2<1e-10*rk1rk1) {
	cerr << "WARNING:: Conj Grad - low demoninator (rk2rk2)" << endl; 
	if (rk2rk2<=0) {
	  cerr << "Aborting conj grad ..." << endl;
	  return 1;  
	}
      }
      betak = rk1rk1 / rk2rk2;
      pk = rk1 + betak * pk;  // note RHS pk is p(k-1) in algorithm
    }  
    // stop if sufficient accuracy is achieved
    if (rk1rk1<reltol*reltol*r00) return 0;

    apk = A * pk;  // the *big* calculation in this algorithm

    ColumnVector pap = pk.t() * apk;
    if (pap.AsScalar()<0) { 
      cerr << "ERROR:: Conj Grad - negative eigenvector found (matrix must be symmetric and positive-definite)\nAborting ... " << endl; 
      return 2;
    } else if (pap.AsScalar()<1e-10) { 
      cerr << "WARNING:: Conj Grad - nearly null eigenvector found (terminating early)" << endl; 
      return 1;
    } else {
      alphak = rk1rk1 / pap.AsScalar();
    }
    x = x + alphak * pk;  // note LHS is x(k) and RHS is x(k-1) in algorithm
    rk2 = rk1;  // update prior to the next step
    rk1 = rk1 - alphak * apk;  // note LHS is r(k) in algorithm
  }
  return 0;
}

int conjgrad(ColumnVector& x, const Matrix& A, const ColumnVector& b, int maxit)
{ 
  return conjgrad(x,A,b,maxit,1e-10);
}

Mark Woolrich's avatar
Mark Woolrich committed
float csevl(const float x, const ColumnVector& cs, const int n)
  {
 
    float b0 = 0;
    float b1 = 0;
    float b2 = 0;
    const float twox=2*x;
    
    for(int i=1; i<=n; i++)
      {
	b2=b1;
	b1=b0;
	b0=twox*b1-b2+cs(n+1-i);
      }
    
    return 0.5*(b0-b2);
  }
Mark Woolrich's avatar
Mark Woolrich committed
  float digamma(const float x)
  { 
    int ntapsi(16);
    int ntpsi(23);
    ColumnVector psics(ntpsi);
    ColumnVector apsics(ntapsi);

    psics << -.038057080835217922E0<<
      .49141539302938713E0<<
      -.056815747821244730E0<<
      .008357821225914313E0<<
      -.001333232857994342E0<<
      .000220313287069308E0<<
      -.000037040238178456E0<<
      .000006283793654854E0<<
      -.000001071263908506E0<<
      .000000183128394654E0<<
      -.000000031353509361E0<<
      .000000005372808776E0<<
      -.000000000921168141E0<<
      .000000000157981265E0<<
      -.000000000027098646E0<<
      .000000000004648722E0<<
      -.000000000000797527E0<<
      .000000000000136827E0<<
      -.000000000000023475E0<<
      .000000000000004027E0<<
      -.000000000000000691E0<<
      .000000000000000118E0<<
      -.000000000000000020E0;
	  
    apsics <<-.0204749044678185E0<<
      -.0101801271534859E0<<
      .0000559718725387E0<<
      -.0000012917176570E0<<
      .0000000572858606E0<<
      -.0000000038213539E0<<
      .0000000003397434E0<<
      -.0000000000374838E0<<
      .0000000000048990E0<<
      -.0000000000007344E0<<
      .0000000000001233E0<<
      -.0000000000000228E0<<
      .0000000000000045E0<<
      -.0000000000000009E0<<
      .0000000000000002E0<<
      -.0000000000000000E0;

    float y = fabs(x);
    float psi;

    if(y<2.0)
      {
	// do we need to deal with the following case?
	// c psi(x) for -2. .lt. x .lt. 2.

	int n = int(floor(x));
	y = x - n;
	n = n - 1;
	psi = csevl(2*y-1, psics, ntpsi);
	if(n==-1)
	  {
	    psi = psi - 1.0/x;
	  }
      }
    else
      {
	const float aux = csevl(8/(Sqr(y))-1, apsics, ntapsi);
Mark Woolrich's avatar
Mark Woolrich committed
  void glm_vb(const Matrix& X, const ColumnVector& Y, ColumnVector& B, SymmetricMatrix& ilambda_B, int niters)
Mark Woolrich's avatar
Mark Woolrich committed
    // Does Variational Bayes inference on GLM Y=XB+e with ARD priors on B
    // design matrix X should be num_tpts*num_evs

Mark Woolrich's avatar
Mark Woolrich committed
    /////////////////////
    // setup
    OUT("Setup");

    int ntpts=Y.Nrows();
Mark Woolrich's avatar
Mark Woolrich committed
    int nevs=X.Ncols();
Mark Woolrich's avatar
Mark Woolrich committed
    if(ntpts!=X.Nrows())
Mark Woolrich's avatar
Mark Woolrich committed
      throw Exception("COCK");

    OUT(nevs);
    OUT(ntpts);

Mark Woolrich's avatar
Mark Woolrich committed
    ColumnVector gam_m(nevs);
    gam_m=1e10;
    float gam_y;

    ColumnVector lambdaB(nevs);
    if(nevs<ntpts-10)
Mark Woolrich's avatar
Mark Woolrich committed
	// initialise with OLS
	B=pinv(X)*Y;
	ColumnVector res=Y-X*B;
	gam_y=(ntpts-nevs)/(res.t()*res).AsScalar();

	ilambda_B << (X.t()*X*gam_y).i();
	lambdaB=0;
	for(int l=1; l <= nevs; l++)
	  {
	    lambdaB(l)=ilambda_B(l,l);
	  }
Mark Woolrich's avatar
Mark Woolrich committed
    else
      {
	OUT("no ols");
	B.ReSize(nevs);
	B=0;
	lambdaB=1;
Mark Woolrich's avatar
Mark Woolrich committed
// 	ColumnVector res=Y-X*B;
// 	gam_y=ntpts/(res.t()*res).AsScalar();
Mark Woolrich's avatar
Mark Woolrich committed
	gam_y=10;
      }

//     OUT(B(1));
//     OUT(lambdaB(1));
Mark Woolrich's avatar
Mark Woolrich committed

    float trace_ilambdaZZ=1;

    SymmetricMatrix ZZ;
Mark Woolrich's avatar
Mark Woolrich committed
    ZZ << X.t()*X;
Mark Woolrich's avatar
Mark Woolrich committed
    Matrix ZY = X.t()*Y;
Mark Woolrich's avatar
Mark Woolrich committed

    float YY=0;
    for(int t=1; t <= ntpts; t++)
      YY += Sqr(Y(t));

    /////////////////////
    // iterate
    OUT("Iterate");

    int i = 1;;
    for(; i<=niters; i++)
      {
Mark Woolrich's avatar
Mark Woolrich committed
	cout<<i<<",";
Mark Woolrich's avatar
Mark Woolrich committed
	////////////////////
	// update phim
Mark Woolrich's avatar
Mark Woolrich committed
	  {
	    float b_m0 = 1e10;
Mark Woolrich's avatar
Mark Woolrich committed
	    float c_m0 = 2;
Mark Woolrich's avatar
Mark Woolrich committed

	    float c_m = 1.0/2.0 + c_m0;	    
	    float b_m = 1.0/(0.5*(Sqr(B(l))+lambdaB(l))+1.0/b_m0);
	    gam_m(l) = b_m*c_m;	    
Mark Woolrich's avatar
Mark Woolrich committed
// 	OUT(gam_m(1));

Mark Woolrich's avatar
Mark Woolrich committed
	////////////////////
	// update B
	ColumnVector beta(nevs);
	beta = 0;
	SymmetricMatrix lambda_B(nevs);
	lambda_B = 0;

	for(int l=1; l <= nevs; l++)
	  lambda_B(l,l)=gam_m(l);
Mark Woolrich's avatar
Mark Woolrich committed

	SymmetricMatrix tmp = lambda_B + gam_y*ZZ;
	lambda_B << tmp;

	beta += gam_y*ZY;

	ilambda_B << lambda_B.i();
Mark Woolrich's avatar
Mark Woolrich committed
	B=ilambda_B*beta;
Mark Woolrich's avatar
Mark Woolrich committed

	lambdaB.ReSize(nevs);
	lambdaB=0;
	for(int l=1; l <= nevs; l++)
	  {
	    lambdaB(l)=ilambda_B(l,l);
	  }
	
	////////////////////
	// compute trace for noise precision phiy update
	
	SymmetricMatrix tmp3;
	tmp3 << ilambda_B;
	
	SymmetricMatrix tmp2;
	tmp2 << tmp3*ZZ;
	
	trace_ilambdaZZ=tmp2.Trace();	
Mark Woolrich's avatar
Mark Woolrich committed
// 	OUT(trace_ilambdaZZ);
Mark Woolrich's avatar
Mark Woolrich committed
	/////////////////////
	// update phiy
	float b_y0 = 1e10;
	float c_y0 = 1;
Mark Woolrich's avatar
Mark Woolrich committed
	float c_y = (ntpts-1)/2.0 + c_y0;
Mark Woolrich's avatar
Mark Woolrich committed
	float sum = YY + (B.t()*ZZ*B).AsScalar() - 2*(B.t()*ZY).AsScalar();
Mark Woolrich's avatar
Mark Woolrich committed
	float b_y = 1.0/(0.5*(sum + trace_ilambdaZZ)+1/b_y0);
Mark Woolrich's avatar
Mark Woolrich committed
	gam_y = b_y*c_y;

// 	OUT(gam_y);	     

      }
Mark Woolrich's avatar
Mark Woolrich committed
    cout << endl;
Mark Woolrich's avatar
Mark Woolrich committed
vector<float> ColumnVector2vector(const ColumnVector& col)
{
  vector<float> vec(col.Nrows());
  
  for(int c = 0; c < col.Nrows(); c++)
    vec[c] = col(c+1);
  
  return vec;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

// Uninteresting byte swapping functions
typedef struct { unsigned char a,b ; } TWObytes ;

void Swap_2bytes( int n , void *ar )    /* 2 bytes at a time */
{
Mark Woolrich's avatar
Mark Woolrich committed
  register int ii ;
   register TWObytes *tb = (TWObytes *)ar ;
   register unsigned char tt ;

   for( ii=0 ; ii < n ; ii++ ){
     tt = tb[ii].a ; tb[ii].a = tb[ii].b ; tb[ii].b = tt ;
   }
   return ;
}

/*---------------------------------------------------------------------------*/

typedef struct { unsigned char a,b,c,d ; } FOURbytes ;

void Swap_4bytes( int n , void *ar )    /* 4 bytes at a time */
{
   register int ii ;
   register FOURbytes *tb = (FOURbytes *)ar ;
   register unsigned char tt ;

   for( ii=0 ; ii < n ; ii++ ){
     tt = tb[ii].a ; tb[ii].a = tb[ii].d ; tb[ii].d = tt ;
     tt = tb[ii].b ; tb[ii].b = tb[ii].c ; tb[ii].c = tt ;
   }
   return ;
}

/*---------------------------------------------------------------------------*/

typedef struct { unsigned char a,b,c,d , D,C,B,A ; } EIGHTbytes ;

void Swap_8bytes( int n , void *ar )    /* 8 bytes at a time */
{
   register int ii ;
   register EIGHTbytes *tb = (EIGHTbytes *)ar ;
   register unsigned char tt ;

   for( ii=0 ; ii < n ; ii++ ){
     tt = tb[ii].a ; tb[ii].a = tb[ii].A ; tb[ii].A = tt ;
     tt = tb[ii].b ; tb[ii].b = tb[ii].B ; tb[ii].B = tt ;
     tt = tb[ii].c ; tb[ii].c = tb[ii].C ; tb[ii].C = tt ;
     tt = tb[ii].d ; tb[ii].d = tb[ii].D ; tb[ii].D = tt ;
   }
   return ;
}

/*---------------------------------------------------------------------------*/

typedef struct { unsigned char a,b,c,d,e,f,g,h ,
                               H,G,F,E,D,C,B,A  ; } SIXTEENbytes ;

void Swap_16bytes( int n , void *ar )    /* 16 bytes at a time */
{
   register int ii ;
   register SIXTEENbytes *tb = (SIXTEENbytes *)ar ;
   register unsigned char tt ;

   for( ii=0 ; ii < n ; ii++ ){
     tt = tb[ii].a ; tb[ii].a = tb[ii].A ; tb[ii].A = tt ;
     tt = tb[ii].b ; tb[ii].b = tb[ii].B ; tb[ii].B = tt ;
     tt = tb[ii].c ; tb[ii].c = tb[ii].C ; tb[ii].C = tt ;
     tt = tb[ii].d ; tb[ii].d = tb[ii].D ; tb[ii].D = tt ;

     tt = tb[ii].e ; tb[ii].e = tb[ii].E ; tb[ii].E = tt ;
     tt = tb[ii].f ; tb[ii].f = tb[ii].F ; tb[ii].F = tt ;
     tt = tb[ii].g ; tb[ii].g = tb[ii].G ; tb[ii].G = tt ;
     tt = tb[ii].h ; tb[ii].h = tb[ii].H ; tb[ii].H = tt ;
   }
   return ;
}

/*---------------------------------------------------------------------------*/

void Swap_Nbytes( int n , int siz , void *ar )  /* subsuming case */
{
   switch( siz ){
     case 2:  Swap_2bytes ( n , ar ) ; break ;
     case 4:  Swap_4bytes ( n , ar ) ; break ;
     case 8:  Swap_8bytes ( n , ar ) ; break ;
     case 16: Swap_16bytes( n , ar ) ; break ;
   }
   return ;
}

// end namespace MISCMATHS
}