Пример #1
0
double intensita(gsl_complex A, gsl_complex B) {
	// PROVVISORIO
	//~ double intensita = (gsl_complex_abs2(A) + gsl_complex_abs2(B) )/(2*in.Z_st);
	double intensita = gsl_complex_abs(gsl_complex_mul(gsl_complex_add(A,B) , gsl_complex_conjugate(gsl_complex_add(A,B))));
	printf("I=%e\n",intensita);
	#ifdef DEBUG
	printf("|A|=%e\n",gsl_complex_abs(A));
	printf("|B|=%e\n",gsl_complex_abs(B));
	printf("intensita=%e\n",intensita);
	#endif
	return intensita;
}
Пример #2
0
gsl_complex
gsl_linalg_complex_LU_sgndet (gsl_matrix_complex * LU, int signum)
{
  size_t i, n = LU->size1;

  gsl_complex phase = gsl_complex_rect((double) signum, 0.0);

  for (i = 0; i < n; i++)
    {
      gsl_complex z = gsl_matrix_complex_get (LU, i, i);
      
      double r = gsl_complex_abs(z);

      if (r == 0)
	{
	  phase = gsl_complex_rect(0.0, 0.0);
	  break;
	}
      else
        {
          z = gsl_complex_div_real(z, r);
          phase = gsl_complex_mul(phase, z);
        }
    }

  return phase;
}
Пример #3
0
gsl_complex gsl_complex_inverse(gsl_complex a)
{				/* z=1/a */
    double s = 1.0 / gsl_complex_abs(a);

    gsl_complex z;
    GSL_SET_COMPLEX(&z, (GSL_REAL(a) * s) * s, -(GSL_IMAG(a) * s) * s);
    return z;
}
Пример #4
0
/* ------------------------------------------------------ */
gsl_complex gsl_complex_cabs (gsl_complex a)
{        
  gsl_complex z;

  GSL_SET_COMPLEX (&z, gsl_complex_abs(a), 0);

  return z;
}
Пример #5
0
/* NOTE: Assumes z is in fundamental parallelogram  */
void wP_and_prime(gsl_complex z, gsl_complex tau, const gsl_complex *g, gsl_complex *p, gsl_complex *pp)
{
  int N = 6;  /* Enough iterations for good P, not so good P' */
  int i;
  gsl_complex z0;
  gsl_complex z02;
  gsl_complex pout, ppout;
  gsl_complex ppsolve;

  z = near_origin(z,tau);

  z0 = gsl_complex_div_real(z,(double)(1 << N));
  z02 = gsl_complex_mul(z0,z0);

  /* Laurent expansion:  P \approx 1/z^2 + (g2/20)z^2 + (g3/28) z^4 */
  pout = gsl_complex_add(gsl_complex_inverse(z02),
			 gsl_complex_add(gsl_complex_mul(z02,gsl_complex_mul_real(g[0],0.05)),
					 gsl_complex_mul(gsl_complex_mul(z02,z02),gsl_complex_mul_real(g[1],_CONST_1_28))));

  /* Laurent expansion:  P' \approx -2/z^3 + g2/10z + g3/7 z^3 */
  ppout = gsl_complex_add(gsl_complex_mul_real(gsl_complex_inverse(gsl_complex_mul(z0,z02)),-2.0),
			  gsl_complex_add(gsl_complex_mul(z0,gsl_complex_mul_real(g[0],0.1)),
					  gsl_complex_mul(gsl_complex_mul(z0,z02),gsl_complex_mul_real(g[1],_CONST_1_7))));

  for (i=0;i<N;i++) {
    P_and_Pprime_doubler(&pout, &ppout, g);
  }

  /* At this point ppout is a decent but not great approximation of P'(z)        */
  /* Instead of using it directly, we use it as a guide for which square root of */
  /* (4P^3 - g2 P - g3) should be selected.                                      */

  ppsolve = gsl_complex_sqrt(
                gsl_complex_sub(
                    gsl_complex_mul_real(gsl_complex_mul(pout,gsl_complex_mul(pout,pout)),4.0),
		    gsl_complex_add(gsl_complex_mul(g[0],pout),g[1])
                )
	    );

  *p = pout;
  if (gsl_complex_abs(gsl_complex_sub(ppsolve,ppout)) < gsl_complex_abs(gsl_complex_add(ppsolve,ppout)))
    *pp = ppsolve;
  else
    *pp = gsl_complex_negative(ppsolve);
}
Пример #6
0
/* print square complex matrix */
void printComMat(gsl_matrix_complex * M, int N){
int i,j;
	for (i=0; i<N; i++){
		for (j=0; j<N; j++)
			printf("%.3lf ", gsl_complex_abs(gsl_matrix_complex_get(M,i,j)));
		printf("\n");
	}


}
Пример #7
0
int
print_correlation(const char *filename, poltor_workspace *w)
{
  int s = 0;
  const size_t p = w->p;
  const size_t nmax = GSL_MIN(w->nmax_int, w->nmax_sh);
  const size_t mmax = GSL_MIN(w->mmax_int, w->mmax_sh);
  gsl_matrix_complex *B;
  size_t n;
  FILE *fp;

  fp = fopen(filename, "w");
  if (!fp)
    {
      fprintf(stderr, "print_correlation: unable to open %s: %s\n",
              filename, strerror(errno));
      return -1;
    }

  B = gsl_matrix_complex_calloc(p, p);

  /* compute correlation matrix */
  s = lls_complex_correlation(B, w->lls_workspace_p);

  for (n = 1; n <= nmax; ++n)
    {
      int ni = (int) GSL_MIN(n, mmax);
      int m;

      for (m = -ni; m <= ni; ++m)
        {
          size_t idx1 = poltor_nmidx(POLTOR_IDX_PINT, n, m, w);
          size_t idx2 = poltor_jnmidx(0, n, m, w);
          gsl_complex z = gsl_matrix_complex_get(B, idx1, idx2);

          fprintf(fp, "%3zu %3d %e\n",
                  n,
                  m,
                  gsl_complex_abs(z));
        }

      fprintf(fp, "\n\n");
    }

  gsl_matrix_complex_free(B);

  fclose(fp);

  return s;
} /* print_correlation() */
Пример #8
0
static int matrix_is_equal(gsl_matrix_complex *m1, gsl_matrix_complex *m2, gsl_complex *c)
{
  gsl_complex a, b, ab, absave;
  double eps = 1e-6;
  size_t i, j;
  absave.dat[0] = 99999;
  absave.dat[1] = 99999;
  if (m1->size1 != m2->size1 ||  m1->size2 != m2->size2) return 0;
  for (i = 0; i < m1->size1; i++) {
    for (j = 0; j < m1->size2; j++) {
      a = gsl_matrix_complex_get(m1, i, j);
      b = gsl_matrix_complex_get(m2, i, j);
      if (!gsl_fcmp(gsl_complex_abs(b), 0.0, eps)) continue;
      ab = gsl_complex_div(a, b);
      if (!gsl_fcmp(gsl_complex_abs(ab), 0.0, eps)) continue;
      if ((int) absave.dat[0] == 99999) absave = ab;
      if (gsl_fcmp(ab.dat[0], absave.dat[0], eps)) return 0;
      if (gsl_fcmp(ab.dat[1], absave.dat[1], eps)) return 0;
    }
  }
  if ((int) absave.dat[0] == 99999) return 0;
  *c = ab;
  return 1;
}
Пример #9
0
double
gsl_linalg_complex_LU_lndet (gsl_matrix_complex * LU)
{
  size_t i, n = LU->size1;

  double lndet = 0.0;

  for (i = 0; i < n; i++)
    {
      gsl_complex z = gsl_matrix_complex_get (LU, i, i);
      lndet += log (gsl_complex_abs (z));
    }

  return lndet;
}
Пример #10
0
gsl_complex gsl_complex_div(gsl_complex a, gsl_complex b)
{				/* z=a/b */
    double ar = GSL_REAL(a), ai = GSL_IMAG(a);
    double br = GSL_REAL(b), bi = GSL_IMAG(b);

    double s = 1.0 / gsl_complex_abs(b);

    double sbr = s * br;
    double sbi = s * bi;

    double zr = (ar * sbr + ai * sbi) * s;
    double zi = (ai * sbr - ar * sbi) * s;

    gsl_complex z;
    GSL_SET_COMPLEX(&z, zr, zi);
    return z;
}
Пример #11
0
gsl_complex theta40(gsl_complex q)
{
  int n=0;
  gsl_complex accum = gsl_complex_rect(0.5,0.0);
  gsl_complex q2 = gsl_complex_mul(q,q);
  gsl_complex nextm = gsl_complex_negative(gsl_complex_mul(q,q2));
  gsl_complex qpower = gsl_complex_negative(q);

  while ((gsl_complex_abs(qpower) > 2.0*GSL_DBL_EPSILON) && (n < THETA_ITER_MAX)) {
    accum = gsl_complex_add(accum, qpower);
    qpower = gsl_complex_mul(qpower, nextm);
    nextm = gsl_complex_mul(nextm, q2);
    n++;
  }
  if (n >= THETA_ITER_MAX)
    return(gsl_complex_rect(0.0,0.0));
  return gsl_complex_mul_real(accum,2.0);
}
Пример #12
0
gsl_complex theta20(gsl_complex q, gsl_complex q14)
{
  int n=0;
  gsl_complex accum = gsl_complex_rect(0.0,0.0);
  gsl_complex q2 = gsl_complex_mul(q,q);
  gsl_complex nextm = q2;
  gsl_complex qpower = gsl_complex_rect(1.0,0.0);

  while ((gsl_complex_abs(qpower) > 2.0*GSL_DBL_EPSILON) && (n < THETA_ITER_MAX)) {
    accum = gsl_complex_add(accum, qpower);
    qpower = gsl_complex_mul(qpower, nextm);
    nextm = gsl_complex_mul(nextm, q2);
    n++;
  }
  if (n >= THETA_ITER_MAX)
    return(gsl_complex_rect(0.0,0.0));
  return gsl_complex_mul_real(gsl_complex_mul(q14,accum),2.0);
}
Пример #13
0
void tabulate_integral(Params *params,
                       const Contour *contour,
                       double r0,
                       double r1,
                       int n,
                       FILE *os)
{
    for (int i = 0; i < n; ++i)
    {
        double r = r0 + (r1 - r0) * ((double)i / (n - 1));

        params->r = r;
        gsl_complex res = integrate_contour(params, contour);

        double abs = gsl_complex_abs(res);

        fprintf(os, "%g %g %g %g %g\n",
                r, GSL_REAL(res), GSL_IMAG(res), abs, -abs);
    }
}
Пример #14
0
gsl_complex theta3(gsl_complex z, gsl_complex q)
{
  int n=0;
  gsl_complex accum = gsl_complex_rect(0.5,0.0);
  gsl_complex q2 = gsl_complex_mul(q,q);
  gsl_complex nextm = gsl_complex_mul(q,q2);
  gsl_complex qpower = q;
  gsl_complex term = gsl_complex_rect(1.0,0.0);

  while ((gsl_complex_abs(qpower) > 2.0*GSL_DBL_EPSILON) && (n < THETA_ITER_MAX)) {
    term = gsl_complex_mul(qpower, gsl_complex_cos(gsl_complex_mul_real(z,2*(n+1))));
    accum = gsl_complex_add(accum, term);
    qpower = gsl_complex_mul(qpower, nextm);
    nextm = gsl_complex_mul(nextm, q2);
    n++;
  }
  if (n >= THETA_ITER_MAX)
    return(gsl_complex_rect(0.0,0.0));
  return gsl_complex_mul_real(accum,2.0);
}
Пример #15
0
gsl_complex theta1(gsl_complex z, gsl_complex q, gsl_complex q14)
{
  int n=0;
  gsl_complex accum = gsl_complex_rect(0.0,0.0);
  gsl_complex q2 = gsl_complex_mul(q,q);
  gsl_complex nextm = gsl_complex_negative(q2);
  gsl_complex qpower = gsl_complex_rect(1.0,0.0);
  gsl_complex term = gsl_complex_rect(1.0,0.0);

  while ((gsl_complex_abs(term) > 2.0*GSL_DBL_EPSILON) && (n < THETA_ITER_MAX)) {
    term = gsl_complex_mul(qpower, gsl_complex_sin(gsl_complex_mul_real(z,2*n+1)));
    accum = gsl_complex_add(accum, term);
    qpower = gsl_complex_mul(qpower, nextm);
    nextm = gsl_complex_mul(nextm, q2);
    n++;
  }
  if (n >= THETA_ITER_MAX)
    return(gsl_complex_rect(0.0,0.0));
  return gsl_complex_mul_real(gsl_complex_mul(q14,accum),2.0);
}
Пример #16
0
void tabulate(FILE *os,
              ComplexFunction fun,
              void *params,
              gsl_complex z0,
              gsl_complex z1,
              int n)
{
    gsl_complex k = gsl_complex_sub(z1, z0);
    for (int i = 0; i < n; ++i)
    {
        double t = (double)i / (n - 1);
        gsl_complex z = gsl_complex_add(z0, gsl_complex_mul_real(k, t));
        gsl_complex f = fun(z, params);
        double abs = gsl_complex_abs(f);

        fprintf(os, "%g %g %g %g %g %g %g\n",
                t,
                GSL_REAL(z), GSL_IMAG(z),
                GSL_REAL(f), GSL_IMAG(f), abs, -abs);
    }
}
Пример #17
0
void MAIAllocator::UpdateInputLink(){

	for (int u=0;u<M();u++) { // user loop

		// user errors
		pAgent->Update(umapUserErrsVec[u],gsl_vector_uint_get(errs,u));

		// channel coefficients
		for (int j=0;j<N();j++) {
			double coeffVal = gsl_complex_abs(gsl_matrix_complex_get(Hmat,j,u));
			pAgent->Update(chansCoeffValueMat[u*N()+j],mudisp::lintodb(coeffVal));
		} // j loop

		// current allocation (channels and powers)
		for (int j=0;j<J();j++) {
			unsigned int carr = gsl_matrix_uint_get(signature_frequencies,u,j);
			double power = gsl_matrix_get(signature_powers,u,j);
			pAgent->Update(umapUserCarrCidMat[u*J()+j],carr);
			pAgent->Update(umapUserCarrPowerMat[u*J()+j],power);
		} // j loop
	} // user loop

}
Пример #18
0
/* Assuming z is in the (1,tau) parallelogram, return the point
   closest to the origin among all translates of z by the lattice. */
