Esempio n. 1
0
int
gsl_linalg_complex_cholesky_solve (const gsl_matrix_complex * cholesky,
                                   const gsl_vector_complex * b,
                                   gsl_vector_complex * x)
{
  if (cholesky->size1 != cholesky->size2)
    {
      GSL_ERROR ("cholesky matrix must be square", GSL_ENOTSQR);
    }
  else if (cholesky->size1 != b->size)
    {
      GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
    }
  else if (cholesky->size2 != x->size)
    {
      GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
    }
  else
    {
      gsl_vector_complex_memcpy (x, b);

      /* solve for y using forward-substitution, L y = b */

      gsl_blas_ztrsv (CblasLower, CblasNoTrans, CblasNonUnit, cholesky, x);

      /* perform back-substitution, L^H x = y */

      gsl_blas_ztrsv (CblasLower, CblasConjTrans, CblasNonUnit, cholesky, x);

      return GSL_SUCCESS;
    }
} /* gsl_linalg_complex_cholesky_solve() */
Esempio n. 2
0
int
gsl_linalg_complex_LU_solve (const gsl_matrix_complex * LU, const gsl_permutation * p, const gsl_vector_complex * b, gsl_vector_complex * x)
{
  if (LU->size1 != LU->size2)
    {
      GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
    }
  else if (LU->size1 != p->size)
    {
      GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
    }
  else if (LU->size1 != b->size)
    {
      GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
    }
  else if (LU->size2 != x->size)
    {
      GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
    }
  else
    {
      /* Copy x <- b */

      gsl_vector_complex_memcpy (x, b);

      /* Solve for x */

      gsl_linalg_complex_LU_svx (LU, p, x);

      return GSL_SUCCESS;
    }
}
Esempio n. 3
0
static VALUE rb_gsl_blas_zaxpy2(int argc, VALUE *argv, VALUE obj)
{
  gsl_complex *a = NULL;
  gsl_vector_complex *x = NULL, *y = NULL, *y2 = NULL;
  switch (TYPE(obj)) {
  case T_MODULE:
  case T_CLASS:
  case T_OBJECT:
    get_vector_complex2(argc-1, argv+1, obj, &x, &y);
    CHECK_COMPLEX(argv[0]);
    Data_Get_Struct(argv[0], gsl_complex, a);
    break;
  default:
    Data_Get_Struct(obj, gsl_vector_complex, x);
    if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
			    argc);
    CHECK_COMPLEX(argv[0]);
    CHECK_VECTOR_COMPLEX(argv[1]);
    Data_Get_Struct(argv[0], gsl_complex, a);
    Data_Get_Struct(argv[1], gsl_vector_complex, y);
    break;
  }
  y2 = gsl_vector_complex_alloc(y->size);
  gsl_vector_complex_memcpy(y2, y);
  gsl_blas_zaxpy(*a, x, y2);
  return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, y2);
}
Esempio n. 4
0
static VALUE rb_gsl_blas_zscal2(int argc, VALUE *argv, VALUE obj)
{
  gsl_complex *a = NULL;
  gsl_vector_complex *x = NULL, *xnew = NULL;
  CHECK_COMPLEX(argv[0]);
  switch (TYPE(obj)) {
  case T_MODULE:
  case T_CLASS:
  case T_OBJECT:
    if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
			    argc);
    CHECK_VECTOR_COMPLEX(argv[1]);
    Data_Get_Struct(argv[0], gsl_complex, a);
    Data_Get_Struct(argv[1], gsl_vector_complex, x);
    break;
  default:
    if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)",
			    argc);
    Data_Get_Struct(obj, gsl_vector_complex, x);
    Data_Get_Struct(argv[0], gsl_complex, a);
    break;
  }
  xnew = gsl_vector_complex_alloc(x->size);
  gsl_vector_complex_memcpy(xnew, x);
  gsl_blas_zscal(*a, xnew);
  return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, xnew);
}
Esempio n. 5
0
static VALUE rb_fft_complex_trans(int argc, VALUE *argv, VALUE obj,
				  int (*transform)(gsl_complex_packed_array, 
						   size_t, size_t, 
						   const gsl_fft_complex_wavetable *,
						   gsl_fft_complex_workspace *),
				  int sss)
{
  int flag = 0;
  // local variable "status" was defined and set, but never used
  //int status;
  size_t stride, n;
  gsl_complex_packed_array data;
  gsl_vector_complex *vin, *vout;
  gsl_fft_complex_wavetable *table = NULL;
  gsl_fft_complex_workspace *space = NULL;
  flag = gsl_fft_get_argv_complex(argc, argv, obj, &vin, &data, &stride, &n, &table, &space);
  if (sss == RB_GSL_FFT_COPY) {
    vout = gsl_vector_complex_alloc(n);
    gsl_vector_complex_memcpy(vout, vin);
    /*status =*/ (*transform)(vout->data, vout->stride /*1*/, vout->size /*n*/, table, space);
    gsl_fft_free(flag, (GSL_FFT_Wavetable *) table, (GSL_FFT_Workspace *) space);
    return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vout);
  } else {    /* in-place */
    /*status =*/ (*transform)(data, stride, n, table, space);
    gsl_fft_free(flag, (GSL_FFT_Wavetable *) table, (GSL_FFT_Workspace *) space);
    return obj;
  }
}
Esempio n. 6
0
//copy
complex_spinor::complex_spinor(const complex_spinor & another){
	
	this->_numSites = another._numSites;
	this->dimension = another.dimension;
	this->priv_pVector = gsl_vector_complex_alloc(this->dimension);
	gsl_vector_complex_memcpy(this->priv_pVector, another.priv_pVector);

}
Esempio n. 7
0
int
gsl_linalg_complex_LU_refine (const gsl_matrix_complex * A, const gsl_matrix_complex * LU, const gsl_permutation * p, const gsl_vector_complex * b, gsl_vector_complex * x, gsl_vector_complex * residual)
{
  if (A->size1 != A->size2)
    {
      GSL_ERROR ("matrix a must be square", GSL_ENOTSQR);
    }
  if (LU->size1 != LU->size2)
    {
      GSL_ERROR ("LU matrix must be square", GSL_ENOTSQR);
    }
  else if (A->size1 != LU->size2)
    {
      GSL_ERROR ("LU matrix must be decomposition of a", GSL_ENOTSQR);
    }
  else if (LU->size1 != p->size)
    {
      GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
    }
  else if (LU->size1 != b->size)
    {
      GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
    }
  else if (LU->size1 != x->size)
    {
      GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
    }
  else if (singular (LU)) 
    {
      GSL_ERROR ("matrix is singular", GSL_EDOM);
    }
  else
    {
      int status;

      /* Compute residual, residual = (A * x  - b) */

      gsl_vector_complex_memcpy (residual, b);

      {
        gsl_complex one = GSL_COMPLEX_ONE;
        gsl_complex negone = GSL_COMPLEX_NEGONE;
        gsl_blas_zgemv (CblasNoTrans, one, A, x, negone, residual);
      }

      /* Find correction, delta = - (A^-1) * residual, and apply it */

      status = gsl_linalg_complex_LU_svx (LU, p, residual);

      {
        gsl_complex negone= GSL_COMPLEX_NEGONE;
        gsl_blas_zaxpy (negone, residual, x);
      }

      return status;
    }
}
Esempio n. 8
0
static CVECTOR *VECTOR_copy(CVECTOR *_object)
{
	CVECTOR *copy = VECTOR_create(SIZE(THIS), COMPLEX(THIS), FALSE);
	if (!COMPLEX(THIS))
		gsl_vector_memcpy(VEC(copy), VEC(THIS));
	else
		gsl_vector_complex_memcpy(CVEC(copy), CVEC(THIS));
	
	return copy;
}
Esempio n. 9
0
/* module function */
static VALUE Vector_complex_memcpy(VALUE obj, VALUE dest, VALUE src) {
  gsl_vector_complex * vdest, * vsrc;

  Data_Get_Struct(dest, gsl_vector_complex, vdest);
  Data_Get_Struct(src, gsl_vector_complex, vsrc);

  gsl_vector_complex_memcpy(vdest, vsrc);

  return obj;
}
Esempio n. 10
0
int
lls_complex_solve(const double lambda, gsl_vector_complex *c, lls_complex_workspace *w)
{
  if (c->size != w->p)
    {
      fprintf(stderr, "lls_complex_solve: coefficient vector has wrong size\n");
      return GSL_EBADLEN;
    }
  else
    {
      int s = 0;

      /* solve (AHA + lambda^2 I) c = AHb and estimate condition number */
      s = lls_lapack_zposv(lambda, c, w);

      /* compute residual || AHA c - AHb || */
      gsl_vector_complex_memcpy(w->work_b, w->AHb);
      gsl_blas_zhemv(CblasUpper, GSL_COMPLEX_ONE, w->AHA, c, GSL_COMPLEX_NEGONE, w->work_b);
      w->residual = gsl_blas_dznrm2(w->work_b);

      /* compute chi^2 = b^H b - 2 c^H A^H b + c^H A^H A c */
      {
        gsl_complex negtwo = gsl_complex_rect(-2.0, 0.0);
        gsl_complex val;

        /* compute: AHA c - 2 AHb */
        gsl_vector_complex_memcpy(w->work_b, w->AHb);
        gsl_blas_zhemv(CblasUpper, GSL_COMPLEX_ONE, w->AHA, c, negtwo, w->work_b);

        /* compute: c^H ( AHA c - 2 AHb ) */
        gsl_blas_zdotc(c, w->work_b, &val);

        w->chisq = w->bHb + GSL_REAL(val);
      }

      /* save coefficient vector for future robust iterations */
      gsl_vector_complex_memcpy(w->c, c);

      ++(w->niter);

      return s;
    }
} /* lls_complex_solve() */
Esempio n. 11
0
static VALUE rb_gsl_heapsort_vector_complex2(VALUE obj)
{
  gsl_vector_complex *v = NULL, *vnew = NULL;
  if (!rb_block_given_p()) rb_raise(rb_eRuntimeError, "Proc is not given");
  Data_Get_Struct(obj, gsl_vector_complex, v);
  vnew = gsl_vector_complex_alloc(v->size);
  gsl_vector_complex_memcpy(vnew, v);
  gsl_heapsort(vnew->data, vnew->size, sizeof(gsl_complex), rb_gsl_comparison_complex);
  return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vnew);
}
Esempio n. 12
0
static VALUE rb_gsl_fft_complex_radix2_dif_transform(VALUE obj, VALUE val_sign)
{
  size_t stride, n;
  gsl_complex_packed_array data;
  gsl_vector_complex *vin, *vout;
  gsl_fft_direction sign;
  sign = NUM2INT(val_sign);
  get_complex_stride_n(obj, &vin, &data, &stride, &n);
  vout = gsl_vector_complex_alloc(n);
  gsl_vector_complex_memcpy(vout, vin); 
  gsl_fft_complex_radix2_dif_transform(vout->data, vout->stride /*1*/, vout->size /*n*/, sign);
  return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vout);
}
Esempio n. 13
0
static VALUE rb_fft_complex_radix2(VALUE obj,
				   int (*trans)(gsl_complex_packed_array, 
						size_t, size_t), int flag)
{
  size_t stride, n;
  gsl_complex_packed_array data;
  gsl_vector_complex *vin, *vout;
  VALUE ary;
  ary = get_complex_stride_n(obj, &vin, &data, &stride, &n);
  if (flag == RB_GSL_FFT_COPY) {
    vout = gsl_vector_complex_alloc(n);
    gsl_vector_complex_memcpy(vout, vin); 
    (*trans)(vout->data, vout->stride /*1*/, vout->size /*n*/);
    return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vout);
  } else { /* in-place */
    (*trans)(data, stride, n);
    return ary;
  }
}
Esempio n. 14
0
static VALUE rb_gsl_fft_complex_transform(int argc, VALUE *argv, VALUE obj)
{
  int flag = 0;
  // local variable "status" was defined and set, but never used
  //int status;
  size_t stride, n;
  gsl_vector_complex *vin, *vout;
  gsl_fft_direction sign;
  gsl_complex_packed_array data;
  gsl_fft_complex_wavetable *table = NULL;
  gsl_fft_complex_workspace *space = NULL;
  CHECK_FIXNUM(argv[argc-1]);
  sign = FIX2INT(argv[argc-1]);
  flag = gsl_fft_get_argv_complex(argc-1, argv, obj, &vin, &data, &stride, &n, &table, &space);
  vout = gsl_vector_complex_alloc(n);
  gsl_vector_complex_memcpy(vout, vin);
  /*status =*/ gsl_fft_complex_transform(vout->data, stride, n, table, space, sign);
  gsl_fft_free(flag, (GSL_FFT_Wavetable *) table, (GSL_FFT_Workspace *) space);
  return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, vout);
}
Esempio n. 15
0
File: test.c Progetto: lemahdi/mglib
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() */
Esempio n. 16
0
 /** Assign */
 vector<complex>& vector<complex>::operator=(const vector<complex>& v)
 {
     gsl_vector_complex_memcpy(_vector, v.as_gsl_type_ptr());
     return *this;
 }
