예제 #1
0
파일: largefit.c 프로젝트: FMX/gsl
int
main(int argc, char *argv[])
{
  const size_t n = 50000;   /* number of observations */
  const size_t p = 15;      /* polynomial order + 1 */
  double lambda = 0.0;      /* regularization parameter */
  gsl_vector *c_tsqr = gsl_vector_alloc(p);
  gsl_vector *c_normal = gsl_vector_alloc(p);

  if (argc > 1)
    lambda = atof(argv[1]);

  /* solve system with TSQR method */
  solve_system(gsl_multilarge_linear_tsqr, lambda, n, p, c_tsqr);

  /* solve system with Normal equations method */
  solve_system(gsl_multilarge_linear_normal, lambda, n, p, c_normal);

  /* output solutions */
  {
    gsl_vector *v = gsl_vector_alloc(p);
    double t;

    for (t = 10.0; t <= 11.0; t += 0.01)
      {
        double f_exact = func(t);
        double f_tsqr, f_normal;

        build_row(t, v);
        gsl_blas_ddot(v, c_tsqr, &f_tsqr);
        gsl_blas_ddot(v, c_normal, &f_normal);

        printf("%f %e %e %e\n", t, f_exact, f_tsqr, f_normal);
      }

    gsl_vector_free(v);
  }

  gsl_vector_free(c_tsqr);
  gsl_vector_free(c_normal);

  return 0;
}
예제 #2
0
int input_output_on_files ( void )
{
   int fail,rc;

   fail = syscon_read_system();
   printf("\nThe system in the container : \n");
   fail = syscon_write_system();
   fail = solve_system(&rc);
   printf("\nThe root count : %d\n",rc);
   printf("\nThe solutions :\n");
   fail = solcon_write_solutions();

   return fail;
}
예제 #3
0
bool calc_weights(
    const coord_t & center,
    std::vector<coord_t> & coords,
    const primary_cov_model_t & primary_cov,
    const cross_cov_model_t & cross_cov,
    double secondary_variance,
    std::vector<kriging_weight_t> & weights
)
{
    std::vector<double> A;
    std::vector<double> b;

    // build system
    build_system(center, coords, primary_cov, cross_cov, secondary_variance, A, b);
    // solve systemk
    return solve_system(A, b, weights);
}
예제 #4
0
int standard_interactive_input_output ( void )
{
   int n,fail,k,nc,i,rc,len,nbtasks;
   char ch,p[800];

   printf("\nGive the number of polynomials in the system : ");
   scanf("%d",&n);

   fail = syscon_initialize_number_of_standard_polynomials(n);
   printf("\nReading %d polynomials, ",n);
   printf("terminate each with ; (semicolon)...\n");
   for(k=1; k<=n; k++)
   { 
      printf("-> polynomial %d : ",k);
      ch = getchar();    /* skip previous newline symbol */
      read_poly(&nc,p);
     /* printf("  p[%d] = ",k); 
        for(i=0; i<nc; i++) printf("%c",p[i]);
        printf("\n"); */
      fail = syscon_store_standard_polynomial(nc,n,k,p);
   }
   printf("The system in the container : \n");
   syscon_write_standard_system();
   printf("\nGive the number of tasks : "); scanf("%d",&nbtasks);
   fail = solve_system(&rc,nbtasks);
   printf("\nThe root count : %d\n",rc);
   /* printf("\nThe solutions :\n");
   fail = solcon_write_standard_solutions(); */
   printf("interactive selection of solutions... \n");
   do
   {
      printf("\nGive an index to a solution (-1 to exit) : ");
      scanf("%d",&k);
      if(k < 0) break;
      fail = solcon_length_standard_solution_string(k,&len);
      {
         char s[len];
         fail = solcon_write_standard_solution_string(k,len,s);
         printf("\nSolution %d : \n%s",k,s);
      } 
   } while (k >= 0);

   return fail;
}
예제 #5
0
int input_output_on_files ( int precision )
{
   int fail,rc,nbtasks;

   if(precision == 0)
   {
      fail = syscon_read_standard_system();
      printf("\nThe system in the container : \n");
      fail = syscon_write_standard_system();
      printf("\nGive the number of tasks : "); scanf("%d",&nbtasks);
      fail = solve_system(&rc,nbtasks);
      printf("\nThe root count : %d\n",rc);
      printf("\nThe solutions :\n");
      fail = solcon_write_standard_solutions();
   }
   else if(precision == 1)
   {
      fail = syscon_read_dobldobl_system();
      printf("\nThe system in the container : \n");
      fail = syscon_write_dobldobl_system();
      printf("\nGive the number of tasks : "); scanf("%d",&nbtasks);
      fail = solve_dobldobl_system(&rc,nbtasks);
      printf("\nThe root count : %d\n",rc);
      printf("\nThe solutions :\n");
      fail = solcon_write_dobldobl_solutions();
   }
   else if(precision == 2)
   {
      fail = syscon_read_quaddobl_system();
      printf("\nThe system in the container : \n");
      fail = syscon_write_quaddobl_system();
      printf("\nGive the number of tasks : "); scanf("%d",&nbtasks);
      fail = solve_quaddobl_system(&rc,nbtasks);
      printf("\nThe root count : %d\n",rc);
      printf("\nThe solutions :\n");
      fail = solcon_write_quaddobl_solutions();
   }

   return fail;
}
예제 #6
0
int main(int argc, char** argv)