gsl_complex near_origin(gsl_complex z, gsl_complex tau)
{
  gsl_complex znew;

  znew = gsl_complex_sub_real(z,1.0);
  if (gsl_complex_abs(z) > gsl_complex_abs(znew))
    z = znew;

  znew = gsl_complex_sub(z,tau);
  if (gsl_complex_abs(z) > gsl_complex_abs(znew))
    z = znew;

  znew = gsl_complex_sub(z,gsl_complex_add_real(tau,1.0));
  if (gsl_complex_abs(z) > gsl_complex_abs(znew))
    z = znew;

  return z;
}
Пример #19
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);
	}
Пример #20
0
/**
 * \brief Find a change point in complex data
 *
 * This function is based in the Bayesian Blocks algorithm of \cite Scargle1998 that finds "change points" in data -
 * points at which the statistics of the data change. It is based on calculating evidence, or odds, ratios. The
 * function first computes the marginal likelihood (or evidence) that the whole of the data is described by a single
 * Gaussian (with mean of zero). This comes from taking a Gaussian likelihood function and analytically marginalising
 * over the standard deviation (using a prior on the standard deviation of \f$1/\sigma\f$), giving (see
 * [\cite DupuisWoan2005]) a Students-t distribution (see
 * <a href="https://wiki.ligo.org/foswiki/pub/CW/PulsarParameterEstimationNestedSampling/studentst.pdf">here</a>).
 * Following this the data is split into two segments (with lengths greater than, or equal to the minimum chunk length)
 * for all possible combinations, and the joint evidence for each of the two segments consisting of independent
 * Gaussian (basically multiplying the above equation calculated for each segment separately) is calculated and the
 * split point recorded. However, the value required for comparing to that for the whole data set, to give the odds
 * ratio, is the evidence that having any split is better than having no split, so the individual split data evidences
 * need to be added incoherently to give the total evidence for a split. The index at which the evidence for a single
 * split is maximum (i.e. the most favoured split point) is that which is returned.
 *
 * \param data [in] a complex data vector
 * \param logodds [in] a pointer to return the natural logarithm of the odds ratio/Bayes factor
 * \param minlength [in] the minimum chunk length
 *
 * \return The position of the change point
 */
