コード例 #1
0
ファイル: test.c プロジェクト: lemahdi/mglib
void
test_eigen_nonsymm(void)
{
  size_t N_max = 20;
  size_t n, i;
  gsl_rng *r = gsl_rng_alloc(gsl_rng_default);

  for (n = 1; n <= N_max; ++n)
    {
      gsl_matrix * m = gsl_matrix_alloc(n, n);
      gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(n);

      for (i = 0; i < 5; ++i)
        {
          create_random_nonsymm_matrix(m, r, -10, 10);

          gsl_eigen_nonsymmv_params(0, w);
          test_eigen_nonsymm_matrix(m, i, "random, unbalanced", w);

          gsl_eigen_nonsymmv_params(1, w);
          test_eigen_nonsymm_matrix(m, i, "random, balanced", w);
        }

      gsl_matrix_free(m);
      gsl_eigen_nonsymmv_free(w);
    }

  gsl_rng_free(r);

  {
    double dat1[] = { 0, 1, 1, 1,
                      1, 1, 1, 1,
                      0, 0, 0, 0,
                      0, 0, 0, 0 };
    double dat2[] = { 1, 1, 0, 1,
                      1, 1, 1, 1,
                      1, 1, 1, 1,
                      0, 1, 0, 0 };
    gsl_matrix_view v;
    gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(4);
    
    v = gsl_matrix_view_array (dat1, 4, 4);
    test_eigen_nonsymm_matrix(&v.matrix, 0, "integer", w);

    v = gsl_matrix_view_array (dat2, 4, 4);
    test_eigen_nonsymm_matrix(&v.matrix, 1, "integer", w);

    gsl_eigen_nonsymmv_free(w);
  }
} /* test_eigen_nonsymm() */
コード例 #2
0
ファイル: fbfoptics.c プロジェクト: jonbramble/FBF-Optics
void expm(gsl_matrix_complex * L, gsl_complex t, gsl_matrix * m)
     {
    int i,j,s;
	gsl_vector_complex *eval = gsl_vector_complex_alloc(4);
	gsl_matrix_complex *evec = gsl_matrix_complex_alloc(4, 4);
	gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(4);
	gsl_matrix_complex *evalmat = gsl_matrix_complex_alloc(4, 4);
	gsl_matrix_complex *vd = gsl_matrix_complex_alloc(4, 4);
	gsl_complex one = gsl_complex_rect(1, 0);
	gsl_complex zero = gsl_complex_rect(0, 0);

	gsl_matrix_complex *K = gsl_matrix_complex_alloc(4, 4);
	gsl_permutation *p = gsl_permutation_alloc(4);
	gsl_vector_complex *x = gsl_vector_complex_alloc(4);

	gsl_vector_complex_view bp;
	gsl_complex z;

	gsl_eigen_nonsymmv(m, eval, evec, w);
	gsl_eigen_nonsymmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);

	gsl_eigen_nonsymmv_free(w); // clear workspace

	for (i = 0; i < 4; i++)
	{
		gsl_complex eval_i = gsl_vector_complex_get(eval, i);
		gsl_complex expeval = gsl_complex_mul(eval_i,t);
		expeval = gsl_complex_exp(expeval);
		gsl_matrix_complex_set(evalmat, i, i, expeval);
	}

	gsl_vector_complex_free(eval); // clear vector for eigenvalues

	// v'L'=De'v'
	gsl_blas_zgemm(CblasTrans, CblasTrans, one, evalmat, evec, zero, vd);
	gsl_matrix_complex_transpose(evec);//transpose v

	gsl_matrix_complex_memcpy(K,evec);

	for (i = 0; i < 4; i++)
	{
		bp = gsl_matrix_complex_column(vd, i);
		gsl_linalg_complex_LU_decomp(evec, p, &s);
		gsl_linalg_complex_LU_solve(evec, p, &bp.vector, x);
			for (j = 0; j < 4; j++)
			{
				z = gsl_vector_complex_get(x, j);
				gsl_matrix_complex_set(L,i,j,z); //'through the looking glass' transpose
			}
		gsl_matrix_complex_memcpy(evec,K);
	}


	gsl_permutation_free(p);
	gsl_vector_complex_free(x);
	gsl_matrix_complex_free(vd);
	gsl_matrix_complex_free(evec);
	gsl_matrix_complex_free(evalmat);
	gsl_matrix_complex_free(K);
}
コード例 #3
0
ファイル: linal.c プロジェクト: kcoltin/blackjack
//-----------------------------------------------------------------------------
// Returns the eigendecomposition A = X*D*X^-1. 
// d is the diagonal entries of D. 
// X and d must be preallocated: X should be n x n and d should be length n. 
//-----------------------------------------------------------------------------
void eigendecomp (double **A, double complex **X, double complex *d, int n)
{
	double *a; 
	double complex **Xtmp; 
	double complex *dtmp; 
	gsl_matrix_view m; 
	gsl_vector_complex *eval; 
	gsl_matrix_complex *evec; 
	gsl_eigen_nonsymmv_workspace *w; 

	// Use GSL routine to compute eigenvalues and eigenvectors 
	a = matrixToVector (A, n, n); 
	m = gsl_matrix_view_array (a, n, n); 
	eval = gsl_vector_complex_alloc (n); 
	evec = gsl_matrix_complex_alloc (n, n); 
	w = gsl_eigen_nonsymmv_alloc (n); 

	gsl_eigen_nonsymmv (&m.matrix, eval, evec, w); 
	gsl_eigen_nonsymmv_free (w); 

	// Convert from GSL to intrinsic types 
	Xtmp = gslmToCx (evec, n, n); 
	dtmp = gslvToCx (eval, n); 

	copycm_inplace (X, Xtmp, n, n); 
	copycv_inplace (d, dtmp, n); 

	freecmatrix(Xtmp, n); 
	free(a);
	free(dtmp); 
	gsl_vector_complex_free(eval); 
	gsl_matrix_complex_free(evec); 
}
コード例 #4
0
ファイル: gslpp_matrix_double.cpp プロジェクト: shehu0/HEPfit
  void matrix<double>::eigensystem(matrix<complex> &U, vector<complex> &S) {
      matrix<double> m(*this);

      gsl_eigen_nonsymmv_workspace *ws;

      ws = gsl_eigen_nonsymmv_alloc(size_i());

      gsl_eigen_nonsymmv(m.as_gsl_type_ptr(), S.as_gsl_type_ptr(),
              U.as_gsl_type_ptr(), ws);

      gsl_eigen_nonsymmv_free(ws);
  }
