Exemple #1
0
void testEigen()
{
    JACOBI_ARRAY ja(3), value;
    JACOBI_MATRIX jm, vector;
    for (int i = 0; i < 100; ++i)
    {
        jm.clear();
        ja[0] = 1;
        ja[1] = -1;
        ja[2] = 0;
        jm.push_back(ja);
        ja[0] = -1;
        ja[1] = 1;
        ja[2] = 2;
        jm.push_back(ja);
        ja[0] = 0;
        ja[1] = 2;
        ja[0] = 1;
        jm.push_back(ja);
        Jacobi j;
        j.setMatrix(jm);
        value = j.getEigenvalues();
        vector = j.getEigenvectors();
        j.printEigen();
    }
}
Exemple #2
0
BoundaryInt::BoundaryInt( string _name, Mesh* _mesh, Field* _field, Field* _fPrev ) : RHSOp( _name, _mesh, 0.0, _field ) {
        int 		endLoop;
        double 		xCurr, xPrev, x0;
        bool* 		done;
        double* 	temp;
        double 		minVal;
        int 		minInd;
        Jacobi* 	P 		= mesh->el->polys[mesh->el->N+1];

	fPrev = _fPrev;

	/* calculate the gaussian quadrature weights and abcissa */
        weight  = new double[mesh->el->N+1];
        abcissa = new double[mesh->el->N+1];
        endLoop = (mesh->el->N+1)%2 == 1 ? mesh->el->N/2 : (mesh->el->N+1)/2;
        for( int pt_i = 0; pt_i < endLoop; pt_i++ ) {
                x0 = (pt_i == 0) ? -1.0 : abcissa[pt_i-1];
                do {
                        x0 += STEP;
                        xCurr = x0;
                        do {
                                xPrev = xCurr;
                                xCurr = xPrev - P->Eval( xPrev )/P->EvalDeriv( xPrev );
                        } while( fabs( xCurr - xPrev ) > TOL );
                } while( Found( pt_i, abcissa, xCurr ) || xCurr > 0.0 - STEP );
                abcissa[pt_i] = xCurr;
                abcissa[mesh->el->N-pt_i] = -abcissa[pt_i];
        }
        if( (mesh->el->N+1)%2 == 1 ) {
                abcissa[(mesh->el->N+1)/2] = 0.0;
        }
        done = new bool[mesh->el->N+1];
        temp = new double[mesh->el->N+1];
        minInd = 999999999;
        for( int pt_i = 0; pt_i < mesh->el->N+1; pt_i++ ) {
                done[pt_i] = false;
        }
        for( int pt_i = 0; pt_i < mesh->el->N+1; pt_i++ ) {
                minVal = 1.0e+99;
                for( int pt_j = 0; pt_j < mesh->el->N+1; pt_j++ ) {
                        if( !done[pt_j] && abcissa[pt_j] < minVal ) {
                                minVal = abcissa[pt_j];
                                minInd = pt_j;
                        }
                }
                temp[pt_i] = minVal;
                done[minInd] = true;
        }
        for( int pt_i = 0; pt_i < mesh->el->N+1; pt_i++ ) {
                abcissa[pt_i] = temp[pt_i];
        }
        delete[] temp;
        delete[] done;

        for( int pt_i = 0; pt_i < mesh->el->N+1; pt_i++ ) {
                weight[pt_i] = 2.0/((1.0 - abcissa[pt_i]*abcissa[pt_i])*P->EvalDeriv(abcissa[pt_i])*P->EvalDeriv(abcissa[pt_i]));
        }
}
Exemple #3
0
Jacobi DefKNuton::createJacobian( const System & system )
{
	Jacobi det;

	auto undefinedVar = system.getUnknownVariables();

	const auto& initialData = NS_CORE Singletons::getInstance()->getInitialData();

	if ( initialData._numberOfGears * initialData._numberOfPlanetaryGears == undefinedVar.size() )
	{
		det.setSize( initialData._numberOfGears * initialData._numberOfPlanetaryGears );
		for ( size_t i = 0; i < initialData._numberOfGears; i++ )
		{
			for ( size_t j = 0; j < initialData._numberOfPlanetaryGears; j++ )
			{
				for ( size_t k = 0; k < undefinedVar.size(); k++ )
				{
					auto undefVarListeners = undefinedVar[k].getAllListeners();
					auto gearSetVariables = system.getVariablesSet( NS_CORE GearNumber( i ), j );
					for ( const auto & variable : undefVarListeners )
					{
						if ( ( !gearSetVariables[variable->getElement().getElemN()].getDefined() ) && variable->getElement().getGearSetN().getValue() == j + 1 && variable->getGear() == NS_CORE GearNumber( i + 1 ) )
						{
							auto eq = Equations::getEquation( variable->getElement().getElemN() );
							det.setEquation( i*initialData._numberOfPlanetaryGears + j, k, eq );
						}
					}
				}
			}
		}
	}
	else
	{
		//exception
		int a = 5;
	}
	return det;
}
Exemple #4
0
Matrix DefKNuton::createMatrix( const Jacobi& jacobian, const System & system )
{
	auto size = jacobian.size();

	Matrix ret( size );
	auto undefinedVar = system.getUnknownVariables();

	const auto& initialData = NS_CORE Singletons::getInstance()->getInitialData();

	for ( size_t i = 0; i < initialData._numberOfGears; i++ )
	{
		for ( size_t j = 0; j < initialData._numberOfPlanetaryGears; j++ )
		{
			for ( size_t k = 0; k < undefinedVar.size(); k++ )
			{
				ret.at( i*initialData._numberOfPlanetaryGears + j, k ) = jacobian[i*initialData._numberOfPlanetaryGears + j][k]( system.getVariablesSet( NS_CORE GearNumber( i ), j ) );
			}
		}
	}

	return ret;
}
Exemple #5
0
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Coordinate is a struct of x,y,z
// component is the model to calculate the PCA for
// eVectors are the eigen vectors and values for the three axes
// the longest axis is in eVectors[0]
void PCA(vector<Coordinate> &component, vector<Coordinate> &eVectors, vector<float> &eValues){

	int i,j,k;

	/*
	//find the boundary voxels
	vector<Coordinate>voxels;
	for (i=0; i<component.size(); i++){
		int cntr=0;
		for (j=0; j<component.size(); j++){
			if (getDistance(component[i], component[j])<1.05)
				cntr++;
		}
		if (cntr<7 && cntr>3)
			voxels.push_back(component[i]);
	}
	*/
	vector<Coordinate>voxels (component);

	//cout<<endl<<"Number of boundary voxels: "<<voxels.size()<<endl;

	//calculate the centroid
	Coordinate centroid = getCentroid(voxels);

	vector<Coordinate> newVoxels (voxels.size());
	float covXX=0.0, covYY=0.0, covZZ=0.0, covXY=0.0, covXZ=0.0, covYZ=0.0;

	for (i=0; i<voxels.size(); i++){
		newVoxels[i].x = voxels[i].x-centroid.x;
		newVoxels[i].y = voxels[i].y-centroid.y;
		newVoxels[i].z = voxels[i].z-centroid.z;

		covXX += newVoxels[i].x*newVoxels[i].x;
		covYY += newVoxels[i].y*newVoxels[i].y;
		covZZ += newVoxels[i].z*newVoxels[i].z;
		covXY += newVoxels[i].x*newVoxels[i].y;
		covXZ += newVoxels[i].x*newVoxels[i].z;
		covYZ += newVoxels[i].y*newVoxels[i].z;
	}

	covXX /= voxels.size()-1;
	covYY /= voxels.size()-1;
	covZZ /= voxels.size()-1;
	covXY /= voxels.size()-1;
	covXZ /= voxels.size()-1;
	covYZ /= voxels.size()-1;


	vector<vector<float> > covMatrix(3, vector<float> (3));

	covMatrix[0][0] = covXX;
	covMatrix[0][1] = covXY;
	covMatrix[0][2] = covXZ;

	covMatrix[1][0] = covXY;
	covMatrix[1][1] = covYY;
	covMatrix[1][2] = covYZ;

	covMatrix[2][0] = covXZ;
	covMatrix[2][1] = covYZ;
	covMatrix[2][2] = covZZ;


	/*
	//print covMatrix
	cout<<"covMatrix: "<<endl;
	for (i=0; i<3; i++){
		for (j=0; j<3; j++)
			cout<<covMatrix[i][j]<<" ";
		cout<<endl;
	}
	*/

	Jacobi J;


	J.matrix.resize(3);
	for (i=0; i<3; i++)
		 J.matrix[i].resize(3);


	J.matrix = covMatrix;
	J.dimen = 3;
	J.eigenvalues.resize(J.dimen);
	J.eigenvectors.resize(J.dimen);
	J.e = 1e-8;
	J.jacobi();


	eValues.clear();
	eValues.resize(3);

	eVectors.clear();
	eVectors.resize(3);

	vector<vector<float> > Evector(3, vector<float> (3));

	eValues = J.getEigenvalues();
	Evector = J.getEigenvectors();

	for (i=0; i<3; i++){
		eVectors[i].x = Evector[i][0];
		eVectors[i].y = Evector[i][1];
		eVectors[i].z = Evector[i][2];
	}


	//cout<<endl<<"PCA..."<<endl;
	//J.printEigen();
}
Exemple #6
0
Jacobi::LogCountTable::LogCountTable(const Jacobi &jac,unsigned p_)
 : p(p_),
   table(p_*p_)
 {
  jac.addLog(p_, [=] (unsigned a,unsigned b) { add(a,b); } );
 }