UINT4 find_change_point( gsl_vector_complex *data, REAL8 *logodds, UINT4 minlength ){
  UINT4 changepoint = 0, i = 0;
  UINT4 length = (UINT4)data->size, lsum = 0;

  REAL8 datasum = 0.;

  REAL8 logsingle = 0., logtot = -INFINITY;
  REAL8 logdouble = 0., logdouble_min = -INFINITY;
  REAL8 logratio = 0.;

  REAL8 sumforward = 0., sumback = 0.;
  gsl_complex dval;

  /* check that data is at least twice the minimum length, if not return an odds ratio of zero (log odds = -inf [or close to that!]) */
  if ( length < (UINT4)(2*minlength) ){
    logratio = -INFINITY;
    memcpy(logodds, &logratio, sizeof(REAL8));
    return 0;
  }

  /* calculate the sum of the data squared */
  for (i = 0; i < length; i++) {
    dval = gsl_vector_complex_get( data, i );
    datasum += SQUARE( gsl_complex_abs( dval ) );
  }

  /* calculate the evidence that the data consists of a Gaussian data with a single standard deviation */
  logsingle = -LAL_LN2 - (REAL8)length*LAL_LNPI + gsl_sf_lnfact(length-1) - (REAL8)length * log( datasum );

  lsum = length - 2*minlength + 1;

  for ( i = 0; i < length; i++ ){
    dval = gsl_vector_complex_get( data, i );
    if ( i < minlength-1 ){ sumforward += SQUARE( gsl_complex_abs( dval ) ); }
    else{ sumback += SQUARE( gsl_complex_abs( dval ) ); }
  }

  /* go through each possible change point and calculate the evidence for the data consisting of two independent
   * Gaussian's either side of the change point. Also calculate the total evidence for any change point.
   * Don't allow single points, so start at the second data point. */
  for (i = 0; i < lsum; i++){
    UINT4 ln1 = i+minlength, ln2 = (length-i-minlength);

    REAL8 log_1 = 0., log_2 = 0.;

    dval = gsl_vector_complex_get( data, ln1-1 );
    REAL8 adval = SQUARE( gsl_complex_abs( dval ) );
    sumforward += adval;
    sumback -= adval;

    /* get log evidences for the individual segments */
    log_1 = -LAL_LN2 - (REAL8)ln1*LAL_LNPI + gsl_sf_lnfact(ln1-1) - (REAL8)ln1 * log( sumforward );
    log_2 = -LAL_LN2 - (REAL8)ln2*LAL_LNPI + gsl_sf_lnfact(ln2-1) - (REAL8)ln2 * log( sumback );

    /* get evidence for the two segments */
    logdouble = log_1 + log_2;

    /* add to total evidence for a change point */
    logtot = LOGPLUS(logtot, logdouble);

    /* find maximum value of logdouble and record that as the change point */
    if ( logdouble > logdouble_min ){
      changepoint = ln1;
      logdouble_min = logdouble;
    }
  }

  /* get the log odds ratio of segmented versus non-segmented model */
  logratio = logtot - logsingle;
  memcpy(logodds, &logratio, sizeof(REAL8));

  return changepoint;
}
Пример #21
0
/** ----------------------------------------------------------------------------
 * Sort eigenvectors
 */
