Ejemplo n.º 1
0
void manual_test ( int n )
{
   int i, j, m[n+1], k;
   dcmplx x[n+1], f[n+1], ff[n+1], tmp;
   double tol=1.0e-8;

   printf("multiple points test:\n");  
   printf("x[i] are multiple points,please give the f value like this:\n");
   printf("x[i], function value at the x[i],\n");
   printf("x[i+1], the first order derivative function value at x[i],\n");
   printf("x[i+2], the second order derivative function value at x[i].\n");
 
   for(i=0; i<=n; i++ ) 
   {
     printf("input x[%d]:\n", i);
     read_dcmplx(&x[i]);
     printf("input the multiplicity of the point\n");
     scanf("%d", &(m[i]));
     printf("input f[%d]:\n", i);
     read_dcmplx(&f[i]);
   }

   group_points(n+1, tol, x, f, m);
   for(i=0; i<=n; i++)
     ff[i]=f[i];
   divided_difference ( n+1,  x, f, m );

   printf("the result polynomial is:\n");
   for(j=0; j<=n; j++)
   {
      printf("f[%d]=", j);
      writeln_dcmplx(f[j]);
   }
   for(i=0; i<=n; i++)
   {
     printf("the point x with multiplicity %d is: ", m[i]);
     writeln_dcmplx(x[i]);
     printf("the given function value at point x is:");
     writeln_dcmplx(ff[i]);
     printf("the result polynomial value at point x is:");
     tmp=horner(n+1, f, x[i]);
     writeln_dcmplx(tmp);
     printf("\n");
     if(m[i]>1)  /*skip the multiple points*/
     {
       k=m[i];
       while(k>1)
       {
         i++;
         k--;
       }  
     }
   }
}
/* 
    data: N data instance
    assignment: N by 1 vector indicating assignment of one atom
*/
void compute_means (vector<Instance*>& data, vector<int>& assignment, int FIX_DIM, vector< vector<double> >& means) {
    int N = data.size();
    assert (assignment.size() == N);
    int nClusters = means.size();  // for 0-index
    vector< vector<int> > group_points (nClusters, vector<int>());
    for (int i = 0; i < N; i ++) 
        group_points[assignment[i]].push_back(i);
    for (int c = 0; c < nClusters; c++) {
        int num_points = group_points[c].size();
        if (num_points == 0) {
            std::fill(means[c].begin(), means[c].end(), -INF);
            continue;
        }
        std::fill(means[c].begin(), means[c].end(), 0.0);
        double ** local_dist_mat = mat_init(num_points, num_points);
        for (int x = 0; x < num_points; x++) {
            for (int y = 0; y < num_points; y++) {
                Instance* a = data[group_points[c][x]];
                Instance* b = data[group_points[c][y]];
                double dist = L2norm(a, b, FIX_DIM);
                local_dist_mat[x][y] = dist * dist;
            }
        }
        double* sum_dist = new double [num_points];
        mat_sum_col(local_dist_mat, sum_dist, num_points, num_points);
        int min_index = -1;
        double min_value = INF;
        for (int j = 0; j < num_points; j++) 
            if (sum_dist[j] < min_value) {
                min_index = j;
                min_value = sum_dist[j];
            }
        assert (min_index < num_points);
        Instance* new_medoid = data[group_points[c][min_index]];
        for (int f = 0; f < new_medoid->fea.size(); f ++)
            means[c][new_medoid->fea[f].first-1] = new_medoid->fea[f].second ;
        delete [] sum_dist;
        mat_free (local_dist_mat, num_points, num_points);
    }
}