Пример #1
0
double sigmac_radius_interp(double m)
{
  static int flag=0,prev_cosmo=0;
  static double *x,*y,*y2, pnorm;
  int i,n=100;
  double dlogm,max=80.0,min=0.1,a,b,m1,m2,dm1,xi,power,rm,sig,b1,b2,mass;

  if(!flag || RESET_COSMOLOGY!=prev_cosmo)
    {
      if(!ThisTask && OUTPUT)
	fprintf(stdout,"RESET: resetting bias for %f %f\n",OMEGA_M,SIGMA_8);
      if(!flag)
	{
	  x=dvector(1,n);
	  y=dvector(1,n);
	  y2=dvector(1,n);
	}
      flag=1;
      dlogm = (log(max) - log(min))/(n-1);
      pnorm=SIGMA_8/sigmac(8.0);
      for(i=1;i<=n;++i)
	{
	  rm = exp((i-1)*dlogm)*min;
	  sig=log(pnorm*sigmac(rm));
	  x[i] = log(rm);
	  y[i] = sig;
	}
      spline(x,y,n,2.0E+30,2.0E+30,y2);
      prev_cosmo=RESET_COSMOLOGY;

    }
  m=log(m);
  splint(x,y,y2,n,m,&a);
  return exp(a);
}
Пример #2
0
void output_matter_variance()
{
  int k,nr=50;
  double dlogr,r,slin,snl,mass,rmin = 0.05,rmax = 80.0,pnorm,pnorm_nl;
  FILE *fp;
  char aa[100];

  fprintf(stderr,"\n\nCALCULATING MATTER VARIANCE.\n");
  fprintf(stderr,    "----------------------------\n\n");

  sprintf(aa,"%s.sigma_r",Task.root_filename);
  fp = fopen(aa,"w");

  dlogr = (log(rmax) - log(rmin))/(nr-1);
  pnorm = SIGMA_8/sigmac(8.0);
  pnorm_nl = pnorm*sigmac(80.0)/nonlinear_sigmac(80.0);

  for(k=0;k<nr;++k)
    {
      r = exp(k*dlogr)*rmin;
      mass = 4./3.*PI*r*r*r*RHO_CRIT*OMEGA_M;
      slin = sigmac(r)*pnorm;
      snl = nonlinear_sigmac(r)*pnorm_nl;
      fprintf(fp,"%e %e %e %e\n",r,slin,snl,mass);
    }
  fclose(fp);

}
Пример #3
0
/*** solve for M_* ***/
double sigma_Mmdelta_c(double lnM)
{ 
  double sig,M,rm;

  M=exp(lnM);
  rm=pow(3.0*M/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  sig=pnorm1*sigmac(rm);

  return sig-DELTA_CRIT;
}
Пример #4
0
double mstar()
{
  double sig,lnMmin,lnMmax,M_star;

  sig=sigmac(8.0); 
  pnorm1 = SIGMA_8/sig;
  
  lnMmin=log(1e7);
  lnMmax=log(1e18);
  M_star=zbrent(sigma_Mmdelta_c,lnMmin,lnMmax,1e-5);
  M_star=exp(M_star);
  if(!ThisTask)
    fprintf(stderr,"M_star = %e h^{-1}M_sol\n",M_star);
  return(M_star); 
}
/* This returns the linear power 
 *   Delta = 4pi*k^3 P(k)/(2pi)^3
 */
double linear_power_spectrum(double xk)
{
  static double *kk,*pknl,*y2,pnorm=-1,ahi,bhi;
  static int flag=1,nk=1000,prev_cosmology=0;
  double a,psp,x1[4],y1[4];
  int i;

  if(pnorm<0 || prev_cosmology!=RESET_COSMOLOGY)
    {
      pnorm=SIGMA_8/sigmac(8.0);  
      pnorm*=pnorm;
      prev_cosmology=RESET_COSMOLOGY;
    }
  if(ITRANS>0)
    psp=pow(xk,SPECTRAL_INDX)*pow(transfnc(xk),2.);
  else
    psp=pow(xk,SPECTRAL_INDX);
  //  printf("BOO %e %e\n",xk,psp*pnorm);
  psp=psp*pnorm*xk*xk*xk/(2*PI*PI);
  return(psp);
}
Пример #6
0
double bias(double mass)
{
  double rm,sig,k,neff,b,logk,khi,klo,phi,plo,nu,psp,x;
  static int flag=0;
  static double pnorm, prev_delta=-1, prev_cosmo=-1;

  // variables for the SO(DELTA) bias functions
  static double bias_A, bias_a, bias_B, bias_b, bias_c, bias_C;

  /* Original SMT parameters */
  double a=0.707,bb=0.5,c=0.6;

  pnorm=SIGMA_8/sigmac(8.0);
  rm=pow(3.0*mass/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  sig=pnorm*sigmac(rm);

  // Tinker et al parameters 
  /*
  a=0.707;
  bb=0.35;
  c=0.8;

  // Fitting to Mike Warren's simulations.
  bb=0.28; 
  */

  /* Use the globel parameters. */
  a=BIAS_A; bb=BIAS_B; c=BIAS_C;

  /* First normalize the power spectrum
   */
  pnorm=SIGMA_8/sigmac(8.0);
  rm=pow(3.0*mass/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  sig=pnorm*sigmac(rm);


  /* This is from Tinker etal in prep for SO halos
   */
  if((DELTA_HALO != prev_delta) || RESET_COSMOLOGY!=prev_cosmo)
    {
      bias_A = pow(fabs(log10(DELTA_HALO)-2.69),2)*0.16 + 0.785;
      bias_a = (log10(DELTA_HALO) - 2.28)*0.45;
      bias_B = 0.4*(log10(DELTA_HALO)-2.3)+0.7*log10(DELTA_HALO)*exp(-pow(4/log10(DELTA_HALO),4.0))+1.23;
      bias_b = 2.4;
      fprintf(stderr,"BIAS PARAMS: %f %f %f %f\n",bias_A, bias_a, bias_B, bias_b);
      prev_delta = DELTA_HALO;
      prev_cosmo = RESET_COSMOLOGY;


      // with updated parameters: Wed Oct  7 08:00:17 PDT 2009
      x = log10(DELTA_HALO);
      bias_A = 1.00 + 0.24*x*exp(-pow(4/x,4));
      bias_a = (x-2.0)*0.44;
      bias_B = 0.4;
      bias_b = 1.5;
      bias_C = ((x-2.6)*0.4 + 1.11 + 0.7*x*exp(-pow(4/x,4)))*0.94;
      bias_c = 2.4;
    }
  
  a = pow(sig,-bias_a);
  b = 1 - bias_A*a/(a+1) + bias_B*pow(sig,-bias_b) + bias_C*pow(sig,-bias_c);

  //WARNING -- EXTRA ADD HOCK FACTOR HERE FOR TESTING -- RMR
  return(b);

  /* Sheth-Tormen with Seljak-Warren fitting (from Mandelbaum et al 2005)
   */
  nu=DELTA_CRIT/sig*DELTA_CRIT/sig;
  b=1+(0.73*nu-1)/DELTA_CRIT + 2*0.15/DELTA_CRIT/(1+pow(0.73*nu,0.15));
  return b;


  /* This is Sheth & Tormen
   */
  nu = DELTA_CRIT/sig;
  nu = nu*nu;
  return(1 + (0.707*nu - 1)/DELTA_CRIT + 2*0.3/DELTA_CRIT/(1+pow(0.707*nu,0.3)));
 
  /* This is the Seljak & Warren (2004) bias.
   * There's an alpha_s in the correction term, which here I set to zero. (RUNNING.)
   * See App. A of Tinker et al (M/L) for a comparison of this bias with above bias.
   */
  x=mass/MSTAR;
  b=(0.53+0.39*pow(x,0.45)+0.13/(40*x+1) + 5.0e-4*pow(x,1.5));
  //b=b+log10(x)*(0.4*(OMEGA_M-0.3+SPECTRAL_INDX-1)+0.3*(SIGMA_8-0.9+HUBBLE-0.7)+0.8*0.0);
  return(b);



  /* This is the Sheth Mo Tormen bias.
   * (possible that parameter values have been changed to better fit simulations,
   * ie from Tinker etal 2005 ML paper).
   */
  nu=DELTA_CRIT/sig;
  b=1+1.0/(sqrt(a)*DELTA_CRIT)*(sqrt(a)*a*nu*nu + sqrt(a)*bb*pow(a*nu*nu,1-c) - 
				(pow(a*nu*nu,c)/(pow(a*nu*nu,c)+bb*(1-c)*(1-c/2.))));
  return(b);


  /* This is the old Mo & White (1996) formula
   */
  return(1+DELTA_CRIT/sig/sig-1/DELTA_CRIT);
 

}
Пример #7
0
/* Just like the halo mass function, we'll set this up such that
 * you just need to interpolate.
 *
 * Now this has been changed to calculate the spatial-scale dependence
 * of the bias factor. If you don't want the scale dep. b, then just
 * input r<0.
 *
 * If the global flag LINEAR_PSP==0, uses the scale dependence calculated for
 * for halo bias relative to the non-linear matter \xi_m(r):
 *
 * f^2(r) = (1.0+xi*1.17)^1.49/(1.0+xi*0.69)^2.09  --> b(r) = b0*f(r)
 *
 * For LINEAR_PSP==1, use scale dependence determined for the linear P(k):
 *
 * f(r) = 1 + exp[-(r/A)^0.7] --> where A is a parameter that we're gonna have to 
 *                                determine in more detail, but now A \approx 1
 */
double bias_interp(double m, double r)
{
  static int flag=0,prev_cosmo=0, n;
  static double *x,*y,*y2, pnorm;
  int i;
  double dm,max=16.3,min=9,a,b,m1,m2,dm1,xi,power,rm,sig,b1,b2,mass,rvir,a1;
  double c1,c2,d1,d2;

  if(!flag || RESET_COSMOLOGY!=prev_cosmo)
    {
      n = 100;
      if(!ThisTask && OUTPUT)
	fprintf(stdout,"RESET: resetting bias for %f %f\n",OMEGA_M,SIGMA_8);
      if(!flag)
	{
	  x=dvector(1,n);
	  y=dvector(1,n);
	  y2=dvector(1,n);
	}
      flag=1;
      dm=(double)(max-min)/n;
      for(i=1;i<=n;++i)
	{
	  x[i]=pow(10.0,min+i*dm);
	  y[i]=log(bias(x[i]));
	  //printf("BIAS %e %e\n",x[i], exp(y[i]));
	  if(isinf(y[i])){ n = i-1; break; }
	  if(isnan(y[i])){ n = 1-1; break; }
	  x[i] = log(x[i]);
	  continue;

	  // no longer need to do this part, since we're taking into account
	  // halo overdensity in the bias formula.
	  /*
	  if(DELTA_HALO!=200)
	    {
	      x[i]=log(halo_mass_conversion2(x[i],halo_c200(x[i]),200.0,DELTA_HALO));
	    }
	  else
	    {
	      x[i]=log(x[i]);
	    }
	  */
	}
      spline(x,y,n,2.0E+30,2.0E+30,y2);
      prev_cosmo=RESET_COSMOLOGY;
      pnorm=SIGMA_8/sigmac(8.0);

    }


  m=log(m);
  splint(x,y,y2,n,m,&a);
  a = exp(a);

  // if we're using systematic errors in an MCMC, adjust parameter a1 (amplitude)
  if(USE_ERRORS)
    a *= M2N.bias_amp;

  // if no scale-dependence required, return the large-scale value
  if(r<0) return a;


  /* 
   * SCALE DEPENDENT BIAS!!!
   *----------------------------------------------------------------------
   */

  /* FOR M/N analysis, use the Tinker etal 2005 result:
   */  
  xi = xi_interp(r);  
  if(M2N.scalebias_selfcal)
    {
      b = pow(1.0+xi*M2N.bias_amp1,M2N.bias_pow1)*pow(1.0+xi*0.69,-1.045);
    }
  else
    {
	  //Attempting to correct scale-dep bias issue
	  //SCALE-DEP BIAS CORRECTION
	  rvir = pow(3*exp(m)/(4*PI*200*RHO_CRIT*OMEGA_M),THIRD);
//      if(r<2.4*rvir)r=2.4*rvir;
      if(r<2.8*rvir)r=2.8*rvir;
	  	
      //rvir = pow(3*exp(m)/(4*PI*DELTA_HALO*RHO_CRIT*OMEGA_M),THIRD);
      //if(r<2*rvir)r=2*rvir;
	  
      xi = xi_interp(r);
      //parameters for scale-dep bias
      //original values
      //c1 = 1.17;
      //c2 = 0.69;
      //d1 = 1.49;
      //d2 = -2.09;
      c1 = HBIAS_C1;
      c2 = HBIAS_C2;
      d1 = HBIAS_D1;
      d2 = (-1.)*HBIAS_D2;
      /**
      c1 = 1.52;
      c2 = 0.84;
      d1 = 1.49;
      d2 = -2.09;
      **/
      b = pow(1.0+xi*c1,d1/2.)*pow(1.0+xi*c2,d2/2.0);
      
      // if we're using systematic errors in an MCMC, adjust parameter a1 (amplitude)
      if(USE_ERRORS) {
	b = (b-1)*M2N.scalebias_amp + 1;
	if(b<0)b=0;
      }
    }
  return b*a;
  

  /* first, calculate the standard Zheng-like, mass-independent scale bias
   * based on the non-linear correlation function
   * (re-calibrated using the SO catalogs)
   */
  xi = xi_interp(r);  
  b = pow(1.0+xi*0.92,2.08)*pow(1.0+xi*0.74,-2.37);
  if(b<0.6)b=0.6;

  /* Now the mass-dependent term.
   * Where the mass-dependence comes in the form of the large-scale bias itself
   */
  sig = sigmac_radius_interp(r);
  //if(a<0)
  //b *= pow(1 + 0.028*pow(a,1.53)*pow(sig,1.57),2.59)/
  //pow(1 + 0.253*pow(a,1.24)*pow(sig,1.71),0.397);

  // if we're using systematic errors in an MCMC, adjust parameter a1 (amplitude)
  if(USE_ERRORS) {
    b = (b-1)*M2N.scalebias_amp + 1;
    if(b<0)b=0;
  }
  return b*a;

}
double halo_mass_function(double mass)
{
  double sig,logm,a,slo,shi,rm,rlo,rhi,mlo,mhi,dsdM,n,nuprime,nufnu,p,A, fac;
  static int flag=0,SO180=0,SO324=0,WARREN=0,ST=0,JENKINS=0;
  static double pnorm, prev_delta, prev_cosmo;
  double btemp = -1;

  static double
    a1 = 0.325277,
    a2 = 0.492785,
    a3 = 0.310289,
    a4 = 1.317104,
    a5 = 2.425681;
    
  /* Jenkins et al. SO 180 best-fit
   */
  if(SO180)
    {
      JENKINS_A = 0.301;
      JENKINS_B = 0.64;
      JENKINS_C = 3.82;
    }      
  if(SO324)
    {
      JENKINS_A = 0.316;
      JENKINS_B = 0.67;
      JENKINS_C = 3.82;
    }      
  if(JENKINS) //their .2 fof function
    {
      JENKINS_A = 0.315;
      JENKINS_B = 0.61;
      JENKINS_C = 3.8;
    }      


  /* First normalize the power spectrum
   */
  pnorm=SIGMA_8/sigmac(8.0);
  rm=pow(3.0*mass/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  sig=pnorm*sigmac(rm);
  logm=log10(mass);
  
  mlo=0.99*mass;
  mhi=1.01*mass;
  rlo=pow(3.0*mlo/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  rhi=pow(3.0*mhi/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);

  slo=pnorm*sigmac(rlo);
  shi=pnorm*sigmac(rhi);
  dsdM=(shi-slo)/(mhi-mlo);

  if(SO324)goto JENKINS_FUNCTION;
  if(SO180)goto JENKINS_FUNCTION;
  if(WARREN)goto WARREN_FUNCTION;
  if(ST)goto ST_FUNCTION;
  if(JENKINS)goto JENKINS_FUNCTION;

  /* Tinker et al. (2008) for SO as a function of DELTA_HALO
   */
  if(DELTA_HALO != prev_delta || prev_cosmo != RESET_COSMOLOGY)
    {
      initialize_mass_function(&a1,&a2,&a3,&a4);
      prev_delta = DELTA_HALO;
      prev_cosmo = RESET_COSMOLOGY;
      //a4*=1.3;
      //a1*=1.15;
      fprintf(stderr,"MF PARAMS for DELTA=%f %f %f %f %f\n",DELTA_HALO,a1,a2,a3,a4);
    }
  
  n = -a1*(pow(sig/a3,-a2)+1)*exp(-a4/sig/sig)*OMEGA_M*RHO_CRIT/mass/sig*dsdM;
  // NB! factor for accounting for satellites
  mass = log10(mass)-11;
  fac = 1.0;
  //fac = 1.1;
  //if(mass>0.15)
  // fac = pow(mass-0.15,0.5)/(1+exp(pow(mass,1.1))*2)*wpl.satfac + 1;
  //printf("NB! altering halo mass function by: %f %f\n",fac,mass);
  n = n*fac;
  return(n);

  /* Jenkins et al. FOF .2 best-fit (unless SO180==1)
   */
 JENKINS_FUNCTION:
  a=-JENKINS_A*OMEGA_M*RHO_CRIT/mass/sig;
  n=a*dsdM*exp(-pow(fabs(JENKINS_B-log(sig)),JENKINS_C));
  return(n);

  /* Warren et al. (calibrated only on concordance cosmology, FOF.2)
   */
 WARREN_FUNCTION:
  n = -0.7234*(pow(sig,-1.625)+0.2538)*exp(-1.198/sig/sig)*OMEGA_M*RHO_CRIT/mass/sig*dsdM;
  return(n);

 ST_FUNCTION:

  /* This is a bunch of Sheth-Tormen stuff.
   */
  nuprime=0.841*DELTA_CRIT/sig;
  nufnu=0.644*(1+1.0/pow(nuprime,0.6))*(sqrt(nuprime*nuprime/2/PI))*exp(-nuprime*nuprime/2);
  //n=RHO_CRIT*OMEGA_M/mass*mass*nufnu*fabs(dsdM);
  n=RHO_CRIT*OMEGA_M/mass*nufnu*fabs(dsdM)/sig;
  return(n);

}
Пример #9
0
double halo_mass_function(double mass)
{
  double sig,logm,a,slo,shi,rm,rlo,rhi,mlo,mhi,dsdM,n,nuprime,nufnu,p,A;
  static int flag=0,SO180=0,SO324=0,WARREN=0,ST=0,JENKINS=0,prev_cosmo=0;
  static double pnorm, prev_delta;
  double btemp = -1;

  static double
    a1 = 0.325277,
    a2 = 0.492785,
    a3 = 0.310289,
    a4 = 1.317104,
    a5 = 2.425681;
    
  /* Jenkins et al. SO 180 best-fit
   */
  if(SO180)
    {
      JENKINS_A = 0.301;
      JENKINS_B = 0.64;
      JENKINS_C = 3.82;
    }      
  if(SO324)
    {
      JENKINS_A = 0.316;
      JENKINS_B = 0.67;
      JENKINS_C = 3.82;
    }      
  if(JENKINS) //their .2 fof function
    {
      JENKINS_A = 0.315;
      JENKINS_B = 0.61;
      JENKINS_C = 3.8;
    }      


  /* First normalize the power spectrum
   */
  pnorm=SIGMA_8/sigmac(8.0);
  rm=pow(3.0*mass/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  sig=pnorm*sigmac(rm);
  logm=log10(mass);
  
  mlo=0.99*mass;
  mhi=1.01*mass;
  rlo=pow(3.0*mlo/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  rhi=pow(3.0*mhi/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);

  slo=pnorm*sigmac(rlo);
  shi=pnorm*sigmac(rhi);
  dsdM=(shi-slo)/(mhi-mlo);

  if(SO324)goto JENKINS_FUNCTION;
  if(SO180)goto JENKINS_FUNCTION;
  if(WARREN)goto WARREN_FUNCTION;
  if(ST)goto ST_FUNCTION;
  if(JENKINS)goto JENKINS_FUNCTION;

  /* Tinker et al. (in prep) for SO 200
   */
  if(DELTA_HALO != prev_delta || prev_cosmo != RESET_COSMOLOGY)
    {
      initialize_mass_function(&a1,&a2,&a3,&a4);
      prev_delta = DELTA_HALO;
      prev_cosmo = RESET_COSMOLOGY;

      // if we're using systematic errors in an MCMC, adjust parameter a1 (amplitude)
      if(USE_ERRORS)
	a1 *= M2N.mf_amp;
      fprintf(stderr,"MF PARAMS for DELTA=%f %f %f %f %f\n",DELTA_HALO,a1,a2,a3,a4);
    }
  
  n = -a1*(pow(sig/a3,-a2)+1)*exp(-a4/sig/sig)*OMEGA_M*RHO_CRIT/mass/sig*dsdM;
  return(n);

  /* Jenkins et al. FOF .2 best-fit (unless SO180==1)
   */
 JENKINS_FUNCTION:
  a=-JENKINS_A*OMEGA_M*RHO_CRIT/mass/sig;
  n=a*dsdM*exp(-pow(fabs(JENKINS_B-log(sig)),JENKINS_C));
  return(n);

  /* Warren et al. (calibrated only on concordance cosmology, FOF.2)
   */
 WARREN_FUNCTION:
  n = -0.7234*(pow(sig,-1.625)+0.2538)*exp(-1.198/sig/sig)*OMEGA_M*RHO_CRIT/mass/sig*dsdM;
  return(n);



  /* Need to find the derivative dlog(sig)/dlog(M)
   */
  mlo=0.99*logm;
  mhi=1.01*logm;
  rlo=pow(3.0*pow(10.0,mlo)/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  rhi=pow(3.0*pow(10.0,mhi)/(4.0*PI*OMEGA_M*RHO_CRIT),1.0/3.0);
  slo=log10(pnorm*sigmac(rlo));
  shi=log10(pnorm*sigmac(rhi));
  dsdM=(shi-slo)/(mhi-mlo);

 ST_FUNCTION:

  /* This is a bunch of Sheth-Tormen stuff.
   * NB! because I'm skipping the above derivative (dlogs/dlogM), i'm using the lower
   */
  nuprime=0.841*DELTA_CRIT/sig;
  nufnu=0.644*(1+1.0/pow(nuprime,0.6))*(sqrt(nuprime*nuprime/2/PI))*exp(-nuprime*nuprime/2);
  //n=RHO_CRIT*OMEGA_M/mass*mass*nufnu*fabs(dsdM);
  n=RHO_CRIT*OMEGA_M/mass*nufnu*fabs(dsdM)/sig;
  return(n);




}
Пример #10
0
cv::Mat performSuperpixelSegmentation_VariableSandM(std::vector<std::vector<double> > &kseeds, std::vector<double> &kseedsx, std::vector<double> &kseedsy, const std::vector<cv::Mat> &vec, const cv::Size &size, const int &STEP, const int &NUMITR = 10) {
    
    int sz = size.width*size.height;
	const int numk = (int) kseedsx.size();
	int numitr = 0;
    
	int offset = (STEP < 10) ? STEP*1.5 : STEP;
    
    cv::Mat klabels = cv::Mat(size, cv::DataType<int>::type);
    klabels = -1;
    
    std::vector<std::vector<double> > sigmac(0);
    for (int c = 0; c < vec.size(); c++) {
        sigmac.push_back(std::vector<double>(numk, 0));
    }
    std::vector<double> sigmax(numk, 0);
    std::vector<double> sigmay(numk, 0);
    std::vector<int> clustersize(numk, 0);
    std::vector<double> inv(numk, 0);//to store 1/clustersize[k] values
    std::vector<double> distxy(sz, DBL_MAX);
    std::vector<double> distc(sz, DBL_MAX);
    std::vector<double> distvec(sz, DBL_MAX);
    std::vector<double> maxc(numk, 10*10);//THIS IS THE VARIABLE VALUE OF M, just start with 10
    std::vector<double> maxxy(numk, STEP*STEP);//THIS IS THE VARIABLE VALUE OF M, just start with 10
    
	double invxywt = 1.0/(STEP*STEP);//NOTE: this is different from how usual SLIC/LKM works
    
	while( numitr < NUMITR )
	{
		//------
		//cumerr = 0;
		numitr++;
		//------
        
		distvec.assign(sz, DBL_MAX);
		for( int n = 0; n < numk; n++ )
		{
			int y1 = std::max(double(0), kseedsy[n]-offset);
			int y2 = std::min(double(size.height),	kseedsy[n]+offset);
			int x1 = std::max(double(0), kseedsx[n]-offset);
			int x2 = std::min(double(size.width),	kseedsx[n]+offset);
            
			for( int y = y1; y < y2; y++ )
			{
				for( int x = x1; x < x2; x++ )
				{
					int i = y*size.width + x;
					if( !(y < size.height && x < size.width && y >= 0 && x >= 0) ) {
                        throw cv::Exception();
                    }
                    
					distc[i] =	0;
                    for (int c = 0; c < vec.size(); c++) {
                        distc[i] += (vec[c].ptr<double>()[i] - kseeds[c][n]) * (vec[c].ptr<double>()[i] - kseeds[c][n]);
                    }
                    
					distxy[i] =		(x - kseedsx[n])*(x - kseedsx[n]) + (y - kseedsy[n])*(y - kseedsy[n]);
                    
					double dist = distc[i]/maxc[n] + distxy[i]*invxywt;//only varying m, prettier superpixels
					
					if( dist < distvec[i] )
					{
						distvec[i] = dist;
						klabels.ptr<int>()[i]  = n;
					}
				}
			}
		}
		//-----------------------------------------------------------------
		// Assign the max color distance for a cluster
		//-----------------------------------------------------------------
		if(0 == numitr)
		{
			maxc.assign(numk,1);
			maxxy.assign(numk,1);
		}
		{for( int i = 0; i < sz; i++ )
		{
			if(maxc[klabels.ptr<int>()[i]] < distc[i]) maxc[klabels.ptr<int>()[i]] = distc[i];
			if(maxxy[klabels.ptr<int>()[i]] < distxy[i]) maxxy[klabels.ptr<int>()[i]] = distxy[i];
		}}
		//-----------------------------------------------------------------
		// Recalculate the centroid and store in the seed values
		//-----------------------------------------------------------------
        
        for (int c = 0; c < sigmac.size(); c++) {
            sigmac[c].assign(numk, 0);
        }
		sigmax.assign(numk, 0);
		sigmay.assign(numk, 0);
		clustersize.assign(numk, 0);
        
		for( int j = 0; j < sz; j++ )
		{
			if(!(klabels.ptr<int>()[j] >= 0))
                throw cv::Exception();
            
            for (int c = 0; c < sigmac.size(); c++) {
                sigmac[c][klabels.ptr<int>()[j]] += vec[c].ptr<double>()[j];
            }
			sigmax[klabels.ptr<int>()[j]] += (j%size.width);
			sigmay[klabels.ptr<int>()[j]] += (j/size.width);
            
			clustersize[klabels.ptr<int>()[j]]++;
		}
        
		{for( int k = 0; k < numk; k++ )
		{
			if( clustersize[k] <= 0 ) clustersize[k] = 1;
			inv[k] = 1.0/double(clustersize[k]);//computing inverse now to multiply, than divide later
		}}
		
		{for( int k = 0; k < numk; k++ )
		{
            for (int c = 0; c < kseeds.size(); c++) {
                kseeds[c][k] = sigmac[c][k] * inv[k];
            }
			kseedsx[k] = sigmax[k]*inv[k];
			kseedsy[k] = sigmay[k]*inv[k];
		}}
	}
    
    return klabels;
}