Esempio n. 17
0
int
lls_complex_fold(const gsl_matrix_complex *A, const gsl_vector_complex *b,
                 lls_complex_workspace *w)
{
  const size_t n = A->size1;

  if (A->size2 != w->p)
    {
      fprintf(stderr, "lls_complex_fold: A has wrong size2\n");
      return GSL_EBADLEN;
    }
  else if (n != b->size)
    {
      fprintf(stderr, "lls_complex_fold: b has wrong size\n");
      return GSL_EBADLEN;
    }
  else
    {
      int s = 0;
      double bnorm;
#if 0
      size_t i;

      gsl_vector_view wv = gsl_vector_subvector(w->w_robust, 0, n);

      if (w->niter > 0)
        {
          gsl_vector_complex_view rc = gsl_vector_complex_subvector(w->r_complex, 0, n);
          gsl_vector_view rv = gsl_vector_subvector(w->r, 0, n);

          /* calculate residuals with previously computed coefficients: r = b - A c */
          gsl_vector_complex_memcpy(&rc.vector, b);
          gsl_blas_zgemv(CblasNoTrans, GSL_COMPLEX_NEGONE, A, w->c, GSL_COMPLEX_ONE, &rc.vector);

          /* compute Re(r) */
          for (i = 0; i < n; ++i)
            {
              gsl_complex ri = gsl_vector_complex_get(&rc.vector, i);
              gsl_vector_set(&rv.vector, i, GSL_REAL(ri));
            }

          /* calculate weights with robust weighting function */
          gsl_multifit_robust_weights(&rv.vector, &wv.vector, w->robust_workspace_p);
        }
      else
        gsl_vector_set_all(&wv.vector, 1.0);

      /* compute final weights as product of input and robust weights */
      gsl_vector_mul(wts, &wv.vector);

#endif
 
      /* AHA += A^H A, using only the upper half of the matrix */
      s = gsl_blas_zherk(CblasUpper, CblasConjTrans, 1.0, A, 1.0, w->AHA);
      if (s)
        return s;

      /* AHb += A^H b */
      s = gsl_blas_zgemv(CblasConjTrans, GSL_COMPLEX_ONE, A, b, GSL_COMPLEX_ONE, w->AHb);
      if (s)
        return s;

      /* bHb += b^H b */
      bnorm = gsl_blas_dznrm2(b);
      w->bHb += bnorm * bnorm;

      fprintf(stderr, "norm(AHb) = %.12e, bHb = %.12e\n",
              gsl_blas_dznrm2(w->AHb), w->bHb);

      if (!gsl_finite(w->bHb))
        {
          fprintf(stderr, "bHb is NAN\n");
          exit(1);
        }

      return s;
    }
} /* lls_complex_fold() */
Esempio n. 18
0
 vector<complex>::vector(const gsl_vector_complex& v)
 {
     _vector = gsl_vector_complex_alloc(v.size);
     gsl_vector_complex_memcpy(_vector, &v);
 }
