コード例 #1
0
ファイル: interpolator.c プロジェクト: nowb/lmp10
void make_spl (points_t * pts, spline_t * spl){
	int n = pts->n - 1;
	matrix_t *eqs = make_matrix(n*3, n*3+1);
	double *x = pts->x;
	double *y = pts->y;

	int i;

	for(i= 0; i < n; i++){
		double dx = x[i+1] - x[i];
		int if1 = 3*i;
		int if2 = if1+1;
		int if3 = if2+1;
		put_entry_matrix(eqs, if1, if1, dx);
		put_entry_matrix(eqs, if1, if2, dx*dx/2);
		put_entry_matrix(eqs, if1, if3, dx*dx*dx/6);
		put_entry_matrix(eqs, if1, n*3, y[i+1]-y[i]);
		put_entry_matrix(eqs, if2, if1, 1);
		put_entry_matrix(eqs, if2, if2, dx);
		put_entry_matrix(eqs, if2, if3, dx*dx/2);
		if(if3+1 < n*3)
			put_entry_matrix(eqs, if2, if3+1, -1);
		else
			put_entry_matrix(eqs, if2, if1, 0);
		put_entry_matrix(eqs, if3, if2, 1);
		put_entry_matrix(eqs, if3, if3, dx);
		if(if3+2 < n*3)
			put_entry_matrix(eqs, if3, if3+2, -1);
	}

#ifdef DEBUG
	write_matrix(eqs, stdout);
#endif

	if(piv_ge_solver(eqs)) {
		spl->n = 0;
		return;
	}

#ifdef DEBUG
	write_matrix(eqs, stdout);
#endif

	if(alloc_spl(spl, pts->n) == 0){
		spl->n = pts->n;
		for(i = 0; i < n; i++) {
			spl->x[i] = pts->x[i];
			spl->f[i] = pts->y[i];
			spl->f1[i] = get_entry_matrix(eqs, 3*i,   3*n);
			spl->f2[i] = get_entry_matrix(eqs, 3*i+1, 3*n);
			spl->f3[i] = get_entry_matrix(eqs, 3*i+2, 3*n);
		}
		spl->x[n] = pts->x[n];
		spl->f[n] = pts->y[n];
		spl->f1[n] = spl->f1[n-1];
		spl->f2[n] = 0;
		spl->f3[n] = 0;
	}
}
コード例 #2
0
void
make_spl(points_t * pts, spline_t * spl)
{

    double         *x = pts->x;
    double         *y = pts->y;
    int		i, j, m, n=pts->n;
    double a0=0.0,a[n],b[n], p = x[0], q = x[n-1],dt;

    m = (n-1)/2;

    for(i=0; i<n ; i++)
    {
        a0+=y[i];
    }
    a0/=n;

    for(i=0; i<m ; i++)
    {
        a[i]=0.0;
        for(j=0; j<n ; j++)
        {
            a[i]+=y[j]*cos((2*M_PI*i*j)/n);
        }
        a[i]*=2;
        a[i]/=n;

        b[i]=0.0;
        for(j=0; j<n ; j++)
        {
            b[i]+=y[j]*sin((2*M_PI*i*j)/n);
        }
        b[i]*=2;
        b[i]/=n;
    }




    if (alloc_spl(spl, n) == 0)
    {
        for(i = 0 ; i < n ; i++)
        {
            double xx = spl->x[i] = p + i*(q-p)/(m-1);
            spl->f[i] = 0;
            spl->f1[i] = 0;
            spl->f2[i] = 0;
            spl->f3[i] = 0;
            for (j = 0 ; j < m ; j++)
            {
                dt = 2.0*M_PI*j/n;
                spl->f[i] += a[j]*cos(dt*xx) + b[j]*sin(dt*xx);
                spl->f1[i] += (-1)*(dt*(a[j]*sin(dt*xx) - b[j]*cos(dt*xx)));
                spl->f2[i] += (-1)*dt*dt*(a[j]*cos(dt*xx) + b[j]*sin(dt*xx));
                spl->f3[i] += pow(dt,3)*(a[j]*sin(dt*xx) - b[j]*cos(dt*xx));
            }
        }
    }
}
コード例 #3
0
ファイル: tryg.c プロジェクト: Danalscy/lmp10
void make_spl (points_t * pts, spline_t * spl){
	
	if (alloc_spl(spl, pts->n) == 0) {
        	spl->x = pts->x;
	
		int i,j;
		int n=pts->n;
		int L;
		int nb;
		if(n % 2)
			L=((n-1)/2);
		else
			L=(n/2);
		char *nbEnv= getenv( "APPROX_BASE_SIZE" );
        	if( nbEnv != NULL && atoi( nbEnv ) > 0 && atoi(nbEnv)<(n/2))
                	nb = atoi( nbEnv );
		else
			nb=L-1;

		for( i=0; i<n; i++)
                	spl->a[0]+=(1.0/L)*pts->y[i];
		
		spl->b[0]=0;
		for(j=1;j<=nb;j++){
			spl->a[j]=0.0;
			spl->b[j]=0.0;
			for(i=0;i<n;i++){
				spl->a[j]+=(1.0/L)*pts->y[i]*cos((M_PI*i*j)/L);	
				spl->b[j]+=(1.0/L)*pts->y[i]*sin((M_PI*i*j)/L);
			}
		}
		for(j=0;j<n;j++){
			spl->f[j]=spl->a[0]/2;
			spl->f1[j]=0.0;
			spl->f2[j]=0.0;
			spl->f3[j]=0.0;
			spl->f4[j]=0.0;
			double tmp=spl->x[j];
			for(i=1;i<=nb;i++){
                		spl->f[j]+=spl->a[i]*cos(i * (tmp)) + spl->b[i]*sin(i * (tmp));
                		spl->f1[j]+= -i * spl->a[i]*sin(i*(tmp)) + i *spl->b[i]*cos(i*(tmp));
                		spl->f2[j]+= -i * i *spl->a[i]*cos(i*(tmp)) - i * i * spl->b[i]*sin(i*(tmp));
                		spl->f3[j]+=  i * i * i * spl->a[i]*sin(i*(tmp)) - i * i * i * spl->b[i]*cos(i*(tmp));
				spl->f4[j]+=  i * i * i * i * spl->a[i]*cos(i*(tmp)) + i * i * i * i * spl->b[i]*sin(i*(tmp));
        		}
		}
	}
 }
