Example #1
0
/// Resize the vector
/// @param n :: The new length
void ComplexVector::resize(const size_t n) {
  auto oldVector = m_vector;
  m_vector = gsl_vector_complex_alloc(n);
  size_t m = oldVector->size < n ? oldVector->size : n;
  for (size_t i = 0; i < m; ++i) {
    gsl_vector_complex_set(m_vector, i, gsl_vector_complex_get(oldVector, i));
  }
  for (size_t i = m; i < n; ++i) {
    gsl_vector_complex_set(m_vector, i, gsl_complex{{0, 0}});
  }
  gsl_vector_complex_free(oldVector);
}
Example #2
0
vectorc operator*(const gsl_complex n, const vectorc & vec)
{
	vectorc result(vec.size_m);
	for (int i=0; i<vec.size_m; i++)
		gsl_vector_complex_set(result.v_m, i, gsl_complex_mul(n,gsl_vector_complex_get(vec.v_m,i)));
	return result;	
}
Example #3
0
vectorc vectorc::operator*(const gsl_complex n) const
{
	vectorc result(size_m);
	for (int i=0; i<size_m; i++)
		gsl_vector_complex_set(result.v_m, i, gsl_complex_mul(n,gsl_vector_complex_get(v_m,i)));
	return result;
}
Example #4
0
static VALUE Vector_complex_new(VALUE klass, VALUE arg) {
  gsl_vector_complex * v = NULL;
  size_t i, n, dim;
  switch (TYPE(arg)) {
    case T_FIXNUM:
    case T_BIGNUM:
      dim = NUM2INT(arg);
      v = gsl_vector_complex_calloc(dim);
      break;
    case T_ARRAY:
      n = RARRAY(arg)->len;
      dim = n/2;
      v = gsl_vector_complex_alloc(dim);
      for (i = 0; i < dim; i++){
        double d0, d1;
        gsl_complex z;
        d0 = NUM2DBL(rb_ary_entry(arg, 2*i));
        d1 = NUM2DBL(rb_ary_entry(arg, 2*i+1));
        GSL_SET_COMPLEX(&z, d0, d1);
        gsl_vector_complex_set(v, i, z);
      }
      break;
    default:
      rb_raise(rb_eArgError, "Illegal argument for constructor");
  }

  return Data_Wrap_Struct(klass, 0, gsl_vector_complex_free, v);
}
Example #5
0
VALUE rb_gsl_math_complex_eval(gsl_complex (*func)(gsl_complex), VALUE obj)
{
  gsl_complex *z, *znew;
  gsl_vector_complex *v, *vnew;
  gsl_matrix_complex *m, *mnew;
  size_t i, j;
  if (COMPLEX_P(obj)) {
    Data_Get_Struct(obj, gsl_complex, z);
    znew = xmalloc(sizeof(gsl_complex));
    *znew = (*func)(*z);
    return Data_Wrap_Struct(cgsl_complex, 0, free, znew);
  } else if (VECTOR_COMPLEX_P(obj)) {
    Data_Get_Struct(obj, gsl_vector_complex, v);
    vnew = gsl_vector_complex_alloc(v->size);
    for (i = 0; i < v->size; i++) {
      z = GSL_COMPLEX_AT(v, i);
      gsl_vector_complex_set(vnew, i, (*func)(*z));
    }
    return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
  } else if (MATRIX_COMPLEX_P(obj)) {
    Data_Get_Struct(obj, gsl_matrix_complex, m);
    mnew = gsl_matrix_complex_alloc(m->size1, m->size2);
    for (i = 0; i < m->size1; i++) {
      for (j = 0; j < m->size2; j++) {
        gsl_matrix_complex_set(mnew, i, j, (*func)(gsl_matrix_complex_get(m, i, j)));
      }
    }
    return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew);
  } else {
    rb_raise(rb_eTypeError,
             "wrong argument type %s "
             " (GSL::Complex or GSL::Vector::Complex expected)",
             rb_class2name(CLASS_OF(obj)));
  }
}
Example #6
0
int
lls_complex_btransform(const gsl_vector *L, gsl_vector_complex *c,
                       lls_complex_workspace *w)
{
  size_t p = c->size;

  if (L->size != p)
    {
      GSL_ERROR("L and c vectors have different sizes", GSL_EBADLEN);
    }
  else
    {
      size_t i;

      for (i = 0; i < p; ++i)
        {
          gsl_complex ci = gsl_vector_complex_get(c, i);
          double Li = gsl_vector_get(L, i);

          if (Li == 0.0)
            {
              GSL_ERROR("L matrix is singular", GSL_ESING);
            }
          
          gsl_vector_complex_set(c, i, gsl_complex_div_real(ci, Li));
        }

      return GSL_SUCCESS;
    }
} /* lls_complex_btransform() */
Example #7
0
 vector<complex>::vector(const vector<double>& v)
 {
     size_t i, n;
     n = v.size();
     _vector = gsl_vector_complex_alloc(n);
     for (i = 0; i < n; i++)
         gsl_vector_complex_set(_vector, i, v(i) + 0. * complex::i());
 }
Example #8
0
void vectorc::set_values(double *vals) const
{
	gsl_complex z;
	for (int i=0; i<size_m; i++)
		{
			GSL_SET_COMPLEX(&z, vals[2*i], vals[2*i+1]);
			gsl_vector_complex_set(v_m, i, z);
		}
}
Example #9
0
static VALUE Vector_complex_set1(VALUE obj, VALUE vi, VALUE va)
{
  gsl_vector_complex * v;
  gsl_complex * pz;
  Data_Get_Struct(va, gsl_complex, pz);
  Data_Get_Struct(obj, gsl_vector_complex, v);
  gsl_vector_complex_set(v, NUM2INT(vi), *pz);
  return obj;
}
Example #10
0
static CVECTOR *VECTOR_convert_to_complex(CVECTOR *_object)
{
	CVECTOR *v = VECTOR_create(SIZE(THIS), TRUE, FALSE);
	int i;
	
	for (i = 0; i < SIZE(THIS); i++)
		gsl_vector_complex_set((gsl_vector_complex *)v->vector, i, gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0));
	
	return v;
}
Example #11
0
vectorc::vectorc(const double values[], const int size) : size_m(size), v_m(gsl_vector_complex_alloc(size))

