Beispiel #1
0
int
main(void)
{
    int i;
    flint_rand_t state;

    printf("init/ clear... ");
    fflush(stdout);

    _randinit(state);

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
/**
* Main function
*
* @param argc
* @param argv
*/
int main(int argc, char **argv)
{
    // create PAPI routines
    PapiCounterList papi_routines;
    papi_routines.AddRoutine("matvec");

    // allocate arrays
    float a[ROWS][COLS];
    float b[COLS];
    float c[ROWS] = {0.0f};

    printf("Rows: %d\n", ROWS);
    printf("Cols: %d\n", COLS);
    printf("Runs: %d\n", RUNS);

    // initialize matrix & vector
    mat_init(ROWS, COLS, 1.0, a);
    vec_init(COLS, 3.0, b);

    // do the measurement
    papi_routines["matvec"].Start();
    for (unsigned i = 0; i < RUNS; i++)
        mat_vec_mul(ROWS, COLS, a, b, c);
    papi_routines["matvec"].Stop();

    // print results
    printf("Control sum: %f\n", vec_sum(ROWS, c));
    printf("\n");
    papi_routines.PrintScreen();

    return EXIT_SUCCESS;
}
double second_subproblem_obj (double ** ytwo, double ** z, double ** wtwo, double rho, int N, double* lambda) {

    double ** temp = mat_init (N, N);
    double ** difftwo = mat_init (N, N);
    mat_zeros (difftwo, N, N);

    // reg = 0.5 * sum_k max_n | w_nk |  -> group-lasso
    mat_zeros (temp, N, N);
    double * maxn = new double [N];
    for (int i = 0; i < N; i ++) { // Ian: need initial
        maxn[i] = -INF;
    }

    for (int i = 0; i < N; i ++) {
        for (int j = 0; j < N; j ++) {
            if (wtwo[i][j] > maxn[j])
                maxn[j] = wtwo[i][j];
        }
    }
    double sumk = 0.0;
    for (int i = 0; i < N; i ++) {
        sumk += lambda[i]*maxn[i];
    }
    double group_lasso = sumk;

    // sum2 = y_2^T dot (w_2 - z) -> linear
    mat_zeros (temp, N, N);
    mat_sub (wtwo, z, difftwo, N, N);
    mat_tdot (ytwo, difftwo, temp, N, N);
    double sum2 = mat_sum (temp, N, N);

    // sum3 = 0.5 * rho * || w_2 - z_2 ||^2 -> quadratic mat_zeros (temp, N, N);
    mat_sub (wtwo, z, temp, N, N);
    double sum3 = 0.5 * rho * mat_norm2 (temp, N, N);

    mat_free (temp, N, N);

    // ouput values of each components
#ifdef BLOCKWISE_DUMP
    cout << "[Blockwise] (group_lasso, linear, quadratic) = ("
         << group_lasso << ", " << sum2 << ", " << sum3
         << ")" << endl;
#endif

    //cerr << group_lasso << ", " << sum2 << ", " << sum3 << endl;
    return group_lasso + sum2 + sum3;
}
/**
* Main function
*
* @param argc
* @param argv
*/
int main(int argc, char **argv)
{
    // allocate matrix
    float *A = (float*)_mm_malloc(ROWS * COLS * sizeof(float), 64);
    if(A == NULL)
    {
        fprintf(stderr, "_mm_malloc error !\n");
        return EXIT_FAILURE;
    }

    // allocate vectors
    float *B = (float*)_mm_malloc(COLS * sizeof(float), 64);
    if(A == NULL)
    {
        fprintf(stderr, "_mm_malloc error !\n");
        _mm_free(A);
        return EXIT_FAILURE;
    }

    float *C = (float*)_mm_malloc(ROWS * sizeof(float), 64);
    if(A == NULL)
    {
        fprintf(stderr, "_mm_malloc error !\n");
        _mm_free(A);
        _mm_free(B);
        return EXIT_FAILURE;
    }

    printf("Rows: %d\n", ROWS);
    printf("Cols: %d\n", COLS);
    printf("Runs: %d\n", RUNS);

    // initialize matrix & vector
    const double tstart = omp_get_wtime();

    mat_init(ROWS, COLS, 1.0, A);
    vec_init(COLS, 3.0, B);

    const double tinit = omp_get_wtime();
    // do the measurement
    for(unsigned i = 0; i < RUNS; i++)
    {
        mat_vec_mul(ROWS, COLS, A, B, C);
    }
    const double tcalc = omp_get_wtime();

    // print results
    printf("Control sum: %f\n", vec_sum(ROWS, C));
    printf("Time Initialization: %0.3f\n", tinit - tstart);
    printf("Time Calculation: %0.3f\n", tcalc - tinit);
    printf("Time Total: %0.3f\n", tcalc - tstart);

    // free memory
    _mm_free(A);
    _mm_free(B);
    _mm_free(C);

    return EXIT_SUCCESS;
}
void init_matrix(MatrixType& M,
                 const std::vector<typename MatrixType::GlobalOrdinalType>& rows,
                 const std::vector<typename MatrixType::LocalOrdinalType>& row_offsets,
                 const std::vector<int>& row_coords,
                 int global_nodes_x,
                 int global_nodes_y,
                 int global_nodes_z,
                 typename MatrixType::GlobalOrdinalType global_nrows,
                 const simple_mesh_description<typename MatrixType::GlobalOrdinalType>& mesh)
{
  MatrixInitOp<MatrixType> mat_init(rows, row_offsets, row_coords,
                                 global_nodes_x, global_nodes_y, global_nodes_z,
                                 global_nrows, mesh, M);

  for(int i=0; i<mat_init.n; ++i) {
    mat_init(i);
  }
}
Beispiel #6
0
extern inline Matrix mat_O(int m,int n){
  Matrix R;
  int i;
  srand(time(NULL)+rand());
  mat_init(&R,m,n);
  for(i=0;i<m*n;i++){
    R.body[i]=0.;
  }
  return R;
}
double first_subproblm_obj (double ** dist_mat, double ** yone, double ** zone, double ** wone, double rho, int N) {
    double ** temp = mat_init (N, N);
    double ** diffone = mat_init (N, N);
    mat_zeros (diffone, N, N);

    // sum1 = 0.5 * sum_n sum_k (w_nk * d^2_nk) -> loss
    mat_zeros (temp, N, N);
    mat_times (wone, dist_mat, temp, N, N);
    double sum1 = 0.5 * mat_sum (temp, N, N);

    // sum2 = y_1^T dot (w_1 - z) -> linear
    mat_zeros (temp, N, N);
    mat_sub (wone, zone, diffone, N, N); // temp = w_1 - z_1
    mat_tdot (yone, diffone, temp, N, N);
    double sum2 = mat_sum (temp, N, N);

    // sum3 = 0.5 * rho * || w_1 - z_1 ||^2 -> quadratic
    mat_zeros (temp, N, N);
    mat_sub (wone, zone, temp, N, N);
    double sum3 = 0.5 * rho * mat_norm2 (temp, N, N);

    // sum4 = r dot (1 - sum_k w_nk) -> dummy
    double * temp_vec = new double [N];
    mat_sum_row (wone, temp_vec, N, N);
    double dummy_penalty = 0.0;
    for (int i = 0; i < N; i ++) {
        dummy_penalty += r*(1 - temp_vec[i]);
    }
    double total = sum1+sum2+sum3+dummy_penalty;
#ifdef FRANK_WOLFE_DUMP
    cout << "[Frank_wolfe] (loss, linear, quadratic, dummy, total) = ("
         << sum1 << ", " << sum2 << ", " << sum3 << ", " << dummy_penalty << ", " << total
         <<  ")" << endl;
#endif

    mat_free (temp, N, N);
    mat_free (diffone, N, N);
    delete [] temp_vec;

    return total;
}
Beispiel #8
0
int main(int argc, char** args)
{
    Matrix a; 
    mat_init(a, HEIGHT_OF_A, WIDTH_OF_A);

    Matrix b; 
    mat_init(b, HEIGHT_OF_B, WIDTH_OF_B);

    Matrix c; 
    mat_init(c, HEIGHT_OF_A, WIDTH_OF_B);

    mat_print(a, "The matrix A:\n");
    mat_print(b, "The matrix B:\n");
    printf("APPLYING KERNEL...\n\n");
    MatMul(a, b, c);
//    MatMul_Fast(a, b, c);
    mat_print(c, "The result:\n");

    mat_free(a);
    mat_free(b);
    mat_free(c);
}
Beispiel #9
0
extern inline Matrix mat_I(int m,int n){
  Matrix I;
  int i,j;
  mat_init(&I,m,n);
  for(i=0;i<m;i++){
    for(j=0;j<m;j++){
      if(i==j)
	mat_set(I,i,j,1.0);
      else
	mat_set(I,i,j,0.0);
    }
  }
  return I;
}
Beispiel #10
0
int main(void)
{
	
	uint8_t delay1 = 0, delay2 = 0, delay3 = 0;
	
	seg_init();
	mat_init();
	uart_init();
	sei();
	
	uart_puts("\n\n");
	uart_puts("/o/i/booting\r\n");
	
    while(1)
    {	
		
		for(uint8_t i=0; i<3;i++) uart_process();
		
		
		delay2++;
		if(delay2 == 200){
			delay2 = 0;
			mat_check();		
			
			
			delay1++;
			if(delay1 == 200){
				delay1 = 0;
				mat_debug();
				test();
				uart_puts("d\r\n");
				
				if(delay3 == 10){
					delay3 = 0;
					uart_puts("\n");
				}
				connect(); //checks hastoconnect variable
			}
		}			
		_delay_us(5);
    }
}
/* 
    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);
    }
}
Beispiel #12
0
static void
pdf_gstate_init(pdf_page *p)
{
    pdf_prs *s = p->s;
    int rotate = p->rotate;
    pdf_extgstate *gs;
    gs = &s->gs;

    gs->th = 1;
    mat_init(&gs->ctm, 1, 0, 0, 1, 0, 0);
    if (rotate)
    {
        switch (rotate)
        {
            case 90:
                mat_init(&gs->ctm, 0, -1, 1, 0, 0, p->mediabox.y1);
                break;
            case 180:
                mat_init(&gs->ctm, -1, 0, 0, 1, p->mediabox.x1, 0);
                break;
            case 270:
                mat_init(&gs->ctm, 0, 1, -1, 0, p->mediabox.x1, 0);
                break;
            case 360:
                mat_init(&gs->ctm, 1, 0, 0, -1, 0, p->mediabox.y1);
                break;
            default:
                break;
        }
    }
    mat_init(&gs->txt_ctm, 1, 0, 0, 1, 0, 0);
    mat_init(&gs->txt_lm, 1, 0, 0, 1, 0, 0);
    // path stack
    gs->path_base = p->i->path_stk;
    gs->path_top = gs->path_base;
    gs->D_n = 0;
    gs->D_OFFSET = 0;
}
Beispiel #13
0
int main(int argc, char *argv[])
{
    double err = 0.0;
    struct timeval start;
    float time = 0.0;
    int n;

    nr_nodes =  get_nr_nodes();
    initial_node = get_node_id () ;
    next_node = (initial_node + 1) % nr_nodes ;
    nr_migrations = nr_nodes + 1 ;

    parse_args(argc, argv);

    if (verbose>=2)
      {
	printf("gram-schmidt 'uni-proc' for %dx%d in", numcols, numrows);
	fflush(stdout);
      }

    time = 0 ;
    for (n=0 ; n<numloops ; n++)
      {        
	mat_init(&mat);
	
	timer_start(&start);
	up_mgs(&mat);
	time += timer_stop(&start);
	if (verbose > 2)
	  printf ("up_gs finished, starting orth_err\n");

	if (migration == 1 &&
	    get_node_id() != initial_node)
	  migrate_self (initial_node);
	
	err = orth_err(&mat);
	printf ("\n   Err = %g\n", err);
	if ( err < ERROR )
	  printf ("-- MGS test : PASSED --\n\n\n");
	else
	  {
	    printf ("-- MGS test : FAILED --\n\n\n");
	    exit(-1) ;
	  }
      }

    /* migration ici = pb */

    if (verbose>2)
      printf ("up-gs : terminé\n");

    if (verbose >= 1)
      {
        printf("\ntime :");
	printf(" %g s (err: %g)\n", time/(float)numloops, err);
      }

    if (save_matrix)
         save_normalized_matrix();

    if (verbose>2)
      printf ("up_gs termine\n");
    
    return 0;
}
Beispiel #14
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("transpose... ");
    fflush(stdout);

    _randinit(state);

    /* Check aliasing */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C;

        m = n_randint(state, 50) + 1;
        n = m;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_set(B, A, ctx);
        mat_transpose(C, B, ctx);
        mat_transpose(B, B, ctx);

        result = mat_equal(B, C, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix B\n");
            mat_print(B, ctx);
            printf("\n");
            printf("Matrix C\n");
            mat_print(C, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    /* Check that (A^t)^t == A */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, n, m, ctx);
        mat_init(C, m, n, ctx);

        mat_randtest(A, state, ctx);

        mat_transpose(B, A, ctx);
        mat_transpose(C, B, ctx);

        result = mat_equal(A, C, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix B\n");
            mat_print(B, ctx);
            printf("\n");
            printf("Matrix C\n");
            mat_print(C, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
int main (int argc, char ** argv) {
    if (argc < 5) {
        cerr << "Usage: " << endl;
        cerr << "\tHDP_MEDOIDS [dataFile] [nRuns] [lambda_global] [lambda_local]" << endl;
        exit(-1);
    }

    // PARSE arguments
    char* dataFile = argv[1];
    int nRuns = atoi(argv[2]);
    vector<double> LAMBDAs (2, 0.0);
    LAMBDAs[0] = atof(argv[3]); // lambda_global
    LAMBDAs[1] = atof(argv[4]); // lambda_local

    objmin_trace << "time objective" << endl;

    // read in data
    int FIX_DIM;
    Parser parser;
    vector<Instance*>* pdata;
    vector<Instance*> data;
    pdata = parser.parseSVM(dataFile, FIX_DIM);
    data = *pdata;

    // init lookup_tables
    vector< pair<int,int> > doc_lookup;
    get_doc_lookup (data, doc_lookup);
    Lookups lookup_tables;
    lookup_tables.doc_lookup = &doc_lookup;
    lookup_tables.nWords = data.size();
    lookup_tables.nDocs = lookup_tables.doc_lookup->size();
    
    int seed = time(NULL);
    srand (seed);
    cerr << "###########################################" << endl;
    cerr << "nDocs = " << lookup_tables.nDocs << endl; // # documents
    cerr << "nWords = " << lookup_tables.nWords << endl; // # words
    cerr << "lambda_global = " << LAMBDAs[0] << endl;
    cerr << "lambda_local = " << LAMBDAs[1] << endl;
    cerr << "TRIM_THRESHOLD = " << TRIM_THRESHOLD << endl;
    cerr << "seed = " << seed << endl;
    cerr << "###########################################" << endl;

    // Run sparse convex clustering
    int N = lookup_tables.nWords;
    int D = lookup_tables.nDocs;
    double** W = mat_init (N, N);
    // dist_mat computation and output
    dist_func df = L2norm;
    double** dist_mat = mat_init (N, N);
    compute_dist_mat (data, dist_mat, N, FIX_DIM, df, true);

    start_time = omp_get_wtime();
    double min_obj = INF;
    vector< vector<double> > min_means;
    for (int j = 0; j < nRuns; j ++) {
        vector< vector<double> > means;
        // inner-doc shuffle
        for (int d = 0; d < D; d++) {
            int begin_i = doc_lookup[d].first;
            int end_i = doc_lookup[d].second;
            random_shuffle(data.begin()+begin_i, data.begin()+end_i);
        }
        // between-doc shuffle
        vector<pair<int,int> > s_doc_lookup (doc_lookup);
        random_shuffle(s_doc_lookup.begin(), s_doc_lookup.end());
        vector<Instance*> s_data (N, NULL);
        int p = 0;
        for (int d = 0; d < D; d ++) {
            for (int i = s_doc_lookup[d].first; i < s_doc_lookup[d].second; i ++) {
                s_data[p] = data[i];
                p ++;
            }
        }
        lookup_tables.doc_lookup = &s_doc_lookup;
        double obj = HDP_MEDOIDS (s_data, means, &lookup_tables, LAMBDAs, df, FIX_DIM);
        lookup_tables.doc_lookup = &doc_lookup;
        cerr << "###################################################" << endl;
        if (obj < min_obj) {
            min_obj = obj;
            min_means = means;
        }
    }
     
    /* Output objective */ 
    output_objective (min_obj);
    ofstream model_out ("opt_model");
    for (int i = 0; i < min_means.size(); i ++) {
        model_out << "mean[" << i << "] "; 
        for (int j = 0; j < min_means[i].size(); j ++) {
            model_out << min_means[i][j] << " " ;
        }
        model_out << endl;
    }
    model_out.close();
    /* Output cluster centroids */
    // output_model (W, &lookup_tables);
    /* Output assignment */
    // output_assignment (W, &lookup_tables);

    /* reallocation */
    mat_free (W, N, N);
    mat_free (dist_mat, N, N);
    objmin_trace.close();
}
void cvx_clustering ( double ** dist_mat, int fw_max_iter, int max_iter, int D, int N, double* lambda, double ** W) {
    // parameters
    double alpha = 0.1;
    double rho = 1;
    // iterative optimization
    double error = INF;
    double ** wone = mat_init (N, N);
    double ** wtwo = mat_init (N, N);
    double ** yone = mat_init (N, N);
    double ** ytwo = mat_init (N, N);
    double ** z = mat_init (N, N);
    double ** diffzero = mat_init (N, N);
    double ** diffone = mat_init (N, N);
    double ** difftwo = mat_init (N, N);
    mat_zeros (yone, N, N);
    mat_zeros (ytwo, N, N);
    mat_zeros (z, N, N);
    mat_zeros (diffzero, N, N);
    mat_zeros (diffone, N, N);
    mat_zeros (difftwo, N, N);

    int iter = 0; // Ian: usually we count up (instead of count down)
    while ( iter < max_iter ) { // stopping criteria

#ifdef SPARSE_CLUSTERING_DUMP
        cout << "it is place 0 iteration #" << iter << ", going to get into frank_wolfe"  << endl;
#endif
        // mat_set (wone, z, N, N);
        // mat_set (wtwo, z, N, N);
        // mat_zeros (wone, N, N);
        // mat_zeros (wtwo, N, N);
        // STEP ONE: resolve w_1 and w_2
        frank_wolf (dist_mat, yone, z, wone, rho, N, fw_max_iter); // for w_1
#ifdef SPARSE_CLUSTERING_DUMP
        cout << "norm2(w_1) = " << mat_norm2 (wone, N, N) << endl;
#endif

        blockwise_closed_form (ytwo, z, wtwo, rho, lambda, N);  // for w_2
#ifdef SPARSE_CLUSTERING_DUMP
        cout << "norm2(w_2) = " << mat_norm2 (wtwo, N, N) << endl;
#endif

        // STEP TWO: update z by averaging w_1 and w_2
        mat_add (wone, wtwo, z, N, N);
        mat_dot (0.5, z, z, N, N);
#ifdef SPARSE_CLUSTERING_DUMP
        cout << "norm2(z) = " << mat_norm2 (z, N, N) << endl;
#endif

        // STEP THREE: update the y_1 and y_2 by w_1, w_2 and z

        mat_sub (wone, z, diffone, N, N);
        double trace_wone_minus_z = mat_norm2 (diffone, N, N);
        mat_dot (alpha, diffone, diffone, N, N);
        mat_add (yone, diffone, yone, N, N);

        mat_sub (wtwo, z, difftwo, N, N);
        //double trace_wtwo_minus_z = mat_norm2 (difftwo, N, N);
        mat_dot (alpha, difftwo, difftwo, N, N);
        mat_add (ytwo, difftwo, ytwo, N, N);

        // STEP FOUR: trace the objective function
        if (iter % 100 == 0) {
            error = overall_objective (dist_mat, lambda, N, z);
            // get current number of employed centroid
#ifdef NCENTROID_DUMP
            int nCentroids = get_nCentroids (z, N, N);
#endif
            cout << "[Overall] iter = " << iter
                 << ", Overall Error: " << error
#ifdef NCENTROID_DUMP
                 << ", nCentroids: " << nCentroids
#endif
                 << endl;
        }
        iter ++;
    }

    // STEP FIVE: memory recollection
    mat_free (wone, N, N);
    mat_free (wtwo, N, N);
    mat_free (yone, N, N);
    mat_free (ytwo, N, N);
    mat_free (diffone, N, N);
    mat_free (difftwo, N, N);
    // STEP SIX: put converged solution to destination W
    mat_copy (z, W, N, N);
}
// entry main function
int main (int argc, char ** argv) {
    // exception control: illustrate the usage if get input of wrong format
    if (argc < 5) {
        cerr << "Usage: cvx_clustering [dataFile] [fw_max_iter] [max_iter] [lambda] " << endl;
        cerr << "Note: dataFile must be scaled to [0,1] in advance." << endl;
        exit(-1);
    }

    // parse arguments
    char * dataFile = argv[1];
    int fw_max_iter = atoi(argv[2]);
    int max_iter = atoi(argv[3]);
    double lambda_base = atof(argv[4]);
    char * dmatFile = argv[5];

    // vector<Instance*> data;
    // readFixDim (dataFile, data, FIX_DIM);

    // read in data
    int FIX_DIM;
    Parser parser;
    vector<Instance*>* pdata;
    vector<Instance*> data;
    pdata = parser.parseSVM(dataFile, FIX_DIM);
    data = *pdata;
    // vector<Instance*> data;
    // readFixDim (dataFile, data, FIX_DIM);

    // explore the data
    int dimensions = -1;
    int N = data.size(); // data size
    for (int i = 0; i < N; i++) {
        vector< pair<int,double> > * f = &(data[i]->fea);
        int last_index = f->size()-1;
        if (f->at(last_index).first > dimensions) {
            dimensions = f->at(last_index).first;
        }
    }
    assert (dimensions == FIX_DIM);

    int D = dimensions;
    cerr << "D = " << D << endl; // # features
    cerr << "N = " << N << endl; // # instances
    cerr << "lambda = " << lambda_base << endl;
    cerr << "r = " << r << endl;
    int seed = time(NULL);
    srand (seed);
    cerr << "seed = " << seed << endl;

    //create lambda with noise
    double* lambda = new double[N];
    for(int i=0; i<N; i++) {
        lambda[i] = lambda_base + noise();
    }

    // pre-compute distance matrix
    dist_func df = L2norm;
    double ** dist_mat = mat_init (N, N);
    //  double ** dist_mat = mat_read (dmatFile, N, N);
    mat_zeros (dist_mat, N, N);
    compute_dist_mat (data, dist_mat, N, D, df, true);
    ofstream dist_mat_out ("dist_mat");
    dist_mat_out << mat_toString(dist_mat, N, N);
    dist_mat_out.close();

    // Run sparse convex clustering
    double ** W = mat_init (N, N);
    mat_zeros (W, N, N);
    cvx_clustering (dist_mat, fw_max_iter, max_iter, D, N, lambda, W);
    ofstream W_OUT("w_out");
    W_OUT<< mat_toString(W, N, N);
    W_OUT.close();

    // Output cluster
    output_objective(clustering_objective (dist_mat, W, N));

    /* Output cluster centroids */
    output_model (W, N);

    /* Output assignment */
    output_assignment (W, data, N);

    /* reallocation */
    mat_free (dist_mat, N, N);
    mat_free (W, N, N);
}
void blockwise_closed_form (double ** ytwo, double ** ztwo, double ** wtwo, double rho, double* lambda, int N) {

    // STEP ONE: compute the optimal solution for truncated problem
    double ** wbar = mat_init (N, N);
    mat_zeros (wbar, N, N);
    mat_dot (rho, ztwo, wbar, N, N); // wbar = rho * z_2
    mat_sub (wbar, ytwo, wbar, N, N); // wbar = rho * z_2 - y_2
    mat_dot (1.0/rho, wbar, wbar, N, N); // wbar = (rho * z_2 - y_2) / rho

    // STEP TWO: find the closed-form solution for second subproblem
    for (int j = 0; j < N; j ++) {
        // 1. bifurcate the set of values
        vector< pair<int,double> > alpha_vec;
        for (int i = 0; i < N; i ++) {
            double value = wbar[i][j];
            /*if( wbar[i][j] < 0 ){
              cerr << "wbar[" << i << "][" << j << "]" << endl;
              exit(0);
              }*/
            alpha_vec.push_back (make_pair(i, abs(value)));
        }
        // 2. sorting
        std::sort (alpha_vec.begin(), alpha_vec.end(), pairComparator);
        /*
           for (int i = 0; i < N; i ++) {
           if (alpha_vec[i].second != 0)
           cout << alpha_vec[i].second << endl;
           }
           */
        // 3. find mstar
        int mstar = 0; // number of elements support the sky
        double separator;
        double max_term = -INF, new_term;
        double sum_alpha = 0.0;
        for (int i = 0; i < N; i ++) {
            sum_alpha += alpha_vec[i].second;
            new_term = (sum_alpha - lambda[j]) / (i + 1.0);
            if ( new_term > max_term ) {
                separator = alpha_vec[i].second;
                max_term = new_term;
                mstar = i;
            }
        }
        // 4. assign closed-form solution to wtwo
        if( max_term < 0 ) {
            for(int i=0; i<N; i++)
                wtwo[i][j] = 0.0;
            continue;
        }
        for (int i = 0; i < N; i ++) {
            // harness vector of pair
            double value = wbar[i][j];
            if ( abs(value) >= separator ) {
                wtwo[i][j] = max_term;
            } else {
                // its ranking is above m*, directly inherit the wbar
                wtwo[i][j] = max(wbar[i][j],0.0);
            }
        }
    }
    // compute value of objective function
    double penalty = second_subproblem_obj (ytwo, ztwo, wtwo, rho, N, lambda);
    // report the #iter and objective function
    /*cout << "[Blockwise] second_subproblem_obj: " << penalty << endl;
      cout << endl;*/

    // STEP THREE: recollect temporary variable - wbar
    mat_free (wbar, N, N);
}
Beispiel #19
0
int 
adios_read_icee_schedule_read_byid(const ADIOS_FILE *adiosfile,
                                   const ADIOS_SELECTION *sel,
                                   int varid,
                                   int from_steps,
                                   int nsteps,
                                   void *data)
{   
    int i;
    icee_fileinfo_rec_ptr_t fp = (icee_fileinfo_rec_ptr_t) adiosfile->fh;
    log_debug("%s (%d:%s)\n", __FUNCTION__, varid, fp->fname);
    //assert((varid < fp->nvars) || (fp->nvars == 0));

    if (nsteps != 1)
    {
        adios_error (err_invalid_timestep,
                     "Only one step can be read from a stream at a time. "
                     "You requested % steps in adios_schedule_read()\n", 
                     nsteps);
        return err_invalid_timestep;
    }
    
    icee_varinfo_rec_ptr_t vp = NULL;
    vp = icee_varinfo_search_byname(fp->varinfo, adiosfile->var_namelist[varid]);
    if (adios_verbose_level > 5) icee_varinfo_print(vp);

    if (!vp)
    {
        adios_error(err_invalid_varid, "Invalid variable id: %d\n", varid);
        return adios_errno;
    }

    while (fp->merge_count != fp->nchunks)
    {
        log_debug("Waiting the rest of blocks (%d/%d)\n", fp->merge_count, fp->nchunks);
        
        usleep(0.1*1E6);
    }

    if (sel==0)
        memcpy(data, vp->data, vp->varlen);
    else
        switch(sel->type)
        {
        case ADIOS_SELECTION_WRITEBLOCK:
        {
            //DUMP("fp->rank: %d", fp->rank);
            //DUMP("u.block.index: %d", sel->u.block.index);

            if (fp->comm_rank != sel->u.block.index)
                adios_error(err_unspecified,
                            "Block id missmatch. "
                            "Not yet supported by ICEE\n");

            // Possible memory overwrite
            memcpy(data, vp->data, vp->varlen);
            break;
        }
        case ADIOS_SELECTION_BOUNDINGBOX:
        {
            if (vp->ndims != sel->u.bb.ndim)
                adios_error(err_invalid_dimension,
                            "Dimension mismatch\n");

            log_debug("Merging operation (total nvars: %d).\n", fp->nchunks);
            if (adios_verbose_level > 5) icee_sel_bb_print(sel);

            while (vp != NULL)
            {
                icee_matrix_t m_sel;
                icee_matrix_t m_var;
                icee_matrix_view_t v_sel;
                icee_matrix_view_t v_var;
                uint64_t start[10];
                int64_t count[10]; // should be signed to check validity
                uint64_t s_offsets[10], v_offsets[10];
                int i;

                if (adios_verbose_level > 5) icee_varinfo_print(vp);
                    
                mat_init(&m_sel, vp->typesize, vp->ndims, sel->u.bb.count, data);
                mat_init(&m_var, vp->typesize, vp->ndims, vp->ldims, vp->data);

                for (i=0; i<vp->ndims; i++)
                    start[i] = MYMAX(sel->u.bb.start[i], vp->offsets[i]);

                for (i=0; i<vp->ndims; i++)
                {
                    count[i] = 
                        MYMIN(sel->u.bb.start[i]+sel->u.bb.count[i],
                              vp->offsets[i]+vp->ldims[i]) - start[i];
                }
                    
                for (i=0; i<vp->ndims; i++)
                {
                    if (count[i] <= 0)
                    {
                        log_debug("No ROI. Skip\n");
                        goto next;
                    }
                }

                for (i=0; i<vp->ndims; i++)
                    s_offsets[i] = start[i] - sel->u.bb.start[i];

                for (i=0; i<vp->ndims; i++)
                    v_offsets[i] = start[i] - vp->offsets[i];

                view_init (&v_sel, &m_sel, count, s_offsets);
                view_init (&v_var, &m_var, count, v_offsets);
                view_copy (&v_sel, &v_var);
                    
            next:
                vp = icee_varinfo_search_byname(vp->next, adiosfile->var_namelist[varid]);
            }
            break;
        }
        case ADIOS_SELECTION_AUTO:
        {
            // Possible memory overwrite
            memcpy(data, vp->data, vp->varlen);
            break;
        }
        case ADIOS_SELECTION_POINTS:
        {
            adios_error(err_operation_not_supported,
                        "ADIOS_SELECTION_POINTS not yet supported by ICEE.\n");
            break;
        }
        }

    return adios_errno;
}
Beispiel #20
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("zero... ");
    fflush(stdout);

    _randinit(state);

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(A, ctx);

        result = mat_is_zero(A, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            abort();
        }

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A;

        m = n_randint(state, 100) + 1;
        n = n_randint(state, 100) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(A, ctx);

        result = mat_is_zero(A, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            abort();
        }

        mat_clear(A, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #21
0
int main(void)
{
    char *str;  /* String for the input polynomial P */
    mpoly_t P;  /* Input polynomial P */
    int n;      /* Number of variables minus one */
    long K;     /* Required t-adic precision */
    long N, Nw;
    long b;     /* Matrix dimensions */
    long i, j, k;

    mat_t M;
    ctx_t ctxM;

    mon_t *rows, *cols;

    padic_mat_struct *C;
    fmpz_t p;

    fmpz_poly_mat_t B;
    long vB;

    printf("solve... \n");
    fflush(stdout);

    /* Example 3-1-1 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[1 1 1]"; */

    /* Example 3-3-2 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[2 1 0] (2  0 1)[0 2 1] (2  0 1)[1 0 2]"; */

    /* Example 4-4-2 */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[3 1 0 0] (2  0 1)[1 0 1 2] (2  0 1)[0 1 0 3]"; */

    /* Example ... */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[1 1 1 1]"; */

    /* Example from AKR */
    str = "4  (1  3)[0 3 0 0] (2  0 3)[0 1 2 0] "
          "(2  0 -1)[1 1 1 0] (2  0 3)[1 1 0 1] "
          "(2  0 -1)[2 1 0 0] [0 0 3 0] (2  0 -1)[1 0 2 0] "
          "(1  2)[0 0 0 3] [3 0 0 0]";

    n  = atoi(str) - 1;
    K  = 616;
    N  = 10;
    Nw = 22;

    fmpz_init(p);
    fmpz_set_ui(p, 5);
    ctx_init_fmpz_poly_q(ctxM);

    ctxM->print   = &__fmpz_poly_q_print_pretty;

    mpoly_init(P, n + 1, ctxM);
    mpoly_set_str(P, str, ctxM);

    printf("P = "), mpoly_print(P, ctxM), printf("\n");

    b = gmc_basis_size(n, mpoly_degree(P, -1, ctxM));

    mat_init(M, b, b, ctxM);
    fmpz_poly_mat_init(B, b, b);
    vB = 0;

    gmc_compute(M, &rows, &cols, P, ctxM);

    mat_print(M, ctxM);
    printf("\n");

    gmde_solve(&C, K, p, N, Nw, M, ctxM);
    gmde_convert_soln(B, &vB, C, K, p);

    printf("Solution to (d/dt + M) C = 0:\n");
    fmpz_poly_mat_print(B, "t");
    printf("vB = %ld\n", vB);

    gmde_check_soln(B, vB, p, N, K, M, ctxM);

    mpoly_clear(P, ctxM);
    mat_clear(M, ctxM);
    free(rows);
    free(cols);
    fmpz_poly_mat_clear(B);
    ctx_clear(ctxM);
    fmpz_clear(p);

    for (i = 0; i < K; i++)
        padic_mat_clear(C + i);
    free(C);

    _fmpz_cleanup();

    return EXIT_SUCCESS;
}
Beispiel #22
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("add... ");
    fflush(stdout);

    _randinit(state);

    /* Check that addition is abelian */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_randtest(B, state, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_randtest(B, state, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Check that the zero matrix does what it's supposed to do */

    /* Unmanaged element type (long) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_long(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(B, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(A, C, ctx) && mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    /* Managed element type (mpq_t) */
    for (i = 0; i < 100; i++)
    {
        long m, n;
        ctx_t ctx;
        mat_t A, B, C, D;

        m = n_randint(state, 50) + 1;
        n = n_randint(state, 50) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, m, n, ctx);
        mat_init(B, m, n, ctx);
        mat_init(C, m, n, ctx);
        mat_init(D, m, n, ctx);

        mat_randtest(A, state, ctx);
        mat_zero(B, ctx);

        mat_add(C, A, B, ctx);
        mat_add(D, B, A, ctx);

        result = mat_equal(A, C, ctx) && mat_equal(C, D, ctx);
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("Matrix A\n");
            mat_print(A, ctx);
            printf("\n");
            printf("Matrix A\n");
            mat_print(B, ctx);
            printf("\n");
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }

    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #23
0
int
main(void)
{
    int i, result;
    flint_rand_t state;

    printf("inv... ");
    fflush(stdout);

    _randinit(state);

    /* Check aliasing */

    /* Managed element type (mpq_t) */
    for (i = 0; i < 50; i++)
    {
        long n;
        ctx_t ctx;
        mat_t A, B, C;
        long ansB, ansC;

        n = n_randint(state, 20) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, n, n, ctx);
        mat_init(B, n, n, ctx);
        mat_init(C, n, n, ctx);

        mat_randtest(A, state, ctx);

        mat_set(B, A, ctx);
        ansC = mat_inv(C, B, ctx);
        ansB = mat_inv(B, B, ctx);

        result = ((ansB == ansC) && (!ansB || mat_equal(B, C, ctx)));
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("A: \n"), mat_print(A, ctx), printf("\n");
            printf("B: \n"), mat_print(B, ctx), printf("\n");
            printf("C: \n"), mat_print(C, ctx), printf("\n");
            printf("ansB = %ld\n", ansB);
            printf("ansC = %ld\n", ansC);
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        ctx_clear(ctx);
    }

    /* Check A * A^{-1} == A^{-1} * A == Id */

    /* Managed element type (mpq_t) */
    for (i = 0; i < 50; i++)
    {
        long n;
        ctx_t ctx;
        mat_t A, B, C, D, I;
        long ansB;

        n = n_randint(state, 20) + 1;

        ctx_init_mpq(ctx);
        mat_init(A, n, n, ctx);
        mat_init(B, n, n, ctx);
        mat_init(C, n, n, ctx);
        mat_init(D, n, n, ctx);
        mat_init(I, n, n, ctx);

        mat_randtest(A, state, ctx);

        ansB = mat_inv(B, A, ctx);
        if (!ansB)
        {
            mat_mul(C, A, B, ctx);
            mat_mul(D, B, A, ctx);
        }

        result = (ansB || (mat_equal(C, D, ctx) && mat_is_one(C, ctx)));
        if (!result)
        {
            printf("FAIL:\n\n");
            printf("A: \n"), mat_print(A, ctx), printf("\n");
            printf("B: \n"), mat_print(B, ctx), printf("\n");
            printf("C: \n"), mat_print(C, ctx), printf("\n");
            printf("D: \n"), mat_print(D, ctx), printf("\n");
            printf("ansB = %ld\n", ansB);
        }

        mat_clear(A, ctx);
        mat_clear(B, ctx);
        mat_clear(C, ctx);
        mat_clear(D, ctx);
        ctx_clear(ctx);
    }


    _randclear(state);
    _fmpz_cleanup();
    printf("PASS\n");
    return EXIT_SUCCESS;
}
Beispiel #24
0
int main(void)
{
    char *str;  /* String for the input polynomial P */
    mpoly_t P;  /* Input polynomial P */
    long n;     /* Number of variables minus one */
    long K;     /* Required t-adic precision */
    long N, Nw;
    long i, b;

    mat_t M;
    ctx_t ctxM;

    mon_t *rows, *cols;

    padic_mat_struct *C;
    fmpz_t p;

    printf("valuations... \n");
    fflush(stdout);

    /* Example 3-1-1 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[1 1 1]"; */

    /* Example 4-4-2 */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[3 1 0 0] (2  0 1)[1 0 1 2] (2  0 1)[0 1 0 3]"; */

    /* Example 3-3-6 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 314)[2 1 0] (2  0 42)[0 2 1] (2  0 271)[1 0 2] (2  0 -23)[1 1 1]"; */

    /* Example 3-3-2 */
    /* str = "3  [3 0 0] [0 3 0] [0 0 3] (2  0 1)[2 1 0] (2  0 1)[0 2 1] (2  0 1)[1 0 2]"; */

    /* Example ... */
    /* str = "4  [4 0 0 0] [0 4 0 0] [0 0 4 0] [0 0 0 4] (2  0 1)[1 1 1 1]"; */

    /* Cubic surface from AKR */
    str = "4  (1  3)[0 3 0 0] (2  0 3)[0 1 2 0] "
          "(2  0 -1)[1 1 1 0] (2  0 3)[1 1 0 1] "
          "(2  0 -1)[2 1 0 0] [0 0 3 0] (2  0 -1)[1 0 2 0] "
          "(1  2)[0 0 0 3] [3 0 0 0]";

    n  = atoi(str) - 1;
    N  = 10;
    Nw = 22;
    K  = 616;

    ctx_init_fmpz_poly_q(ctxM);

    mpoly_init(P, n + 1, ctxM);
    mpoly_set_str(P, str, ctxM);

    printf("P = "), mpoly_print(P, ctxM), printf("\n");

    b = gmc_basis_size(n, mpoly_degree(P, -1, ctxM));

    mat_init(M, b, b, ctxM);
    gmc_compute(M, &rows, &cols, P, ctxM);
    mat_print(M, ctxM);
    printf("\n");

    fmpz_init(p);
    fmpz_set_ui(p, 5);

    gmde_solve(&C, K, p, N, Nw, M, ctxM);

    printf("Valuations\n");

    for (i = 0; i < K; i++)
    {
        long v = padic_mat_val(C + i);

        if (v < LONG_MAX)
            printf("  i = %ld val = %ld val/log(i) = %f\n", i, v, 
                (i > 1) ? (double) v / log(i) : 0);
        else
            printf("  i = %ld val = +infty\n", i);
    }

    fmpz_clear(p);
    mpoly_clear(P, ctxM);
    mat_clear(M, ctxM);
    for (i = 0; i < K; i++)
        padic_mat_clear(C + i);
    free(C);
    ctx_clear(ctxM);

    return EXIT_SUCCESS;
}
void 
checkCorrect ()
{
  double *A,*B,*C,*cA,*cB,*cC;
  int minDim = 1;
  int maxDim = 256;
  int maxnbytes = sizeof(double) * SQR (maxDim);
  int i, j;

  A = (double*) malloc (maxnbytes);
  B = (double*) malloc (maxnbytes);
  C = (double*) malloc (maxnbytes);
  
  cA = (double*) malloc (maxnbytes);
  cB = (double*) malloc (maxnbytes);
  cC = (double*) malloc (maxnbytes);
  
  fprintf (stderr, "Checking for correctness on sizes:"); 

#if !RANDOM_TESTS
  for (i = 0; i < 2; i++) for (j = 0; j < num_tests[i]; j++) 
    {
      int matdim = test_sizes[i][j];
#else
  for (i = 0; i < NUM_CORRECTNESS_CHECKS; i++) 
    {
      int matdim = rrand (minDim, maxDim);
#endif
      double err;
      int nbytes = sizeof(double) * SQR(matdim);

      fprintf (stderr, " %d", matdim); 

      mat_init (A, matdim, matdim);
      mat_init (B, matdim, matdim);
      mat_init (C, matdim, matdim);

      bcopy ((void*)A, (void*)cA, nbytes);
      bcopy ((void*)B, (void*)cB, nbytes);
      bcopy ((void*)C, (void*)cC, nbytes);

      naive_mm (matdim, matdim, matdim, cA, cB, cC);
      MUL_MFMF_MF (matdim, A, B, C);

      if (bcmp ((void*)A, (void*)cA, nbytes) != 0 ||
          bcmp ((void*)B, (void*)cB, nbytes) != 0) 
        {
          fprintf (stderr, "Source matrices were modified.  DISQUALIFIED!!!\n")
;
          //exit (0);
        }

      if ((err = error (C, cC, matdim, matdim)) > MAX_ERROR)
        {
          fprintf (stderr, "Error for test case %dx%d is %f > %f. DISQUALIFIED!
!!\n",
                   matdim, matdim, err, MAX_ERROR);
          //exit (0);
        }

    }
  fprintf (stderr,"\n"); 

  free (A); free (B); free (C); 
  free (cA); free (cB); free (cC);
}

void
timeIt ()
{
  double *A, *B, *C;
  double *oA[TEST_RUNS], *oB[TEST_RUNS], *oC[TEST_RUNS];
  int i, j, k;
  int test;

  for (k = 0; k < 2; k++)
    {
      if (k > 0) printf ("\n");

      for (test = 0; test < num_tests[k]; test++) 
        {
          int matdim = test_sizes[k][test];
          const int num_iters = CALC_ITERS (matdim);
          double max_mflops = 0.0;
          int run;
        
          /* make sure these are quad-word (i.e., 16-byte) aligned */
#if 0
          A = oA = (double*) malloc ((SQR(matdim)+1) * sizeof(double));
          B = oB = (double*) malloc ((SQR(matdim)+1) * sizeof(double));
          C = oC = (double*) malloc ((SQR(matdim)+1) * sizeof(double));
#endif
          
          for (run = 0; run < TEST_RUNS; run++) 
            {
              int iter;
              double mflops;
              double utime;

              /* use different matricies for each trial so that the OS page map
ping */
              /* won't affect the results... */
              A = oA[run] = (double*) malloc ((SQR(matdim)+rrand(1,10)) * sizeof(double));
              B = oB[run] = (double*) malloc ((SQR(matdim)+rrand(1,10)) * sizeof(double));
              C = oC[run] = (double*) malloc ((SQR(matdim)+rrand(1,10)) * sizeof(double));

              if (((unsigned)A) & 0x8) A = (double*)(((unsigned)A)+0x8);
              if (((unsigned)B) & 0x8) B = (double*)(((unsigned)B)+0x8);
              if (((unsigned)C) & 0x8) C = (double*)(((unsigned)C)+0x8);

              mat_init (A, matdim, matdim);
              mat_init (B, matdim, matdim);
              mat_init (C, matdim, matdim);

              START_TIMING;
              for (iter=0;iter<num_iters;iter++) 
                {
                  // iteratively accumulate into C
                  MUL_MFMF_MF (matdim, A, B, C);
                }
              STOP_TIMING;

              utime = reportTiming();
              // (2 * n^3) FLOPs (n^3 mul-adds)
              mflops = 2.0 * CUBE(matdim) * num_iters * 1e-6 / utime;
              max_mflops = MAX (max_mflops, mflops);
            }

          printf("%d %.0f\n", matdim, max_mflops); 
          fflush(stdout);

          for (run = 0; run < TEST_RUNS; run++) 
            {
              free (oA[run]); 
              free (oB[run]); 
              free (oC[run]); 
            }
        }
    }
}
Beispiel #26
0
void frob(const mpoly_t P, const ctx_t ctxFracQt,
          const qadic_t t1, const qadic_ctx_t Qq,
          prec_t *prec, const prec_t *prec_in,
          int verbose)
{
    const padic_ctx_struct *Qp = &Qq->pctx;
    const fmpz *p = Qp->p;
    const long a  = qadic_ctx_degree(Qq);
    const long n  = P->n - 1;
    const long d  = mpoly_degree(P, -1, ctxFracQt);
    const long b  = gmc_basis_size(n, d);

    long i, j, k;

    /* Diagonal fibre */
    padic_mat_t F0;

    /* Gauss--Manin Connection */
    mat_t M;
    mon_t *bR, *bC;
    fmpz_poly_t r;

    /* Local solution */
    fmpz_poly_mat_t C, Cinv;
    long vC, vCinv;

    /* Frobenius */
    fmpz_poly_mat_t F;
    long vF;

    fmpz_poly_mat_t F1;
    long vF1;

    fmpz_poly_t cp;

    clock_t c0, c1;
    double c;

    if (verbose)
    {
        printf("Input:\n");
        printf("  P  = "), mpoly_print(P, ctxFracQt), printf("\n");
        printf("  p  = "), fmpz_print(p), printf("\n");
        printf("  t1 = "), qadic_print_pretty(t1, Qq), printf("\n");
        printf("\n");
        fflush(stdout);
    }

    /* Step 1 {M, r} *********************************************************/

    c0 = clock();

    mat_init(M, b, b, ctxFracQt);
    fmpz_poly_init(r);

    gmc_compute(M, &bR, &bC, P, ctxFracQt);

    {
        fmpz_poly_t t;

        fmpz_poly_init(t);
        fmpz_poly_set_ui(r, 1);
        for (i = 0; i < M->m; i++)
            for (j = 0; j < M->n; j++)
            {
                fmpz_poly_lcm(t, r, fmpz_poly_q_denref(
                                  (fmpz_poly_q_struct *) mat_entry(M, i, j, ctxFracQt)));
                fmpz_poly_swap(r, t);
            }
        fmpz_poly_clear(t);
    }

    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;

    if (verbose)
    {
        printf("Gauss-Manin connection:\n");
        printf("  r(t) = "), fmpz_poly_print_pretty(r, "t"), printf("\n");
        printf("  Time = %f\n", c);
        printf("\n");
        fflush(stdout);
    }

    {
        qadic_t t;

        qadic_init2(t, 1);
        fmpz_poly_evaluate_qadic(t, r, t1, Qq);

        if (qadic_is_zero(t))
        {
            printf("Exception (deformation_frob).\n");
            printf("The resultant r evaluates to zero (mod p) at t1.\n");
            abort();
        }
        qadic_clear(t);
    }

    /* Precisions ************************************************************/

    if (prec_in != NULL)
    {
        *prec = *prec_in;
    }
    else
    {
        deformation_precisions(prec, p, a, n, d, fmpz_poly_degree(r));
    }

    if (verbose)
    {
        printf("Precisions:\n");
        printf("  N0   = %ld\n", prec->N0);
        printf("  N1   = %ld\n", prec->N1);
        printf("  N2   = %ld\n", prec->N2);
        printf("  N3   = %ld\n", prec->N3);
        printf("  N3i  = %ld\n", prec->N3i);
        printf("  N3w  = %ld\n", prec->N3w);
        printf("  N3iw = %ld\n", prec->N3iw);
        printf("  N4   = %ld\n", prec->N4);
        printf("  m    = %ld\n", prec->m);
        printf("  K    = %ld\n", prec->K);
        printf("  r    = %ld\n", prec->r);
        printf("  s    = %ld\n", prec->s);
        printf("\n");
        fflush(stdout);
    }

    /* Initialisation ********************************************************/

    padic_mat_init2(F0, b, b, prec->N4);

    fmpz_poly_mat_init(C, b, b);
    fmpz_poly_mat_init(Cinv, b, b);

    fmpz_poly_mat_init(F, b, b);
    vF = 0;

    fmpz_poly_mat_init(F1, b, b);
    vF1 = 0;

    fmpz_poly_init(cp);

    /* Step 2 {F0} ***********************************************************/

    {
        padic_ctx_t pctx_F0;
        fmpz *t;

        padic_ctx_init(pctx_F0, p, FLINT_MIN(prec->N4 - 10, 0), prec->N4, PADIC_VAL_UNIT);
        t = _fmpz_vec_init(n + 1);

        c0 = clock();

        mpoly_diagonal_fibre(t, P, ctxFracQt);

        diagfrob(F0, t, n, d, prec->N4, pctx_F0, 0);
        padic_mat_transpose(F0, F0);

        c1 = clock();
        c  = (double) (c1 - c0) / CLOCKS_PER_SEC;

        if (verbose)
        {
            printf("Diagonal fibre:\n");
            printf("  P(0) = {"), _fmpz_vec_print(t, n + 1), printf("}\n");
            printf("  Time = %f\n", c);
            printf("\n");
            fflush(stdout);
        }

        _fmpz_vec_clear(t, n + 1);
        padic_ctx_clear(pctx_F0);
    }

    /* Step 3 {C, Cinv} ******************************************************/
    /*
        Compute C as a matrix over Z_p[[t]].  A is the same but as a series
        of matrices over Z_p.  Mt is the matrix -M^t, and Cinv is C^{-1}^t,
        the local solution of the differential equation replacing M by Mt.
     */

    c0 = clock();
    {
        const long K = prec->K;
        padic_mat_struct *A;

        gmde_solve(&A, K, p, prec->N3, prec->N3w, M, ctxFracQt);
        gmde_convert_soln(C, &vC, A, K, p);

        for(i = 0; i < K; i++)
            padic_mat_clear(A + i);
        free(A);
    }
    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
    if (verbose)
    {
        printf("Local solution:\n");
        printf("  Time for C      = %f\n", c);
        fflush(stdout);
    }

    c0 = clock();
    {
        const long K = (prec->K + (*p) - 1) / (*p);
        mat_t Mt;
        padic_mat_struct *Ainv;

        mat_init(Mt, b, b, ctxFracQt);
        mat_transpose(Mt, M, ctxFracQt);
        mat_neg(Mt, Mt, ctxFracQt);
        gmde_solve(&Ainv, K, p, prec->N3i, prec->N3iw, Mt, ctxFracQt);
        gmde_convert_soln(Cinv, &vCinv, Ainv, K, p);

        fmpz_poly_mat_transpose(Cinv, Cinv);
        fmpz_poly_mat_compose_pow(Cinv, Cinv, *p);

        for(i = 0; i < K; i++)
            padic_mat_clear(Ainv + i);
        free(Ainv);
        mat_clear(Mt, ctxFracQt);
    }
    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
    if (verbose)
    {
        printf("  Time for C^{-1} = %f\n", c);
        printf("\n");
        fflush(stdout);
    }

    /* Step 4 {F(t) := C(t) F(0) C(t^p)^{-1}} ********************************/
    /*
        Computes the product C(t) F(0) C(t^p)^{-1} modulo (p^{N_2}, t^K).
        This is done by first computing the unit part of the product
        exactly over the integers modulo t^K.
     */

    c0 = clock();
    {
        fmpz_t pN;
        fmpz_poly_mat_t T;

        fmpz_init(pN);
        fmpz_poly_mat_init(T, b, b);

        for (i = 0; i < b; i++)
        {
            /* Find the unique k s.t. F0(i,k) is non-zero */
            for (k = 0; k < b; k++)
                if (!fmpz_is_zero(padic_mat_entry(F0, i, k)))
                    break;
            if (k == b)
            {
                printf("Exception (frob). F0 is singular.\n\n");
                abort();
            }

            for (j = 0; j < b; j++)
            {
                fmpz_poly_scalar_mul_fmpz(fmpz_poly_mat_entry(T, i, j),
                                          fmpz_poly_mat_entry(Cinv, k, j),
                                          padic_mat_entry(F0, i, k));
            }
        }

        fmpz_poly_mat_mul(F, C, T);
        fmpz_poly_mat_truncate(F, prec->K);
        vF = vC + padic_mat_val(F0) + vCinv;

        /* Canonicalise (F, vF) */
        {
            long v = fmpz_poly_mat_ord_p(F, p);

            if (v == LONG_MAX)
            {
                printf("ERROR (deformation_frob).  F(t) == 0.\n");
                abort();
            }
            else if (v > 0)
            {
                fmpz_pow_ui(pN, p, v);
                fmpz_poly_mat_scalar_divexact_fmpz(F, F, pN);
                vF = vF + v;
            }
        }

        /* Reduce (F, vF) modulo p^{N2} */
        fmpz_pow_ui(pN, p, prec->N2 - vF);
        fmpz_poly_mat_scalar_mod_fmpz(F, F, pN);

        fmpz_clear(pN);
        fmpz_poly_mat_clear(T);
    }
    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
    if (verbose)
    {
        printf("Matrix for F(t):\n");
        printf("  Time = %f\n", c);
        printf("\n");
        fflush(stdout);
    }

    /* Step 5 {G = r(t)^m F(t)} **********************************************/

    c0 = clock();
    {
        fmpz_t pN;
        fmpz_poly_t t;

        fmpz_init(pN);
        fmpz_poly_init(t);

        fmpz_pow_ui(pN, p, prec->N2 - vF);

        /* Compute r(t)^m mod p^{N2-vF} */
        if (prec->denR == NULL)
        {
            fmpz_mod_poly_t _t;

            fmpz_mod_poly_init(_t, pN);
            fmpz_mod_poly_set_fmpz_poly(_t, r);
            fmpz_mod_poly_pow(_t, _t, prec->m);
            fmpz_mod_poly_get_fmpz_poly(t, _t);
            fmpz_mod_poly_clear(_t);
        }
        else
        {
            /* TODO: We don't really need a copy */
            fmpz_poly_set(t, prec->denR);
        }

        fmpz_poly_mat_scalar_mul_fmpz_poly(F, F, t);
        fmpz_poly_mat_scalar_mod_fmpz(F, F, pN);

        /* TODO: This should not be necessary? */
        fmpz_poly_mat_truncate(F, prec->K);

        fmpz_clear(pN);
        fmpz_poly_clear(t);
    }
    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
    if (verbose)
    {
        printf("Analytic continuation:\n");
        printf("  Time = %f\n", c);
        printf("\n");
        fflush(stdout);
    }

    /* Steps 6 and 7 *********************************************************/

    if (a == 1)
    {
        /* Step 6 {F(1) = r(t_1)^{-m} G(t_1)} ********************************/

        c0 = clock();
        {
            const long N = prec->N2 - vF;

            fmpz_t f, g, t, pN;

            fmpz_init(f);
            fmpz_init(g);
            fmpz_init(t);
            fmpz_init(pN);

            fmpz_pow_ui(pN, p, N);

            /* f := \hat{t_1}, g := r(\hat{t_1})^{-m} */
            _padic_teichmuller(f, t1->coeffs + 0, p, N);
            if (prec->denR == NULL)
            {
                _fmpz_mod_poly_evaluate_fmpz(g, r->coeffs, r->length, f, pN);
                fmpz_powm_ui(t, g, prec->m, pN);
            }
            else
            {
                _fmpz_mod_poly_evaluate_fmpz(t, prec->denR->coeffs, prec->denR->length, f, pN);
            }
            _padic_inv(g, t, p, N);

            /* F1 := g G(\hat{t_1}) */
            for (i = 0; i < b; i++)
                for (j = 0; j < b; j++)
                {
                    const fmpz_poly_struct *poly = fmpz_poly_mat_entry(F, i, j);
                    const long len               = poly->length;

                    if (len == 0)
                    {
                        fmpz_poly_zero(fmpz_poly_mat_entry(F1, i, j));
                    }
                    else
                    {
                        fmpz_poly_fit_length(fmpz_poly_mat_entry(F1, i, j), 1);

                        _fmpz_mod_poly_evaluate_fmpz(t, poly->coeffs, len, f, pN);
                        fmpz_mul(fmpz_poly_mat_entry(F1, i, j)->coeffs + 0, g, t);
                        fmpz_mod(fmpz_poly_mat_entry(F1, i, j)->coeffs + 0,
                                 fmpz_poly_mat_entry(F1, i, j)->coeffs + 0, pN);

                        _fmpz_poly_set_length(fmpz_poly_mat_entry(F1, i, j), 1);
                        _fmpz_poly_normalise(fmpz_poly_mat_entry(F1, i, j));
                    }
                }

            vF1 = vF;
            fmpz_poly_mat_canonicalise(F1, &vF1, p);

            fmpz_clear(f);
            fmpz_clear(g);
            fmpz_clear(t);
            fmpz_clear(pN);
        }
        c1 = clock();
        c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
        if (verbose)
        {
            printf("Evaluation:\n");
            printf("  Time = %f\n", c);
            printf("\n");
            fflush(stdout);
        }
    }
    else
    {
        /* Step 6 {F(1) = r(t_1)^{-m} G(t_1)} ********************************/

        c0 = clock();
        {
            const long N = prec->N2 - vF;
            fmpz_t pN;
            fmpz *f, *g, *t;

            fmpz_init(pN);

            f = _fmpz_vec_init(a);
            g = _fmpz_vec_init(2 * a - 1);
            t = _fmpz_vec_init(2 * a - 1);

            fmpz_pow_ui(pN, p, N);

            /* f := \hat{t_1}, g := r(\hat{t_1})^{-m} */
            _qadic_teichmuller(f, t1->coeffs, t1->length, Qq->a, Qq->j, Qq->len, p, N);
            if (prec->denR == NULL)
            {
                fmpz_t e;
                fmpz_init_set_ui(e, prec->m);
                _fmpz_mod_poly_compose_smod(g, r->coeffs, r->length, f, a,
                                            Qq->a, Qq->j, Qq->len, pN);
                _qadic_pow(t, g, a, e, Qq->a, Qq->j, Qq->len, pN);
                fmpz_clear(e);
            }
            else
            {
                _fmpz_mod_poly_reduce(prec->denR->coeffs, prec->denR->length, Qq->a, Qq->j, Qq->len, pN);
                _fmpz_poly_normalise(prec->denR);

                _fmpz_mod_poly_compose_smod(t, prec->denR->coeffs, prec->denR->length, f, a,
                                            Qq->a, Qq->j, Qq->len, pN);
            }
            _qadic_inv(g, t, a, Qq->a, Qq->j, Qq->len, p, N);

            /* F1 := g G(\hat{t_1}) */
            for (i = 0; i < b; i++)
                for (j = 0; j < b; j++)
                {
                    const fmpz_poly_struct *poly = fmpz_poly_mat_entry(F, i, j);
                    const long len               = poly->length;

                    fmpz_poly_struct *poly2 = fmpz_poly_mat_entry(F1, i, j);

                    if (len == 0)
                    {
                        fmpz_poly_zero(poly2);
                    }
                    else
                    {
                        _fmpz_mod_poly_compose_smod(t, poly->coeffs, len, f, a,
                                                    Qq->a, Qq->j, Qq->len, pN);

                        fmpz_poly_fit_length(poly2, 2 * a - 1);
                        _fmpz_poly_mul(poly2->coeffs, g, a, t, a);
                        _fmpz_mod_poly_reduce(poly2->coeffs, 2 * a - 1, Qq->a, Qq->j, Qq->len, pN);
                        _fmpz_poly_set_length(poly2, a);
                        _fmpz_poly_normalise(poly2);
                    }
                }

            /* Now the matrix for p^{-1} F_p at t=t_1 is (F1, vF1). */
            vF1 = vF;
            fmpz_poly_mat_canonicalise(F1, &vF1, p);

            fmpz_clear(pN);
            _fmpz_vec_clear(f, a);
            _fmpz_vec_clear(g, 2 * a - 1);
            _fmpz_vec_clear(t, 2 * a - 1);
        }
        c1 = clock();
        c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
        if (verbose)
        {
            printf("Evaluation:\n");
            printf("  Time = %f\n", c);
            printf("\n");
            fflush(stdout);
        }

        /* Step 7 {Norm} *****************************************************/
        /*
            Computes the matrix for $q^{-1} F_q$ at $t = t_1$ as the
            product $F \sigma(F) \dotsm \sigma^{a-1}(F)$ up appropriate
            transpositions because our convention of columns vs rows is
            the opposite of that used by Gerkmann.

            Note that, in any case, transpositions do not affect
            the characteristic polynomial.
         */

        c0 = clock();
        {
            const long N = prec->N1 - a * vF1;

            fmpz_t pN;
            fmpz_poly_mat_t T;

            fmpz_init(pN);
            fmpz_poly_mat_init(T, b, b);

            fmpz_pow_ui(pN, p, N);

            fmpz_poly_mat_frobenius(T, F1, 1, p, N, Qq);
            _qadic_mat_mul(F1, F1, T, pN, Qq);

            for (i = 2; i < a; i++)
            {
                fmpz_poly_mat_frobenius(T, T, 1, p, N, Qq);
                _qadic_mat_mul(F1, F1, T, pN, Qq);
            }

            vF1 = a * vF1;
            fmpz_poly_mat_canonicalise(F1, &vF1, p);

            fmpz_clear(pN);
            fmpz_poly_mat_clear(T);
        }
        c1 = clock();
        c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
        if (verbose)
        {
            printf("Norm:\n");
            printf("  Time = %f\n", c);
            printf("\n");
            fflush(stdout);
        }
    }

    /* Step 8 {Reverse characteristic polynomial} ****************************/

    c0 = clock();

    deformation_revcharpoly(cp, F1, vF1, n, d, prec->N0, prec->r, prec->s, Qq);

    c1 = clock();
    c  = (double) (c1 - c0) / CLOCKS_PER_SEC;
    if (verbose)
    {
        printf("Reverse characteristic polynomial:\n");
        printf("  p(T) = "), fmpz_poly_print_pretty(cp, "T"), printf("\n");
        printf("  Time = %f\n", c);
        printf("\n");
        fflush(stdout);
    }

    /* Clean up **************************************************************/

    padic_mat_clear(F0);

    mat_clear(M, ctxFracQt);
    free(bR);
    free(bC);
    fmpz_poly_clear(r);

    fmpz_poly_mat_clear(C);
    fmpz_poly_mat_clear(Cinv);

    fmpz_poly_mat_clear(F);
    fmpz_poly_mat_clear(F1);
    fmpz_poly_clear(cp);
}