{
    int D=10;
    srand(time(0));
    double *A;
    double *B;
    init_matrix(&A,D,D);
    init_matrix(&B,D,1);
    printf("The first input matrix is:\n");
    print_matrix(A,D,D);
    printf("The Second input matrix is:\n");
    print_matrix(B,D,1);
    double *x = malloc(D*sizeof(double));
    solve_system(A, D, B, x);
    printf("The result is:\n");
    print_matrix(x,D,1);
    printf("\n");


    return 0;
}
예제 #7
0
파일: nlfit4.c 프로젝트: liigo/gsl-vc2015
int
main (void)
{
  const size_t p = 2000;
  const size_t n = p + 1;
  gsl_vector *f = gsl_vector_alloc(n);
  gsl_vector *x = gsl_vector_alloc(p);

  /* allocate sparse Jacobian matrix with 2*p non-zero elements in triplet format */
  gsl_spmatrix *J = gsl_spmatrix_alloc_nzmax(n, p, 2 * p, GSL_SPMATRIX_TRIPLET);

  gsl_multilarge_nlinear_fdf fdf;
  gsl_multilarge_nlinear_parameters fdf_params =
    gsl_multilarge_nlinear_default_parameters();
  struct model_params params;
  size_t i;

  params.alpha = 1.0e-5;
  params.J = J;

  /* define function to be minimized */
  fdf.f = penalty_f;
  fdf.df = penalty_df;
  fdf.fvv = penalty_fvv;
  fdf.n = n;
  fdf.p = p;
  fdf.params = &params;

  for (i = 0; i < p; ++i)
    {
      /* starting point */
      gsl_vector_set(x, i, i + 1.0);

      /* store sqrt(alpha)*I_p in upper p-by-p block of J */
      gsl_spmatrix_set(J, i, i, sqrt(params.alpha));
    }

  fprintf(stderr, "%-25s %-4s %-4s %-5s %-6s %-4s %-10s %-10s %-7s %-11s %-10s\n",
          "Method", "NITER", "NFEV", "NJUEV", "NJTJEV", "NAEV", "Init Cost",
          "Final cost", "cond(J)", "Final |x|^2", "Time (s)");
  
  fdf_params.scale = gsl_multilarge_nlinear_scale_levenberg;

  fdf_params.trs = gsl_multilarge_nlinear_trs_lm;
  solve_system(x, &fdf, &fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_lmaccel;
  solve_system(x, &fdf, &fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_dogleg;
  solve_system(x, &fdf, &fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_ddogleg;
  solve_system(x, &fdf, &fdf_params);

  fdf_params.trs = gsl_multilarge_nlinear_trs_cgst;
  solve_system(x, &fdf, &fdf_params);

  gsl_vector_free(f);
  gsl_vector_free(x);
  gsl_spmatrix_free(J);

  return 0;
}
/*******************************************************************************
Given a set of matches, compute the "best fitting" homography.

    matches: matching points
    numMatches: number of matching points
    h: returned homography
    isForward: direction of the projection (true = image1 -> image2, false = image2 -> image1)
*******************************************************************************/
bool MainWindow::ComputeHomography(CMatches *matches, int numMatches, double h[3][3], bool isForward)
{
    int error;
    int nEq=numMatches*2;

    dmat M=newdmat(0,nEq,0,7,&error);
    dmat a=newdmat(0,7,0,0,&error);
    dmat b=newdmat(0,nEq,0,0,&error);

    double x0, y0, x1, y1;

    for (int i=0;i<nEq/2;i++)
    {
        if(isForward == false)
        {
            x0 = matches[i].m_X1;
            y0 = matches[i].m_Y1;
            x1 = matches[i].m_X2;
            y1 = matches[i].m_Y2;
        }
        else
        {
            x0 = matches[i].m_X2;
            y0 = matches[i].m_Y2;
            x1 = matches[i].m_X1;
            y1 = matches[i].m_Y1;
        }


        //Eq 1 for corrpoint
        M.el[i*2][0]=x1;
        M.el[i*2][1]=y1;
        M.el[i*2][2]=1;
        M.el[i*2][3]=0;
        M.el[i*2][4]=0;
        M.el[i*2][5]=0;
        M.el[i*2][6]=(x1*x0*-1);
        M.el[i*2][7]=(y1*x0*-1);

        b.el[i*2][0]=x0;
        //Eq 2 for corrpoint
        M.el[i*2+1][0]=0;
        M.el[i*2+1][1]=0;
        M.el[i*2+1][2]=0;
        M.el[i*2+1][3]=x1;
        M.el[i*2+1][4]=y1;
        M.el[i*2+1][5]=1;
        M.el[i*2+1][6]=(x1*y0*-1);
        M.el[i*2+1][7]=(y1*y0*-1);

        b.el[i*2+1][0]=y0;

    }
    int ret=solve_system (M,a,b);
    if (ret!=0)
    {
        freemat(M);
        freemat(a);
        freemat(b);

        return false;
    }
    else
    {
        h[0][0]= a.el[0][0];
        h[0][1]= a.el[1][0];
        h[0][2]= a.el[2][0];

        h[1][0]= a.el[3][0];
        h[1][1]= a.el[4][0];
        h[1][2]= a.el[5][0];

        h[2][0]= a.el[6][0];
        h[2][1]= a.el[7][0];
        h[2][2]= 1;
    }

    freemat(M);
    freemat(a);
    freemat(b);

    return true;
}
예제 #9
0
void MyMainWindow::build_spline(){
  delete[] c;
  delete[] x;
  delete[] f_x;

  int i; double temp1,temp2;
  double omega= (finish-start)/(count_points-1);
  x= new double[count_points];
  f_x= new double[count_points];
  c=new double[count_points*4];

  double *mid_diag = 0;
  mid_diag=new double[count_points];
  double *top_diag = 0;
  top_diag=new double[count_points];
  double *bot_diag = 0;
  bot_diag=new double[count_points];
  double *d = 0;
  d=new double[count_points];
  
 for(i=0;i<count_points;++i){
    x[i]=start+i*omega;
    f_x[i]=f(x[i]);
  };
#ifndef SHORT_WAY
  for (i = 1; i <count_points- 1; ++i){
    temp1 = (f_x[i] - f_x[i - 1]) / (x[i] - x[i - 1]);
    temp2 = (f_x[i + 1] - f_x[i]) / (x[i + 1] - x[i]);
    c[i] = 3 * temp1 * (x[i + 1] - x[i]) + 3 * temp2 * (x[i] - x[i - 1]);
    mid_diag[i] = 2.0 * (x[i + 1] - x[i - 1]);
    top_diag[i] = x[i] - x[i - 1];
    bot_diag[i - 1] = x[i + 1] - x[i];
  }
  mid_diag[0] = x[2]-x[1];
  top_diag[0] = x[2]-x[0];
  mid_diag[count_points - 1] = x[count_points-2]-x[count_points-3];
  bot_diag[count_points - 2] = x[count_points-1]-x[count_points-3];
  c[count_points- 1] = ((f_x[count_points-2]-f_x[count_points-3])/(x[count_points-2]-x[count_points-3])*(x[count_points-1]-x[count_points-2])*(x[count_points-1]-x[count_points-2]) + (f_x[count_points-1]-f_x[count_points-2])/(x[count_points-1]-x[count_points-2])*(x[count_points-2]-x[count_points-3])*(3*x[count_points-1]-2*x[count_points-3]-x[count_points-2]))/(x[count_points-1]-x[count_points-3]);
  c[0] = ((f_x[2]-f_x[1])/(x[2]-x[1])*(x[1]-x[0])*(x[1]-x[0]) + (f_x[1]-f_x[0])/(x[1]-x[0])*(x[2]-x[1])*(2*x[2]+x[1]-3*x[0]))/(x[2]-x[0]);
#endif
#ifdef SHORT_WAY
  for (i = 1; i <count_points- 1; ++i){
    temp1 = (f_x[i] - f_x[i - 1]);
    temp2 = (f_x[i + 1] - f_x[i]);
    c[i] = 3 * temp1 + 3 * temp2;
    mid_diag[i] = omega * 4;
    top_diag[i] = omega;
    bot_diag[i - 1] = omega;
  }
  mid_diag[0] = omega;
  top_diag[0] = omega * 2;
  mid_diag[count_points - 1] = omega;
  bot_diag[count_points - 2] = omega * 2;
  c[count_points- 1] = ((f_x[count_points-2]-f_x[count_points-3]) + (f_x[count_points-1]-f_x[count_points-2])*5)/2.;
  c[0] = ((f_x[2]-f_x[1]) + (f_x[1]-f_x[0])*5)/2.;
#endif
  solve_system(mid_diag, top_diag, bot_diag, c, d);
  
  for (i = 0; i < count_points-1; i++){
    c[4*i+ 0] = f_x[i];
    c[4*i+ 1] = d[i];
    temp2 = x[i + 1] - x[i];
    temp1 = (f_x[i + 1] - f_x[i]) / temp2;
    c[4*i+ 2] = (3*temp1 - 2*d[i] - d[i + 1]) / temp2;
    temp2 *= temp2;
    c[4*i+ 3] = (d[i] + d[i + 1] - 2*temp1) / temp2;
  }
  delete[] top_diag;
  delete[] bot_diag;
  delete[] mid_diag;
  delete[] d;
}