Exemple #1
0
/**
Restores the size, address, and value information for a dvar_vector.
Back up the stream and read the number of bytes written in the
``write function'' corresponding to this ``read function''
\param
*/
dvector restore_dvar_vector_value(const dvar_vector_position& tmp)
{
  dvector temp_vec(tmp.indexmin(),tmp.indexmax());
  for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
  {
    double ttmp = 0.0;
    //gradient_structure::get_fp()->fread(&ttmp,sizeof(double));
    gradient_structure::get_fp()->fread(ttmp);
    temp_vec(i)=ttmp;
  }
  return temp_vec;
}
Exemple #2
0
/**
Restores the size, address, and value information for a dvector.
Back up the stream and read the number of bytes written in the
``write function'' corresponding to this ``read function''

\param tmp To get indexmin and indexmax
\return dvector 
*/
dvector restore_dvector_value(const dvector_position& tmp)
{
  // restores the size, address, and value information for a dvar_vector
  dvector temp_vec(tmp.indexmin(),tmp.indexmax());
  for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
  {
    double ttmp = 0.0;
    gradient_structure::get_fp()->fread(ttmp);
    temp_vec(i)=ttmp;
  }
  return temp_vec;
}
Exemple #3
0
/**
 * Description not yet available.
 * \param
 */
ivector restore_ivector_value(const ivector_position& tmp)
{
  // restores the size, address, and value information for a ivector
  // Back up the stream and read the number of bytes written in the
  // ``write function'' corresponding to this ``read function''
  ivector temp_vec(tmp.indexmin(),tmp.indexmax());
  for (int i=tmp.indexmax();i>=tmp.indexmin();i--)
  {
    int n = 0;
    gradient_structure::get_fp()->fread(&n, sizeof(int));
    temp_vec(i) = n;
  }
  return temp_vec;
  // Back up the stream again for the next function
}
Exemple #4
0
/*!
 * \brief Solve.
 */
void JacobiSolver::iterate( const int max_iters, const double tolerance )
{
    // Extract the linear problem.
    Epetra_CrsMatrix *A = 
	dynamic_cast<Epetra_CrsMatrix*>( d_linear_problem->GetMatrix() );
    Epetra_Vector *x = 
	dynamic_cast<Epetra_Vector*>( d_linear_problem->GetLHS() );
    const Epetra_Vector *b = 
	dynamic_cast<Epetra_Vector*>( d_linear_problem->GetRHS() );

    // Setup the residual.
    Epetra_Map row_map = A->RowMap();
    Epetra_Vector residual( row_map );

    // Iterate.
    Epetra_CrsMatrix H = buildH( A );
    Epetra_Vector temp_vec( row_map );
    d_num_iters = 0;
    double residual_norm = 1.0;
    double b_norm;
    b->NormInf( &b_norm );
    double conv_crit = b_norm*tolerance;
    while ( residual_norm > conv_crit && d_num_iters < max_iters )
    {
	H.Apply( *x, temp_vec );
	x->Update( 1.0, temp_vec, 1.0, *b, 0.0 );

	A->Apply( *x, temp_vec );
	residual.Update( 1.0, *b, -1.0, temp_vec, 0.0 );

	residual.NormInf( &residual_norm );
	++d_num_iters;
    }
}
//Contains Duplicate II,使用unordered_map<int,vector<int>>内存超出
		bool containsNearbyDuplicate(vector<int>& nums, int k)
		{
			int length = nums.size();
			if (length == 0 || k==0)
				return false;
			//用vector<int>来记录nums[i]出现的位置索引
			unordered_map<int, vector<int>> map_index;
			for (int i = 0; i < nums.size(); i++)
			{
				auto find_res = map_index.find(nums[i]);
				if (find_res == map_index.end())
				{
					vector<int> temp_vec(i);
					map_index[nums[i]] = temp_vec;
				}
				else
					find_res->second.push_back(i);
			}
			for (auto iter = map_index.begin(); iter != map_index.end(); iter++)
			{
				if (iter->second.size() > 1)
				{
					auto vec = iter->second;
					for (int j = 1; j < vec.size(); j++)
					{
						if (vec[j] - vec[j - 1] <= k)
							return true;
					}
				}
			}
			return false;
		}
 int EpetraSymOp::ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
 {
   int info=0;
   Epetra_MultiVector temp_vec(OperatorDomainMap(), Y.NumVectors()); 
   //
   // This generalized operator computes Y = (A^T*A)^{-1}*X or Y = (A*A^T)^{-1}*X
   //
   // Transpose the operator (if isTrans_ = true)
   if (!isTrans_) {
     info=Epetra_Op->SetUseTranspose( !isTrans_ );
     if (info!=0) { return info; }
   }
   //
   // Compute A^{-1}*X or A^{-T}*X 
   //
   info=Epetra_Op->ApplyInverse( X, temp_vec );
   if (info!=0) { return info; }
   //
   // Transpose/Un-transpose the operator based on value of isTrans_
   info=Epetra_Op->SetUseTranspose( isTrans_ );
   if (info!=0) { return info; }
   
   // Compute A^{-T}*(A^{-1}*X) or A^{-1}*A^{-T}
   info=Epetra_Op->Apply( temp_vec, Y );
   if (info!=0) { return info; }
   
   // Un-transpose the operator
   info=Epetra_Op->SetUseTranspose( false );
   return info;
 }