コード例 #5
0
int
main (void)
{
  double data[] = { -1.0, 1.0, -1.0, 1.0,
                    -8.0, 4.0, -2.0, 1.0,
                    27.0, 9.0, 3.0, 1.0,
                    64.0, 16.0, 4.0, 1.0 };

  gsl_matrix_view m 
    = gsl_matrix_view_array (data, 4, 4);

  gsl_vector_complex *eval = gsl_vector_complex_alloc (4);
  gsl_matrix_complex *evec = gsl_matrix_complex_alloc (4, 4);

  gsl_eigen_nonsymmv_workspace * w = 
    gsl_eigen_nonsymmv_alloc (4);
  
  gsl_eigen_nonsymmv (&m.matrix, eval, evec, w);

  gsl_eigen_nonsymmv_free (w);

  gsl_eigen_nonsymmv_sort (eval, evec, 
                           GSL_EIGEN_SORT_ABS_DESC);
  
  {
    int i, j;

    for (i = 0; i < 4; i++)
      {
        gsl_complex eval_i 
           = gsl_vector_complex_get (eval, i);
        gsl_vector_complex_view evec_i 
           = gsl_matrix_complex_column (evec, i);

        printf ("eigenvalue = %g + %gi\n",
                GSL_REAL(eval_i), GSL_IMAG(eval_i));
        printf ("eigenvector = \n");
        for (j = 0; j < 4; ++j)
          {
            gsl_complex z = gsl_vector_complex_get(&evec_i.vector, j);
            printf("%g + %gi\n", GSL_REAL(z), GSL_IMAG(z));
          }
      }
  }

  gsl_vector_complex_free(eval);
  gsl_matrix_complex_free(evec);

  return 0;
}
コード例 #6
0
void alloc_gslws(gslws_t *ws) {
	ws->evals = gsl_vector_complex_alloc(6);
        ws->evecs = gsl_matrix_complex_alloc(6,6);
        ws->evecs_inv = gsl_matrix_complex_alloc(6,6);
        ws->P = gsl_matrix_alloc(6,6);
	ws->c_P = gsl_matrix_complex_alloc(6,6);
	ws->temp1 = gsl_matrix_complex_alloc(6,6);
	ws->temp2 = gsl_matrix_complex_alloc(6,6);
	ws->Q_copy = gsl_matrix_alloc(6,6);
	ws->evecs_copy = gsl_matrix_complex_alloc(6,6);
	ws->perm = gsl_permutation_alloc(6);
	ws->eigenws = gsl_eigen_nonsymmv_alloc(6);
	ws->tempvec1 = gsl_vector_alloc(6);
	ws->tempvec2 = gsl_vector_alloc(6);
}
コード例 #7
0
double R0(const double theta[numParam], const double r0time, double * eigenvec)
{

  gsl_matrix * Fmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);
  gsl_matrix * Vmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);
  gsl_matrix * VmatInv = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);
  gsl_matrix * ngm = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);

  createNGM(theta, r0time, Fmat, Vmat);

  gsl_permutation * p = gsl_permutation_alloc(NG*(DS-1)*RG);
  int s;
  gsl_linalg_LU_decomp(Vmat, p, &s);
  gsl_linalg_LU_invert(Vmat, p, VmatInv);
  gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, Fmat, VmatInv, 0.0, ngm);

  gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(NG*(DS-1)*RG);
  gsl_vector_complex * eval = gsl_vector_complex_alloc(NG*(DS-1)*RG);
  gsl_matrix_complex * evec = gsl_matrix_complex_alloc(NG*(DS-1)*RG, NG*(DS-1)*RG);

  gsl_set_error_handler_off();
  gsl_eigen_nonsymmv(ngm, eval, evec, w);

  size_t r0_idx = 0;
  double r0 = 0.0;
  for(size_t i = 0; i < NG*(DS-1)*RG; i++){
    if(GSL_REAL(gsl_vector_complex_get(eval, i)) > r0){
      r0_idx = i;
      r0 = GSL_REAL(gsl_vector_complex_get(eval, i));
    }
  }

  if(eigenvec != NULL){
    for(size_t i = 0; i < NG*(DS-1)*RG; i++)
      eigenvec[i] = GSL_REAL(gsl_matrix_complex_get(evec, i, r0_idx));
  }

  gsl_matrix_free(Fmat);
  gsl_matrix_free(Vmat);
  gsl_matrix_free(VmatInv);
  gsl_matrix_free(ngm);
  gsl_permutation_free(p);
  gsl_eigen_nonsymmv_free(w);
  gsl_vector_complex_free(eval);
  gsl_matrix_complex_free(evec);

  return r0;
}
コード例 #8
0
gsl_vector_complex *
CRebuildGraph::calculateEgeinval (gsl_matrix *target)
{
    
    
    int order = (int)target->size1;
    
    gsl_vector_complex *eval = gsl_vector_complex_alloc (order);
    gsl_matrix_complex *evec = gsl_matrix_complex_alloc (order, order);
    

    gsl_eigen_nonsymmv_workspace * w =
    gsl_eigen_nonsymmv_alloc (order);
    
    gsl_eigen_nonsymmv (target, eval, evec, w);
    
    gsl_eigen_nonsymmv_free (w);
    
    gsl_eigen_nonsymmv_sort (eval, evec,
                             GSL_EIGEN_SORT_ABS_DESC);
    
    {
        int i, j;
        
        for (i = 0; i < order; i++)
        {
            gsl_complex eval_i
            = gsl_vector_complex_get (eval, i);
            gsl_vector_complex_view evec_i
            = gsl_matrix_complex_column (evec, i);
            
            printf ("eigenvalue = %g + %gi\n",
                    GSL_REAL(eval_i), GSL_IMAG(eval_i));
            printf ("eigenvector = \n");
            for (j = 0; j < order; ++j)
            {
                /* gsl_complex z = */
                gsl_vector_complex_get(&evec_i.vector, j);
                //              printf("%g + %gi\n", GSL_REAL(z), GSL_IMAG(z));
            }
        }
    }
    
    //  gsl_vector_complex_free(eval);
    gsl_matrix_complex_free(evec);
    
    return eval;
}
コード例 #9
0
ファイル: matrixutils.cpp プロジェクト: tnachstedt/lpzrobots
  bool eigenValuesVectors(const Matrix &m, Matrix& vals_real, Matrix& vals_imag, 
                          Matrix& vecs_real, Matrix& vecs_imag){
    // check if m is square
    assert(m.getM() == m.getN());
    gsl_matrix* m_gsl = toGSL(m); 

    gsl_eigen_nonsymmv_workspace* w = gsl_eigen_nonsymmv_alloc (m.getM());    
    gsl_vector_complex *eval = gsl_vector_complex_alloc (m.getM());
    gsl_matrix_complex *evec = gsl_matrix_complex_alloc (m.getM(), m.getM());
     
    gsl_eigen_nonsymmv (m_gsl, eval, evec, w);     
    gsl_eigen_nonsymmv_free (w);     
    gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_DESC);
    gsl_vector_view eval_real = gsl_vector_complex_real(eval);
    gsl_vector_view eval_imag = gsl_vector_complex_imag(eval);    
    vals_real = fromGSL(&eval_real.vector);
    vals_imag = fromGSL(&eval_imag.vector);
    vecs_real = fromGSL_real(evec);
    vecs_imag = fromGSL_imag(evec);
    return true;
    
  }
