Пример #1
0
template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
{
  ComplexEigenSolver<MatrixType> eig;
  VERIFY_RAISES_ASSERT(eig.eigenvectors());
  VERIFY_RAISES_ASSERT(eig.eigenvalues());

  MatrixType a = MatrixType::Random(m.rows(),m.cols());
  eig.compute(a, false);
  VERIFY_RAISES_ASSERT(eig.eigenvectors());
}
Пример #2
0
int main()
{

	int N;
	double meanS,meanA;
	double varS,varA;
	int seed;
	
	read_input(N,meanS,varS,meanA,varA,seed, "input.txt");
	Ran r(seed);

	MatrixXcf w(N,N);
	double mean =0;
	for(int i=0;i<N;++i){
		for( int j=0;j<i;++j) {
			double a = meanA+varA*bm_transform(r);
			double s = meanS+varS*bm_transform(r);
			w(i,j) = a+s;
			w(j,i) = -1.*a+s;
			mean += 2*s;
		}
		double s= meanS+varS*bm_transform(r);
		w(i,i) = s;
		mean += s;
	}
	// Eigen object for computing eigensystem
	ComplexEigenSolver<MatrixXcf> eigenw;
	eigenw.compute(w);

	vector<double> eval_re(N);
	vector<double> eval_im(N);

	for(int i=0;i<N;i++) {
		eval_re[i] = eigenw.eigenvalues()[i].real();
		eval_im[i] = eigenw.eigenvalues()[i].imag();
	}

	write_matrix(eval_re,N,"eval_re.csv");	
	write_matrix(eval_im,N,"eval_im.csv");

	return 0;
}
int main(int, char**)
{
  cout.precision(3);
  MatrixXcf A = MatrixXcf::Random(4,4);
cout << "Here is a random 4x4 matrix, A:" << endl << A << endl << endl;

ComplexEigenSolver<MatrixXcf> ces;
ces.compute(A);
cout << "The eigenvalues of A are:" << endl << ces.eigenvalues() << endl;
cout << "The matrix of eigenvectors, V, is:" << endl << ces.eigenvectors() << endl << endl;

complex<float> lambda = ces.eigenvalues()[0];
cout << "Consider the first eigenvalue, lambda = " << lambda << endl;
VectorXcf v = ces.eigenvectors().col(0);
cout << "If v is the corresponding eigenvector, then lambda * v = " << endl << lambda * v << endl;
cout << "... and A * v = " << endl << A * v << endl << endl;

cout << "Finally, V * D * V^(-1) = " << endl
     << ces.eigenvectors() * ces.eigenvalues().asDiagonal() * ces.eigenvectors().inverse() << endl;

  return 0;
}
Пример #4
0
int main()
{

	int N;			// number of neurons
	double meanE;	// mean strength of exc. neurons in units 1/sqrt(N)
	double meanI;	// mean strength of inh. neurons in units 1/sqrt(N)
	double g; 		// gain parameter
	double a;		// varE= g^2/(Na), varI=g^2/N
	int db;
	int seed;		//seed for the random number generator

	string dist;	// normal or  lognormal
		
	read_input(N,meanE,meanI,g,a,db,dist,seed, "input.txt");

	Ran r(seed);
	default_random_engine generator;
	

	// prob. of exc. col. in w
	// s.t. f*meanE + (1-f)*meanI =0
	double f;
	if( meanI-meanE == 0) f = 0.5;
	else f = meanI/(meanI-meanE);


	double varE = g*g/(a*N);
	double varI = g*g/N;
	double stdE = g/sqrt(N*a);
	double stdI = g/sqrt((double)N);
	meanE = meanE/sqrt((double)N);
	meanI = meanI/sqrt((double)N);

	normal_distribution<double> normdist01(0.0,1.0);
	normal_distribution<double> normdistE(meanE,stdE);
	normal_distribution<double> normdistI(meanI,stdI);

	double muE = log(meanE/sqrt(1+varE/(meanE*meanE)));
	double muI = log(-1.*meanI/sqrt(1+varI/(meanI*meanI)));
	double sigE = sqrt(log(1+varE/(meanE*meanE)));
	double sigI = sqrt(log(1+varI/(meanI*meanI)));


	lognormal_distribution<double> lognormdistE(muE,sigE);
	lognormal_distribution<double> lognormdistI(muI,sigI);



	vector<char> EI(N,'I');
	for(int i=0;i<f*N;++i) EI[i] = 'E';
	shuffle(EI,N,r);


	// create w, the matrix of connection strengths
	MatrixXcf w(N,N);
	vector<double> row_sum(N,0.0);


	if(dist=="normal") {	
		// row=i, col=j
		for(int i=0;i<N;++i) {
			for(int j=0;j<N;++j) {
				if(EI[j]=='E') {
					w(i,j) = stdE*normdist01(generator);
					row_sum[i] += w(i,j).real();
				}
				if(EI[j]=='I') {
					w(i,j) = stdI*normdist01(generator);
					row_sum[i] += w(i,j).real();
				}
			}
		}
		// if db = 1 impose detailed balance condition
		if(db==1) {
			for(int i=0;i<N;++i) {
				for(int j=0;j<N;++j) {
					w(i,j) -= row_sum[i]/N;
				}
			}
		}

		// add M to W
		for(int i=0;i<N;++i) {
			for(int j=0;j<N;++j) {
				if(EI[j]=='E') w(i,j) +=  meanE;
				if(EI[j]=='I') w(i,j) +=  meanI;
			}
		}
	}

	if(dist=="lognormal") {	
		// row=i, col=j
		for(int i=0;i<N;++i) {
			for(int j=0;j<N;++j) {
				if(EI[j]=='E') {
					w(i,j) = lognormdistE(generator);
				}
				if(EI[j]=='I') {
					w(i,j) = -1.*lognormdistI(generator);
				}
			}
		}
	}

	// Eigen object for computing eigensystem
	ComplexEigenSolver<MatrixXcf> eigenw;
	eigenw.compute(w);

	vector<double> eval_re(N);
	vector<double> eval_im(N);

	for(int i=0;i<N;i++) {
		eval_re[i] = eigenw.eigenvalues()[i].real();
		eval_im[i] = eigenw.eigenvalues()[i].imag();
	}

	write_matrix(eval_re,N,"eval_re.csv");	
	write_matrix(eval_im,N,"eval_im.csv");

	return 0;
}
Пример #5
0
vector<complex<float> > PMS(vector<vector<complex<float> >> CSC_array)
{
    int CSC_size = (int)CSC_array.size();
    int array_size = 0;
    int i;
    int m,n;

    vector<vector< complex<float>> > Product_mat;
    vector<vector< complex<float>> > S_Z;
    vector<complex<float> > temp_mat;
    vector<complex<float> > Major_eigen_vector;
    complex<float> mat_ij_1;
    complex<float> mat_ij_2;
    complex<float> scalar(0,0);
    S_Z.resize(40, vector< complex<float>>(40, 0));
    for (m = 0; m<40; m++)
    {
        for (n = 0; n<40; n++)
        {
            S_Z[m][n] = 0;
        }
    }

    for(i=0; i<period; i++)
    {
        array_size = (int)(CSC_array[i]).size();

        for(m=0; m<array_size; m++)
        {
            mat_ij_1 = CSC_array[i][m];
            scalar = scalar + mat_ij_1*conj(mat_ij_1);
            for(n=0; n<array_size; n++)
            {
                mat_ij_2 = CSC_array[i][n];
                mat_ij_2 = conj(mat_ij_2);
                temp_mat.push_back(mat_ij_1*mat_ij_2);
            }
            Product_mat.push_back(temp_mat);
            temp_mat.clear();
        }

        for(m=0; m<array_size; m++)
        {
            for(n=0; n<array_size; n++)
            {
                Product_mat[m][n] = Product_mat[m][n]/scalar;
            }
        }

        for(m=0; m<array_size; m++)
        {
            for(n=0; n<array_size; n++)
            {
                S_Z[m][n] = S_Z[m][n] + Product_mat[m][n];
            }
        }

        scalar = 0;
        Product_mat.clear();
    }

    MatrixXcf temp(array_size,array_size);
    MatrixXcf evec(array_size,array_size);
    MatrixXcf eval(array_size,1);

    for(m=0; m<array_size; m++)
    {
        for(n=0; n<array_size; n++)
        {
            temp(m,n) = S_Z[m][n];
        }
    }

    ComplexEigenSolver<MatrixXcf> ces;
    ces.compute(temp);

    evec = ces.eigenvectors();
    eval = ces.eigenvalues();

    //cout << " Eigen " << eval(0,0) << endl;
    //cout << " EE " << evec(0,0) << endl;

    float Max_eigen=0.0;
    int Max_eigen_index=0;
    float temp_value=0.0;

    for(i=0; i<array_size; i++)
    {
        temp_value = norm( (i,0));
        if(temp_value > Max_eigen)
        {
            Max_eigen = temp_value;
            Max_eigen_index = i;
        }
    }

    /*
    cout << "The eigenvector of temp are" << endl << evec << endl;
    cout << "The eigenvalue of temp are" << endl << eval << endl;
    cout << "Max eigenvalue " << endl << Max_eigen << endl;
    cout << "Max eigenvalue index " << endl << Max_eigen_index << endl;
    cout << "Max eigenvector " << endl << Max_eigen*evec(i) << endl;
    */

    for(i=0; i<array_size; i++)
    {
        Major_eigen_vector.push_back(evec(i, Max_eigen_index));
        //Major_eigen_vector.push_back(Max_eigen*evec(i,Max_eigen_index));
        //cout << Major_eigen_vector[i] << endl;
    }

    return Major_eigen_vector;
}