Esempio n. 19
0
 vector<complex>::vector(const gsl_vector_complex* v)
 {
     _vector = gsl_vector_complex_alloc(v->size);
     gsl_vector_complex_memcpy(_vector, v);
 }
Esempio n. 20
0
int
lls_complex_lcurve(gsl_vector *reg_param, gsl_vector *rho, gsl_vector *eta,
                   lls_complex_workspace *w)
{
  const size_t N = rho->size; /* number of points on L-curve */

  if (N != reg_param->size)
    {
      GSL_ERROR("size of reg_param and rho do not match", GSL_EBADLEN);
    }
  else if (N != eta->size)
    {
      GSL_ERROR("size of eta and rho do not match", GSL_EBADLEN);
    }
  else
    {
      int s;
      const gsl_complex negtwo = gsl_complex_rect(-2.0, 0.0);

      /* smallest regularization parameter */
      const double smin_ratio = 16.0 * GSL_DBL_EPSILON;

      double s1, sp, ratio, tmp;
      size_t i;

      /* compute eigenvalues of A^H A */
      gsl_matrix_complex_transpose_memcpy(w->work_A, w->AHA);
      s = gsl_eigen_herm(w->work_A, w->eval, w->eigen_p);
      if (s)
        return s;

      /* find largest and smallest eigenvalues */
      gsl_vector_minmax(w->eval, &sp, &s1);

      /* singular values are square roots of eigenvalues */
      s1 = sqrt(s1);
      if (sp > GSL_DBL_EPSILON)
        sp = sqrt(fabs(sp));

      tmp = GSL_MAX(sp, s1*smin_ratio);
      gsl_vector_set(reg_param, N - 1, tmp);

      /* ratio so that reg_param(1) = s(1) */
      ratio = pow(s1 / tmp, 1.0 / (N - 1.0));

      /* calculate the regularization parameters */
      for (i = N - 1; i > 0 && i--; )
        {
          double rp1 = gsl_vector_get(reg_param, i + 1);
          gsl_vector_set(reg_param, i, ratio * rp1);
        }

      for (i = 0; i < N; ++i)
        {
          double r2;
          double lambda = gsl_vector_get(reg_param, i);
          gsl_complex val;

          lls_complex_solve(lambda, w->c, w);

          /* store ||c|| */
          gsl_vector_set(eta, i, gsl_blas_dznrm2(w->c));

          /* compute: A^H A c - 2 A^H b */
          gsl_vector_complex_memcpy(w->work_b, w->AHb);
          gsl_blas_zhemv(CblasUpper, GSL_COMPLEX_ONE, w->AHA, w->c, negtwo, w->work_b);

          /* compute: c^T A^T A c - 2 c^T A^T b */
          gsl_blas_zdotc(w->c, w->work_b, &val);
          r2 = GSL_REAL(val) + w->bHb;

          gsl_vector_set(rho, i, sqrt(r2));
        }

      return GSL_SUCCESS;
    }
} /* lls_complex_lcurve() */
Esempio n. 21
0
 /** Copy constructor  */
 vector<complex>::vector(const vector<complex>& v)
 {
     _vector = gsl_vector_complex_alloc(v.size());
     gsl_vector_complex_memcpy(_vector, v.as_gsl_type_ptr());
 }
