Beispiel #1
0
static double
quadratic_preduction(const gsl_vector * f, const gsl_matrix * J,
                     const gsl_vector * dx, gsl_vector * work)
{
  const size_t n = f->size;
  const double normf = gsl_blas_dnrm2(f);
  double pred_reduction;
  double norm_beta; /* ||J*dx|| / ||f|| */
  size_t i;

  /* compute beta = J*dx / ||f|| */
  gsl_blas_dgemv(CblasNoTrans, 1.0 / normf, J, dx, 0.0, work);
  norm_beta = gsl_blas_dnrm2(work);

  /* initialize to ( ||J*dx|| / ||f|| )^2 */
  pred_reduction = -norm_beta * norm_beta;

  /* subtract 2*fhat.beta */
  for (i = 0; i < n; ++i)
    {
      double fi = gsl_vector_get(f, i);
      double betai = gsl_vector_get(work, i);

      pred_reduction -= 2.0 * (fi / normf) * betai;
    }

  return pred_reduction;
}
Beispiel #2
0
int
test_fdf(const char * desc, 
         gsl_multimin_function_fdf *f,
         initpt_function initpt,
         const gsl_multimin_fdfminimizer_type *T)
{
  int status;
  size_t iter = 0;
  double step_size;
  
  gsl_vector *x = gsl_vector_alloc (f->n);

  gsl_multimin_fdfminimizer *s;
  fcount = 0; gcount = 0;

  (*initpt) (x);

  step_size = 0.1 * gsl_blas_dnrm2 (x);

  s = gsl_multimin_fdfminimizer_alloc(T, f->n);

  gsl_multimin_fdfminimizer_set (s, f, x, step_size, 0.1);

#ifdef DEBUG
  printf("x "); gsl_vector_fprintf (stdout, s->x, "%g"); 
  printf("g "); gsl_vector_fprintf (stdout, s->gradient, "%g"); 
#endif

  do 
    {
      iter++;
      status = gsl_multimin_fdfminimizer_iterate(s);

#ifdef DEBUG
      printf("%i: \n",iter);
      printf("x "); gsl_vector_fprintf (stdout, s->x, "%g"); 
      printf("g "); gsl_vector_fprintf (stdout, s->gradient, "%g"); 
      printf("f(x) %g\n",s->f);
      printf("dx %g\n",gsl_blas_dnrm2(s->dx));
      printf("\n");
#endif

      status = gsl_multimin_test_gradient(s->gradient,1e-3);
    }
  while (iter < 5000 && status == GSL_CONTINUE);

  status |= (fabs(s->f) > 1e-5);

  gsl_test(status, "%s, on %s: %i iters (fn+g=%d+%d), f(x)=%g",
           gsl_multimin_fdfminimizer_name(s),desc, iter, fcount, gcount, s->f);

  gsl_multimin_fdfminimizer_free(s);
  gsl_vector_free(x);

  return status;
}
Beispiel #3
0
static int
dogleg_preloop(const void * vtrust_state, void * vstate)
{
  int status;
  const gsl_multifit_nlinear_trust_state *trust_state =
    (const gsl_multifit_nlinear_trust_state *) vtrust_state;
  dogleg_state_t *state = (dogleg_state_t *) vstate;
  const gsl_multifit_nlinear_parameters *params = trust_state->params;
  double u;
  double alpha; /* ||g||^2 / ||Jg||^2 */

  /* initialize linear least squares solver */
  status = (params->solver->init)(trust_state, trust_state->solver_state);
  if (status)
    return status;

  /* prepare the linear solver to compute Gauss-Newton step */
  status = (params->solver->presolve)(0.0, trust_state, trust_state->solver_state);
  if (status)
    return status;

  /* solve: J dx_gn = -f for Gauss-Newton step */
  status = (params->solver->solve)(trust_state->f,
                                   state->dx_gn,
                                   trust_state,
                                   trust_state->solver_state);
  if (status)
    return status;

  /* now calculate the steepest descent step */

  /* compute workp = D^{-1} g and its norm */
  gsl_vector_memcpy(state->workp, trust_state->g);
  gsl_vector_div(state->workp, trust_state->diag);
  state->norm_Dinvg = gsl_blas_dnrm2(state->workp);

  /* compute workp = D^{-2} g */
  gsl_vector_div(state->workp, trust_state->diag);

  /* compute: workn = J D^{-2} g */
  gsl_blas_dgemv(CblasNoTrans, 1.0, trust_state->J, state->workp, 0.0, state->workn);
  state->norm_JDinv2g = gsl_blas_dnrm2(state->workn);

  u = state->norm_Dinvg / state->norm_JDinv2g;
  alpha = u * u;

  /* dx_sd = -alpha D^{-2} g */
  gsl_vector_memcpy(state->dx_sd, state->workp);
  gsl_vector_scale(state->dx_sd, -alpha);

  state->norm_Dgn = scaled_enorm(trust_state->diag, state->dx_gn);
  state->norm_Dsd = scaled_enorm(trust_state->diag, state->dx_sd);

  return GSL_SUCCESS;
}
Beispiel #4
0
bool SphereConNet2D::is_floater(int i) {
	bool fltr = false; int n = con.at(i).size();
	if(n==0||n==1) fltr = true;//always a floater when 0 or 1 contacts
	else if(n==2){//check if the two are antiparallel
		double normprod = gsl_blas_dnrm2(rs[i][0])*gsl_blas_dnrm2(rs[i][1]);
		double * dotprod; gsl_blas_ddot(rs[i][0],rs[i][1],dotprod);
		double prodsum = normprod+*dotprod;//if small->antiparallel
		//if not small->not antiparallel->is floater
		if(prodsum>std::pow(10,-14)) fltr = true;
	}
	return fltr;
}
Beispiel #5
0
static void
test_random(const size_t N, const gsl_rng *r, const int compress)
{
  const gsl_splinalg_itersolve_type *T = gsl_splinalg_itersolve_gmres;
  const double tol = 1.0e-8;
  int status;
  gsl_spmatrix *A = create_random_sparse(N, N, 0.3, r);
  gsl_spmatrix *B;
  gsl_vector *b = gsl_vector_alloc(N);
  gsl_vector *x = gsl_vector_calloc(N);

  /* these random matrices require all N iterations to converge */
  gsl_splinalg_itersolve *w = gsl_splinalg_itersolve_alloc(T, N, N);

  const char *desc = gsl_splinalg_itersolve_name(w);

  create_random_vector(b, r);

  if (compress)
    B = gsl_spmatrix_compcol(A);
  else
    B = A;

  status = gsl_splinalg_itersolve_iterate(B, b, tol, x, w);
  gsl_test(status, "%s random status s=%d N=%zu", desc, status, N);

  /* check that the residual satisfies ||r|| <= tol*||b|| */
  {
    gsl_vector *res = gsl_vector_alloc(N);
    double normr, normb;

    gsl_vector_memcpy(res, b);
    gsl_spblas_dgemv(CblasNoTrans, -1.0, A, x, 1.0, res);

    normr = gsl_blas_dnrm2(res);
    normb = gsl_blas_dnrm2(b);

    status = (normr <= tol*normb) != 1;
    gsl_test(status, "%s random residual N=%zu normr=%.12e normb=%.12e",
             desc, N, normr, normb);

    gsl_vector_free(res);
  }

  gsl_spmatrix_free(A);
  gsl_vector_free(b);
  gsl_vector_free(x);
  gsl_splinalg_itersolve_free(w);

  if (compress)
    gsl_spmatrix_free(B);
} /* test_random() */
Beispiel #6
0
int NonLinearLSQ::curvefit() {

  size_t n(nSize());
  size_t p(nParms());

  //  Initialize the solver function information
  _nlsqPointer d = { this };
  gsl_multifit_function_fdf mf;
  mf.f      = &f;
  mf.df     = &df;
  mf.fdf    = &fdf;
  mf.n      =  n;
  mf.p      = p;
  mf.params =  &d;

  const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
  gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc(T, n, p);

  _fitParms = guess();
  gsl_vector *x = NlsqTogsl(_fitParms);
  gsl_matrix *covar = gsl_matrix_alloc(p, p);
  gsl_multifit_fdfsolver_set(s, &mf, x);

  _nIters = 0;
  checkIteration(_nIters, gslToNlsq(s->x), NLVector(p,999.0),
                  gsl_blas_dnrm2(s->f), GSL_CONTINUE);


  do {
    _nIters++;

    _status = gsl_multifit_fdfsolver_iterate(s);
    _fitParms = gslToNlsq(s->x);

    gsl_multifit_covar(s->J, 0.0, covar);
    _uncert = getUncertainty(covar);

    _status = checkIteration(_nIters, _fitParms, _uncert, gsl_blas_dnrm2(s->f),
                             _status);
    if ( _status  ) { break; }
    if(!doContinue()) { break; }

    _status = gsl_multifit_test_delta(s->dx, s->x, absErr(), relErr());
  } while ((_status == GSL_CONTINUE) && (_nIters < _maxIters));

  // Clean up
  gsl_multifit_fdfsolver_free(s);
  gsl_matrix_free(covar);

  return (_status);
}
Beispiel #7
0
Datei: lls.c Projekt: pa345/lib
int
lls_lcurve(gsl_vector *reg_param, gsl_vector *rho, gsl_vector *eta,
           lls_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;
      double smax, smin;
      size_t i;

      /* compute eigenvalues of A^T W A */
      gsl_matrix_transpose_memcpy(w->work_A, w->ATA);
      s = gsl_eigen_symm(w->work_A, w->eval, w->eigen_p);
      if (s)
        return s;

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

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

      gsl_multifit_linear_lreg(smin, smax, reg_param);

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

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

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

          /* compute: A^T A c - 2 A^T y */
          gsl_vector_memcpy(w->work_b, w->ATb);
          gsl_blas_dsymv(CblasUpper, 1.0, w->ATA, w->c, -2.0, w->work_b);

          /* compute: c^T A^T A c - 2 c^T A^T y */
          gsl_blas_ddot(w->c, w->work_b, &r2);

          r2 += w->bTb;
          gsl_vector_set(rho, i, sqrt(r2));
        }

      return GSL_SUCCESS;
    }
} /* lls_lcurve() */
Beispiel #8
0
static double
cod_householder_transform(double *alpha, gsl_vector * v)
{
  double beta, tau;
  double xnorm = gsl_blas_dnrm2(v);

  if (xnorm == 0)
    {
      return 0.0; /* tau = 0 */
    }

  beta = - (*alpha >= 0.0 ? +1.0 : -1.0) * gsl_hypot(*alpha, xnorm);
  tau = (beta - *alpha) / beta;

  {
    double s = (*alpha - beta);
    
    if (fabs(s) > GSL_DBL_MIN) 
      {
        gsl_blas_dscal (1.0 / s, v);
      }
    else
      {
        gsl_blas_dscal (GSL_DBL_EPSILON / s, v);
        gsl_blas_dscal (1.0 / GSL_DBL_EPSILON, v);
      }

    *alpha = beta;
  }
  
  return tau;
}
double MLFitterGSL::iteration(){
  //do one itteration
  //lock the model so user can not add or remove components during itteration
  modelptr->setlocked(true);
  //if the model has changed (a new or removed component eg.) create a new solver
  if (modelptr->has_changed()){
      createmodelinfo();     //this automaticaly calls initfitter() via created_modelinfo()
  }
  //do an itteration step  
  try{
    const int status=gsl_multifit_fdfsolver_iterate(solver);
    #ifdef FITTER_DEBUG
    std::cout <<"solver status: "<<gsl_strerror(status)<<"\n";
    std::cout <<"chi square/dof: "<<gsl_blas_dnrm2(solver->f)<<"\n";
    #endif
    this->setstatus(gsl_strerror(status)); //update status info of fitter
    convertxtoparam(solver->x);
    
    
    
  }
  catch(...){
    Saysomething mysay(0,"Error","Problem with call to gsl_multifit_fdfsolver_iterate, exit MLFitterGSL",true);
    delete(this);
  }
 //unlock the model so user can add or remove components
  modelptr->setlocked(false);
  //put the goodness in the goodnessqueue to check for convergence
  const double newgoodness= goodness_of_fit();
  addgoodness(newgoodness);
  return newgoodness;
}
Beispiel #10
0
static double
nmsimplex_size (nmsimplex_state_t * state)
{
  /* calculates simplex size as average sum of length of vectors 
     from simplex center to corner points:     

     ( sum ( || y - y_middlepoint || ) ) / n 
   */

  gsl_vector *s = state->ws1;
  gsl_vector *mp = state->ws2;

  gsl_matrix *x1 = state->x1;
  size_t i;

  double ss = 0.0;

  /* Calculate middle point */
  nmsimplex_calc_center (state, mp);

  for (i = 0; i < x1->size1; i++)
    {
      gsl_matrix_get_row (s, x1, i);
      gsl_blas_daxpy (-1.0, mp, s);
      ss += gsl_blas_dnrm2 (s);
    }

  return ss / (double) (x1->size1);
}
Beispiel #11
0
void update_unit(unit *u, camera *cam, PLAYERS *players) {
  if(is_unit_dead(u)) {
    if(u->state != NULL)
      unit_dead(u);
    return;
  }

  // set the velocity to 0
  gsl_vector_scale(u->velocity, 0);

  if(!((state *) u->state->value)->update(players, cam, u)) {
    pop_unit_state(u);
  }

  double norm = gsl_blas_dnrm2(u->velocity);
  if(norm > 0) {
    gsl_vector_scale(u->velocity, 1 / norm);

    gsl_vector_memcpy(u->heading, u->velocity);

    gsl_vector_scale(u->velocity, u->attributes.speed);
    gsl_vector_add(u->position, u->velocity);

    gsl_vector_set(u->side, 0, -y(u->heading));
    gsl_vector_set(u->side, 1, x(u->heading));
  }
}
Beispiel #12
0
int lsQRPT(gsl_matrix * A, gsl_vector * b, gsl_vector * x, double * sigma)
{
    int i;
    gsl_vector *tau, *res;
    gsl_permutation *p;
    gsl_vector_view norm;

    if (A->size1 < A->size2) return -1;
    if (A->size1 != b->size) return -1;
    if (A->size2 != x->size) return -1;

    tau = gsl_vector_alloc(x->size);
    res = gsl_vector_alloc(b->size);
    p = gsl_permutation_alloc(x->size);
    norm = gsl_vector_subvector(res, 0, x->size);
    gsl_linalg_QRPT_decomp(A, tau, p, &i, &norm.vector);
    gsl_linalg_QR_lssolve(A, tau, b, x, res);
    gsl_permute_vector_inverse(p, x);
    *sigma = gsl_blas_dnrm2(res);

    gsl_vector_free(tau);
    gsl_vector_free(res);
    gsl_permutation_free(p);

    return 0;
}
Beispiel #13
0
int
main (void)
{
  size_t i,j;
     
  gsl_matrix *m = gsl_matrix_alloc (10, 10);
     
  for (i = 0; i < 10; i++)
    for (j = 0; j < 10; j++)
      gsl_matrix_set (m, i, j, sin (i) + cos (j));
     
  for (j = 0; j < 10; j++)
    {
      gsl_vector_view column = gsl_matrix_column (m, j);
      double d;
     
      d = gsl_blas_dnrm2 (&column.vector);
     
      printf ("matrix column %d, norm = %g\n", j, d);
    }
     
  gsl_matrix_free (m);
     
  return 0;
}
Beispiel #14
0
/** Rotates point b around point a using the rotation matrix R. */
void rotate(bool transpose, const gsl_matrix *R, const gsl_vector *a, gsl_vector *b)
{
        declare_stack_allocated_vector(v, 3);
        gsl_vector_memcpy(v, b);
        gsl_vector_sub(v, a);

        /* Rotate end vector. */
        declare_stack_allocated_vector(w, 3);
        gsl_blas_dgemv(transpose == false ? CblasNoTrans : CblasTrans, 1.0, R, v, 0.0, w);

        /* Update position. */
        gsl_vector_memcpy(b, a);
        gsl_vector_add(b, w);

        assert(gsl_fcmp(gsl_blas_dnrm2(v), gsl_blas_dnrm2(w), 1e-15) == 0);
}
static int
conjugate_fr_set (void *vstate, gsl_multimin_function_fdf * fdf,
                  const gsl_vector * x, double *f, gsl_vector * gradient,
                  double step_size, double tol)
{
  conjugate_fr_state_t *state = (conjugate_fr_state_t *) vstate;

  state->iter = 0;
  state->step = step_size;
  state->max_step = step_size;
  state->tol  = tol;

  GSL_MULTIMIN_FN_EVAL_F_DF (fdf, x, f, gradient);

  /* Use the gradient as the initial direction */

  gsl_vector_memcpy (state->p, gradient);
  gsl_vector_memcpy (state->g0, gradient);

  {
    double gnorm = gsl_blas_dnrm2 (gradient);

    state->pnorm = gnorm;
    state->g0norm = gnorm;
  }

  return GSL_SUCCESS;
}
static void normalizeJacobian( gsl_matrix *jac, gsl_vector *scaling ) {
  for (int i = 0; i < jac->size2; i++) {
    gsl_vector jac_col = gsl_matrix_column(jac, i).vector;
    gsl_vector_set(scaling, i, 1 / gsl_blas_dnrm2(&jac_col));
    gsl_vector_scale(&jac_col, gsl_vector_get(scaling, i));
  }
}			   
Beispiel #17
0
static inline double l2norm(const mtrx* in, int n){
  double out = 0.0;
  for(int i = 0; i< in->size1; i++){
    gsl_vector_view v1 = gsl_vector_view_array(in->data+i*in->tda,n);
      out += gsl_blas_dnrm2(&v1.vector);
  }
  return out/(in->size1);
}
/*
% project v in direction of u
function p=project_vec(v,u)
p = (dot(v,u)/norm(u)^2)*u;
*/
void project_vector(gsl_vector *v, gsl_vector *u, gsl_vector *p){
    double dot_product_val, vec_norm, scalar_val; 
    gsl_blas_ddot(v, u, &dot_product_val);
    vec_norm = gsl_blas_dnrm2(u);
    scalar_val = dot_product_val/(vec_norm*vec_norm);
    gsl_vector_memcpy(p, u);
    gsl_vector_scale (p, scalar_val); 
}
Beispiel #19
0
static VALUE rb_gsl_blas_dnrm(int argc, VALUE *argv, VALUE obj)
{
  gsl_vector *x = NULL;
  double a;
  get_vector1(argc, argv, obj, &x);
  a = gsl_blas_dnrm2(x);
  return rb_float_new(a*a);
}
 void GammaDistributionFitter::printState_(size_t iter, gsl_multifit_fdfsolver * s)
 {
   printf("iter: %3u x = % 15.8f % 15.8f "
          "|f(x)| = %g\n",
          (unsigned int)iter,
          gsl_vector_get(s->x, 0),
          gsl_vector_get(s->x, 1),
          gsl_blas_dnrm2(s->f));
 }
