コード例 #1
0
ファイル: mlgsl_linalg.c プロジェクト: Chris00/gsl-ocaml
CAMLprim value ml_gsl_linalg_exponential_ss(value A, value eA, value mode)
{
  _DECLARE_MATRIX2(A, eA);
  _CONVERT_MATRIX2(A, eA);
  gsl_linalg_exponential_ss(&m_A, &m_eA, GSL_MODE_val(mode));
  return Val_unit;
}
コード例 #2
0
int
main(void)
{
  ESL_STOPWATCH *w = NULL;
  ESL_DMATRIX *Q  = NULL;
  ESL_DMATRIX *P  = NULL;
  double       t = 5.0;
  int          esl_iterations = 100;
  int          i;
#ifdef HAVE_LIBGSL
  gsl_matrix  *Qg = NULL;
  gsl_matrix  *Pg = NULL;
  int          gsl_iterations = 100;
#endif

  w = esl_stopwatch_Create();
  Q = esl_dmatrix_Create(20, 20);
  P = esl_dmatrix_Create(20, 20);
  esl_rmx_SetWAG(Q, NULL);

  esl_stopwatch_Start(w);
  for (i = 0; i < esl_iterations; i++)
    esl_dmx_Exp(Q, t, P);
  esl_stopwatch_Stop(w);
  printf("Easel takes:   %g sec\n", w->user / (double) esl_iterations);

#ifdef HAVE_LIBGSL
  if (esl_dmx_MorphGSL(Q, &Qg)             != eslOK) esl_fatal("morph to gsl_matrix failed");
  if ((Pg = gsl_matrix_alloc(20, 20))      == NULL)  esl_fatal("gsl alloc failed");
  gsl_matrix_scale(Qg, t);
  
  esl_stopwatch_Start(w);
  for (i = 0; i < gsl_iterations; i++)
    gsl_linalg_exponential_ss(Qg, Pg, GSL_PREC_DOUBLE);
  esl_stopwatch_Stop(w);
  printf("  GSL takes:   %g sec\n", w->user / (double) gsl_iterations);

  gsl_matrix_free(Qg);
  gsl_matrix_free(Pg);
#endif /*HAVE_LIBGSL*/

  esl_dmatrix_Destroy(Q);
  esl_dmatrix_Destroy(P);
  esl_stopwatch_Destroy(w);
  return 0;
}
コード例 #3
0
ファイル: mexp.cpp プロジェクト: tlhallock/markov-fit
void set_cdf(const Generator& gen, SummedCdf& cdf, GslContext& context)
{
	gsl_matrix *A = (gsl_matrix *) context.get_input();
	gsl_matrix *B = (gsl_matrix *) context.get_output();

	const Stencil& bounds = cdf.bounds();

	const int n = context.get_size();


//	if (gen.get_size() != 4) throw 1;

//	double *test_q = new double[(n-1) * (n-1)];

	for (int i=0;i<bounds.N;i++)
	{
		double t = bounds.unmap(i);

		for (int j=0;j<n-1;j++)
			for (int k=0;k<n-1;k++)
			{
				gsl_matrix_set(A, j, k, gen.get_prob(j, k) * t);
//				test_q[j * (n-1) + k] = gen.get_prob(j, k) * t;
			}

		gsl_linalg_exponential_ss(A, B, GSL_PREC_DOUBLE);

		double d = 0;
		for (int j=0;j<n-1;j++)
			d += gsl_matrix_get(B, 0, j);

//		std::cout << "x            =" << t << std::endl;
//		std::cout << "d            =" << d << std::endl;
//		std::cout << "approximation=" << mat_exp_3_5(test_q) << std::endl;
//		std::cout << std::endl;

		cdf.set(i, 1-d);
	}

//	delete test_q;
}
コード例 #4
0
int
main(void)
{
  char errbuf[eslERRBUFSIZE];
  char *alphabet = "ACDEFGHIKLMNPQRSTVWY";
  ESL_DMATRIX *Q  = NULL;
  ESL_DMATRIX *P  = NULL;
  gsl_matrix  *Qg = NULL;
  gsl_matrix  *Pg = NULL;
  ESL_DMATRIX *Pge = NULL;
  double       t = 15.0;

  if ((Q = esl_dmatrix_Create(20, 20))     == NULL)  esl_fatal("malloc failed");
  if ((P = esl_dmatrix_Create(20, 20))     == NULL)  esl_fatal("malloc failed");

  if (esl_rmx_SetWAG(Q, NULL)              != eslOK) esl_fatal("_SetWAG() failed");
  if (esl_rmx_ValidateQ(Q, 0.0001, errbuf) != eslOK) esl_fatal("Q validation failed: %s", errbuf);

  if (esl_dmx_Exp(Q, t, P)                 != eslOK) esl_fatal("matrix exponentiation failed");
  if (esl_rmx_ValidateP(P, 0.0001, errbuf) != eslOK) esl_fatal("P validation failed: %s", errbuf);

  if (esl_dmx_MorphGSL(Q, &Qg)             != eslOK) esl_fatal("morph to gsl_matrix failed");
  if ((Pg = gsl_matrix_alloc(20, 20))      == NULL)  esl_fatal("gsl alloc failed");
  gsl_matrix_scale(Qg, t);
  if (gsl_linalg_exponential_ss(Qg, Pg, GSL_PREC_DOUBLE) != 0) esl_fatal("gsl's exponentiation failed");
  if (esl_dmx_UnmorphGSL(Pg, &Pge)         != eslOK) esl_fatal("morph from gsl_matrix failed");  
  
  esl_dmatrix_Dump(stdout, P, alphabet, alphabet);

  if (esl_dmatrix_Compare(Pge, P, 0.00001) != eslOK) esl_fatal("whoops, different answers.");
  
  esl_dmatrix_Destroy(Q);
  esl_dmatrix_Destroy(P);
  esl_dmatrix_Destroy(Pge);
  gsl_matrix_free(Qg);
  gsl_matrix_free(Pg);
  return 0;
}
コード例 #5
0
gsl_matrix* calc_Hs(double s, double tres, double* Qxx_m, double* Qyy_m, double* Qxy_m, double* Qyx_m, int kx, int ky) {
    
    /*
     *
     *
     *     *H(s)
     *I=eye(ky); 
     *expMat=mat_exp(-(s*I-Qyy)*tres); 
     *Hxx_s=Qxx+(Qxy*(s*I-Qyy)^-1)*(I-expMat)*Qyx; 
     *
    */
    
       /* Initialise Qxx,Qxy,Qyx,Qyy  */
   
    gsl_matrix *Qxx=setup_matrix(Qxx_m,kx,kx);
    gsl_matrix *Qxy=setup_matrix(Qxy_m,kx,ky);
    gsl_matrix *Qyx=setup_matrix(Qyx_m,ky,kx);
    gsl_matrix *Qyy=setup_matrix(Qyy_m,ky,ky);
    
    
    gsl_matrix *ky_eye; //identitny matrix of dimension ky,ky
    gsl_matrix *e_eye; //to hold the matrix exponential expMat
    gsl_matrix *detected_tres; //matrix representing period in states y for tres followed by transition to x
    gsl_matrix *seye2;
    gsl_matrix *inv_sI_Qyy;
    gsl_matrix *res;
    gsl_matrix *res2;
    gsl_matrix *res3;
    gsl_matrix *eye3;
    //printf("\tAllocate memory for the matrices\n");
    
    //allocate memory for all the matrices
    
    ky_eye=gsl_matrix_alloc(ky,ky);
    e_eye=gsl_matrix_alloc(ky,ky);
    seye2=gsl_matrix_alloc(ky,ky);
    detected_tres=gsl_matrix_alloc(ky,ky);
    inv_sI_Qyy=gsl_matrix_alloc(ky,ky);
    res=gsl_matrix_alloc(kx,ky);
    res2=gsl_matrix_alloc(ky,kx);
    res3 = gsl_matrix_alloc(kx,kx);
    eye3=gsl_matrix_alloc(ky,ky);
    
    //printf("\tAllocated memory for the matrices\n");
    
    gsl_matrix_set_identity(ky_eye); 
    //exp(-(s*I-Qyy)*tres)
    //build from the identity matrix
    gsl_matrix_memcpy (detected_tres,ky_eye);
    gsl_matrix_scale(detected_tres,s);
    gsl_matrix_sub(detected_tres,Qyy);
    gsl_matrix_scale(detected_tres,-1);
    gsl_matrix_scale(detected_tres,tres);
    gsl_linalg_exponential_ss(detected_tres, e_eye, .01);
    
    //printf("\tCalculated exp(-(s*I-Qyy)*tres)\n");
    
   
    //(s*I-Qyy)             
    gsl_matrix_memcpy (seye2,ky_eye);    
    gsl_matrix_scale(seye2,s);
    gsl_matrix_sub(seye2,Qyy);
    
    //printf("\tCalculated s*I-Qyy\n");
    
    //invert s*I-Qyy
    int sc;    
    gsl_permutation * p = gsl_permutation_alloc (ky);
    gsl_linalg_LU_decomp(seye2, p, &sc); 
    gsl_linalg_LU_invert(seye2, p, inv_sI_Qyy);
    gsl_permutation_free(p);
    
    //printf("\tInverted s*I-Qyy\n");
    
    /*
    for (int i=0; i<ky; i++){
        for (int j=0; j<ky; j++){
            mexPrintf("\t%f",gsl_matrix_get(inv_sI_Qyy,i,j));
        }
        mexPrintf("\n");
    }
    */
    
    //multiply Qxy * (s*I-Qyy)^-1
    gsl_matrix_set_zero(res);
    // res = (Qxy*(s*I-Qyy)^-1)
    gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
                  1.0, Qxy, inv_sI_Qyy,
                  0.0, res);
    

    
    //printf("\tCalculated Qxy * (s*I-Qyy)^-1\n");
    
    //res2 =(I-expMat)*Qyx;
    gsl_matrix_set_zero(res2);   
    gsl_matrix_memcpy (eye3,ky_eye);
    //printf("\tMemcpy (I-expMat)\n");
    gsl_matrix_sub(eye3,e_eye);
    
    //printf("\tSubtract (I-expMat)\n");
    
    gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
                  1.0, eye3, Qyx,
                  0.0, res2);
    
    //res3 = (Qxy*(s*I-Qyy)^-1)*(I-expMat)*Qyx;
    //res3 = res *res2
    //res3 is the result we want to return
    //printf("\t (I-expMat)*Qyx\n");
    
    gsl_matrix_set_zero(res3);
    gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
                  1.0, res, res2,
                  0.0, res3);
    
    
    gsl_matrix_add(res3,Qxx);
    
    /*
    for (int i=0; i<kx; i++){
        for (int j=0; j<kx; j++){
            mexPrintf("\t%f",gsl_matrix_get(res3,i,j));
        }
        mexPrintf("\n");
    }
     */
    
    //printf("\t Calced H(s)\n");
    
    //cleanup
    gsl_matrix_free(Qxx);
    gsl_matrix_free(Qxy);
    gsl_matrix_free(Qyx);
    gsl_matrix_free(Qyy);
    gsl_matrix_free(ky_eye);
    gsl_matrix_free(e_eye);
    gsl_matrix_free(detected_tres);
    gsl_matrix_free(seye2);
    gsl_matrix_free(inv_sI_Qyy);
    gsl_matrix_free(res);
    gsl_matrix_free(res2);
    gsl_matrix_free(eye3);
    
    //printf("\t Cleaned up H(s)\n");
    
    return res3;
}