コード例 #4
0
ファイル: chebyshev_approx.c プロジェクト: churskir/lmp10.2
void
make_spl(points_t * pts, spline_t * spl) {
  matrix_t *eqs = NULL;
  double *x = pts->x;
  double *y = pts->y;
  double a = x[0];
  double b = x[pts->n - 1];
  int i, j, k;
  int nb = pts->n - 3 > 10 ? 10 : pts->n - 3;
  char *nbEnv = getenv("APPROX_BASE_SIZE");

  if (nbEnv != NULL && atoi(nbEnv) > 0)
    nb = atoi(nbEnv);

  eqs = make_matrix(nb, nb + 1);
  for (j = 0; j < nb; j++) {
    for (i = 0; i < nb; i++)
      for (k = 0; k < pts->n; k++)
        add_to_entry_matrix(eqs, j, i, fi(i, x[k]) * fi(j, x[k]));

    for (k = 0; k < pts->n; k++)
      add_to_entry_matrix(eqs, j, nb, y[k] * fi(j, x[k]));
  }

  if (piv_ge_solver(eqs)) {
    spl->n = 0;
    return;
  }

  if (alloc_spl(spl, nb) == 0) {
    for (i = 0; i < spl->n; i++) {
      double xx = spl->x[i] = a + i * (b - a) / (spl->n - 1);
      xx += 10.0 * DBL_EPSILON;  //by nie ulokować punktu w poprz. przedziale
      spl->f[i] = 0;
      spl->f1[i] = 0;
      spl->f2[i] = 0;
      spl->f3[i] = 0;
      for (k = 0; k < nb; k++) {
        double ck = get_entry_matrix(eqs, k, nb);
        spl->f[i] += ck * fi (k, xx);
        spl->f1[i] += ck * dfi (k, xx);
        spl->f2[i] += ck * d2fi(k, xx);
        spl->f3[i] += ck * d3fi(k, xx);
      }
    }
  }
}
コード例 #5
0
ファイル: splines.c プロジェクト: DarekDarecki/PW
int
read_spl (FILE * inf, spline_t * spl)
{
  int i;
  if (fscanf (inf, "%d", &(spl->n)) != 1 || spl->n < 0)
    return 1;

  if (alloc_spl (spl, spl->n))
    return 1;

  for (i = 0; i < spl->n; i++)
    if (fscanf
        (inf, "%lf %lf %lf %lf %lf", spl->x + i, spl->f + i, spl->f1 + i,
         spl->f2 + i, spl->f3 + i) != 5)
      return 1;

  return 0;
}
コード例 #6
0
ファイル: hermite.c プロジェクト: DarekDarecki/PW
void make_spl(points_t * pts, spline_t * spl){
	matrix_t *eqs= NULL;
	double *x = pts->x;
	double *y = pts->y;
	int	i, j, k;
	int	n = pts->n > 8 ? 8 : pts->n;
	double		a = x[0];
	double		b = x[pts->n - 1];
	
	char *nEnv= getenv( "APPROX_BASE_SIZE" );
	if( nEnv != NULL && atoi( nEnv ) > 0 )
		n = atoi( nEnv );
	
	eqs = make_matrix(n, n+1);
	
	wypelnijMacierze(eqs, pts, n);
	
	if (piv_ge_solver(eqs)) {
		spl->n = 0;
		return;
	}
	
	if (alloc_spl(spl, n) == 0) {
		for (i = 0; i < spl->n; i++) {
			double xx = spl->x[i] = a + i * (b - a)/(spl->n - 1);
			xx+= 10.0 * DBL_EPSILON; // zabezpieczenie przed ulokowaniem punktu w poprzednim przedziale
			spl->f[i] = 0;
			spl->f1[i] = 0;
			spl->f2[i] = 0;
			spl->f3[i] = 0;
			for (k = 0; k < n; k++) {
				double	ck = get_entry_matrix(eqs, k, n);
				spl->f[i] += ck * hermite(xx, k);
				spl->f1[i] += ck * hermiteD1(xx, k);
				spl->f2[i] += ck * hermiteD2(xx, k);
				spl->f3[i] += ck * hermiteD3(xx, k);
			}
		}
	}
	free(eqs);
	free(x);
	free(y);
}
コード例 #7
0
ファイル: old_aproksymator.c プロジェクト: radziszk/lmp10
void
make_spl(points_t * pts, spline_t * spl)
{

	matrix_t       *eqs= NULL;
	double         *x = pts->x;
	double         *y = pts->y;
	double		a = x[0];
	double		b = x[pts->n - 1];
	int		i, j, k;
	int		nb = pts->n - 3 > 10 ? 10 : pts->n - 3;
  char *nbEnv= getenv( "APPROX_BASE_SIZE" );

	if( nbEnv != NULL && atoi( nbEnv ) > 0 )
		nb = atoi( nbEnv );

	eqs = make_matrix(nb, nb + 1);

#ifdef DEBUG
#define TESTBASE 500
	{
		FILE           *tst = fopen("debug_base_plot.txt", "w");
		double		dx = (b - a) / (TESTBASE - 1);
		for( j= 0; j < nb; j++ )
			xfi( a, b, nb, j, tst );
		for (i = 0; i < TESTBASE; i++) {
			fprintf(tst, "%g", a + i * dx);
			for (j = 0; j < nb; j++) {
				fprintf(tst, " %g", fi  (a, b, nb, j, a + i * dx));
				fprintf(tst, " %g", dfi (a, b, nb, j, a + i * dx));
				fprintf(tst, " %g", d2fi(a, b, nb, j, a + i * dx));
				fprintf(tst, " %g", d3fi(a, b, nb, j, a + i * dx));
			}
			fprintf(tst, "\n");
		}
		fclose(tst);
	}
#endif

	for (j = 0; j < nb; j++) {
		for (i = 0; i < nb; i++)
			for (k = 0; k < pts->n; k++)
				add_to_entry_matrix(eqs, j, i, fi(a, b, nb, i, x[k]) * fi(a, b, nb, j, x[k]));

		for (k = 0; k < pts->n; k++)
			add_to_entry_matrix(eqs, j, nb, y[k] * fi(a, b, nb, j, x[k]));
	}
#ifdef DEBUG
	write_matrix(eqs, stdout);
#endif

	if (piv_ge_solver(eqs)) {
		spl->n = 0;
		return;
	}
#ifdef DEBUG
	write_matrix(eqs, stdout);
#endif
	if (alloc_spl(spl, nb) == 0) {
		for (i = 0; i < spl->n; i++) {
			double xx = spl->x[i] = a + i*(b-a)/(spl->n-1);
			xx+= 10.0*DBL_EPSILON;  // zabezpieczenie przed ulokowaniem punktu w poprzednim przedziale
			spl->f[i] = 0;
			spl->f1[i] = 0;
			spl->f2[i] = 0;
			spl->f3[i] = 0;
			for (k = 0; k < nb; k++) {
				double		ck = get_entry_matrix(eqs, k, nb);
				spl->f[i]  += ck * fi  (a, b, nb, k, xx);
				spl->f1[i] += ck * dfi (a, b, nb, k, xx);
				spl->f2[i] += ck * d2fi(a, b, nb, k, xx);
				spl->f3[i] += ck * d3fi(a, b, nb, k, xx);
			}
		}
	}

#ifdef DEBUG
	{
		FILE           *tst = fopen("debug_spline_plot.txt", "w");
		double		dx = (b - a) / (TESTBASE - 1);
		for (i = 0; i < TESTBASE; i++) {
			double yi= 0;
			double dyi= 0;
			double d2yi= 0;
			double d3yi= 0;
			double xi= a + i * dx;
			for( k= 0; k < nb; k++ ) {
							yi += get_entry_matrix(eqs, k, nb) * fi(a, b, nb, k, xi);
							dyi += get_entry_matrix(eqs, k, nb) * dfi(a, b, nb, k, xi);
							d2yi += get_entry_matrix(eqs, k, nb) * d2fi(a, b, nb, k, xi);
							d3yi += get_entry_matrix(eqs, k, nb) * d3fi(a, b, nb, k, xi);
			}
			fprintf(tst, "%g %g %g %g %g\n", xi, yi, dyi, d2yi, d3yi );
		}
		fclose(tst);
	}
#endif
}