Beispiel #21
0
void
test_lmder (gsl_multifit_function_fdf * f, double x0[], 
            double * X, double F[], double * cov)
{
  const gsl_multifit_fdfsolver_type *T;
  gsl_multifit_fdfsolver *s;

  const size_t n = f->n;
  const size_t p = f->p;

  int status;
  size_t iter = 0, i;
  
  gsl_vector_view x = gsl_vector_view_array (x0, p);

  T = gsl_multifit_fdfsolver_lmsder;
  s = gsl_multifit_fdfsolver_alloc (T, n, p);
  gsl_multifit_fdfsolver_set (s, f, &x.vector);

  do
    {
      status = gsl_multifit_fdfsolver_iterate (s);

      for (i = 0 ; i < p; i++)
        {
          gsl_test_rel (gsl_vector_get (s->x, i), X[p*iter+i], 1e-5, 
                        "lmsder, iter=%u, x%u", iter, i);
        }

      gsl_test_rel (gsl_blas_dnrm2 (s->f), F[iter], 1e-5, 
                    "lmsder, iter=%u, f", iter);

      iter++;
    }
  while (iter < 20);
  
  {
    size_t i, j;
    gsl_matrix * covar = gsl_matrix_alloc (4, 4);
    gsl_multifit_covar (s->J, 0.0, covar);

    for (i = 0; i < 4; i++) 
      {
        for (j = 0; j < 4; j++)
          {
            gsl_test_rel (gsl_matrix_get(covar,i,j), cov[i*p + j], 1e-7, 
                          "gsl_multifit_covar cov(%d,%d)", i, j) ;
          }
      }

    gsl_matrix_free (covar);
  }

  gsl_multifit_fdfsolver_free (s);

}
Beispiel #22
0
static void
test_scale_x0(gsl_vector *x0, const double scale)
{
  double nx = gsl_blas_dnrm2(x0);

  if (nx == 0.0)
    gsl_vector_set_all(x0, scale);
  else
    gsl_vector_scale(x0, scale);
} /* test_scale_x0() */
Beispiel #23
0
static double
cgst_calc_tau(const gsl_vector * p, const gsl_vector * d,
              const double delta)
{
  double norm_p, norm_d, u;
  double t1, t2, tau;

  norm_p = gsl_blas_dnrm2(p);
  norm_d = gsl_blas_dnrm2(d);

  /* compute (p, d) */
  gsl_blas_ddot(p, d, &u);

  t1 = u / (norm_d * norm_d);
  t2 = t1*u + (delta + norm_p) * (delta - norm_p);
  tau = -t1 + sqrt(t2) / norm_d;

  return tau;
}
Beispiel #24
0
Datei: lls.c Projekt: pa345/lib
int
lls_fold(gsl_matrix *A, gsl_vector *b,
         gsl_vector *wts, lls_workspace *w)
{
  const size_t n = A->size1;

  if (A->size2 != w->p)
    {
      GSL_ERROR("A has wrong size2", GSL_EBADLEN);
    }
  else if (n != b->size)
    {
      GSL_ERROR("b has wrong size", GSL_EBADLEN);
    }
  else if (n != wts->size)
    {
      GSL_ERROR("wts has wrong size", GSL_EBADLEN);
    }
  else
    {
      int s = 0;
      size_t i;
      double bnorm;

      for (i = 0; i < n; ++i)
        {
          gsl_vector_view rv = gsl_matrix_row(A, i);
          double *bi = gsl_vector_ptr(b, i);
          double wi = gsl_vector_get(wts, i);
          double swi = sqrt(wi);

          /* A <- sqrt(W) A */
          gsl_vector_scale(&rv.vector, swi);

          /* b <- sqrt(W) b */
          *bi *= swi;
        }
 
      /* ATA += A^T W A, using only the upper half of the matrix */
      s = gsl_blas_dsyrk(CblasUpper, CblasTrans, 1.0, A, 1.0, w->ATA);
      if (s)
        return s;

      /* ATb += A^T W b */
      s = gsl_blas_dgemv(CblasTrans, 1.0, A, b, 1.0, w->ATb);
      if (s)
        return s;

      /* bTb += b^T W b */
      bnorm = gsl_blas_dnrm2(b);
      w->bTb += bnorm * bnorm;

      return s;
    }
} /* lls_fold() */
Beispiel #25
0
static int vector_bfgs3_set(void *vstate, gsl_multimin_function_fdf * fdf, const gsl_vector * x, double *f, gsl_vector * gradient, double step_size, double tol)
{
	vector_bfgs3_state_t *state = (vector_bfgs3_state_t *) vstate;

	state->iter = 0;
	state->step = step_size;
	state->delta_f = 0;

	GSL_MULTIMIN_FN_EVAL_F_DF(fdf, x, f, gradient);

	/*
	 * Use the gradient as the initial direction 
	 */

	gsl_vector_memcpy(state->x0, x);
	gsl_vector_memcpy(state->g0, gradient);
	state->g0norm = gsl_blas_dnrm2(state->g0);

	gsl_vector_memcpy(state->p, gradient);
	gsl_blas_dscal(-1 / state->g0norm, state->p);
	state->pnorm = gsl_blas_dnrm2(state->p);	       /* should be 1 */
	state->fp0 = -state->g0norm;

	/*
	 * Prepare the wrapper 
	 */

	prepare_wrapper(&state->wrap, fdf, state->x0, *f, state->g0, state->p, state->x_alpha, state->g_alpha);

	/*
	 * Prepare 1d minimisation parameters 
	 */

	state->rho = 0.01;
	state->sigma = tol;
	state->tau1 = 9;
	state->tau2 = 0.05;
	state->tau3 = 0.5;
	state->order = 3;				       /* use cubic interpolation where possible */

	return GSL_SUCCESS;
}
Beispiel #26
0
double shapeAlign::cosineSim(const gsl_matrix* X, const gsl_matrix *Y)
{
	double cosSim = 0;
	double denomX = 0, denomY = 0;
	for (size_t i = 0; i < X->size1; i++){
		double dot = 0;
		// Get vector views of rows
		gsl_vector_const_view Xrow = gsl_matrix_const_row(X,i);
		gsl_vector_const_view Yrow = gsl_matrix_const_row(Y,i);

		// Compute dot product
		gsl_blas_ddot(&Xrow.vector,&Yrow.vector,&dot);
		cosSim +=dot;

		denomX += pow(gsl_blas_dnrm2(&Xrow.vector),2.0);
		denomY += pow(gsl_blas_dnrm2(&Yrow.vector),2.0);
	}
	cosSim = cosSim / (sqrt(denomX * denomY));
	return cosSim;
}
Beispiel #27
0
void
test_eigen_gensymm_results (const gsl_matrix * A, 
                            const gsl_matrix * B,
                            const gsl_vector * eval, 
                            const gsl_matrix * evec, 
                            size_t count,
                            const char * desc,
                            const char * desc2)
{
  const size_t N = A->size1;
  size_t i, j;

  gsl_vector * x = gsl_vector_alloc(N);
  gsl_vector * y = gsl_vector_alloc(N);
  gsl_vector * z = gsl_vector_alloc(N);

  /* check A v = lambda B v */
  for (i = 0; i < N; i++)
    {
      double ei = gsl_vector_get (eval, i);
      gsl_vector_const_view vi = gsl_matrix_const_column(evec, i);
      double norm = gsl_blas_dnrm2(&vi.vector);

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

      gsl_vector_memcpy(z, &vi.vector);

      /* compute y = A z */
      gsl_blas_dgemv (CblasNoTrans, 1.0, A, z, 0.0, y);

      /* compute x = B z */
      gsl_blas_dgemv (CblasNoTrans, 1.0, B, z, 0.0, x);

      /* compute x = lambda B z */
      gsl_blas_dscal(ei, x);

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

          gsl_test_rel(yj, xj, 1e9 * GSL_DBL_EPSILON, 
                       "gensymm(N=%u,cnt=%u), %s, eigenvalue(%d,%d), real, %s", N, count, desc, i, j, desc2);
        }
    }

  gsl_vector_free(x);
  gsl_vector_free(y);
  gsl_vector_free(z);
}
Beispiel #28
0
void
print_state (size_t iter, gsl_multifit_fdfsolver * s)
{
  printf ("iter: %3u x = % 15.8f % 15.8f % 15.8f "
          "|f(x)| = %g\n",
          iter,
          gsl_vector_get (s->x, 0), 
          gsl_vector_get (s->x, 1),
          gsl_vector_get (s->x, 2), 
          gsl_blas_dnrm2 (s->f));
}
Beispiel #29
0
/* ----------- evaluate the objective function --------------*/
double objective(gsl_vector *x, double lambda, gsl_vector *z, int N) {
  double obj = 0;
  double temp =0.0;
  temp = gsl_blas_dnrm2(z);
  temp = temp*temp/(double)(2.0*N);
  double foo;
  foo = gsl_blas_dasum(x);
  //  double recv;
  //  MPI_Allreduce(&foo, &recv, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
  obj = lambda*foo + temp;
  return obj;
}
Beispiel #30
0
static double
trust_calc_rho(const gsl_multilarge_nlinear_trust_state * trust_state,
               const gsl_vector * f_trial, const gsl_vector * dx,
               trust_state_t * state)
{
  int status;
  const gsl_multilarge_nlinear_parameters *params = &(state->params);
  const gsl_multilarge_nlinear_trs *trs = params->trs;
  const gsl_vector * f = trust_state->f;
  const double normf = gsl_blas_dnrm2(f);
  const double normf_trial = gsl_blas_dnrm2(f_trial);
  double rho;
  double actual_reduction;
  double pred_reduction;
  double u;

  /* if ||f(x+dx)|| > ||f(x)|| reject step immediately */
  if (normf_trial >= normf)
    return -1.0;

  /* compute numerator of rho (actual reduction) */
  u = normf_trial / normf;
  actual_reduction = 1.0 - u*u;

  /*
   * compute denominator of rho (predicted reduction); this is calculated
   * inside each trust region subproblem, since it depends on the local
   * model used, which can vary according to each TRS
   */
  status = (trs->preduction)(trust_state, dx, &pred_reduction, state->trs_state);
  if (status)
    return -1.0;

  if (pred_reduction > 0.0)
    rho = actual_reduction / pred_reduction;
  else
    rho = -1.0;

  return rho;
}