Ejemplo n.º 1
0
//------------------------------------------------------------------------------
void BasisHarmonicOscillator::createInitalDiscretization()
{
    C = zeros<cx_mat>(nGrid, nSpatialOrbitals);
    mat Ctmp(nGrid, nSpatialOrbitals);
    Wavefunction *wf;

    for(int j=0; j<nSpatialOrbitals; j++){
        wf = new HarmonicOscillator(cfg, states[2*j]);
        for(int i=0; i<nGrid; i++){
            Ctmp(i,j) = wf->evaluate(grid->at(i));
        }
        delete wf;
    }

    C.set_real(Ctmp);
    // Forcing orthogonality by performing a SVD decomposition
    SVD(C);
#ifdef DEBUG
    cout << "BasisHarmonicOscillator::discretization1d()" << endl;
    for(int i=0; i<nSpatialOrbitals; i++){
        for(int j=0; j<nSpatialOrbitals; j++){
            cout <<  "|C(" << i << ", " << j << ")| = " << sqrt(cdot(C.col(i),C.col(j))) << endl ;
        }
    }
#endif
}
Ejemplo n.º 2
0
void BIC_EM(int &nbr_cls_st, vector<coordonnees_c> &C, coordonnees_p *p){
	double L , Ltmp;
	double BIC, BICtmp;

	coordonnees_c x;
	vector<coordonnees_c> Ctmp(nbr_cls_st);
	
	L = fix_EM(nbr_cls_st, C, p);
	BIC = return_BIC(nbr_cls_st, L);
	/*cout << "Cost : " << L << endl;
	cout << "BIC : " << BIC << endl<< endl;*/


	bool BIC_smaller = true;

	while(BIC_smaller){
		BIC_smaller = false;

		nbr_cls_st++;
		Ctmp.push_back(x);

		for(unsigned i = 0; i < table_size; i++){
			p[i].rc.push_back(0);
		}

		Ltmp = fix_EM(nbr_cls_st, Ctmp, p);
		BICtmp = return_BIC(nbr_cls_st, Ltmp);	
		cout << "Value of L : "<< Ltmp << endl;
		cout << "Cost of CLuster : " << log(table_size)*nbr_cls_st << endl;
		cout << "Value of AIC : "<< BICtmp << endl << endl;


		if(BICtmp < BIC){
			L = Ltmp;
			BIC = BICtmp;
			//if(nbr_cls_st < 7){
				BIC_smaller = true;
			//}
			C.push_back(x);
			swap(C, Ctmp);
		}else{
			nbr_cls_st--;
			for(unsigned i=0; i<table_size; i++)
				p[i].rc.pop_back();
			resp(nbr_cls_st, C, p);
			return;
		}
	}
	return;
}
Ejemplo n.º 3
0
void AIC_K(int &nbr_cls_st, vector<coordonnees_c> &C, coordonnees_p *p){

	double J, Jtmp;
	double AIC, AICtmp;

	coordonnees_c x;
	x.x = 0;
	x.y = 0;

	vector<coordonnees_c> Ctmp(nbr_cls_st);

	/////Do K mean for initial number of cluster.
	J = fixclus(p, nbr_cls_st, C);
	AIC = return_AIC(nbr_cls_st, J);
	//cout << "Value of J : "<< J << " and log : "<< log(J) << endl;
	//cout << "Value of AIC : "<< AIC << endl << endl;
	/////Loop incrementing number of cluster and checking the AIC.
	bool AIC_smaller = true;

	while(AIC_smaller){
		AIC_smaller = false;

		nbr_cls_st++;
		Ctmp.push_back(x);

		for(unsigned i = 0; i < table_size; i++){
			p[i].rc.push_back(0);
		}

		Jtmp = fixclus(p, nbr_cls_st, Ctmp);
		AICtmp = return_AIC(nbr_cls_st, Jtmp);	
		//cout << "Value of J : "<< Jtmp << " and log : "<< log(J) << endl;
		//cout << "Value of AIC : "<< AICtmp << endl << endl;


		if(AICtmp < AIC){
			J = Jtmp;
			AIC = AICtmp;
			AIC_smaller = true;
			C.push_back(x);
			swap(C, Ctmp);
		}else{
			nbr_cls_st--;
			for(unsigned i=0; i<table_size; i++)
				p[i].rc.pop_back();
			closest_c(p, C, nbr_cls_st);
			return;
		}
	}
}
Ejemplo n.º 4
0
void BlockMatrixTest::testAssignment()
{
  std::cout << "--> Test: assignment." <<std::endl;
  SP::SiconosMatrix Btmp(new SimpleMatrix(2, 2));
  SP::SiconosMatrix Ctmp(new SimpleMatrix(2, 5));
  SP::SiconosMatrix Dtmp(new SimpleMatrix(3, 2));
  SP::SiconosMatrix Etmp(new SimpleMatrix(3, 5));

  SP::SiconosMatrix test(new BlockMatrix(Btmp, Ctmp, Dtmp, Etmp));
  // Block = Siconos(Simple)
  unsigned int size0 = test->size(0), size1 = test->size(1);
  SP::SiconosMatrix ref(new SimpleMatrix(size0, size1));
  for (unsigned int i = 0; i < size0 ; ++i)
    for (unsigned int j = 0; j < size1; ++j)
      (*ref)(i, j) = i + j;
  *test = *ref;
  for (unsigned int i = 0; i < size0 ; ++i)
    for (unsigned int j = 0; j < size1; ++j)
      CPPUNIT_ASSERT_EQUAL_MESSAGE("testAssignment: ", fabs((*test)(i, j) - (*ref)(i, j)) < tol , true);

  // Block = Siconos(Block)
  test->zero();

  ref.reset(new BlockMatrix(m, 2, 3));
  *test = *ref;
  for (unsigned int i = 0; i < size0 ; ++i)
    for (unsigned int j = 0; j < size1; ++j)
      CPPUNIT_ASSERT_EQUAL_MESSAGE("testAssignment: ", fabs((*test)(i, j) - (*ref)(i, j)) < tol , true);

  // Block = Block
  test->zero();
  SP::BlockMatrix ref2(new BlockMatrix(m, 2, 3));
  *test = *ref2;
  for (unsigned int i = 0; i < size0 ; ++i)
    for (unsigned int j = 0; j < size1; ++j)
      CPPUNIT_ASSERT_EQUAL_MESSAGE("testAssignment: ", fabs((*test)(i, j) - (*ref2)(i, j)) < tol , true);
  std::cout << "-->  test assignment ended with success." <<std::endl;
}
/****************************************************
 *      Especialización del método dibujar          *
 *  Comienza por dibujar los títulos y etiquetas    *
 *  con funciones del manejador de ventanas GLUT de *
 *  OpenGL. Luego dibuja los datos en pantalla con  *
 *  una línea de la clase Linea2D. Los datos, por   *
 *  estándar de esta biblioteca se representan de   *
 *	0-450 pixeles en el eje Y positivo, de 0-(-150) *
 *  pixeles en el eje Y negativo, de 0-325 pixeles  *
 *  en el eje X positivo y de 0-(-325) pixeles en   *
 *  el eje X negativo. La altura de una linea es    *
 *  una representación del valor contenido en el    *
 *  arreglo. Además se dibuja un círculo de la      *
 *  clase Circulo2D para identificar en que punto   *
 *  de la gráfica está representado un valor.       *
 ****************************************************/
