int CGSolver(SparseMatrix A,    // CSR format matrix
             const std::vector<double> &b,
             std::vector<double> &u,
             double tol,
             std::map<int, std::vector<double>> &soln_iter) {
    unsigned int n_iter = 0;
    long unsigned int max_iter = A.GetRows();

    soln_iter[n_iter] = u;    // store initial state
    auto A_u = A.MulVec(u);   // A*u
    std::vector<double> r = subvec(b, A_u);   // b - A*u
    auto L2normr0 = L2norm(r);
    std::vector<double> p = r;    // p =r

    while (n_iter < max_iter) {
        n_iter++;
        auto A_p = A.MulVec(p);                         // A * p
        double alpha = vecvec(r, r) / vecvec(p, A_p);   // (r * r)/(p * A * p)

        auto alpha_p = scalvec(alpha, p);   // alpha * p
        u = addvec(u, alpha_p);             // u = u + alpha * p

        /**
         * alpha * A * p
         * r_n+1 = r_n - alpha * A * p
         */
        std::vector<double> alpha_A_p = scalvec(alpha, A_p);
        std::vector<double> r_next = subvec(r, alpha_A_p);

        auto L2normr = L2norm(r_next);  // L2normr = L2norm(r_n+1)

        if (L2normr / L2normr0 < tol) {
            soln_iter[n_iter] = u;
            std::cout << "SUCCESS: CG solver converged in "
                      << n_iter << " iterations" << std::endl;
            break;
        } else {
            double beta = vecvec(r_next, r_next) / vecvec(r, r);
            auto beta_p = scalvec(beta, p);

            p = addvec(r_next, beta_p);
            r = r_next;

            if ((n_iter % 10 == 0) || (n_iter == 0)) {
                soln_iter[n_iter] = u;
            }
            if (n_iter == max_iter) {
                std::cout << "FAILURE: CG solver failed to converge" << std::endl;
                return 1;
            }
        }
    }
    return 0;
}
/* 
    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);
    }
}
Exemple #3
0
YtypeScalar norm(grid2D<Ytype,YtypeScalar,Xtype  > & rhs)
{
	return L2norm(rhs);
}
Exemple #4
0
int main()
{
    std_setup();
    
    int n = 256;
    double x0 = 10;
    
    double tf = 10.0;
    double dt = 0.05;
    int expansion_order = 7;
    int hdaf_order = 8;
    
    grid2D<double,double,double > grid( n,-10,10.0, n,-10,10.0 ),u,v,C2,damping;
    grid = 0.0;
    
    u = grid;
    v = grid;
    C2 = grid;
    damping = grid;
    
    u = u_0_func();
    C2 = C2_func();
    damping = damping_func();
    
    sprintf(fname,"/workspace/output/acoustic_propagate_2d/out_dat/C2.dat" );
    writeFile(C2,fname);
    
    
    void * pdata = 0;
    method1_init( &pdata, grid.n1, grid.n2, grid.dx1(), grid.dx2(), C2.array, damping.array, 7, hdaf_order, hdaf_order, 0.8, 0.8 );
    
    //acoustic_propagator P;
    //P.init( grid, 7, C2, damping );
    
    double t = 0.0;
    
    cout << "step: " << t << "\t" << L2norm(u) << endl;
    
    n=0;
    double * b_times = new double [ int(tf/dt+5) ];
    
    while (t <= tf)
    {
    
        output(t,u,v);
        
        double t1 = get_real_time();
        
        method1_execute( pdata, dt, u.array,v.array,u.array,v.array );
        //P( dt, u,v,u,v );
        
        double t2 = get_real_time();
        
        b_times[n] = t2-t1;
        double mean = 0;
        for (int j=0; j<n; j++)
            mean += b_times[j]/n;
        cout << mean << endl;
        n++;
        
        
        double mag = L2norm(u);
        cout << "step: " << t << "\t" << t2-t1 << "\t" << mag << endl;
        
        t += dt;
        
        if ( mag > 1E10 ) break;
    }
       
    output(t,u,v);
}