int
gsl_ext_eigen_sort(gsl_matrix_complex *evec, gsl_vector_complex *eval, int sort_order)
{
    gsl_complex z;

      gsl_matrix_complex *evec_copy;
    gsl_vector_complex *eval_copy;
    
    int *idx_map, i, j, idx_temp;

    double p1, p2;

    if ((evec->size1 != evec->size2) || (evec->size1 != eval->size))
    {
        return -1;
    }

    evec_copy = gsl_matrix_complex_alloc(evec->size1, evec->size2);
    eval_copy = gsl_vector_complex_alloc(eval->size);

    idx_map  = (int *)malloc(sizeof(int) * eval->size);

    gsl_matrix_complex_memcpy(evec_copy, evec);
    gsl_vector_complex_memcpy(eval_copy, eval);

    // calculate new eigenvalue order

    for (i = 0; i < eval->size; i++)
    {
        idx_map[i] = i;
    }

    for (i = 0; i < eval->size - 1; i++)
    {
        for (j = i+1; j < eval->size; j++)
        {
            idx_temp = -1;

            if (sort_order == GSL_EXT_EIGEN_SORT_ABS)
            {
                p1 = gsl_complex_abs(gsl_vector_complex_get(eval, idx_map[i]));
                p2 = gsl_complex_abs(gsl_vector_complex_get(eval, idx_map[j]));
                if (p1 > p2)
                {
                    idx_temp = idx_map[i];
                }
            }

            if (sort_order == GSL_EXT_EIGEN_SORT_PHASE)
            {
                p1 = gsl_complex_arg(gsl_vector_complex_get(eval, idx_map[i]));
                p2 = gsl_complex_arg(gsl_vector_complex_get(eval, idx_map[j]));

                if (p1 >   M_PI) p1 -= 2*M_PI;        
                if (p1 <= -M_PI) p1 += 2*M_PI;        

                if (p2 >   M_PI) p2 -= 2*M_PI;        
                if (p2 <= -M_PI) p2 += 2*M_PI;        

                //if (((p2 < p1) && (p1 - p2 < M_PI)) || )
                if (p2 < p1)
                {
                    idx_temp = idx_map[i];
                }
            }

            if (idx_temp != -1)
            {
                // swap
                //idx_temp = idx_map[i];
                idx_map[i] = idx_map[j];
                idx_map[j] = idx_temp;
            }
        }
    } 

    // reshuffle the eigenvectors and eigenvalues
    for (i = 0; i < eval->size; i++)
    {   
        for (j = 0; j < eval->size; j++)
        {
            z = gsl_matrix_complex_get(evec_copy, idx_map[i], j);
            gsl_matrix_complex_set(evec, i, j, z);            
            //z = gsl_matrix_complex_get(evec_copy, i, idx_map[j]);
            //gsl_matrix_complex_set(evec, i, j, z);                    
        }

        z = gsl_vector_complex_get(eval_copy, idx_map[i]);
        gsl_vector_complex_set(eval, i, z);
    }    

    gsl_matrix_complex_free(evec_copy);
    gsl_vector_complex_free(eval_copy);

    free(idx_map);
 
    return 0;
}
Пример #22
0
void
test_eigen_herm_results (const gsl_matrix_complex * A, 
                         const gsl_vector * eval, 
                         const gsl_matrix_complex * evec, 
                         size_t count,
                         const char * desc,
                         const char * desc2)
{
  const size_t N = A->size1;
  size_t i, j;

  gsl_vector_complex * x = gsl_vector_complex_alloc(N);
  gsl_vector_complex * y = gsl_vector_complex_alloc(N);

  /* check eigenvalues */

  for (i = 0; i < N; i++)
    {
      double ei = gsl_vector_get (eval, i);
      gsl_vector_complex_const_view vi =
        gsl_matrix_complex_const_column(evec, i);
      gsl_vector_complex_memcpy(x, &vi.vector);
      /* compute y = m x (should = lambda v) */
      gsl_blas_zgemv (CblasNoTrans, GSL_COMPLEX_ONE, A, x, 
                      GSL_COMPLEX_ZERO, y);
      for (j = 0; j < N; j++)
        {
          gsl_complex xj = gsl_vector_complex_get (x, j);
          gsl_complex yj = gsl_vector_complex_get (y, j);
          gsl_test_rel(GSL_REAL(yj), ei * GSL_REAL(xj), 1e8*GSL_DBL_EPSILON, 
                       "%s, eigenvalue(%d,%d), real, %s", desc, i, j, desc2);
          gsl_test_rel(GSL_IMAG(yj), ei * GSL_IMAG(xj), 1e8*GSL_DBL_EPSILON, 
                       "%s, eigenvalue(%d,%d), imag, %s", desc, i, j, desc2);
        }
    }

  /* check eigenvectors are orthonormal */

  for (i = 0; i < N; i++)
    {
      gsl_vector_complex_const_view vi = gsl_matrix_complex_const_column(evec, i);
      double nrm_v = gsl_blas_dznrm2(&vi.vector);
      gsl_test_rel (nrm_v, 1.0, N * GSL_DBL_EPSILON, "%s, normalized(%d), %s", 
                    desc, i, desc2);
    }

  for (i = 0; i < N; i++)
    {
      gsl_vector_complex_const_view vi = gsl_matrix_complex_const_column(evec, i);
      for (j = i + 1; j < N; j++)
        {
          gsl_vector_complex_const_view vj 
            = gsl_matrix_complex_const_column(evec, j);
          gsl_complex vivj;
          gsl_blas_zdotc (&vi.vector, &vj.vector, &vivj);
          gsl_test_abs (gsl_complex_abs(vivj), 0.0, 10.0 * N * GSL_DBL_EPSILON, 
                        "%s, orthogonal(%d,%d), %s", desc, i, j, desc2);
        }
    }

  gsl_vector_complex_free(x);
  gsl_vector_complex_free(y);
} /* test_eigen_herm_results() */
Пример #23
0
int
gsl_linalg_complex_LU_decomp (gsl_matrix_complex * A, gsl_permutation * p, int *signum)
{
  if (A->size1 != A->size2)
    {
      GSL_ERROR ("LU decomposition requires square matrix", GSL_ENOTSQR);
    }
  else if (p->size != A->size1)
    {
      GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
    }
  else
    {
      const size_t N = A->size1;
      size_t i, j, k;

      *signum = 1;
      gsl_permutation_init (p);

      for (j = 0; j < N - 1; j++)
	{
	  /* Find maximum in the j-th column */

	  gsl_complex ajj = gsl_matrix_complex_get (A, j, j);
          double max = gsl_complex_abs (ajj);
	  size_t i_pivot = j;

	  for (i = j + 1; i < N; i++)
	    {
	      gsl_complex aij = gsl_matrix_complex_get (A, i, j);
              double ai = gsl_complex_abs (aij);

	      if (ai > max)
		{
		  max = ai;
		  i_pivot = i;
		}
	    }

	  if (i_pivot != j)
	    {
	      gsl_matrix_complex_swap_rows (A, j, i_pivot);
	      gsl_permutation_swap (p, j, i_pivot);
	      *signum = -(*signum);
	    }

	  ajj = gsl_matrix_complex_get (A, j, j);

	  if (!(GSL_REAL(ajj) == 0.0 && GSL_IMAG(ajj) == 0.0))
	    {
	      for (i = j + 1; i < N; i++)
		{
		  gsl_complex aij_orig = gsl_matrix_complex_get (A, i, j);
                  gsl_complex aij = gsl_complex_div (aij_orig, ajj);
		  gsl_matrix_complex_set (A, i, j, aij);

		  for (k = j + 1; k < N; k++)
		    {
		      gsl_complex aik = gsl_matrix_complex_get (A, i, k);
		      gsl_complex ajk = gsl_matrix_complex_get (A, j, k);
                      
                      /* aik = aik - aij * ajk */

                      gsl_complex aijajk = gsl_complex_mul (aij, ajk);
                      gsl_complex aik_new = gsl_complex_sub (aik, aijajk);

		      gsl_matrix_complex_set (A, i, k, aik_new);
		    }
		}
	    }
	}
      
      return GSL_SUCCESS;
    }
}
Пример #24
0
void SoftDemapper::Run() {

  unsigned int nbits,nsymbs,count;


  /// fetch data objects
  gsl_vector_complex_class input = vin1.GetDataObj();	

  // number of input symbs
  nsymbs = input.vec->size;

  //  cout << "received " << nsymbs << " elements in vector." << endl;

  // number of output symbols
  nbits = nsymbs * Nb();


  gsl_vector *llr=gsl_vector_alloc(nbits);
  double *den = (double *)calloc( Nb(), sizeof(double) );
  double *num = (double *)calloc( Nb(), sizeof(double) );


  // determine symb_likelyhood
  for (int i=0;i<nsymbs;i++) {  // cycle through received symbols

    for (int k=0;k<Nb();k++) {
      num[k] = GSL_NEGINF;			
      den[k] = GSL_NEGINF;			
    }

    // the received symbol
    gsl_complex recsym = gsl_vector_complex_get(input.vec,i);

    
//       cout << "received symbol = (" 
// 	   << GSL_REAL(recsym) 
// 	   << ","
// 	   << GSL_IMAG(recsym) 
// 	   << ") " << endl;


    for (int j=0;j<Ns;j++) { // cycle through postulated symbol

      // gray encoded symbol id
      unsigned int symbol_id = gsl_vector_uint_get(gray_encoding,j);
      
      // complex symbol
      gsl_complex refsym = gsl_complex_polar(1.0,symbol_arg * symbol_id );

      // likelyhood metric
      double metric = gsl_complex_abs( gsl_complex_sub(refsym,recsym) );
      metric = -EsNo()*metric*metric;

      //gsl_matrix_complex_set(symb_likelihoods,j,i);


      //
      // HERE is available a metric for symb i and refsymb j
      //      
      int mask = 1 << Nb() - 1;
      
      for (int k=0;k<Nb();k++) {	/* loop over bits */
	if (mask&j) {
	  /* this bit is a one */
	  num[k] = ( *max_star[LMAP()] )( num[k], metric );
	} else {
	  /* this bit is a zero */
	  den[k] = ( *max_star[LMAP()] )( den[k], metric );
	}
	mask = mask >> 1;
      } //bits
    } // alphabet

    for (int k=0;k<Nb();k++) {
      gsl_vector_set(llr,Nb()*i+k,num[k] - den[k]);
    }    
  } // symbols
  

  gsl_vector_class outv(llr);
  
  //  outv.show();

  // output bitwise LLR
  vout1.DeliverDataObj(outv);
  
  gsl_vector_free(llr);


  // free dynamic structures
  free(num);
  free(den);
  
}
char *oph_gsl_complex_to_polar(UDF_INIT * initid, UDF_ARGS * args, char *result, unsigned long *length, char *is_null, char *error)
{
	if (*error) {
		*length = 0;
		*is_null = 0;
		*error = 1;
		return NULL;
	}
	if (*is_null || !args->lengths[2]) {
		*length = 0;
		*is_null = 1;
		*error = 0;
		return NULL;
	}

	if (!initid->ptr) {
		initid->ptr = (char *) calloc(1, sizeof(oph_string));
		if (!initid->ptr) {
			pmesg(1, __FILE__, __LINE__, "Error allocating result\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		oph_stringPtr output = (oph_stringPtr) initid->ptr;

		initid->extension = (char *) calloc(1, sizeof(oph_string));
		if (!initid->extension) {
			pmesg(1, __FILE__, __LINE__, "Error allocating measure\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		oph_stringPtr measure = (oph_stringPtr) initid->extension;

		core_set_type(measure, args->args[0], &(args->lengths[0]));
		if (measure->type != OPH_COMPLEX_INT && measure->type != OPH_COMPLEX_LONG && measure->type != OPH_COMPLEX_FLOAT && measure->type == OPH_COMPLEX_DOUBLE) {
			pmesg(1, __FILE__, __LINE__, "Invalid input type: complex required\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		measure->length = &(args->lengths[2]);

		if (core_set_elemsize(measure)) {
			pmesg(1, __FILE__, __LINE__, "Error on setting element size\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		if (core_set_numelem(measure)) {
			pmesg(1, __FILE__, __LINE__, "Error on counting result elements\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		core_set_type(output, args->args[1], &(args->lengths[1]));
		if (output->type != OPH_COMPLEX_INT && output->type != OPH_COMPLEX_LONG && output->type != OPH_COMPLEX_FLOAT && output->type != OPH_COMPLEX_DOUBLE) {
			pmesg(1, __FILE__, __LINE__, "Invalid output type: complex required\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		if (core_set_elemsize(output)) {
			pmesg(1, __FILE__, __LINE__, "Error on setting element size\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		output->length = (unsigned long *) calloc(1, sizeof(unsigned long));
		if (!output->length) {
			pmesg(1, __FILE__, __LINE__, "Error allocating length\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		*(output->length) = measure->numelem * output->elemsize;
		output->numelem = measure->numelem;

		output->content = (char *) calloc(1, *(output->length));
		if (!output->content) {
			pmesg(1, __FILE__, __LINE__, "Error allocating result string\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
	}

	oph_stringPtr output = (oph_stringPtr) initid->ptr;
	oph_stringPtr measure = (oph_stringPtr) initid->extension;
	measure->content = args->args[2];

	int i, j = 0;
	gsl_complex z;
	double val1, val2;
	switch (measure->type) {
		case OPH_COMPLEX_INT:
			switch (output->type) {
				case OPH_COMPLEX_INT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((int *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((int *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_LONG:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((int *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((int *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_FLOAT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((int *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((int *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_DOUBLE:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((int *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((int *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				default:
					pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
					*length = 0;
					*is_null = 0;
					*error = 1;
					return NULL;
			}
			break;
		case OPH_COMPLEX_LONG:
			switch (output->type) {
				case OPH_COMPLEX_INT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((long long *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((long long *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_LONG:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((long long *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((long long *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_FLOAT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((long long *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((long long *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_DOUBLE:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((long long *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((long long *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				default:
					pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
					*length = 0;
					*is_null = 0;
					*error = 1;
					return NULL;
			}
			break;
		case OPH_COMPLEX_FLOAT:
			switch (output->type) {
				case OPH_COMPLEX_INT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((float *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((float *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_LONG:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((float *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((float *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_FLOAT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((float *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((float *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_DOUBLE:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = (double) ((float *) (args->args[2]))[i];	//real part
						z.dat[1] = (double) ((float *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				default:
					pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
					*length = 0;
					*is_null = 0;
					*error = 1;
					return NULL;
			}
			break;
		case OPH_COMPLEX_DOUBLE:
			switch (output->type) {
				case OPH_COMPLEX_INT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = ((double *) (args->args[2]))[i];	//real part
						z.dat[1] = ((double *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(int)), OPH_DOUBLE, OPH_INT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_LONG:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = ((double *) (args->args[2]))[i];	//real part
						z.dat[1] = ((double *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(long long)), OPH_DOUBLE, OPH_LONG, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_FLOAT:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = ((double *) (args->args[2]))[i];	//real part
						z.dat[1] = ((double *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(float)), OPH_DOUBLE, OPH_FLOAT, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				case OPH_COMPLEX_DOUBLE:
					for (i = 0; i < output->numelem * 2; i += 2) {
						z.dat[0] = ((double *) (args->args[2]))[i];	//real part
						z.dat[1] = ((double *) (args->args[2]))[i + 1];	//imag part
						val1 = gsl_complex_abs(z);
						val2 = gsl_complex_arg(z);
						if (core_oph_type_cast(&val1, output->content + (j * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						if (core_oph_type_cast(&val2, output->content + ((j + 1) * sizeof(double)), OPH_DOUBLE, OPH_DOUBLE, NULL)) {
							pmesg(1, __FILE__, __LINE__, "Error casting output\n");
							*length = 0;
							*is_null = 0;
							*error = 1;
							return NULL;
						}
						j += 2;
					}
					break;
				default:
					pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
					*length = 0;
					*is_null = 0;
					*error = 1;
					return NULL;
			}
			break;
		default:
			pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
	}

	*length = *(output->length);
	*error = 0;
	*is_null = 0;

	return output->content;
}
Пример #26
0
int
gsl_eigen_genv_sort (gsl_vector_complex * alpha, gsl_vector * beta,
                     gsl_matrix_complex * evec, gsl_eigen_sort_t sort_type)
{
  if (evec->size1 != evec->size2)
    {
      GSL_ERROR ("eigenvector matrix must be square", GSL_ENOTSQR);
    }
  else if (alpha->size != evec->size1 || beta->size != evec->size1)
    {
      GSL_ERROR ("eigenvalues must match eigenvector matrix", GSL_EBADLEN);
    }
  else
    {
      const size_t N = alpha->size;
      size_t i;

      for (i = 0; i < N - 1; i++)
        {
          size_t j;
          size_t k = i;

          gsl_complex ak = gsl_vector_complex_get (alpha, i);
          double bk = gsl_vector_get(beta, i);
          gsl_complex ek;

          if (bk < GSL_DBL_EPSILON)
            {
              GSL_SET_COMPLEX(&ek,
                              GSL_SIGN(GSL_REAL(ak)) ? GSL_POSINF : GSL_NEGINF,
                              GSL_SIGN(GSL_IMAG(ak)) ? GSL_POSINF : GSL_NEGINF);
            }
          else
            ek = gsl_complex_div_real(ak, bk);

          /* search for something to swap */
          for (j = i + 1; j < N; j++)
            {
              int test;
              const gsl_complex aj = gsl_vector_complex_get (alpha, j);
              double bj = gsl_vector_get(beta, j);
              gsl_complex ej;

              if (bj < GSL_DBL_EPSILON)
                {
                  GSL_SET_COMPLEX(&ej,
                                  GSL_SIGN(GSL_REAL(aj)) ? GSL_POSINF : GSL_NEGINF,
                                  GSL_SIGN(GSL_IMAG(aj)) ? GSL_POSINF : GSL_NEGINF);
                }
              else
                ej = gsl_complex_div_real(aj, bj);

              switch (sort_type)
                {       
                case GSL_EIGEN_SORT_ABS_ASC:
                  test = (gsl_complex_abs (ej) < gsl_complex_abs (ek));
                  break;
                case GSL_EIGEN_SORT_ABS_DESC:
                  test = (gsl_complex_abs (ej) > gsl_complex_abs (ek));
                  break;
                case GSL_EIGEN_SORT_VAL_ASC:
                case GSL_EIGEN_SORT_VAL_DESC:
                default:
                  GSL_ERROR ("invalid sort type", GSL_EINVAL);
                }

              if (test)
                {
                  k = j;
                  ek = ej;
                }
            }

          if (k != i)
            {
              /* swap eigenvalues */
              gsl_vector_complex_swap_elements (alpha, i, k);
              gsl_vector_swap_elements (beta, i, k);

              /* swap eigenvectors */
              gsl_matrix_complex_swap_columns (evec, i, k);
            }
        }

      return GSL_SUCCESS;
    }
}
Пример #27
0
void AbsArg(param_ param, abs_info_ *abs_info, gsl_complex *W, double *absW,
            double *argW, gsl_complex *complex_rot) {

    /*
     * In this function, we calculate the abs and arg
     * values for every point on the lattice. We also
     * find the total and max values for the abs across
     * the entire lattice.
     */

    int i, j, tmp_int;
    gsl_complex complex_I;
    double *max_array, tmp;
    int max_num_threads;

    int L=param.L;

    GSL_SET_COMPLEX(&complex_I,0.,1.);

    tmp = 0.;
    tmp_int = 0;
    (*abs_info).max=0.;

    #pragma omp parallel
    {

        /*
         * The optimization here creates an array once in the
         * main thread, of length equal to the total number
         * of threads, and then updates local maxima along
         * the individual threads, storing them in the newly
         * created array. At the end, the local maxima are
         * compared.
         */

        #pragma omp master
        {
            max_num_threads = omp_get_num_threads();
            max_array = (double *)calloc(max_num_threads,sizeof(double));
        }

        int num;
        num=omp_get_thread_num();

        #pragma omp barrier //this barrier ensures the array
        //is visible to all threads

        #pragma omp for reduction(+:tmp)

        for(i=0; i<L*L; i++) {
            absW[i]=gsl_complex_abs(W[i]);
            tmp += absW[i];
            if(absW[i] > max_array[num])
                max_array[num]=absW[i];

            argW[i] = gsl_complex_arg(W[i]);

            complex_rot[i] = gsl_complex_exp(gsl_complex_mul_real(\
                                             complex_I,(-0.5)*argW[i]));
        }

        #pragma omp barrier

        #pragma omp master
        {
            for(i=0; i<max_num_threads; i++)
                if(max_array[i] > (*abs_info).max)
                    (*abs_info).max = max_array[i];

            free(max_array);
        }

    } //end parallel construct

    (*abs_info).total = tmp;

    /*
     * Here, we find the total number of
     * points considered active according to
     * param.p_thresh.
     */

    for(j=0; j<param.pr_range; j++) {
        tmp_int = 0;

        #pragma omp parallel for reduction(+:tmp_int)

        for(i=0; i<L*L; i++)
            if(absW[i] >= param.pr_thresh[j]*(*abs_info).max)
                tmp_int++;

        (*abs_info).int_total[j] = tmp_int;
    }

}
Пример #28
0
void Spectrometer::countAmplitudes_bulk_B(Medium &Osrodek, QString DataName, QProgressBar *Progress, QTextBrowser *Browser, double kx, double ky, double w, int polarisation, double x_lenght, double y_lenght, double precision) {

    double a=Osrodek.itsBasis.getLatticeConstant();

    int RecVec=itsRecVectors;

    int dimension=3*(2*RecVec+1)*(2*RecVec+1);

    std::ofstream plik;
    DataName="results/amplitudes/"+ DataName + ".dat";
    QByteArray   bytes  = DataName.toAscii();
    const char * CDataName = bytes.data();
    plik.open(CDataName);

    //inicjalizacje wektorów i macierzy
    gsl_matrix *gamma=gsl_matrix_calloc(dimension, dimension);
    gsl_matrix *V=gsl_matrix_calloc(dimension, dimension);
    gsl_vector *S=gsl_vector_calloc(dimension);
    gsl_vector *work=gsl_vector_calloc(dimension);


    //gamma dla Podłoża
    /*
    S - numeruje transformaty tensora sprężystoœci i gestosci
    i - numeruje wiersze macierzy
    j - numeruje kolumny macierzy
    */
    for(int Nx=-RecVec, i=0, S=0; Nx<=RecVec; Nx++) {
        for(int Ny=-RecVec; Ny<=RecVec; Ny++, i=i+3) {
            for(int Nx_prim=-RecVec, j=0; Nx_prim<=RecVec; Nx_prim++) {
                for(int Ny_prim=-RecVec; Ny_prim<=RecVec; Ny_prim++, j=j+3, S++) {

                    double Elasticity[6][6];
                    itsRecBasisSubstance[S].getElasticity(Elasticity);
                    double Density=itsRecBasisSubstance[S].getDensity();
                    double gx=2*M_PI*Nx/a;
                    double gy=2*M_PI*Ny/a;
                    double gx_prim=2*M_PI*Nx_prim/a;
                    double gy_prim=2*M_PI*Ny_prim/a;

                    gsl_matrix_set(gamma, i, j, Elasticity[0][0]*(kx+gx)*(kx+gx_prim)+Elasticity[3][3]*(ky+gy)*(ky+gy_prim)-Density*w*w);
                    gsl_matrix_set(gamma, i+1, j, Elasticity[0][1]*(kx+gx_prim)*(ky+gy)+Elasticity[3][3]*(ky+gy_prim)*(kx+gx));
                    gsl_matrix_set(gamma, i, j+1, Elasticity[0][1]*(ky+gy_prim)*(kx+gx)+Elasticity[3][3]*(kx+gx_prim)*(ky+gy));
                    gsl_matrix_set(gamma, i+1, j+1, Elasticity[0][0]*(ky+gy_prim)*(ky+gy)+Elasticity[3][3]*(kx+gx_prim)*(kx+gx)-Density*w*w);
                    gsl_matrix_set(gamma, i+2, j+2, Elasticity[3][3]*(ky+gy_prim)*(ky+gy)+Elasticity[3][3]*(kx+gx_prim)*(kx+gx)-Density*w*w);

                }
            }
        }
    }

    //rozwiązanie układu równań
    gsl_linalg_SV_decomp(gamma, V, S, work);

    gsl_complex u;
    double Gx, Gy;

    if (polarisation==3) {

        gsl_complex ux, uy, uz;

        for(double x=0; x < x_lenght; x=x+precision) {
            for(double y=0; y < y_lenght; y=y+precision) {
                ux = gsl_complex_rect(0, 0);
                uy = gsl_complex_rect(0, 0);
                uz = gsl_complex_rect(0, 0);

                //pasek postepu
                int postep=int(100*x/x_lenght);
                Progress->setValue(postep);
                Progress->update();
                QApplication::processEvents();

                for (int Nx=-RecVec, i=0; Nx <= RecVec; Nx++) {
                    for (int Ny=-RecVec; Ny <= RecVec; Ny++, i=i+3) {

                        Gx=2*M_PI*Nx/a;
                        Gy=2*M_PI*Ny/a;

                        double Ax = gsl_matrix_get(V, i, dimension-1);
                        double Ay = gsl_matrix_get(V, i+1, dimension-1);
                        double Az = gsl_matrix_get(V, i+2, dimension-1);

                        gsl_complex expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                        gsl_complex exp1 = gsl_complex_exp(expin1);

                        gsl_complex multiply = gsl_complex_mul_real(exp1, Ax);
                        ux = gsl_complex_add(ux, multiply);

                        expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                        exp1 = gsl_complex_exp(expin1);

                        multiply = gsl_complex_mul_real(exp1, Ay);
                        uy = gsl_complex_add(uy, multiply);

                        expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                        exp1 = gsl_complex_exp(expin1);

                        multiply = gsl_complex_mul_real(exp1, Az);
                        uz = gsl_complex_add(uz, multiply);
                    }
                }

                double U = sqrt(gsl_complex_abs(ux)*gsl_complex_abs(ux)+gsl_complex_abs(uy)*gsl_complex_abs(uy)+gsl_complex_abs(uz)*gsl_complex_abs(uz));

                //zapisanie wartości do pliku
                QString nazwa = Osrodek.itsBasis.getFillingSubstance().getName();
                if(nazwa=="Air")
                {
                    double rad=Osrodek.itsBasis.getRadius();
                    if(sqrt(x*x+y*y) > rad && sqrt((a-x)*(a-x)+y*y) > rad && sqrt((a-x)*(a-x)+(a-y)*(a-y)) > rad && sqrt((a-y)*(a-y)+x*x) > rad)
                    {
                        plik << x << "\t" << y << "\t" << U << "\n";
                    }
                } else {
                    plik << x << "\t" << y << "\t" << U << "\n";
                }

            }



            plik << "\n";
        }

    } else if (polarisation==4) {

            gsl_complex uxx, uxy, uxz, uyx, uyy, uyz, uzx, uzy, uzz;
            double Uxx, Uxy, Uxz, Uyx, Uyy, Uyz, Uzx, Uzy, Uzz;

            for(double x=0; x < x_lenght; x=x+precision) {
                for(double y=0; y < y_lenght; y=y+precision) {

                    uxx = gsl_complex_rect(0, 0);
                    uxy = gsl_complex_rect(0, 0);
                    uxz = gsl_complex_rect(0, 0);
                    uyx = gsl_complex_rect(0, 0);
                    uyy = gsl_complex_rect(0, 0);
                    uyz = gsl_complex_rect(0, 0);
                    uzx = gsl_complex_rect(0, 0);
                    uzy = gsl_complex_rect(0, 0);
                    uzz = gsl_complex_rect(0, 0);

                    double rad=Osrodek.itsBasis.getRadius();
                    double Ela[6][6];
                    if(sqrt(x*x+y*y) > rad && sqrt((a-x)*(a-x)+y*y) > rad && sqrt((a-x)*(a-x)+(a-y)*(a-y)) > rad && sqrt((a-y)*(a-y)+x*x) > rad)
                    {
                        Osrodek.itsBasis.getSubstance().getElasticity(Ela);
                    } else {
                        Osrodek.itsBasis.getFillingSubstance().getElasticity(Ela);
                    }

                    //pasek postepu
                    int postep=int(100*x/x_lenght);
                    Progress->setValue(postep);
                    Progress->update();
                    QApplication::processEvents();

                    for (int Nx=-RecVec, i=0; Nx <= RecVec; Nx++) {
                        for (int Ny=-RecVec; Ny <= RecVec; Ny++, i=i+3) {

                            Gx=2*M_PI*Nx/a;
                            Gy=2*M_PI*Ny/a;

                            double Ax = gsl_matrix_get(V, i, dimension-1);
                            double Ay = gsl_matrix_get(V, i+1, dimension-1);
                            double Az = gsl_matrix_get(V, i+2, dimension-1);

                            gsl_complex expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                            gsl_complex exp1 = gsl_complex_exp(expin1);

                            gsl_complex multiply = gsl_complex_mul_real(exp1, Ax);

                            //uxx
                            gsl_complex multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), kx+Gx));
                            uxx = gsl_complex_add(uxx, multidiff);

                            //uxy
                            multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), ky+Gy));
                            uxy = gsl_complex_add(uxy, multidiff);


                            expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                            exp1 = gsl_complex_exp(expin1);

                            //uy
                            multiply = gsl_complex_mul_real(exp1, Ay);

                            //uyx
                            multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), kx+Gx));
                            uyx = gsl_complex_add(uyx, multidiff);

                            //uyy
                            multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), ky+Gy));
                            uyy = gsl_complex_add(uyy, multidiff);

                            expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                            exp1 = gsl_complex_exp(expin1);

                            multiply = gsl_complex_mul_real(exp1, Az);

                            //uzx
                            multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), kx+Gx));
                            uzx = gsl_complex_add(uzx, multidiff);

                            //uzy
                            multidiff = gsl_complex_mul(multiply, gsl_complex_mul_real(gsl_complex_rect(0,1), ky+Gy));
                            uzy = gsl_complex_add(uzy, multidiff);
                        }
                    }

                    Uxx=gsl_complex_abs(uxx);
                    Uxy=gsl_complex_abs(uxy);
                    Uxz=gsl_complex_abs(uxz);
                    Uyx=gsl_complex_abs(uyx);
                    Uyy=gsl_complex_abs(uyy);
                    Uyz=gsl_complex_abs(uyz);
                    Uzx=gsl_complex_abs(uzx);
                    Uzy=gsl_complex_abs(uzy);
                    Uzz=gsl_complex_abs(uzz);

                    double U = Ela[0][0]*(Uxx*Uxx+Uyy*Uyy+Uzz*Uzz)+2*Ela[0][1]*(Uxx*Uyy+Uxx*Uzz+Uyy*Uzz)+0.25*Ela[3][3]*((Uyz+Uzy)*(Uyz+Uzy)+(Uxz+Uzx)*(Uxz+Uzx)+(Uxy+Uyx)*(Uxy+Uyx));

                    //zapisanie wartości do pliku
                    QString nazwa = Osrodek.itsBasis.getFillingSubstance().getName();
                    if(nazwa=="Air")
                    {
                        double rad=Osrodek.itsBasis.getRadius();
                        if(sqrt(x*x+y*y) > rad && sqrt((a-x)*(a-x)+y*y) > rad && sqrt((a-x)*(a-x)+(a-y)*(a-y)) > rad && sqrt((a-y)*(a-y)+x*x) > rad)
                        {
                            plik << x << "\t" << y << "\t" << U << "\n";
                        }
                    } else {
                        plik << x << "\t" << y << "\t" << U << "\n";
                    }

                }



                plik << "\n";
            }


    } else {

        for(double x=0; x < x_lenght; x=x+precision) {
            for(double y=0; y < y_lenght; y=y+precision) {
                u = gsl_complex_rect(0, 0);

                //pasek postepu
                int postep=int(100*x/x_lenght);
                Progress->setValue(postep);
                Progress->update();
                QApplication::processEvents();

                for (int Nx=-RecVec, i=0; Nx <= RecVec; Nx++) {
                    for (int Ny=-RecVec; Ny <= RecVec; Ny++, i=i+3) {

                        Gx=2*M_PI*Nx/a;
                        Gy=2*M_PI*Ny/a;

                        double A = gsl_matrix_get(V, i+polarisation, dimension-1);

                        gsl_complex expin1 = gsl_complex_rect(0, (kx+Gx)*x+(ky+Gy)*y);
                        gsl_complex exp1 = gsl_complex_exp(expin1);

                        gsl_complex multiply = gsl_complex_mul_real(exp1, A);
                        u = gsl_complex_add(u, multiply);
                    }
                }

                double U = gsl_complex_abs(u);

                //zapisanie wartości do pliku
                plik << x << "\t" << y << "\t" << U << "\n";

            }



            plik << "\n";
        }

    }


    plik.close();
    gsl_matrix_free(gamma);
    gsl_matrix_free(V);
    gsl_vector_free(S);
    gsl_vector_free(work);
}
Пример #29
0
int
gsl_eigen_nonsymmv_sort (gsl_vector_complex * eval,
                         gsl_matrix_complex * evec, 
                         gsl_eigen_sort_t sort_type)
{
  if (evec && (evec->size1 != evec->size2))
    {
      GSL_ERROR ("eigenvector matrix must be square", GSL_ENOTSQR);
    }
  else if (evec && (eval->size != evec->size1))
    {
      GSL_ERROR ("eigenvalues must match eigenvector matrix", GSL_EBADLEN);
    }
  else
    {
      const size_t N = eval->size;
      size_t i;

      for (i = 0; i < N - 1; i++)
        {
          size_t j;
          size_t k = i;

          gsl_complex ek = gsl_vector_complex_get (eval, i);

          /* search for something to swap */
          for (j = i + 1; j < N; j++)
            {
              int test;
              const gsl_complex ej = gsl_vector_complex_get (eval, j);

              switch (sort_type)
                {       
                case GSL_EIGEN_SORT_ABS_ASC:
                  test = (gsl_complex_abs (ej) < gsl_complex_abs (ek));
                  break;
                case GSL_EIGEN_SORT_ABS_DESC:
                  test = (gsl_complex_abs (ej) > gsl_complex_abs (ek));
                  break;
                case GSL_EIGEN_SORT_VAL_ASC:
                  test = complex_less(ej, ek);
                  break;
                case GSL_EIGEN_SORT_VAL_DESC:
                  test = complex_less(ek, ej);
                  break;
                default:
                  GSL_ERROR ("invalid sort type", GSL_EINVAL);
                }

              if (test)
                {
                  k = j;
                  ek = ej;
                }
            }

          if (k != i)
            {
              /* swap eigenvalues */
              gsl_vector_complex_swap_elements (eval, i, k);

              /* swap eigenvectors */
              if (evec)
                gsl_matrix_complex_swap_columns (evec, i, k);
            }
        }

      return GSL_SUCCESS;
    }
}
Пример #30
0
 double complex::abs() const
 {
     return gsl_complex_abs(_complex);
 }