void Grafico2D::dibujar() {
	//Establece el color en Negro
	glColor3f(0.0, 0.0, 0.0);
	//Se mueve hasta la posicion especificada para 
	//imprimir el título
	glRasterPos2i(-40, 400);

	//Imprime el título caracter por caracter con una 
	//letra Helvetica de 18 puntos
	for (GLuint i = 0; i < nombreLen; i++) {
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, nombre[i]);
	}

	//Modifica xRaster para comenzar a imprimir las etiquetas
	//desde la parte izquierda de la pantalla
	GLint xRaster = -270;

	//Imprime las etiquetas caracter por caracter
	//sumando 50 pixeles cuando se llega al delimitador
	//de caracteres, eso asegura de que las etiquetas se
	//mantengan separadas
	for (GLuint i = 0; i < etiquetasLen; i++) {
		if (i % delimitador == 0) {
			glRasterPos2i(xRaster, 0);
			xRaster += 50;
		}

		//Imprime las etiquetas con letra Helvetica de 12 puntos
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, etiquetas[i]);
	}

	//Modifica el valor de xRaster para comenzar a imprimir 
	//las líneas de la gráfica
	xRaster = -260;

	//Recorre el arreglo hasta hacer una linea por cada dato - 1
	//dentro del arreglo de datos
	for (GLuint i = 0; i < datosLen - 1; i++) {
		//Inicializa un punto en el dato en cuestión
		Punto2D Ptmp1(xRaster, datos[i]);
		//Inicializa otro punto en el dato siguiente
		Punto2D Ptmp2(xRaster + 50, datos[i + 1]);
		//Crea una línea a partir de los dos puntos
		//anteriores
		Linea2D Ltmp(Ptmp1, Ptmp2);
		//modifica el color de la línea a un derivado del 
		//Azul
		glColor3f(0.0, 0.3, 1.0);
		//dibuja la linea
		Ltmp.dibujar();
		//Cambia el color a un derivado del rojo (un tono de
		//anaranjado)
		glColor3f(1.0, 0.3, 0.0);
		//Crea un circulo en el punto que representa el dato
		//en cuestion, con un radio de 4 pixeles
		Circulo2D Ctmp(Ptmp1, 4);
		//Dibuja el círculo
		Ctmp.dibujar();

		//Verifica que si es el último circulo a dibujar
		//lo dibuja cuando llega al último dato al que ingresa
		//el ciclo de lo contrario no podrá ser representado
		if (i == datosLen - 2) {
			Circulo2D Ctmp2(Ptmp2, 4);
			Ctmp2.dibujar();
		}

		//Si ya fue representado todo en orden, suma 50 pixeles
		//a la coordenada del xRaster para representar otro punto
		//con 50 pixeles de separación del anterior
		xRaster += 50;
	}
}
Ejemplo n.º 6
0
double fix_EM(const int nbr_cls_st, vector<coordonnees_c> &Cfinal, coordonnees_p *p, bool postK = false){
	double L, Ltmp, Lfinal;
	bool first = true;

	vector<coordonnees_c> C(nbr_cls_st);
	vector<coordonnees_c> Ctmp(nbr_cls_st);

	for(unsigned t=0; t<10; t++){
		bool start =false;

		if(!postK){
			while(!start){
				genrdm(C, nbr_cls_st);
				closest_c(p, C, nbr_cls_st);
				start = init_covar(nbr_cls_st, C, p);
			}
		}else{
			start = init_covar(nbr_cls_st, Cfinal, p);
			C = Cfinal;
			if(!start){
				//cout << "AIC_K start initialization didn't work." << endl << "Jump to normal EM" << endl;
				postK =false;
				while(!start){
					genrdm(C, nbr_cls_st);
					closest_c(p, C, nbr_cls_st);
					start = init_covar(nbr_cls_st, C, p);
				}
			}
		}

		L = log_l(nbr_cls_st, C, p);
		Lfinal = L;
		
		bool L_smaller = true;
		Ctmp = C;
		int n=0;

		while(L_smaller){
			L_smaller = false;

			resp(nbr_cls_st, Ctmp, p);
			mean_EM(nbr_cls_st, Ctmp, p);
			covar_EM(nbr_cls_st, Ctmp, p);
			Ltmp = log_l(nbr_cls_st, Ctmp, p);

			if(Ltmp > L){
				n++;
				L_smaller = true;
				L = Ltmp;
				C = Ctmp;
			}
		}
		if((first == true && !postK) || (L > Lfinal)){
			first = false;
			Lfinal = L;
			Cfinal = C;
		}
		if(postK){
			//cout << "Iteration : " << n << endl;
			break;
		}

	}
	return Lfinal;
}