int main(void){ double *r=NULL; double *theta=NULL; double *phi=NULL; double *M=NULL; /* Global variables */ /* To do: put this as input parameters n_points should be derived when the data is read nmax and lmax should be input parameters filename should be an input parameter output of the data should be an input parameter */ int n_points=856182; int nmax=5; int lmax=5; char filename[40]="LRMWLMC_halo_snap0.txt"; double r_s = 40.85; // ------------------------ /* Allocating memory for pointers */ r = malloc(n_points*sizeof(long double)); theta = malloc(n_points*sizeof(long double)); phi = malloc(n_points*sizeof(long double)); M = malloc(n_points*sizeof(long double)); read_data(filename, n_points, r, theta, phi, M, r_s); cov_matrix(n_points, r, theta, phi, M, nmax, lmax); return 0; }
void rich_cell:: update_eccentricity_and_radius() { //Step1: project the points to the x-y plane std::vector<vnl_vector_fixed<double, 2> > points_2d; std::vector<bool> checked(all_points_.size(), false); for (unsigned int i = 0; i<all_points_.size(); i++) { if (!checked[i]) { //record the x-y position vnl_vector_fixed<double, 2> pt(all_points_[i][0],all_points_[i][1]); points_2d.push_back( pt ); checked[i] = true; //scan through the list to see if other points in the remaining //list contains the same x-y position for (unsigned int j = i+1; j<all_points_.size(); j++) { if (all_points_[j][0] == all_points_[i][0] && all_points_[j][1] == all_points_[i][1]) checked[j] = true; } } } //Step2: compute the scatter matrix // // compute the center vnl_vector_fixed<double, 2> center(0.0,0.0); for (unsigned int i = 0; i<points_2d.size(); i++) { center += points_2d[i]; } center /= (double)points_2d.size(); vnl_matrix<double> cov_matrix(2,2,0.0); std::cout<<"points_2d.size = "<<points_2d.size()<<std::endl; for (unsigned int i = 0; i<points_2d.size(); i++) { cov_matrix += outer_product(points_2d[i]-center, points_2d[i]-center); } cov_matrix /= (double)points_2d.size(); //perform eigen-value decomposition to get the semi-major (a) and minor (b) vnl_svd<double> svd_from( cov_matrix ); //eccentricity=sqrt( 1-(b^2/a^2) ). If the shape is close to //circular, the value is 0. //eccentricity_ =vcl_sqrt( 1- (svd_from.W(1)*svd_from.W(1))/(svd_from.W(0)*svd_from.W(0)) ); eccentricity_ =vcl_sqrt( 1- vnl_math_abs(svd_from.W(1)/svd_from.W(0)) ); float long_axis_mag = vcl_sqrt(vnl_math_abs(svd_from.W(0))); float short_axis_mag = vcl_sqrt(vnl_math_abs(svd_from.W(1))); average_radius_ = 0.5*(long_axis_mag + short_axis_mag); }
CovarianceSet* init_covariance_set( Cokriging_type type, const Covariance<Location>& C11, CokrigTagMap& tags_map, const Parameters_handler* parameters, Error_messages_handler* errors, CokrigDefaultsMap defaults ) { CovarianceSet* cov_set = 0; switch( type ) { case geostat_utils::FULL : // full cokriging { std::string tag_string = tags_map[ geostat_utils::FULL ]; std::vector<std::string> tags = String_Op::decompose_string( tag_string, " ", false ); // we need 2 tags: one for C12 and one for C22 if( tags.size() != 2 ) return 0; Covariance<Location> C12; geostat_utils::initialize_covariance( &C12, tags[0], parameters, errors ); Covariance<Location> C22; geostat_utils::initialize_covariance( &C22, tags[1], parameters, errors ); TNT::Matrix< Covariance<Location> > cov_matrix(2,2); cov_matrix(1,1) = C11; cov_matrix(2,2) = C22; cov_matrix(1,2) = C12; cov_matrix(2,1) = C12; typedef LMC_covariance< Covariance<Location> > LmcCovariance; LmcCovariance* tmp_covset = new LmcCovariance( cov_matrix, 2 ); cov_set = new CovarianceSet( tmp_covset ); delete tmp_covset; break; } case geostat_utils::MM1 : // colocated cokriging with Markov Model 1 { std::string tag_string = tags_map[ geostat_utils::MM1 ]; std::vector<std::string> tags = String_Op::decompose_string( tag_string, " ", false ); // we need 2 tags: one for C12(0) and one for C22(0) if( tags.size() != 2 ) return 0; TNT::Matrix<double> cov_mat(2,2); double C22; CokrigDefaultsMap::const_iterator defaults_it = defaults.find( geostat_utils::MM1 ); if( defaults_it != defaults.end() ) C22 = String_Op::to_number<double>( defaults_it->second ); else C22 = String_Op::to_number<double>( parameters->value(tags[1]) ); cov_mat(1,1) = C11.compute( EuclideanVector( 0, 0, 0 ) ); cov_mat(2,2) = C22; double correl = String_Op::to_number<double>( parameters->value(tags[0]) ); cov_mat(1,2) = correl * std::sqrt( cov_mat(1,1) * cov_mat( 2,2 ) ); cov_mat(2,1) = cov_mat(1,2); typedef MM1_covariance< Covariance<Location> > Mm1Covariance; Mm1Covariance* tmp_mm1 = new Mm1Covariance( C11, cov_mat, 2 ); cov_set = new CovarianceSet( tmp_mm1 ); delete tmp_mm1; break; } case geostat_utils::MM2 : // colocated cokriging with Markov Model 2 { std::string tag_string = tags_map[ geostat_utils::MM2 ]; std::vector<std::string> tags = String_Op::decompose_string( tag_string, " ", false ); // we need 2 tags: one for C12(0) and one for C22(h) if( tags.size() != 2 ) return 0; Covariance<Location> C22; geostat_utils::initialize_covariance( &C22, tags[1], parameters, errors ); TNT::Matrix<double> cov_mat(2,2); cov_mat(1,1) = C11.compute( EuclideanVector( 0, 0, 0 ) ); cov_mat(2,2) = C22.compute( EuclideanVector( 0, 0, 0 ) ); double correl = String_Op::to_number<double>( parameters->value(tags[0]) ); cov_mat(1,2) = correl * cov_mat(1,1) * cov_mat( 2,2 ); cov_mat(2,1) = cov_mat(1,2); std::vector< Covariance<Location> > cov_vector(2); cov_vector[0] = C11; cov_vector[1] = C22; typedef MM2_covariance< Covariance<Location> > Mm2Covariance; Mm2Covariance* tmp_mm2 = new Mm2Covariance( cov_vector.begin(), cov_vector.end(), cov_mat, 2 ); cov_set = new CovarianceSet( tmp_mm2 ); delete tmp_mm2; break; } } // end switch return cov_set; }