Exemple #1
0
void qr<T>::output(array_2d<T>& x) const
{
	if(x.rows() != m || x.columns() != n)
		x.resize(idx(m, n));

	lapack_interface<T>::call_lapack_geqrf(x, a);
}
Exemple #2
0
void write_array_2d(const array_2d <T, K>& a, std::ostream& s)
{
	size_t arr = a.columns();  // number of arrays
	write_size(arr, s);

	size_t dim = a.rows();  // dimension of each array
	for (size_t n = 0, p = 0; n < arr; n++, p += dim) {
		write_size(dim, s);
		write(a[p], s, dim);
	}
}
typename image<T, D>::create_new get_quadrange_sub_pix(const image<T, D>& a,
		const array_2d<double>& map_matrix, const size_array& dst_size)
{
	IplImage* src = a.ipl();
	IplImage* dst;

	if(dst_size[0] == 0 && dst_size[1] == 0) {
		dst = cvCreateImage(cvGetSize(src),
		image_details::ipl_depth<T>(), int(a.channels()));
	} else {
		dst = cvCreateImage(cvSize(dst_size[1], dst_size[0]),
		image_details::ipl_depth<T>(), int(a.channels()));
	}

	CHECK(map_matrix.rows() == 2, eshape);
	CHECK(map_matrix.columns() == 2, eshape);

	CvMat* cvmap_matrix = cvCreateMat(2, 3, image_details::cv_type<double>());
	cvmSet(cvmap_matrix, 0, 0, map_matrix(0, 0));
	cvmSet(cvmap_matrix, 0, 1, map_matrix(0, 1));
	cvmSet(cvmap_matrix, 0, 2, map_matrix(0, 2));
	cvmSet(cvmap_matrix, 1, 0, map_matrix(1, 0));
	cvmSet(cvmap_matrix, 1, 1, map_matrix(1, 1));
	cvmSet(cvmap_matrix, 1, 2, map_matrix(1, 2));

	/*cvmap_matrix->data.db[0] = map_matrix(0, 0);
	cvmap_matrix->data.db[1] = map_matrix(0, 1);
	cvmap_matrix->data.db[2] = map_matrix(0, 2);
	cvmap_matrix->data.db[3] = map_matrix(1, 0);
	cvmap_matrix->data.db[4] = map_matrix(1, 1);
	cvmap_matrix->data.db[5] = map_matrix(1, 2);*/

	cvGetQuadrangleSubPix(src, dst, cvmap_matrix);

	typename image<T, D>::create_new r(dst);

	cvReleaseMat(&cvmap_matrix);
	cvReleaseImage(&src);
	cvReleaseImage(&dst);
	return r;
}
Exemple #4
0
void kd_tree::build_tree(array_2d<double> &mm){
    
    array_1d<double> i_min,i_max;
    int i;
    
    for(i=0;i<mm.get_cols();i++){
        i_min.set(i,0.0);
        i_max.set(i,1.0);
    }
    
    build_tree(mm,i_min,i_max);
}
Exemple #5
0
void qr<T>::output(array_2d<T>& q, array_2d<T>& r) const
{
	array<T> tau(min_nm);

	if(r.rows() != m || r.columns() != n)
		r.resize(idx(m, n));

	lapack_interface<T>::call_lapack_geqrf(r, tau, a);

	//Now construct Q
	//Q should be a MxM matrix
	if(q.rows() != m || q.columns() != m)
		q.resize(idx(m, m));

	if(n <= m) {
		//Q can hold the MxN values of R needed by lapack's xORGQR routines
		for(size_t i = 0; i < m; i++)
			for(size_t j = 0; j < n; j++)
				q[m*j + i] = r(i,j);

		lapack_interface<T>::call_lapack_orgqr(q, tau);

	} else {
		//create a new array and do a copy since
		//Q cannot hold the MxN values of R as needed by lapack's xORGQR routines
		array_2d<T> temp_q(r(all(), size_range(0, m - 1)));

		lapack_interface<T>::call_lapack_orgqr(temp_q, tau);

		//copy the Q values from temp
		for(size_t i = 0; i < m; i++)
			for(size_t j = 0; j < m; j++)
				q[m*j + i] = temp_q[m*j + i];
	}

	// R is upper triangular
	for(size_t i = 0; i < m; i++)
		for(size_t j = 0; j < n; j++)
			if(i > j)
				r(i,j) = 0;
}
Exemple #6
0
void write(const array_2d <T>& a, std::ostream& s)
{
	write(a[0], s, a.length());
}
Exemple #7
0
void read(array_2d <T>& a, std::istream& s)
{
	read(a[0], s, a.length());
}
Exemple #8
0
	lapack_from_std_out_and_unroll(ct* dst, ct* w, size_t rows, size_t cols)
		: dst(dst), w_ptr(w), a(rows, cols)
	{
		ptr = a.c_ptr();
	}