void MultiColored<OperatorType, VectorType, ValueType>::Decompose_(void) {

  LOG_DEBUG(this, "MultiColored::Decompose_()",
            " * beging");

  if (this->decomp_ == true) {

    assert(this->num_blocks_ > 0);
    assert(this->block_sizes_ != NULL);

    int *offsets = NULL;
    allocate_host(this->num_blocks_+1, &offsets);    

    offsets[0] = 0 ;
    for(int i=0; i<this->num_blocks_; ++i)
      offsets[i+1] = this->block_sizes_[i];

    // sum up
    for(int i=0; i<this->num_blocks_; ++i)
      offsets[i+1] += offsets[i];

    this->diag_solver_ = new Solver<OperatorType, VectorType, ValueType>* [this->num_blocks_];

    this->preconditioner_block_ = new OperatorType** [this->num_blocks_]; 
    for (int i=0; i<this->num_blocks_; ++i) 
      this->preconditioner_block_[i] = new OperatorType* [this->num_blocks_];

    this->x_block_    = new VectorType* [this->num_blocks_];
    this->diag_block_ = new VectorType* [this->num_blocks_];

    for(int i=0; i<this->num_blocks_; ++i)
      for(int j=0; j<this->num_blocks_; ++j) {
        this->preconditioner_block_[i][j] = new LocalMatrix<ValueType>;
        this->preconditioner_block_[i][j]->CloneBackend( *this->op_ );
        
      }

    this->preconditioner_->ExtractSubMatrices(this->num_blocks_,
                                              this->num_blocks_,
                                              offsets,
                                              offsets,
                                              this->preconditioner_block_);

    free_host(&offsets);

    int x_offset = 0;
    for (int i=0; i<this->num_blocks_; ++i) {
      this->diag_block_[i] = new VectorType;
      this->diag_block_[i]->CloneBackend(*this->op_); // clone backend
      this->diag_block_[i]->Allocate("Diagonal preconditioners blocks",
                                     this->block_sizes_[i]);
      
      this->preconditioner_block_[i][i]->ExtractDiagonal(this->diag_block_[i]);

      
      this->x_block_[i] = new VectorType; // empty vector
      
      this->x_block_[i]->CloneBackend(*this->op_); // clone backend
      this->x_block_[i]->Allocate("MultiColored Preconditioner x_block_",
                                  this->block_sizes_[i]);
      
      
      x_offset += this->block_sizes_[i];

      Jacobi<OperatorType, VectorType, ValueType> *jacobi = new Jacobi<OperatorType, VectorType, ValueType>;
      jacobi->SetOperator(*this->preconditioner_block_[i][i]);
      jacobi->Build();
      
      this->diag_solver_[i] = jacobi;
      
      this->preconditioner_block_[i][i]->Clear();
    }
    
    // Clone the format 
    // e.g. the preconditioner block matrices will have the same format as this->op_
    if (this->op_mat_format_ == true)
      for(int i=0; i<this->num_blocks_; ++i)
        for(int j=0; j<this->num_blocks_; ++j)
          this->preconditioner_block_[i][j]->ConvertTo(this->precond_mat_format_);
  
  } else {

    this->diag_.CloneBackend(*this->op_);
    this->preconditioner_->ExtractDiagonal(&this->diag_);

  }

  this->x_.CloneBackend(*this->op_); 
  this->x_.Allocate("Permuted solution vector",
                    this->op_->get_nrow());

  LOG_DEBUG(this, "MultiColored::Decompose_()",
            " * end");

}
Exemple #8
0
int main(int argc, char* argv[]) {

    Mat<double>    mH;
    Col<double>    mV;
    Mat<cx_double> mEigVec;
    Col<cx_double> mEigVal;

    int          iN, iIt, iRot;
    int          iMethod = 0;
    Jacobi       *oJacobi = new Jacobi();
    time_t       tStart, tStop;
    stringstream ssFile;

    // Default Matrix

    if(argc == 4) {
        iMethod = atoi(argv[1]);
        ssFile << "Data/";
        if(atoi(argv[3]) == 0) ssFile << "Std/";
        if(atoi(argv[3]) == 1) ssFile << "Eff/";
        if(atoi(argv[3]) == 2) ssFile << "EffECut/";
        ssFile << "Ham-2P-" << argv[2] << "R.arma";
        mH.load(ssFile.str());
    } else {
        cout << endl;
        cout << "Jacobi takes three arguments: " << endl;
        cout << " #1 : Method" << endl;
        cout << "      0 = Armadillo eig_gen()" << endl;
        cout << "      1 = Simple w/Rotate" << endl;
        cout << "      2 = Simple" << endl;
        cout << "      3 = Cyclic" << endl;
        cout << "      4 = Parallel" << endl;
        cout << " #2 : Number of shells for the Hamiltonian (2-20)" << endl;
        cout << " #3 : Interaction type" << endl;
        cout << "      0 = Standard" << endl;
        cout << "      1 = Effective" << endl;
        cout << "      2 = Effective w/energy cut" << endl;
        cout << endl;
        return 0;
    }

    iN   = (int)mH.n_cols;
    iIt  = 0;
    iRot = 0;

    mV   = zeros(iN);

    cout << endl;
    #ifndef ONLY_LOWEST
        cout << "Initial Matrix:" << endl;
        cout << mH << endl;
    #endif
    cout << "Dimension: " << iN << "x" << iN << endl;
    cout << "Sparsity:  " << oJacobi->Sparsity(&mH) << endl;
    cout << endl;

    switch(iMethod) {

        case 0: // Aramdillo's Eigenvalue Solver

            tStart = clock();
            eig_gen(mEigVal, mEigVec, mH);
            tStop  = clock();

            mV = sort(real(mEigVal));

            cout << "Armadillo's eig_gen():" << endl;
            cout << "   Time:         " << ((double)tStop-tStart)/CLOCKS_PER_SEC << " sec" << endl;
            cout << "   Iterations:   N/A" << endl;
            cout << "   Rotations:    N/A" << endl;

            break;

        case 1: // Simple Jacobi w/Rotate

            tStart = clock();
            oJacobi->SimpleRotate(&mH, &mV, &iIt, &iRot);
            tStop  = clock();

            mV = sort(mV);

            cout << "Simple Jacobi w/Rotations:" << endl;
            cout << "   Time:         " << ((double)tStop-tStart)/CLOCKS_PER_SEC << " sec" << endl;
            cout << "   Iterations:   " << iIt << endl;
            cout << "   Rotations:    " << iRot << endl;

            break;

        case 2: // Simple Jacobi

        tStart = clock();
        oJacobi->Simple(&mH, &mV, &iIt, &iRot);
        tStop  = clock();

        mV = sort(mV);

        cout << "Simple Jacobi:" << endl;
        cout << "   Time:         " << ((double)tStop-tStart)/CLOCKS_PER_SEC << " sec" << endl;
        cout << "   Iterations:   " << iIt << endl;
        cout << "   Rotations:    " << iRot << endl;

        break;

    case 3: // Cyclic Jacobi

        tStart = clock();
        oJacobi->Cyclic(&mH, &mV, &iIt, &iRot);
        tStop  = clock();

        mV = sort(mV);

        cout << "Cyclic Jacobi:" << endl;
        cout << "   Time:         " << ((double)tStop-tStart)/CLOCKS_PER_SEC << " sec" << endl;
        cout << "   Iterations:   " << iIt << endl;
        cout << "   Rotations:    " << iRot << endl;

        break;
    }

    #ifdef ONLY_LOWEST
        if(mV.n_elem > 0) cout << "   Eigenvalue 1: " << mV(0) << endl;
        if(mV.n_elem > 1) cout << "   Eigenvalue 2: " << mV(1) << endl;
        if(mV.n_elem > 2) cout << "   Eigenvalue 3: " << mV(2) << endl;
        if(mV.n_elem > 3) cout << "   Eigenvalue 4: " << mV(3) << endl;
        if(mV.n_elem > 4) cout << "   Eigenvalue 5: " << mV(4) << endl;
    #else
        cout << "   Eigenvalues:" << endl << mV << endl;
    #endif
    cout << endl;

    return 0;
}