void Mesh::findNeighbors(void) {
  std::vector<std::vector<int>> temp_vec(vertices.size());
  for (const auto &e : edges) {
    temp_vec[e.v1].push_back(e.v2);
    temp_vec[e.v2].push_back(e.v1);
  }
  neighbors = temp_vec;
}
Exemple #8
0
void LR::init_omega()
{
	 float init_value = 0.0;
	//float init_value = (float)1/class_set_size;
	for (int i = 0; i < feat_set_size; i++) {
		vector<float> temp_vec(class_set_size, init_value);
		omega.push_back(temp_vec);
	}
}
void LocalGeometryRef::SVD(int num_eigen)
{
	vnl_symmetric_eigensystem<double> eigen(this->probability_matrix);
	vnl_matrix<double> eig_vec = eigen.V;
	vnl_diag_matrix<double> eig_val = eigen.D;
	
	vnl_vector<double> temp_vec(this->num_row);
	for(int row = 0; row<eig_val.rows(); row++)
	{
		temp_vec(row) = eig_val(row,row);
	}

	this->eigVals.set_size(num_eigen);
	this->eigVecs.set_size(this->num_row, num_eigen);
	for(int i=0; i<num_eigen; i++)
	{
		this->eigVals(i) = temp_vec.max_value();
		int pos = temp_vec.arg_max();
		temp_vec(pos) = temp_vec.min_value() - 1;
		this->eigVecs.set_column(i, eig_vec.get_column(pos) * this->eigVals(i) );
	}

	//output files for debugging
	FILE *fp1 = fopen("eigenvecters.txt","w");
	for(int i=0; i<this->num_row; i++)
	{
		for(int j=0; j<num_eigen; j++)
		{
			fprintf(fp1,"%f\t",this->eigVecs(i, j));
		}
		fprintf(fp1,"\n");
	}
	fclose(fp1);

	FILE *fp2 = fopen("eigenvalues.txt","w");
	for(int i=0; i<num_eigen; i++)
	{
		fprintf(fp2,"%f\t",this->eigVals(i));
		std::cout<<this->eigVals(i)<<std::endl;
		fprintf(fp2,"\n");
	}
	fclose(fp2);
}
 //
 // AnasaziOperator applications
 //
 void EpetraSymMVOp::Apply ( const MultiVec<double>& X, MultiVec<double>& Y ) const 
 {
   int info=0;
   MultiVec<double> & temp_X = const_cast<MultiVec<double> &>(X);
   Epetra_MultiVector* vec_X = dynamic_cast<EpetraMultiVecAccessor*>(&temp_X)->GetEpetraMultiVec();
   Epetra_MultiVector* vec_Y = dynamic_cast<EpetraMultiVecAccessor*>(&Y)->GetEpetraMultiVec();
   
   if (isTrans_) {
     
     Epetra_MultiVector temp_vec( *MV_localmap, temp_X.GetNumberVecs() );
     
     /* A'*X */
     info = temp_vec.Multiply( 'T', 'N', 1.0, *Epetra_MV, *vec_X, 0.0 );
     TEUCHOS_TEST_FOR_EXCEPTION( info != 0, OperatorError, 
                         "Anasazi::EpetraSymMVOp::Apply(): Error returned from Epetra_MultiVector::Multiply()" );
     
     /* A*(A'*X) */
     info = vec_Y->Multiply( 'N', 'N', 1.0, *Epetra_MV, temp_vec, 0.0 );      
     TEUCHOS_TEST_FOR_EXCEPTION( info != 0, OperatorError, 
                         "Anasazi::EpetraSymMVOp::Apply(): Error returned from Epetra_MultiVector::Multiply()" );
   } 
   else {
     
     Epetra_MultiVector temp_vec( *MV_blockmap, temp_X.GetNumberVecs() );
     
     /* A*X */
     info = temp_vec.Multiply( 'N', 'N', 1.0, *Epetra_MV, *vec_X, 0.0 );
     TEUCHOS_TEST_FOR_EXCEPTION( info != 0, OperatorError, 
                         "Anasazi::EpetraSymMVOp::Apply(): Error returned from Epetra_MultiVector::Multiply()" );
     
     /* A'*(A*X) */
     info = vec_Y->Multiply( 'T', 'N', 1.0, *Epetra_MV, temp_vec, 0.0 );
     TEUCHOS_TEST_FOR_EXCEPTION( info != 0, OperatorError, 
                         "Anasazi::EpetraSymMVOp::Apply(): Error returned from Epetra_MultiVector::Multiply()" );
   }
 }
 //-------------------------------------------------------------
 //
 // this[i] = alpha[i] * this[i]
 // 
 //-------------------------------------------------------------
 void EpetraOpMultiVec::MvScale ( const std::vector<double>& alpha )
 {
   // Check to make sure the vector is as long as the multivector has columns.
   int numvecs = this->GetNumberVecs();
   TEUCHOS_TEST_FOR_EXCEPTION( (int)alpha.size() != numvecs, std::invalid_argument, 
       "Anasazi::EpetraOpMultiVec::MvScale() alpha argument size was inconsistent with number of vectors in mv.");
   
   std::vector<int> tmp_index( 1, 0 );
   for (int i=0; i<numvecs; i++) {
     Epetra_MultiVector temp_vec(Epetra_DataAccess::View, *Epetra_MV, &tmp_index[0], 1);
     TEUCHOS_TEST_FOR_EXCEPTION( 
         temp_vec.Scale( alpha[i] ) != 0,
         EpetraSpecializedMultiVecFailure, "Anasazi::EpetraOpMultiVec::MvScale() call to Epetra_MultiVector::Scale() returned a nonzero value.");
     tmp_index[0]++;
   }
 }
  void EpetraOpMultiVec::SetBlock( const MultiVec<double>& A, const std::vector<int>& index ) 
  {
    // this should be revisited to e
    EpetraOpMultiVec temp_vec(Epetra_OP, Epetra_DataAccess::View, *Epetra_MV, index);

    int numvecs = index.size();
    if ( A.GetNumberVecs() != numvecs ) {
      std::vector<int> index2( numvecs );
      for(int i=0; i<numvecs; i++)
        index2[i] = i;
      EpetraOpMultiVec *tmp_vec = dynamic_cast<EpetraOpMultiVec *>(&const_cast<MultiVec<double> &>(A)); 
      TEUCHOS_TEST_FOR_EXCEPTION( tmp_vec==NULL, std::invalid_argument, "Anasazi::EpetraOpMultiVec::SetBlocks() cast of MultiVec<double> to EpetraOpMultiVec failed.");
      EpetraOpMultiVec A_vec(Epetra_OP, Epetra_DataAccess::View, *(tmp_vec->GetEpetraMultiVector()), index2);
      temp_vec.MvAddMv( 1.0, A_vec, 0.0, A_vec );
    }
    else {
      temp_vec.MvAddMv( 1.0, A, 0.0, A );
    }
  }