コード例 #10
0
double epidemicGrowthRate(const double theta[numParam], const double r0time, double * eigenvec)
{

  gsl_matrix * Fmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);
  gsl_matrix * Vmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG);

  createNGM(theta, r0time, Fmat, Vmat);
  gsl_matrix_sub(Fmat, Vmat);

  gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(NG*(DS-1)*RG);
  gsl_vector_complex * eval = gsl_vector_complex_alloc(NG*(DS-1)*RG);
  gsl_matrix_complex * evec = gsl_matrix_complex_alloc(NG*(DS-1)*RG, NG*(DS-1)*RG);

  gsl_set_error_handler_off();
  gsl_eigen_nonsymmv(Fmat, eval, evec, w);

  size_t growth_rate_idx = 0;
  double growth_rate = -INFINITY;
  for(size_t i = 0; i < NG*(DS-1)*RG; i++){
    if(GSL_REAL(gsl_vector_complex_get(eval, i)) > growth_rate){
      growth_rate_idx = i;
      growth_rate = GSL_REAL(gsl_vector_complex_get(eval, i));
    }
  }

  if(eigenvec != NULL){
    for(size_t i = 0; i < NG*(DS-1)*RG; i++)
      eigenvec[i] = GSL_REAL(gsl_matrix_complex_get(evec, i, growth_rate_idx));
  }

  gsl_matrix_free(Fmat);
  gsl_matrix_free(Vmat);
  gsl_vector_complex_free(eval);
  gsl_matrix_complex_free(evec);
  gsl_eigen_nonsymmv_free(w);

  return growth_rate;
}
コード例 #11
0
void MarkovChain::setupCDFS(const gsl_matrix * Q)
{
	double cdf, norm;
	gsl_vector_complex *eval;
	gsl_matrix_complex *evec;
	gsl_eigen_nonsymmv_workspace * w;
	gsl_vector_complex_view S;

	gsl_matrix_memcpy (m_cdfQ, Q);
	eval = gsl_vector_complex_alloc (Q->size1);
	evec = gsl_matrix_complex_alloc (Q->size1, Q->size2);
	w = gsl_eigen_nonsymmv_alloc(Q->size1);

	gsl_eigen_nonsymmv (m_cdfQ, eval, evec, w);
    gsl_eigen_nonsymmv_sort (eval, evec,
                             GSL_EIGEN_SORT_ABS_DESC);

    /*vector of stationary probabilities corresponding to the eigenvalue 1 */
	S = gsl_matrix_complex_column(evec, 0);
	/*sum of vector elements*/
	norm = 0.0;
	for(size_t i = 0; i < Q->size1; ++i)
	{
		norm += GSL_REAL(gsl_vector_complex_get(&S.vector, i));
	}

	/*cdfs*/
	cdf = 0.0;
	for(size_t i = 0; i < Q->size1; ++i)
	{
		cdf += GSL_REAL(gsl_vector_complex_get(&S.vector, i)) / norm;
		gsl_vector_set(m_cdfS, i, cdf);
	}

	gsl_eigen_nonsymmv_free (w);
    gsl_vector_complex_free(eval);
    gsl_matrix_complex_free(evec);
}
コード例 #12
0
ファイル: basic-tests.c プロジェクト: Cybernetic1/genifer5-c
void K_wandering_test()
	{
	int neuronsOfLayer[] = {10, 10, 10}; // first = input layer, last = output layer
	int numLayers = sizeof (neuronsOfLayer) / sizeof (int);
	NNET *Net = create_NN(numLayers, neuronsOfLayer);
	LAYER lastLayer = Net->layers[numLayers - 1];

	// **** Calculate spectral radius of weight matrices
	printf("Eigen values = \n");
	for (int l = 1; l < numLayers; ++l) // except first layer which has no weights
		{
		int N = 10;
		// assume weight matrix is square, if not, fill with zero rows perhaps (TO-DO)
		gsl_matrix *A = gsl_matrix_alloc(N, N);
		for (int n = 0; n < N; ++n)
			for (int i = 0; i < N; ++i)
				gsl_matrix_set(A, n, i, Net->layers[l].neurons[n].weights[i]);

		gsl_eigen_nonsymmv_workspace *wrk = gsl_eigen_nonsymmv_alloc(N);
		gsl_vector_complex *Aval = gsl_vector_complex_alloc(N);
		gsl_matrix_complex *Avec = gsl_matrix_complex_alloc(N, N);

		gsl_eigen_nonsymmv(A, Aval, Avec, wrk);
		gsl_eigen_nonsymmv_free(wrk);

		gsl_eigen_nonsymmv_sort(Aval, Avec, GSL_EIGEN_SORT_ABS_DESC);

		printf("[ ");
		for (int i = 0; i < N; i++)
			{
			gsl_complex v = gsl_vector_complex_get(Aval, i);
			// printf("%.02f %.02f, ", GSL_REAL(v), GSL_IMAG(v));
			printf("%.02f ", gsl_complex_abs(v));
			}
		printf(" ]\n");

		gsl_matrix_free(A);
		gsl_matrix_complex_free(Avec);
		gsl_vector_complex_free(Aval);
		}

	start_K_plot();
	printf("\nPress 'Q' to quit\n\n");

	// **** Initialize K vector
	for (int k = 0; k < dim_K; ++k)
		K[k] = (rand() / (float) RAND_MAX) - 0.5f;

	double K2[dim_K];
	int quit = 0;
	for (int j = 0; j < 10000; j++) // max number of iterations
		{
		ForwardPropMethod(Net, dim_K, K);

		// printf("%02d", j);
		double d = 0.0;

		// copy output to input
		for (int k = 0; k < dim_K; ++k)
			{
			K2[k] = K[k];
			K[k] = lastLayer.neurons[k].output;
			// printf(", %0.4lf", K[k]);
			double diff = (K2[k] - K[k]);
			d += (diff * diff);
			}

		plot_trainer(0); // required to clear window
		plot_K();
		if (quit = delay_vis(60)) // delay in milliseconds
			break;

		// printf("\n");
		if (d < 0.000001)
			{
			fprintf(stderr, "terminated after %d cycles,\t delta = %lf\n", j, d);
			break;
			}
		}

	beep();

	if (!quit)
		pause_graphics();
	else
		quit_graphics();
	free_NN(Net, neuronsOfLayer);
	}