{
	gsl_complex z;
	for (int i=0; i<size_m; i++)
	{
		GSL_SET_COMPLEX(&z, values[2*i], values[2*i+1]);
		gsl_vector_complex_set(v_m, i, z);
	}	
}
Example #12
0
vectorc vectorc::operator=(const vectorc& vec)
{
	if (this==&vec)
		return *this;
	gsl_vector_complex_free (v_m);
	size_m=vec.size_m;
	v_m=gsl_vector_complex_alloc(size_m);
	for (int i=0; i<size_m; i++)
		gsl_vector_complex_set(v_m, i, gsl_vector_complex_get(vec.v_m, i));
	return *this;
}
Example #13
0
static void
cholesky_complex_conj_vector(gsl_vector_complex *v)
{
  size_t i;

  for (i = 0; i < v->size; ++i)
    {
      gsl_complex z = gsl_vector_complex_get(v, i);
      gsl_vector_complex_set(v, i, gsl_complex_conjugate(z));
    }
} /* cholesky_complex_conj_vector() */
Example #14
0
/* Given a variable-length argument list consisting of 1 or more
 * complex numbers, create a vector with size = argc
 */
static gsl_vector_complex * alloc_vector_from_complex(int argc, VALUE * argv)
{
  gsl_vector_complex * v = NULL;
  gsl_complex * pz;
  int n, i;
  n = argc;
  v = gsl_vector_complex_alloc(n);
  for (i = 0; i < n; i++) {
    Data_Get_Struct(argv[i], gsl_complex, pz);
    gsl_vector_complex_set(v, i, *pz);
  }
  return v;
}
Example #15
0
/// set an element
/// @param i :: The element index
/// @param value :: The new value
void ComplexVector::set(size_t i, const ComplexType &value) {
  if (i < m_vector->size) {
    gsl_vector_complex_set(m_vector, i,
                           gsl_complex{{value.real(), value.imag()}});

  } else {
    std::stringstream errmsg;
    errmsg << "ComplexVector index = " << i
           << " is out of range = " << m_vector->size
           << " in ComplexVector.set()";
    throw std::out_of_range(errmsg.str());
  }
}
Example #16
0
int
print_Lcurve(const char *filename, poltor_workspace *w)
{
  int s = 0;
  FILE *fp;
  const size_t p = w->p;
  double rnorm, Lnorm;
  gsl_vector_complex_view v = gsl_vector_complex_subvector(w->rhs, 0, p);
  size_t i;

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

  /* construct A and b, and calculate chi^2 = ||b - A c||^2 */
  poltor_build_ls(0, w);
  rnorm = sqrt(w->chisq);

  /* compute v = L c; L is stored in w->L by poltor_solve() */
  for (i = 0; i < p; ++i)
    {
      gsl_complex ci = gsl_vector_complex_get(w->c, i);
      double li = gsl_vector_get(w->L, i);
      gsl_complex val = gsl_complex_mul_real(ci, li);

      gsl_vector_complex_set(&v.vector, i, val);
    }

  /* compute || L c || */
  Lnorm = gsl_blas_dznrm2(&v.vector);

  fprintf(fp, "%.12e %.12e %.6e %.6e %.6e\n",
          log(rnorm),
          log(Lnorm),
          w->alpha_int,
          w->alpha_sh,
          w->alpha_tor);

  printcv_octave(w->residuals, "r");
  printcv_octave(w->c, "c");
  printv_octave(w->L, "L");

  fclose(fp);

  return s;
} /* print_Lcurve() */
Example #17
0
void PSKMapper::Run() {

  unsigned int nbits,nsymbs,count;


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

  // number of input bits
  nbits = input.vec->size;

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

  gsl_vector_complex *tmp=gsl_vector_complex_alloc(nsymbs);

  count=0;

  for (int n=0; n<nsymbs; n++) {

    symbol_id=0;

    //////// I take Nb bits from input and map it in new_symbol

    for (int i=0;i<Nb();i++) {
      symbol_id = (symbol_id << 1);
      symbol_id += gsl_vector_uint_get(input.vec,count++);
    }



    new_symbol = gsl_complex_polar(1.0,
				   symbol_arg * 
				   double(gsl_vector_uint_get(gray_encoding, 
							      symbol_id)));

    gsl_vector_complex_set(tmp,n,new_symbol);
   }


  // show output
  //  gsl_vector_complex_show(tmp);

  // deliver data
  vout1.DeliverDataObj(gsl_vector_complex_class(tmp));

  gsl_vector_complex_free(tmp);
 
}
Example #18
0
void VECTOR_ensure_complex(CVECTOR *_object)
{
	gsl_vector_complex *v;
	int size = SIZE(THIS);
	int i;
	
	if (COMPLEX(THIS))
		return;
	
	v = gsl_vector_complex_alloc(size);
	for (i = 0; i < size; i++)
		gsl_vector_complex_set(v, i, gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0));
	
	gsl_vector_free(VEC(THIS));
	THIS->vector = v;
	THIS->complex = TRUE;
}
Example #19
0
static VALUE rb_gsl_vector_int_to_complex(VALUE obj)
{
  gsl_vector_int *v;
  gsl_vector_complex *vnew;
  gsl_complex z;
  size_t i;
  Data_Get_Struct(obj, gsl_vector_int, v);
  vnew = gsl_vector_complex_alloc(v->size);
  for (i = 0; i < v->size; i++) {
    GSL_SET_REAL(&z, (double) gsl_vector_int_get(v, i));
    GSL_SET_IMAG(&z, 0.0);
    gsl_vector_complex_set(vnew, i, z);
  }
  if (VECTOR_INT_COL_P(obj))
    return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, vnew);
  else
    return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
}
Example #20
0
void MCPMPCoeffs::GeoUpdate(double seconds) {

	for (int i=0;i<2*M();i++) {



		gsl_complex v = gsl_vector_complex_get(geoVelocities,i); // expressed in deltalon/s,deltalat/s
		gsl_complex p = gsl_vector_complex_get(geoPositions,i); // expressed in lon,lat
		gsl_complex np = gsl_complex_add(p,gsl_complex_mul_real(v, seconds));

		gsl_vector_complex_set(geoPositions,i,np);

		//   cout << "node " << i << " position = " << GSL_IMAG(np) << ", " << GSL_REAL(np) << endl;

	}



}
Example #21
0
static VALUE Vector_complex_new2(int argc, VALUE * argv, VALUE klass)
{
  gsl_vector_complex * v = NULL;
  int type0;
  type0 = TYPE(argv[0]);

  if (type0 == T_FIXNUM || type0 == T_BIGNUM) {
    gsl_complex * pz;
    VALUE vz;
    int i, n;
    n = NUM2INT(argv[0]);
    Data_Get_Struct(argv[1], gsl_complex, pz);
    vz = Complex_new_intern(rbgsl_cComplex, pz);
    v = gsl_vector_complex_alloc(n);
    for (i = 0; i < n; i++)
      gsl_vector_complex_set(v, i, *pz);
    return Data_Wrap_Struct(klass, 0, gsl_vector_complex_free, v);
    }
  v = alloc_vector_from_complex(argc, argv);
  return Data_Wrap_Struct(klass, 0, gsl_vector_complex_free, v);
}
Example #22
0
File: test.c Project: lemahdi/mglib
void
create_random_complex_posdef_matrix(gsl_matrix_complex *m, gsl_rng *r,
                                    gsl_vector_complex *work)
{
  const size_t N = m->size1;
  size_t i, j;
  double x, y;
  gsl_complex z;
  gsl_complex tau;

  GSL_SET_IMAG(&z, 0.0);

  /* make a positive diagonal matrix */
  gsl_matrix_complex_set_zero(m);
  for (i = 0; i < N; ++i)
    {
      x = gsl_rng_uniform(r);
      GSL_SET_REAL(&z, x);
      gsl_matrix_complex_set(m, i, i, z);
    }

  /* now generate random householder reflections and form P D P^H */
  for (i = 0; i < N; ++i)
    {
      /* form complex vector */
      for (j = 0; j < N; ++j)
        {
          x = 2.0 * gsl_rng_uniform(r) - 1.0;
          y = 2.0 * gsl_rng_uniform(r) - 1.0;
          GSL_SET_COMPLEX(&z, x, y);
          gsl_vector_complex_set(work, j, z);
        }

      tau = gsl_linalg_complex_householder_transform(work);
      gsl_linalg_complex_householder_hm(tau, work, m);
      gsl_linalg_complex_householder_mh(gsl_complex_conjugate(tau), work, m);
    }
} /* create_random_complex_posdef_matrix() */
void LALInferenceTemplateROQ(LALInferenceModel *model)
{
double m1,m2,mc,eta,q,iota=0;
/* Prefer m1 and m2 if available (i.e. for injection template) */
  if(LALInferenceCheckVariable(model->params,"mass1")&&LALInferenceCheckVariable(model->params,"mass2"))
  {
    m1=*(REAL8 *)LALInferenceGetVariable(model->params,"mass1");
    m2=*(REAL8 *)LALInferenceGetVariable(model->params,"mass2");
    eta=m1*m2/((m1+m2)*(m1+m2));
    mc=pow(eta , 0.6)*(m1+m2);
  }
  else
  {
    if (LALInferenceCheckVariable(model->params,"q")) {
      q = *(REAL8 *)LALInferenceGetVariable(model->params,"q");
      q2eta(q, &eta);
    }
    else
      eta = *(REAL8*) LALInferenceGetVariable(model->params, "eta");
    mc       = *(REAL8*) LALInferenceGetVariable(model->params, "chirpmass");
    mc2masses(mc, eta, &m1, &m2);
  }

  iota = acos(LALInferenceGetREAL8Variable(model->params, "costheta_jn"));     /* zenith angle between J and N in radians */

  double cosiota = cos(iota);
  double plusCoef  = 0.5 * (1.0 + cosiota*cosiota);
  double crossCoef = cosiota;
  /* external: SI; internal: solar masses */
  const REAL8 m = m1 + m2;
  const REAL8 m_sec = m * LAL_MTSUN_SI;  /* total mass in seconds */
  const REAL8 etap2 = eta * eta;
  const REAL8 etap3 = etap2 * eta;
  const REAL8 piM = LAL_PI * m_sec;
  const REAL8 mSevenBySix = -7./6.;
  const REAL8 phic = *(REAL8 *)LALInferenceGetVariable(model->params,"phase");
  const REAL8 r = 1e6*LAL_PC_SI;
  REAL8 logv0 = log(1.); //the standard tf2 definition is log(v0), but I've changed it to reflect Scott's convention
  REAL8 shft, amp0;//, f_max;
  REAL8 psiNewt, psi2, psi3, psi4, psi5, psi6, psi6L, psi7, psi3S, psi4S, psi5S;
  REAL8 eta_fac = -113. + 76. * eta;
  REAL8 chi=0; //NOTE: chi isn't used here yet, so we just set it to zero
  gsl_complex h_i;

  /* extrinsic parameters */
  amp0 = -pow(m_sec, 5./6.) * sqrt(5.*eta / 24.) / (Pi_p2by3 * r / LAL_C_SI);
  shft = 0;//LAL_TWOPI * (tStart.gpsSeconds + 1e-9 * tStart.gpsNanoSeconds);

  /* spin terms in the amplitude and phase (in terms of the reduced
   * spin parameter */
  psi3S = 113.*chi/3.;
  psi4S = 63845.*(-81. + 4.*eta)*chi*chi/(8. * eta_fac * eta_fac);
  psi5S = -565.*(-146597. + 135856.*eta + 17136.*etap2)*chi/(2268.*eta_fac);

  /* coefficients of the phase at PN orders from 0 to 3.5PN */
  psiNewt = 3./(128.*eta);
  psi2 = 3715./756. + 55.*eta/9.;
  psi3 = psi3S - 16.*LAL_PI;
  psi4 = 15293365./508032. + 27145.*eta/504. + 3085.*eta*eta/72. + psi4S;
  psi5 = (38645.*LAL_PI/756. - 65.*LAL_PI*eta/9. + psi5S);
  psi6 = 11583231236531./4694215680. - (640.*Pi_p2)/3. - (6848.*LAL_GAMMA)/21.
           + (-5162.983708047263 + 2255.*Pi_p2/12.)*eta
           + (76055.*etap2)/1728. - (127825.*etap3)/1296.;
  psi6L = -6848./21.;
  psi7 = (77096675.*LAL_PI)/254016. + (378515.*LAL_PI*eta)/1512.
           - (74045.*LAL_PI*eta*eta)/756.;

  for (unsigned int i = 0; i < model->roq->frequencyNodes->size; i++) {
    /* fourier frequency corresponding to this bin */
    const REAL8 f = gsl_vector_get(model->roq->frequencyNodes, i);
    const REAL8 v3 = piM*f;

    /* PN expansion parameter */
    REAL8 v, v2, v4, v5, v6, v7, logv, Psi, amp;
    v = cbrt(v3);
    v2 = v*v; v4 = v3*v; v5 = v4*v; v6 = v3*v3; v7 = v6*v;
    logv = log(v);

    /* compute the phase and amplitude */
    Psi = psiNewt / v5 * (1.
         + psi2 * v2 + psi3 * v3 + psi4 * v4
         + psi5 * v5 * (1. + 3. * (logv - logv0))
         + (psi6 + psi6L * (log4 + logv)) * v6 + psi7 * v7);

    amp = amp0 * pow(f, mSevenBySix);

    GSL_SET_COMPLEX(&h_i, amp * cos(Psi + shft * f - 2.*phic - LAL_PI_4), - amp * sin(Psi + shft * f - 2.*phic - LAL_PI_4));

    gsl_vector_complex_set(model->roq->hplus, i, gsl_complex_mul_real(h_i,plusCoef));
    gsl_vector_complex_set(model->roq->hcross, i, gsl_complex_mul_real(gsl_complex_mul_imag(h_i,-1.0),crossCoef));

  }
	return;
}
Example #24
0
void MCPMPChan::Run() {

  /// fetch data objects
  gsl_matrix_complex inmat =  min1.GetDataObj();
  gsl_matrix_complex cmat  =  min2.GetDataObj();

  // inmat : input signal matrix x(n) (NxM)
  //                              i
  // complex sample at time n from Tx number i

  // cmat : channel coeffs matrix h(n) (M**2xN)
  //                               ij 
  // cmat matrix structure
  //
  //   +-                 -+
  //   | h(0) . . . . h(n) | |
  //   |  11           11  | |
  //   |                   | | Rx1
  //   | h(0) . . . . h(n) | |
  //   |  12           12  | |
  //   |                   |
  //   | h(0) . . . . h(n) | |
  //   |  21           21  | |
  //   |                   | | Rx2
  //   | h(0) . . . . h(n) | |
  //   |  22           22  | |
  //   +-                 -+
  // 
  //   where h(n) represents the channel impulse response
  //          ij
  //
  //   at time n, from tx i to rx j
  //   the matrix has MxM rows and N comumns.
  //   The (i,j) channel is locater at row i*M+j
  //   with i,j in the range [0,M-1] and rows counting from 0
  //
  //
  gsl_matrix_complex_set_zero(outmat);

  for (int rx=0;rx<M();rx++) { //loop through Rx

    //
    // csubmat creates a view on cmat extracting the MxN submatrix for Rx number u
    //
    gsl_matrix_complex_const_view csubmat = gsl_matrix_complex_const_submatrix(&cmat,rx*M(),0,M(),N());

    //
    // cut a slice of outmat
    //
    gsl_vector_complex_view outvec = gsl_matrix_complex_column(outmat,rx);

    for (int tx=0;tx<M();tx++) { // loop through Tx

      //
      // input signal from tx
      //
      gsl_vector_complex_view x = gsl_matrix_complex_column(&inmat,tx);
      gsl_vector_complex *tmp = gsl_vector_complex_alloc(N());

      //
      //
      // extract the current tx-rx channel matrix
      //
      //
      for (int i=0; i<N(); i++) {
	gsl_complex h = gsl_matrix_complex_get(&csubmat.matrix,tx,(N()-i)%N());
	for (int j=0; j<N(); j++) {
	  gsl_matrix_complex_set(user_chan,j,(j+i) % N(),h);
	}
      }


      // cout << "Channel (" << tx << "-" << rx << "):" << endl;
      // gsl_matrix_complex_show(user_chan);
      
      //
      // compute the signal rx = H tx 
      //   
      gsl_blas_zgemv(CblasNoTrans, 
		     gsl_complex_rect(1.0,0), 
		     user_chan, 
		     &x.vector,
		     gsl_complex_rect(0,0),
		     tmp);

      //
      // sum for each tx
      //
      gsl_vector_complex_add(&outvec.vector,tmp);

      gsl_vector_complex_free(tmp);
      
    } // tx loop 

    for (int i=0; i< N(); i++) {
      gsl_complex noisesample = gsl_complex_rect( gsl_ran_gaussian(ran,noisestd),
						  gsl_ran_gaussian(ran,noisestd));
      gsl_complex ctmp = gsl_complex_add(gsl_vector_complex_get(&outvec.vector,i),noisesample);
      gsl_vector_complex_set(&outvec.vector,i,ctmp);
    }
    
    
  } // rx loop
  
  // cout << "received signals matrix (" << N() << "x" << M() << ")" << endl;
  // gsl_matrix_complex_show(outmat);
  

  //////// production of data
  mout1.DeliverDataObj( *outmat );
  
}
Example #25
0
static void
nonsymmv_get_right_eigenvectors(gsl_matrix *T, gsl_matrix *Z,
                                gsl_vector_complex *eval,
                                gsl_matrix_complex *evec,
                                gsl_eigen_nonsymmv_workspace *w)
{
  const size_t N = T->size1;
  const double smlnum = GSL_DBL_MIN * N / GSL_DBL_EPSILON;
  const double bignum = (1.0 - GSL_DBL_EPSILON) / smlnum;
  int i;              /* looping */
  size_t iu,          /* looping */
         ju,
         ii;
  gsl_complex lambda; /* current eigenvalue */
  double lambda_re,   /* Re(lambda) */
         lambda_im;   /* Im(lambda) */
  gsl_matrix_view Tv, /* temporary views */
                  Zv;
  gsl_vector_view y,  /* temporary views */
                  y2,
                  ev,
                  ev2;
  double dat[4],      /* scratch arrays */
         dat_X[4];
  double scale;       /* scale factor */
  double xnorm;       /* |X| */
  gsl_vector_complex_view ecol, /* column of evec */
                          ecol2;
  int complex_pair;   /* complex eigenvalue pair? */
  double smin;

  /*
   * Compute 1-norm of each column of upper triangular part of T
   * to control overflow in triangular solver
   */

  gsl_vector_set(w->work3, 0, 0.0);
  for (ju = 1; ju < N; ++ju)
    {
      gsl_vector_set(w->work3, ju, 0.0);
      for (iu = 0; iu < ju; ++iu)
        {
          gsl_vector_set(w->work3, ju,
                         gsl_vector_get(w->work3, ju) +
                         fabs(gsl_matrix_get(T, iu, ju)));
        }
    }

  for (i = (int) N - 1; i >= 0; --i)
    {
      iu = (size_t) i;

      /* get current eigenvalue and store it in lambda */
      lambda_re = gsl_matrix_get(T, iu, iu);

      if (iu != 0 && gsl_matrix_get(T, iu, iu - 1) != 0.0)
        {
          lambda_im = sqrt(fabs(gsl_matrix_get(T, iu, iu - 1))) *
                      sqrt(fabs(gsl_matrix_get(T, iu - 1, iu)));
        }
      else
        {
          lambda_im = 0.0;
        }

      GSL_SET_COMPLEX(&lambda, lambda_re, lambda_im);

      smin = GSL_MAX(GSL_DBL_EPSILON * (fabs(lambda_re) + fabs(lambda_im)),
                     smlnum);
      smin = GSL_MAX(smin, GSL_NONSYMMV_SMLNUM);

      if (lambda_im == 0.0)
        {
          int k, l;
          gsl_vector_view bv, xv;

          /* real eigenvector */

          /*
           * The ordering of eigenvalues in 'eval' is arbitrary and
           * does not necessarily follow the Schur form T, so store
           * lambda in the right slot in eval to ensure it corresponds
           * to the eigenvector we are about to compute
           */
          gsl_vector_complex_set(eval, iu, lambda);

          /*
           * We need to solve the system:
           *
           * (T(1:iu-1, 1:iu-1) - lambda*I)*X = -T(1:iu-1,iu)
           */

          /* construct right hand side */
          for (k = 0; k < i; ++k)
            {
              gsl_vector_set(w->work,
                             (size_t) k,
                             -gsl_matrix_get(T, (size_t) k, iu));
            }

          gsl_vector_set(w->work, iu, 1.0);

          for (l = i - 1; l >= 0; --l)
            {
              size_t lu = (size_t) l;

              if (lu == 0)
                complex_pair = 0;
              else
                complex_pair = gsl_matrix_get(T, lu, lu - 1) != 0.0;

              if (!complex_pair)
                {
                  double x;

                  /*
                   * 1-by-1 diagonal block - solve the system:
                   *
                   * (T_{ll} - lambda)*x = -T_{l(iu)}
                   */

                  Tv = gsl_matrix_submatrix(T, lu, lu, 1, 1);
                  bv = gsl_vector_view_array(dat, 1);
                  gsl_vector_set(&bv.vector, 0,
                                 gsl_vector_get(w->work, lu));
                  xv = gsl_vector_view_array(dat_X, 1);

                  gsl_schur_solve_equation(1.0,
                                           &Tv.matrix,
                                           lambda_re,
                                           1.0,
                                           1.0,
                                           &bv.vector,
                                           &xv.vector,
                                           &scale,
                                           &xnorm,
                                           smin);

                  /* scale x to avoid overflow */
                  x = gsl_vector_get(&xv.vector, 0);
                  if (xnorm > 1.0)
                    {
                      if (gsl_vector_get(w->work3, lu) > bignum / xnorm)
                        {
                          x /= xnorm;
                          scale /= xnorm;
                        }
                    }

                  if (scale != 1.0)
                    {
                      gsl_vector_view wv;

                      wv = gsl_vector_subvector(w->work, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                    }

                  gsl_vector_set(w->work, lu, x);

                  if (lu > 0)
                    {
                      gsl_vector_view v1, v2;

                      /* update right hand side */

                      v1 = gsl_matrix_subcolumn(T, lu, 0, lu);
                      v2 = gsl_vector_subvector(w->work, 0, lu);
                      gsl_blas_daxpy(-x, &v1.vector, &v2.vector);
                    } /* if (l > 0) */
                } /* if (!complex_pair) */
              else
                {
                  double x11, x21;

                  /*
                   * 2-by-2 diagonal block
                   */

                  Tv = gsl_matrix_submatrix(T, lu - 1, lu - 1, 2, 2);
                  bv = gsl_vector_view_array(dat, 2);
                  gsl_vector_set(&bv.vector, 0,
                                 gsl_vector_get(w->work, lu - 1));
                  gsl_vector_set(&bv.vector, 1,
                                 gsl_vector_get(w->work, lu));
                  xv = gsl_vector_view_array(dat_X, 2);

                  gsl_schur_solve_equation(1.0,
                                           &Tv.matrix,
                                           lambda_re,
                                           1.0,
                                           1.0,
                                           &bv.vector,
                                           &xv.vector,
                                           &scale,
                                           &xnorm,
                                           smin);

                  /* scale X(1,1) and X(2,1) to avoid overflow */
                  x11 = gsl_vector_get(&xv.vector, 0);
                  x21 = gsl_vector_get(&xv.vector, 1);

                  if (xnorm > 1.0)
                    {
                      double beta;

                      beta = GSL_MAX(gsl_vector_get(w->work3, lu - 1),
                                     gsl_vector_get(w->work3, lu));
                      if (beta > bignum / xnorm)
                        {
                          x11 /= xnorm;
                          x21 /= xnorm;
                          scale /= xnorm;
                        }
                    }

                  /* scale if necessary */
                  if (scale != 1.0)
                    {
                      gsl_vector_view wv;

                      wv = gsl_vector_subvector(w->work, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                    }

                  gsl_vector_set(w->work, lu - 1, x11);
                  gsl_vector_set(w->work, lu, x21);

                  /* update right hand side */
                  if (lu > 1)
                    {
                      gsl_vector_view v1, v2;

                      v1 = gsl_matrix_subcolumn(T, lu - 1, 0, lu - 1);
                      v2 = gsl_vector_subvector(w->work, 0, lu - 1);
                      gsl_blas_daxpy(-x11, &v1.vector, &v2.vector);

                      v1 = gsl_matrix_subcolumn(T, lu, 0, lu - 1);
                      gsl_blas_daxpy(-x21, &v1.vector, &v2.vector);
                    }

                  --l;
                } /* if (complex_pair) */
            } /* for (l = i - 1; l >= 0; --l) */

          /*
           * At this point, w->work is an eigenvector of the
           * Schur form T. To get an eigenvector of the original
           * matrix, we multiply on the left by Z, the matrix of
           * Schur vectors
           */

          ecol = gsl_matrix_complex_column(evec, iu);
          y = gsl_matrix_column(Z, iu);

          if (iu > 0)
            {
              gsl_vector_view x;

              Zv = gsl_matrix_submatrix(Z, 0, 0, N, iu);

              x = gsl_vector_subvector(w->work, 0, iu);

              /* compute Z * w->work and store it in Z(:,iu) */
              gsl_blas_dgemv(CblasNoTrans,
                             1.0,
                             &Zv.matrix,
                             &x.vector,
                             gsl_vector_get(w->work, iu),
                             &y.vector);
            } /* if (iu > 0) */

          /* store eigenvector into evec */

          ev = gsl_vector_complex_real(&ecol.vector);
          ev2 = gsl_vector_complex_imag(&ecol.vector);

          scale = 0.0;
          for (ii = 0; ii < N; ++ii)
            {
              double a = gsl_vector_get(&y.vector, ii);

              /* store real part of eigenvector */
              gsl_vector_set(&ev.vector, ii, a);

              /* set imaginary part to 0 */
              gsl_vector_set(&ev2.vector, ii, 0.0);

              if (fabs(a) > scale)
                scale = fabs(a);
            }

          if (scale != 0.0)
            scale = 1.0 / scale;

          /* scale by magnitude of largest element */
          gsl_blas_dscal(scale, &ev.vector);
        } /* if (GSL_IMAG(lambda) == 0.0) */
      else
        {
          gsl_vector_complex_view bv, xv;
          size_t k;
          int l;
          gsl_complex lambda2;

          /* complex eigenvector */

          /*
           * Store the complex conjugate eigenvalues in the right
           * slots in eval
           */
          GSL_SET_REAL(&lambda2, GSL_REAL(lambda));
          GSL_SET_IMAG(&lambda2, -GSL_IMAG(lambda));
          gsl_vector_complex_set(eval, iu - 1, lambda);
          gsl_vector_complex_set(eval, iu, lambda2);

          /*
           * First solve:
           *
           * [ T(i:i+1,i:i+1) - lambda*I ] * X = 0
           */

          if (fabs(gsl_matrix_get(T, iu - 1, iu)) >=
              fabs(gsl_matrix_get(T, iu, iu - 1)))
            {
              gsl_vector_set(w->work, iu - 1, 1.0);
              gsl_vector_set(w->work2, iu,
                             lambda_im / gsl_matrix_get(T, iu - 1, iu));
            }
          else
            {
              gsl_vector_set(w->work, iu - 1,
                             -lambda_im / gsl_matrix_get(T, iu, iu - 1));
              gsl_vector_set(w->work2, iu, 1.0);
            }
          gsl_vector_set(w->work, iu, 0.0);
          gsl_vector_set(w->work2, iu - 1, 0.0);

          /* construct right hand side */
          for (k = 0; k < iu - 1; ++k)
            {
              gsl_vector_set(w->work, k,
                             -gsl_vector_get(w->work, iu - 1) *
                             gsl_matrix_get(T, k, iu - 1));
              gsl_vector_set(w->work2, k,
                             -gsl_vector_get(w->work2, iu) *
                             gsl_matrix_get(T, k, iu));
            }

          /*
           * We must solve the upper quasi-triangular system:
           *
           * [ T(1:i-2,1:i-2) - lambda*I ] * X = s*(work + i*work2)
           */

          for (l = i - 2; l >= 0; --l)
            {
              size_t lu = (size_t) l;

              if (lu == 0)
                complex_pair = 0;
              else
                complex_pair = gsl_matrix_get(T, lu, lu - 1) != 0.0;

              if (!complex_pair)
                {
                  gsl_complex bval;
                  gsl_complex x;

                  /*
                   * 1-by-1 diagonal block - solve the system:
                   *
                   * (T_{ll} - lambda)*x = work + i*work2
                   */

                  Tv = gsl_matrix_submatrix(T, lu, lu, 1, 1);
                  bv = gsl_vector_complex_view_array(dat, 1);
                  xv = gsl_vector_complex_view_array(dat_X, 1);

                  GSL_SET_COMPLEX(&bval,
                                  gsl_vector_get(w->work, lu),
                                  gsl_vector_get(w->work2, lu));
                  gsl_vector_complex_set(&bv.vector, 0, bval);

                  gsl_schur_solve_equation_z(1.0,
                                             &Tv.matrix,
                                             &lambda,
                                             1.0,
                                             1.0,
                                             &bv.vector,
                                             &xv.vector,
                                             &scale,
                                             &xnorm,
                                             smin);

                  if (xnorm > 1.0)
                    {
                      if (gsl_vector_get(w->work3, lu) > bignum / xnorm)
                        {
                          gsl_blas_zdscal(1.0/xnorm, &xv.vector);
                          scale /= xnorm;
                        }
                    }

                  /* scale if necessary */
                  if (scale != 1.0)
                    {
                      gsl_vector_view wv;

                      wv = gsl_vector_subvector(w->work, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                      wv = gsl_vector_subvector(w->work2, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                    }

                  x = gsl_vector_complex_get(&xv.vector, 0);
                  gsl_vector_set(w->work, lu, GSL_REAL(x));
                  gsl_vector_set(w->work2, lu, GSL_IMAG(x));

                  /* update the right hand side */
                  if (lu > 0)
                    {
                      gsl_vector_view v1, v2;

                      v1 = gsl_matrix_subcolumn(T, lu, 0, lu);
                      v2 = gsl_vector_subvector(w->work, 0, lu);
                      gsl_blas_daxpy(-GSL_REAL(x), &v1.vector, &v2.vector);

                      v2 = gsl_vector_subvector(w->work2, 0, lu);
                      gsl_blas_daxpy(-GSL_IMAG(x), &v1.vector, &v2.vector);
                    } /* if (lu > 0) */
                } /* if (!complex_pair) */
              else
                {
                  gsl_complex b1, b2, x1, x2;

                  /*
                   * 2-by-2 diagonal block - solve the system
                   */

                  Tv = gsl_matrix_submatrix(T, lu - 1, lu - 1, 2, 2);
                  bv = gsl_vector_complex_view_array(dat, 2);
                  xv = gsl_vector_complex_view_array(dat_X, 2);

                  GSL_SET_COMPLEX(&b1,
                                  gsl_vector_get(w->work, lu - 1),
                                  gsl_vector_get(w->work2, lu - 1));
                  GSL_SET_COMPLEX(&b2,
                                  gsl_vector_get(w->work, lu),
                                  gsl_vector_get(w->work2, lu));
                  gsl_vector_complex_set(&bv.vector, 0, b1);
                  gsl_vector_complex_set(&bv.vector, 1, b2);

                  gsl_schur_solve_equation_z(1.0,
                                             &Tv.matrix,
                                             &lambda,
                                             1.0,
                                             1.0,
                                             &bv.vector,
                                             &xv.vector,
                                             &scale,
                                             &xnorm,
                                             smin);

                  x1 = gsl_vector_complex_get(&xv.vector, 0);
                  x2 = gsl_vector_complex_get(&xv.vector, 1);

                  if (xnorm > 1.0)
                    {
                      double beta;

                      beta = GSL_MAX(gsl_vector_get(w->work3, lu - 1),
                                     gsl_vector_get(w->work3, lu));
                      if (beta > bignum / xnorm)
                        {
                          gsl_blas_zdscal(1.0/xnorm, &xv.vector);
                          scale /= xnorm;
                        }
                    }

                  /* scale if necessary */
                  if (scale != 1.0)
                    {
                      gsl_vector_view wv;

                      wv = gsl_vector_subvector(w->work, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                      wv = gsl_vector_subvector(w->work2, 0, iu + 1);
                      gsl_blas_dscal(scale, &wv.vector);
                    }
                  gsl_vector_set(w->work, lu - 1, GSL_REAL(x1));
                  gsl_vector_set(w->work, lu, GSL_REAL(x2));
                  gsl_vector_set(w->work2, lu - 1, GSL_IMAG(x1));
                  gsl_vector_set(w->work2, lu, GSL_IMAG(x2));

                  /* update right hand side */
                  if (lu > 1)
                    {
                      gsl_vector_view v1, v2, v3, v4;

                      v1 = gsl_matrix_subcolumn(T, lu - 1, 0, lu - 1);
                      v4 = gsl_matrix_subcolumn(T, lu, 0, lu - 1);
                      v2 = gsl_vector_subvector(w->work, 0, lu - 1);
                      v3 = gsl_vector_subvector(w->work2, 0, lu - 1);

                      gsl_blas_daxpy(-GSL_REAL(x1), &v1.vector, &v2.vector);
                      gsl_blas_daxpy(-GSL_REAL(x2), &v4.vector, &v2.vector);
                      gsl_blas_daxpy(-GSL_IMAG(x1), &v1.vector, &v3.vector);
                      gsl_blas_daxpy(-GSL_IMAG(x2), &v4.vector, &v3.vector);
                    } /* if (lu > 1) */

                  --l;
                } /* if (complex_pair) */
            } /* for (l = i - 2; l >= 0; --l) */

          /*
           * At this point, work + i*work2 is an eigenvector
           * of T - backtransform to get an eigenvector of the
           * original matrix
           */

          y = gsl_matrix_column(Z, iu - 1);
          y2 = gsl_matrix_column(Z, iu);

          if (iu > 1)
            {
              gsl_vector_view x;

              /* compute real part of eigenvectors */

              Zv = gsl_matrix_submatrix(Z, 0, 0, N, iu - 1);
              x = gsl_vector_subvector(w->work, 0, iu - 1);

              gsl_blas_dgemv(CblasNoTrans,
                             1.0,
                             &Zv.matrix,
                             &x.vector,
                             gsl_vector_get(w->work, iu - 1),
                             &y.vector);


              /* now compute the imaginary part */
              x = gsl_vector_subvector(w->work2, 0, iu - 1);

              gsl_blas_dgemv(CblasNoTrans,
                             1.0,
                             &Zv.matrix,
                             &x.vector,
                             gsl_vector_get(w->work2, iu),
                             &y2.vector);
            }
          else
            {
              gsl_blas_dscal(gsl_vector_get(w->work, iu - 1), &y.vector);
              gsl_blas_dscal(gsl_vector_get(w->work2, iu), &y2.vector);
            }

          /*
           * Now store the eigenvectors into evec - the real parts
           * are Z(:,iu - 1) and the imaginary parts are
           * +/- Z(:,iu)
           */

          /* get views of the two eigenvector slots */
          ecol = gsl_matrix_complex_column(evec, iu - 1);
          ecol2 = gsl_matrix_complex_column(evec, iu);

          /*
           * save imaginary part first as it may get overwritten
           * when copying the real part due to our storage scheme
           * in Z/evec
           */
          ev = gsl_vector_complex_imag(&ecol.vector);
          ev2 = gsl_vector_complex_imag(&ecol2.vector);
          scale = 0.0;
          for (ii = 0; ii < N; ++ii)
            {
              double a = gsl_vector_get(&y2.vector, ii);

              scale = GSL_MAX(scale,
                              fabs(a) + fabs(gsl_vector_get(&y.vector, ii)));

              gsl_vector_set(&ev.vector, ii, a);
              gsl_vector_set(&ev2.vector, ii, -a);
            }

          /* now save the real part */
          ev = gsl_vector_complex_real(&ecol.vector);
          ev2 = gsl_vector_complex_real(&ecol2.vector);
          for (ii = 0; ii < N; ++ii)
            {
              double a = gsl_vector_get(&y.vector, ii);

              gsl_vector_set(&ev.vector, ii, a);
              gsl_vector_set(&ev2.vector, ii, a);
            }

          if (scale != 0.0)
            scale = 1.0 / scale;

          /* scale by largest element magnitude */

          gsl_blas_zdscal(scale, &ecol.vector);
          gsl_blas_zdscal(scale, &ecol2.vector);

          /*
           * decrement i since we took care of two eigenvalues at
           * the same time
           */
          --i;
        } /* if (GSL_IMAG(lambda) != 0.0) */
    } /* for (i = (int) N - 1; i >= 0; --i) */
} /* nonsymmv_get_right_eigenvectors() */
Example #26
0
void vector_acc(gsl_vector_complex *V,unsigned n, double _Complex a){
	gsl_complex T1;
	T1=gsl_vector_complex_get(V,n);
	gsl_vector_complex_set(V,n,gsl_complex_add(T1,c2g(a)));
	}
Example #27
0
void vector_set(gsl_vector_complex *V,unsigned n, double _Complex a){
	gsl_vector_complex_set(V,n,c2g(a));
	}
Example #28
0
double Calculator::getIntensity(double Q)
{
	std::complex<double> cppf1, cppf2;
	gsl_complex alpha, beta;
	gsl_complex gslf1, gslf2;
	int s;
	double avFactor;

	cppf1 = m_sf1->F(0.0, 0.0, Q, m_energy);
	cppf2 = m_sf2->F(0.0, 0.0, Q, m_energy);

	gslf1 = gsl_complex_rect(cppf1.real(), cppf1.imag());
	gslf2 = gsl_complex_rect(cppf2.real(), cppf2.imag());

	avFactor = exp(-2.0 / m_N);

	alpha = gsl_complex_rect (1.0, 0.0);
	beta = gsl_complex_rect (0.0, 0.0);

	/*set vector F (scattering factors)*/
	gsl_vector_complex_set(F, 0, gslf1);
	gsl_vector_complex_set(F, 1, gslf2);

	/*set vector conj(F) (scattering factors)*/
	gsl_vector_complex_set(Fconj, 0, gsl_complex_conjugate(gslf1));
	gsl_vector_complex_set(Fconj, 1, gsl_complex_conjugate(gslf2));

	/*set exp matrix*/
	setMatrixExp(m_Exp, Q);

	/*find W = P * Exp * Ps * conj(F)  vector:*/
	/* (1) W = alpha * Ps * conj(F) + beta * W */
	gsl_blas_zgemv (CblasNoTrans, alpha, m_Ps, Fconj, beta, W);

	/*printf("W(1):\n");
	gsl_vector_complex_fprintf (stdout, W, "%g");*/

	/* (2) W = alpha * Exp * tmp_vec + beta * W */
	gsl_blas_zgemv (CblasNoTrans, alpha, m_Exp, W, beta, tmp_vec);

	/*printf("W(2):\n");
	gsl_vector_complex_fprintf (stdout, tmp_vec, "%g");*/

	/* (3) W = alpha * P * tmp_vec + beta * W */
	gsl_blas_zgemv (CblasNoTrans, alpha, m_P, tmp_vec, beta, W);

	/*Find J0 = F.(Ps * conj(F)) */
	gsl_blas_zgemv (CblasNoTrans, alpha, m_Ps, Fconj, beta, tmp_vec);
	gsl_blas_zdotu (F, tmp_vec, &J0);

	/*alpha = exp(-2 / N)*/
	alpha = gsl_complex_rect (avFactor, 0.0);
	beta = gsl_complex_rect (0.0, 0.0);

	/*find T matrix: T = alpha * P * exp + beta * T*/
	gsl_blas_zgemm (CblasNoTrans, CblasNoTrans, alpha, m_P, m_Exp, beta, T);

	/*printf("T:\n");
	gsl_matrix_complex_fprintf (stdout, T, "%g");*/

	/*Find Jns = F. (G * W)  */
	/*tmp_mat = I */
	gsl_matrix_complex_set_identity (tmp_mat);
	/*tmp_mat = I - T */
	gsl_matrix_complex_sub (tmp_mat, T);
	/*LU decomposition*/
	gsl_linalg_complex_LU_decomp(tmp_mat, perm, &s);
	/*calculate product G * W = (I - T)^(-1) W directly using LU decomposition*/
	gsl_linalg_complex_LU_solve (tmp_mat, perm, W, tmp_vec);
	/*calculate F.(G * W)*/
	gsl_blas_zdotu (F, tmp_vec, &Jns);

	/*Find Js = F.(G^2 * (I - T^N) * W)  however, this term should be negligible*/

	/*result = N *(2 * Jns + J0) - Js */
	alpha = gsl_complex_mul_real (Jns, 2.0 * avFactor);
	alpha = gsl_complex_add (alpha, J0);

	return m_N * m_I0 * GSL_REAL(alpha) * getPLGfactor(getTheta(Q)) + m_Ibg;
}
Example #29
0
vectorc::ComplexProxy& vectorc::ComplexProxy::operator=(gsl_complex c)
{

	gsl_vector_complex_set(theVector.v_m, ComplexIndex, c);
	return *this;
}
Example #30
0
vectorc::vectorc(const vectorc& vec) : size_m(vec.size_m),  v_m(gsl_vector_complex_alloc(vec.size_m))
{
	for (int i=0; i<size_m; i++)
		gsl_vector_complex_set(v_m, i, gsl_vector_complex_get(vec.v_m, i));
}