Esempio n. 22
0
/// Copy constructor.
/// @param v :: The other vector
ComplexVector::ComplexVector(const ComplexVector &v) {
  m_vector = gsl_vector_complex_alloc(v.size());
  gsl_vector_complex_memcpy(m_vector, v.gsl());
}
Esempio n. 23
0
/// Copy from a gsl vector
/// @param v :: A vector to copy from.
ComplexVector::ComplexVector(const gsl_vector_complex *v) {
  m_vector = gsl_vector_complex_alloc(v->size);
  gsl_vector_complex_memcpy(m_vector, v);
}
Esempio n. 24
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;
}
Esempio n. 25
0
File: test.c Progetto: lemahdi/mglib
void
test_eigen_nonsymm_results (const gsl_matrix * m, 
                            const gsl_vector_complex * eval, 
                            const gsl_matrix_complex * evec, 
                            size_t count,
                            const char * desc,
                            const char * desc2)
{
  size_t i,j;
  size_t N = m->size1;

  gsl_vector_complex * x = gsl_vector_complex_alloc(N);
  gsl_vector_complex * y = gsl_vector_complex_alloc(N);
  gsl_matrix_complex * A = gsl_matrix_complex_alloc(N, N);

  /* we need a complex matrix for the blas routines, so copy m into A */
  for (i = 0; i < N; ++i)
    {
      for (j = 0; j < N; ++j)
        {
          gsl_complex z;
          GSL_SET_COMPLEX(&z, gsl_matrix_get(m, i, j), 0.0);
          gsl_matrix_complex_set(A, i, j, z);
        }
    }

  for (i = 0; i < N; i++)
    {
      gsl_complex ei = gsl_vector_complex_get (eval, i);
      gsl_vector_complex_const_view vi = gsl_matrix_complex_const_column(evec, i);
      double norm = gsl_blas_dznrm2(&vi.vector);

      /* check that eigenvector is normalized */
      gsl_test_rel(norm, 1.0, N * GSL_DBL_EPSILON,
                   "nonsymm(N=%u,cnt=%u), %s, normalized(%d), %s", N, count, desc, i, desc2);

      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);

      /* compute x = lambda v */
      gsl_blas_zscal(ei, x);

      /* now test if y = x */
      for (j = 0; j < N; j++)
        {
          gsl_complex xj = gsl_vector_complex_get (x, j);
          gsl_complex yj = gsl_vector_complex_get (y, j);

          /* use abs here in case the values are close to 0 */
          gsl_test_abs(GSL_REAL(yj), GSL_REAL(xj), 1e8*GSL_DBL_EPSILON, 
                       "nonsymm(N=%u,cnt=%u), %s, eigenvalue(%d,%d), real, %s", N, count, desc, i, j, desc2);
          gsl_test_abs(GSL_IMAG(yj), GSL_IMAG(xj), 1e8*GSL_DBL_EPSILON, 
                       "nonsymm(N=%u,cnt=%u), %s, eigenvalue(%d,%d), imag, %s", N, count, desc, i, j, desc2);
        }
    }

  gsl_matrix_complex_free(A);
  gsl_vector_complex_free(x);
  gsl_vector_complex_free(y);
}
Esempio n. 26
0
/// Copy assignment operator
/// @param v :: The other vector
ComplexVector &ComplexVector::operator=(const ComplexVector &v) {
  gsl_vector_complex_free(m_vector);
  m_vector = gsl_vector_complex_alloc(v.size());
  gsl_vector_complex_memcpy(m_vector, v.gsl());
  return *this;
}