Exemple #13
0
C3DPlotCanvas::C3DPlotCanvas(Project* project_s, C3DPlotFrame* t_frame,
							 HighlightState* highlight_state_s,
							 const std::vector<GeoDaVarInfo>& v_info,
							 const std::vector<int>& col_ids,
							 wxWindow *parent,
							 wxWindowID id, const wxPoint& pos,
							 const wxSize& size, long style)
: wxGLCanvas(parent, id, pos, size, style), ball(0),
project(project_s), table_int(project_s->GetTableInt()),
num_obs(project_s->GetTableInt()->GetNumberRows()), num_vars(v_info.size()),
num_time_vals(1), highlight_state(highlight_state_s), var_info(v_info),
data(v_info.size()), scaled_d(v_info.size()),
c3d_plot_frame(t_frame)
{
	selectable_fill_color = GdaConst::three_d_plot_default_point_colour;
	highlight_color = GdaConst::three_d_plot_default_highlight_colour;
	canvas_background_color=GdaConst::three_d_plot_default_background_colour;
	
	data_stats.resize(var_info.size());
	var_min.resize(var_info.size());
	var_max.resize(var_info.size());
	
	std::vector<double> temp_vec(num_obs);
	for (int v=0; v<num_vars; v++) {
		table_int->GetColData(col_ids[v], data[v]);
		table_int->GetColData(col_ids[v], scaled_d[v]);
		int data_times = data[v].shape()[0];
		data_stats[v].resize(data_times);
		for (int t=0; t<data_times; t++) {
			for (int i=0; i<num_obs; i++) {
				temp_vec[i] = data[v][t][i];
			}
			data_stats[v][t].CalculateFromSample(temp_vec);
		}
	}
	
	c3d_plot_frame->ClearAllGroupDependencies();
	for (int i=0, sz=var_info.size(); i<sz; ++i) {
		c3d_plot_frame->AddGroupDependancy(var_info[i].name);
	}
	
	VarInfoAttributeChange();
	UpdateScaledData();
	
	xs = 0.1;
	xp = 0.0;
    ys = 0.1;
	yp = 0.0;
	zs = 0.1;
	zp = 0.0;
	
	m_bLButton = false;
	m_bRButton = false;
	m_brush = false;

	bb_min[0] = -1;
	bb_min[1] = -1;
	bb_min[2] = -1;
	bb_max[0] = 1;
	bb_max[1] = 1;
	bb_max[2] = 1;
	Vec3f b_min(bb_min);
	Vec3f b_max(bb_max);
	Vec3f ctr((bb_max[0]+bb_min[0])/2.0f, (bb_max[1]+bb_min[1])/2.0f,
			  (bb_max[2]+bb_min[2])/2.0f);
	
	float radius = norm(b_max - ctr);
	ball = new Arcball();
	ball->bounding_sphere(ctr, radius);

	m_x = false;
	m_y = false;
	m_z = false;
	m_d = true;
	b_select = false;
	isInit = false;
	
	highlight_state->registerObserver(this);
}