Exemple #9
0
void kd_tree::build_tree(array_2d<double> &mm,
    array_1d<double> &nmin, array_1d<double> &nmax){
   
    if(nmin.get_dim()!=mm.get_cols()){
        printf("WARNING nimin dim %d cols %d\n",nmin.get_dim(),mm.get_cols());
        throw -1;
    }
   
    if(nmax.get_dim()!=mm.get_cols()){
        printf("WARNING nmax dim %d cols %d\n",nmax.get_dim(),mm.get_cols());
        throw -1;
    } 
    
    search_time=0.0;
    search_ct=0;
    
    search_ct_solo=0;
    search_time_solo=0.0;
    
    data.reset();
    tree.reset();
   
    array_1d<int> inn,use_left,use_right;
    array_1d<double> tosort,sorted;
  
    int i,j,k,l,inp;

    tol=1.0e-7;

    diagnostic=1;
  
    tree.set_dim(mm.get_rows(),4);
    data.set_dim(mm.get_rows(),mm.get_cols());
   
    use_left.set_name("kd_tree_constructor_use_left");
    use_right.set_name("kd_tree_constructor_use_right");
    tree.set_name("kd_tree_tree");
    data.set_name("kd_tree_data");
   
    //tree[i][0] will be the dimension being split
    //tree[i][1] will be left hand node (so, lt)
    //tree[i][2] will be right hand node (so, ge)
    //tree[i][3] will be parent
      
    mins.set_name("kd_tree_mins");
    maxs.set_name("kd_tree_maxs");
   
    for(i=0;i<data.get_cols();i++){
        mins.set(i,nmin.get_data(i));
        maxs.set(i,nmax.get_data(i));
    }
   
    array_1d<double> vector;
   
    for(i=0;i<data.get_rows();i++){
         data.set_row(i,(*mm(i)));
    }

    sorted.set_name("kd_tree_constructor_sorted");
    tosort.set_name("kd_tree_constructor_tosort");
    inn.set_name("kd_tree_constructor_inn");
    
    /*sort the data points by their 0th dimension component*/
    for(i=0;i<data.get_rows();i++){
         tosort.set(i,data.get_data(i,0));
         inn.set(i,i);
    }

    sort_and_check(tosort,sorted,inn);

    /*try to pick the median value as the first node in the tree (the
    masterparent)*/
    inp=data.get_rows()/2;
    while(inp>0 && sorted.get_data(inp)-sorted.get_data(inp-1)<tol){
         /*make sure that the division doesn't come in the middle of a bunch
         of identical points*/
         
         inp--;
    }
   
    masterparent=inn.get_data(inp);
    
    /*assign the remaining points to either the left branch or the right
    branch of the masterparent*/
    for(j=0;j<data.get_rows();j++){
        if(masterparent!=j){
            if(data.get_data(j,0)<sorted.get_data(inp)){
                use_left.add(j);
            }
            else{
                use_right.add(j);
            }
        }
    } 

    /*organize all of the points on the left branch of the masterparent*/
    if(use_left.get_dim()>0){
        organize(use_left,0,masterparent,1,use_left.get_dim(),1);   
    }
    else tree.set(masterparent,1,-1);
   
    /*organize all of the points on the right branch of the masterparent*/
    if(use_right.get_dim()>0){
        organize(use_right,0,masterparent,1,use_right.get_dim(),2);    
    }
    else tree.set(masterparent,2,-1);
   
    tree.set(masterparent,3,-1);/*masterparent has no parent of its own*/
    
    tree.set(masterparent,0,0);/*masterparent split on the 0th dimension*/
   
    /*check to make sure everything has the dimensions that it should*/
    if(mins.get_dim()!=maxs.get_dim() || mins.get_dim()!=data.get_cols() || tree.get_rows()!=data.get_rows()){
        printf("WARNING tried to make tree but\n");
        printf("nmax %d\n",maxs.get_dim());
        printf("nmin %d\n",mins.get_dim());
        printf("tree %d %d data %d %d\n",tree.get_rows(),tree.get_cols(),
        data.get_rows(),data.get_cols());
       
        exit(1);
    }
  
}
array_8d<double> Nevpt2_npdm::compute_EEEE_matrix(array_2d<double>& onepdm, array_4d<double>& twopdm, array_6d<double>& threepdm, array_8d<double>& fourpdm)
{
if( mpigetrank() == 0 ) {

  std::cout << "Building spatial <0|EEEE|0>\n";

  int dim = threepdm.dim1(); 
  assert( onepdm.dim1() == twopdm.dim1() );
  array_8d<double> eeee_matrix(dim,dim,dim,dim,dim,dim,dim,dim);
  eeee_matrix.Clear();

  // Output text file
  double factor = 1.0;
  char file[5000];
  sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/EEEE_matrix.", 0, 0,".txt");
  ofstream ofs(file);
  ofs << dim << endl;

  for(int i=0; i<dim; ++i) {
    for(int j=0; j<dim; ++j) {

      for(int k=0; k<dim; ++k) {
        int d_jk = ( j == k );
        for(int l=0; l<dim; ++l) {
          for(int m=0; m<dim; ++m) {
            int d_lm = ( l == m );
            int d_jm = ( j == m );
            for(int n=0; n<dim; ++n) {
              for(int p=0; p<dim; ++p) {
                int d_np = ( n == p );
                int d_lp = ( l == p );
                int d_jp = ( j == p );
                for(int q=0; q<dim; ++q) {

                  double val = 0.0;
                  // 1PDM terms
                  val += d_jk * d_lm * d_np * onepdm(i,q);
                  // 2PDM terms 
                  val += d_jk * d_lm * 2.0*twopdm(i,p,q,n); // Note factor of two difference between Block 2PDMs and other codes
                  val += d_jk * d_np * 2.0*twopdm(i,m,q,l);
                  val += d_jk * d_lp * 2.0*twopdm(i,m,n,q);
                  val += d_jm * d_np * 2.0*twopdm(i,k,l,q);
                  val += d_jm * d_lp * 2.0*twopdm(i,k,q,n);
                  val += d_lm * d_np * 2.0*twopdm(i,k,q,j);
                  val += d_lm * d_jp * 2.0*twopdm(i,k,n,q);
                  // 3PDM terms
                  val += d_jk * threepdm(i,m,p,q,n,l);
                  val += d_jm * threepdm(i,k,p,q,l,n);
                  val += d_lm * threepdm(i,k,p,q,n,j);
                  val += d_np * threepdm(i,k,m,q,l,j);
                  val += d_lp * threepdm(i,k,m,n,q,j);
                  val += d_jp * threepdm(i,k,m,n,l,q);
                  // 4PDM terms
                  val += fourpdm(i,k,m,p,j,l,n,q);

                  if ( abs(val) > 1e-14 ) {
                    ofs << boost::format("%d %d %d %d %d %d %d %d %20.14e\n") % i % j % k % l % m % n % p % q % val;
                    eeee_matrix(i,j,k,l,m,n,p,q) = val;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  ofs.close();

  // Save binary matrix
  sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/EEEE_matrix.", 0, 0,".bin");
  std::ofstream ofs2(file, std::ios::binary);
  boost::archive::binary_oarchive save(ofs2);
  save << eeee_matrix;
  ofs2.close();

  return eeee_matrix;

}
}