示例#1
0
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);
}
示例#2
0
文件: test.c 项目: lemahdi/mglib
void
test_eigen_nonsymm_matrix(const gsl_matrix * m, size_t count,
                          const char * desc,
                          gsl_eigen_nonsymmv_workspace *w)
{
  const size_t N = m->size1;
  gsl_matrix * A = gsl_matrix_alloc(N, N);
  gsl_matrix * Z = gsl_matrix_alloc(N, N);
  gsl_matrix_complex * evec = gsl_matrix_complex_alloc(N, N);
  gsl_vector_complex * eval = gsl_vector_complex_alloc(N);

  /*
   * calculate eigenvalues and eigenvectors - it is sufficient to
   * test gsl_eigen_nonsymmv() since that function calls
   * gsl_eigen_nonsymm() for the eigenvalues
   */ 
  gsl_matrix_memcpy(A, m);
  gsl_eigen_nonsymmv(A, eval, evec, w);
  test_eigen_nonsymm_results (m, eval, evec, count, desc, "unsorted");

  /* test sort routines */
  gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC);
  test_eigen_nonsymm_results (m, eval, evec, count, desc, "abs/asc");

  gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_DESC);
  test_eigen_nonsymm_results (m, eval, evec, count, desc, "abs/desc");

  /* test Schur vectors */
  gsl_matrix_memcpy(A, m);
  gsl_eigen_nonsymmv_Z(A, eval, evec, Z, w);
  gsl_linalg_hessenberg_set_zero(A);
  test_eigen_schur(m, A, Z, Z, count, "nonsymm", desc);

  /* test if Z is an orthogonal matrix */
  if (w->nonsymm_workspace_p->do_balance == 0)
    test_eigen_orthog(Z, count, "nonsymm", desc);

  gsl_matrix_free(A);
  gsl_matrix_free(Z);
  gsl_matrix_complex_free(evec);
  gsl_vector_complex_free(eval);
}
示例#3
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;
}
示例#4
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;
}
示例#5
0
  bool eigenValues(const Matrix &m, Matrix& real, Matrix& imag){
    // check if m is square
    assert(m.getM() == m.getN());
    gsl_matrix* m_gsl = toGSL(m); 

    gsl_eigen_nonsymm_workspace* w = gsl_eigen_nonsymm_alloc (m.getM());    
    gsl_vector_complex *eval = gsl_vector_complex_alloc (m.getM());
     
    gsl_eigen_nonsymm (m_gsl, eval,  w);     
    gsl_eigen_nonsymm_free (w);     
    gsl_eigen_nonsymmv_sort (eval, 0, 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);
    real = fromGSL(&eval_real.vector);
    imag = fromGSL(&eval_imag.vector);
    return true;
  }
示例#6
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);
}
示例#7
0
文件: test.c 项目: lemahdi/mglib
void
test_eigen_gen_pencil(const gsl_matrix * A, const gsl_matrix * B,
                      size_t count, const char * desc, int test_schur,
                      test_eigen_gen_workspace *w)
{
  const size_t N = A->size1;
  size_t i;

  gsl_matrix_memcpy(w->A, A);
  gsl_matrix_memcpy(w->B, B);

  if (test_schur)
    {
      gsl_eigen_genv_QZ(w->A, w->B, w->alphav, w->betav, w->evec, w->Q, w->Z, w->genv_p);
      test_eigen_schur(A, w->A, w->Q, w->Z, count, "genv/A", desc);
      test_eigen_schur(B, w->B, w->Q, w->Z, count, "genv/B", desc);
    }
  else
    gsl_eigen_genv(w->A, w->B, w->alphav, w->betav, w->evec, w->genv_p);

  test_eigen_gen_results(A, B, w->alphav, w->betav, w->evec, count, desc, "unsorted");

  gsl_matrix_memcpy(w->A, A);
  gsl_matrix_memcpy(w->B, B);

  if (test_schur)
    {
      gsl_eigen_gen_params(1, 1, 0, w->gen_p);
      gsl_eigen_gen_QZ(w->A, w->B, w->alpha, w->beta, w->Q, w->Z, w->gen_p);
      test_eigen_schur(A, w->A, w->Q, w->Z, count, "gen/A", desc);
      test_eigen_schur(B, w->B, w->Q, w->Z, count, "gen/B", desc);
    }
  else
    {
      gsl_eigen_gen_params(0, 0, 0, w->gen_p);
      gsl_eigen_gen(w->A, w->B, w->alpha, w->beta, w->gen_p);
    }

  /* compute eval = alpha / beta values */
  for (i = 0; i < N; ++i)
    {
      gsl_complex z, ai;
      double bi;

      ai = gsl_vector_complex_get(w->alpha, i);
      bi = gsl_vector_get(w->beta, i);
      GSL_SET_COMPLEX(&z, GSL_REAL(ai) / bi, GSL_IMAG(ai) / bi);
      gsl_vector_complex_set(w->eval, i, z);

      ai = gsl_vector_complex_get(w->alphav, i);
      bi = gsl_vector_get(w->betav, i);
      GSL_SET_COMPLEX(&z, GSL_REAL(ai) / bi, GSL_IMAG(ai) / bi);
      gsl_vector_complex_set(w->evalv, i, z);
    }

  /* sort eval and evalv and test them */
  gsl_eigen_nonsymmv_sort(w->eval, NULL, GSL_EIGEN_SORT_ABS_ASC);
  gsl_eigen_nonsymmv_sort(w->evalv, NULL, GSL_EIGEN_SORT_ABS_ASC);
  test_eigenvalues_complex(w->evalv, w->eval, "gen", desc);

  gsl_eigen_genv_sort(w->alphav, w->betav, w->evec, GSL_EIGEN_SORT_ABS_ASC);
  test_eigen_gen_results(A, B, w->alphav, w->betav, w->evec, count, desc, "abs/asc");
  gsl_eigen_genv_sort(w->alphav, w->betav, w->evec, GSL_EIGEN_SORT_ABS_DESC);
  test_eigen_gen_results(A, B, w->alphav, w->betav, w->evec, count, desc, "abs/desc");
} /* test_eigen_gen_pencil() */
示例#8
0
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);
	}