int main(void) { int subms_len, origin_y; while (1) { scanf("%d%d", &subms_len, &origin_y); if (subms_len == 0 && origin_y == 0) break; int i = 0; subm_t * subms = create_subms(subms_len); for (; i < subms_len; i++) { scanf("%d%d", &subms[i].x, &subms[i].y); if (subms[i].y > origin_y) { i--; subms_len--; } } print_points(subms, subms_len); rank_t * ranks; rank_t * rank = select_subms(subms, subms_len, &ranks); int len = rank->rank; printf("%d\n", len); i = 0; for (; i < len; i++) { printf("%d %d\n", rank->subm->x, rank->subm->y); rank = rank->prev; } free(ranks); free(subms); } return 0; }
void print_tree(Node *node, int level){ print_points(level); if(node->node_type == NODE_ID || node->node_type == NODE_INT_LIT || node->node_type == NODE_REAL_LIT || node->node_type == NODE_STRING){ print_terminal(node); } else{ printf("%s\n", NODE_NAME[node->node_type]); } Node *child = node->first_child; if(child != NULL){ print_tree(child, level+1); while(child->brother != NULL){ child = child->brother; print_tree(child, level+1); } } free(node); }
int main(void){ /*defines the variables*/ int n_points =12; /*call to the external function*/ print_points(n_points); return 0; }
void KMCenters::show(std::ostream& out) const{ print_points("Center_Points", *centers_,out); }
double estimate_ml_t(log_like_function_t *log_like, const double* t, size_t n_pts, const double tolerance, bsm_t* model, bool* success, const double min_t, const double max_t) { *success = false; point_t *starting_pts = malloc(sizeof(point_t) * n_pts); evaluate_ll(log_like, t, n_pts, starting_pts); const size_t orig_n_pts = n_pts; point_t* points = select_points(log_like, starting_pts, &n_pts, DEFAULT_MAX_POINTS, min_t, max_t); free(starting_pts); if (points == NULL) { fprintf(stderr, "ERROR: select_points returned NULL\n"); *success = false; return NAN; } curve_type_t curvature = classify_curve(points, n_pts); if (!(curvature == CRV_ENC_MAXIMA || curvature == CRV_MONO_DEC)) { fprintf(stderr, "ERROR: " "points don't enclose a maximum and aren't decreasing\n"); free(points); *success = false; return NAN; } /* From here on, curvature is CRV_ENC_MAXIMA or CRV_MONO_DEC, and * thus ml_t is zero or positive (but not infinite). */ assert(n_pts >= orig_n_pts); if (n_pts > orig_n_pts) { /* Subset to top orig_n_pts */ subset_points(points, n_pts, orig_n_pts); n_pts = orig_n_pts; } assert(points[0].t >= min_t); assert(points[n_pts - 1].t <= max_t); #ifdef LCFIT_AUTO_VERBOSE fprintf(stderr, "starting iterative fit\n"); fprintf(stderr, "starting points: "); print_points(stderr, points, n_pts); fprintf(stderr, "\n"); #endif /* LCFIT_AUTO_VERBOSE */ /* Allocate an extra point for scratch */ points = realloc(points, sizeof(point_t) * (n_pts + 1)); size_t iter = 0; const point_t* max_pt = NULL; double ml_t = 0.0; double prev_t = 0.0; for (iter = 0; iter < MAX_ITERS; iter++) { max_pt = max_point(points, n_pts); /* Re-fit */ lcfit_bsm_rescale(max_pt->t, max_pt->ll, model); double* tbuf = malloc(sizeof(double) * n_pts); double* lbuf = malloc(sizeof(double) * n_pts); blit_points_to_arrays(points, n_pts, tbuf, lbuf); double* wbuf = malloc(sizeof(double) * n_pts); double alpha = (double) iter / (MAX_ITERS - 1); for (size_t i = 0; i < n_pts; ++i) { wbuf[i] = exp(alpha * (lbuf[i] - max_pt->ll)); } #ifdef LCFIT_AUTO_VERBOSE fprintf(stderr, "weights: "); for (size_t i = 0; i < n_pts; ++i) { fprintf(stderr, "%g ", wbuf[i]); } fprintf(stderr, "\n"); #endif /* LCFIT_AUTO_VERBOSE */ lcfit_fit_bsm_weight(n_pts, tbuf, lbuf, wbuf, model, 250); free(tbuf); free(lbuf); free(wbuf); ml_t = lcfit_bsm_ml_t(model); if (isnan(ml_t)) { fprintf(stderr, "ERROR: " "lcfit_bsm_ml_t returned NaN" ", model = { %.3f, %.3f, %.6f, %.6f }\n", model->c, model->m, model->r, model->b); *success = false; break; } if (curvature == CRV_ENC_MAXIMA) { /* Stop if the modeled maximum likelihood branch length is * within tolerance of the empirical maximum. */ if (rel_err(max_pt->t, ml_t) <= tolerance) { *success = true; break; } } double next_t = bound_point(ml_t, points, n_pts, min_t, max_t); /* Stop if the next sample point is within tolerance of the * previous sample point. */ if (rel_err(prev_t, next_t) <= tolerance) { *success = true; break; } points[n_pts].t = next_t; points[n_pts].ll = log_like->fn(next_t, log_like->args); prev_t = next_t; sort_by_t(points, n_pts + 1); curvature = classify_curve(points, n_pts + 1); if (!(curvature == CRV_ENC_MAXIMA || curvature == CRV_MONO_DEC)) { fprintf(stderr, "ERROR: " "after iteration points don't enclose a maximum " "and aren't decreasing\n"); *success = false; break; } ++n_pts; points = realloc(points, sizeof(point_t) * (n_pts + 1)); #ifdef LCFIT_AUTO_VERBOSE fprintf(stderr, "current points: "); print_points(stderr, points, n_pts); fprintf(stderr, "\n"); #endif /* LCFIT_AUTO_VERBOSE */ } if (iter == MAX_ITERS) { fprintf(stderr, "WARNING: maximum number of iterations reached\n"); } free(points); #ifdef LCFIT_AUTO_VERBOSE fprintf(stderr, "ending iterative fit after %zu iteration(s)\n", iter); #endif /* LCFIT_AUTO_VERBOSE */ if (ml_t < min_t) { ml_t = min_t; } else if (ml_t > max_t) { ml_t = max_t; } return ml_t; }
int main(int argc, char **argv) { if (argc < 4) { printf("Usage: %s <input_path> <input_dimensions> <num_clusters>\n", argv[0]); exit(1); } int numtasks, /* number of tasks in partition */ taskid, /* a task identifier */ numworkers, /* number of worker tasks */ source, /* task id of message source */ dest, /* task id of message destination */ mtype, /* message type */ rows, /* rows of 'points array' sent to each worker */ averow, extra, offset, /* used to determine points sent to each worker */ i, j, k, rc; /* misc */ MPI_Status status; /* Parse dimension of input */ int input_dim = atoi(argv[2]); /* Parse number of clusters */ int num_clusters = atoi(argv[3]); /* Cluster specific data structures */ int num_points; //no. of points in dataset double *points; //data structure for points int *membership_new; //membership for clusters double *centroids = (double*)malloc((num_clusters*input_dim)*sizeof(double)); //centroids of clusters /* MPI Initialization */ MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&taskid); MPI_Comm_size(MPI_COMM_WORLD,&numtasks); if (numtasks < 2 ) { printf("Need at least two MPI tasks. Quitting...\n"); MPI_Abort(MPI_COMM_WORLD, rc); exit(1); } numworkers = numtasks-1; if(taskid == MASTER) { FILE *fp = fopen(argv[1], "r"); num_points = 0; /* Going through the file to find the number of points */ while (!feof(fp)) { double t1, t2; fscanf(fp, "%lf,%lf\n", &t1, &t2); num_points++; } /* Initialize array for points */ points = (double*)malloc((num_points*input_dim)*sizeof(double)); /* Send file pointer to beginning of file */ rewind(fp); /* Read the points into an array */ i = 0, j = 0; while (!feof(fp)) { for (j=0; j<input_dim; ++j) { int idx = (i*input_dim) + j; if (j == 0) fscanf(fp, "%lf", &points[idx]); else fscanf(fp, ",%lf", &points[idx]); } fscanf(fp, "\n"); i++; } //print_points(points, num_points, input_dim); /* Arrays to keep track of point memberships */ int *membership_old = (int*)malloc(num_points*sizeof(int)); membership_new = (int*)malloc(num_points*sizeof(int)); /* initialize to dummy cluster values */ for (i=0; i<num_points; ++i) { membership_new[i] = 1; } /* Initialize cluster centroids */ int *centroid_idx = (int*)malloc(num_clusters*sizeof(int)); init_cluster_centroids(centroid_idx, num_clusters, num_points); for (i=0; i<num_clusters; ++i) { for (j=0; j<input_dim; ++j) { int c_idx = (i*input_dim) + j; int p_idx = (centroid_idx[i]*input_dim) + j; centroids[c_idx] = points[p_idx]; } } print_points(centroids, num_clusters, input_dim); /* The K-means loop */ int master_msg = CONTINUE; int num_iter = 0; do { num_iter++; if (num_iter % 10 == 0) printf("ITER: %d\n", num_iter); for (i=0; i<num_points; i++) { membership_old[i] = membership_new[i]; } /* Send data to nodes via MPI */ averow = num_points/numworkers; extra = num_points%numworkers; offset = 0; mtype = FROM_MASTER; for (dest=1; dest<=numworkers; dest++){ rows = (dest <= extra) ? averow+1 : averow; MPI_Send(&master_msg, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD); MPI_Send(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD); MPI_Send(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD); MPI_Send(&points[offset*input_dim], rows*input_dim, MPI_DOUBLE, dest, mtype,MPI_COMM_WORLD); MPI_Send(centroids, num_clusters*input_dim, MPI_DOUBLE, dest, mtype, MPI_COMM_WORLD); MPI_Send(&membership_new[offset], rows, MPI_INT, dest, mtype, MPI_COMM_WORLD); offset = offset + rows; } mtype = FROM_WORKER; for (dest=1; dest<=numworkers; dest++){ MPI_Recv(&offset, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD, &status); MPI_Recv(membership_new + offset, rows, MPI_INT, dest, mtype, MPI_COMM_WORLD, &status); } find_centroids(centroids, num_clusters, points, membership_new, num_points, input_dim); } while (num_changed_members(membership_old, membership_new, num_points) != 0); //end loop when two consecutive iterations have no change in assignment of clusters master_msg = EXIT; mtype = FROM_MASTER; for (dest=1; dest<=numworkers; ++dest) { MPI_Send(&master_msg, 1, MPI_INT, dest, mtype, MPI_COMM_WORLD); } printf("Finished in %d iterations\n", num_iter); printf("The final centroids are:\n"); print_points(centroids, num_clusters, input_dim); } //worker nodes if(taskid > MASTER) { int master_msg = EXIT; do { mtype = FROM_MASTER; /* Get message from master about continuing or exiting */ MPI_Recv(&master_msg, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); if (master_msg == EXIT) { break; } /* Receive offset and number of rows */ MPI_Recv(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); MPI_Recv(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); /* Allocate an array to store the points */ points = (double *)malloc((rows*input_dim)*sizeof(double)); /* Receive the points */ MPI_Recv(points, rows*input_dim, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); //print_points(points, rows, input_dim); /* Receive the centroids */ MPI_Recv(centroids, num_clusters*input_dim, MPI_DOUBLE, MASTER, mtype, MPI_COMM_WORLD, &status); /* Allocate array to store memberships */ membership_new = (int *)malloc(rows*sizeof(int)); /* Receive memberships */ MPI_Recv(membership_new, rows, MPI_INT, MASTER, mtype, MPI_COMM_WORLD, &status); int i; /* Assign points to clusters */ for (i=0; i<rows; ++i) { int p_idx = i*input_dim; double min_dist = distance((points+p_idx), (centroids), input_dim); int min_dist_idx = 1; /* For each point */ for (j=0; j<num_clusters; ++j) { /* For each cluster, find distance */ int c_idx = j*input_dim; double dist = distance((points+p_idx), (centroids+c_idx), input_dim); if (dist < min_dist) { min_dist_idx = j+1; min_dist = dist; } } /* Give the point its new cluster */ membership_new[i] = min_dist_idx; } mtype = FROM_WORKER; MPI_Send(&offset, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD); MPI_Send(&rows, 1, MPI_INT, MASTER, mtype, MPI_COMM_WORLD); MPI_Send(membership_new, rows, MPI_INT, MASTER, mtype, MPI_COMM_WORLD); } while (master_msg != EXIT); } MPI_Finalize(); return 0; }
int main(int argc, char * argv[]) { long int t, i, j, k; long int ip, im, jp, jm; int a, b, neighbors, flag; // for timekeeping int ts_return = -1; struct timeval start, end, result; double tdiff = 0.0; double total = 0.0; #ifdef DEBUG printf("Size of the world = %ldx%ld\nNumber of generations to evolve = %ld\n", N, N, T); #endif /* Initialization */ srand(42); // seed with a constant value to verify results for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { life[0][i][j] = (rand() & 0x1) ? 1 : 0; life[1][i][j] = 0; } } /* // preset patterns */ /* for (i = 0; i < N; i++) { */ /* for (j = 0; j < N; j++) { */ /* life[0][i][j] = 0; */ /* life[1][i][j] = 0; */ /* } */ /* } */ /* // toad - oscillator - period 2 */ /* life[0][2][2] = 1; */ /* life[0][2][3] = 1; */ /* life[0][2][4] = 1; */ /* life[0][3][1] = 1; */ /* life[0][3][2] = 1; */ /* life[0][3][3] = 1; */ /* // pulsar - oscillator - period 3 */ /* life[0][2][4] = 1; */ /* life[0][2][5] = 1; */ /* life[0][2][6] = 1; */ /* life[0][2][10] = 1; */ /* life[0][2][11] = 1; */ /* life[0][2][12] = 1; */ /* life[0][4][2] = 1; */ /* life[0][4][7] = 1; */ /* life[0][4][9] = 1; */ /* life[0][4][14] = 1; */ /* life[0][5][2] = 1; */ /* life[0][5][7] = 1; */ /* life[0][5][9] = 1; */ /* life[0][5][14] = 1; */ /* life[0][6][2] = 1; */ /* life[0][6][7] = 1; */ /* life[0][6][9] = 1; */ /* life[0][6][14] = 1; */ /* life[0][7][4] = 1; */ /* life[0][7][5] = 1; */ /* life[0][7][6] = 1; */ /* life[0][7][10] = 1; */ /* life[0][7][11] = 1; */ /* life[0][7][12] = 1; */ /* life[0][9][4] = 1; */ /* life[0][9][5] = 1; */ /* life[0][9][6] = 1; */ /* life[0][9][10] = 1; */ /* life[0][9][11] = 1; */ /* life[0][9][12] = 1; */ /* life[0][10][2] = 1; */ /* life[0][10][7] = 1; */ /* life[0][10][9] = 1; */ /* life[0][10][14] = 1; */ /* life[0][11][2] = 1; */ /* life[0][11][7] = 1; */ /* life[0][11][9] = 1; */ /* life[0][11][14] = 1; */ /* life[0][12][2] = 1; */ /* life[0][12][7] = 1; */ /* life[0][12][9] = 1; */ /* life[0][12][14] = 1; */ /* life[0][14][4] = 1; */ /* life[0][14][5] = 1; */ /* life[0][14][6] = 1; */ /* life[0][14][10] = 1; */ /* life[0][14][11] = 1; */ /* life[0][14][12] = 1; */ #ifdef DEBUG // display the initial setting print_points(0); #endif IF_TIME(t_start = rtclock()); #pragma scop for (t = 0; t < T; t++) { for (i = 1; i < N-1; i++) { for (j = 1; j < N-1; j++) { life[(t+1)%2][i][j] = b2s23(life[t%2][i][j], life[t%2][i-1][j+1] + life[t%2][i-1][j] + life[t%2][i-1][j-1] + life[t%2][i][j+1] + life[t%2][i][j-1] + life[t%2][i+1][j+1] + life[t%2][i+1][j] + life[t%2][i+1][j-1]); } } /* print_points(1-k); */ } #pragma endscop IF_TIME(t_end = rtclock()); IF_TIME(fprintf(stdout, "%0.6lfs\n", t_end - t_start)); if (fopen(".test", "r")) { print_points(T); } return 0; }
int main( int argc, char *argv[] ) { // Start SDL if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) { printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() ); return 1; } int result = 1; point_t *points = NULL; unsigned point_count; size_t array_size; // Parse From Text File if ( argc > 1 ) { FILE *file; file = fopen( argv[1], "r" ); char buffer[256]; if ( file == NULL ) { printf( "Unable to open file '%s'\n", argv[1] ); return 1; } if ( feof( file ) ) { printf( "Empty File!" ); fclose( file ); return 1; } // SDL to time clock_t end_time; clock_t start_time = clock(); // Get Size Info and Allocate Memory fgets( buffer, 256, file ); sscanf( buffer, "%u", &point_count ); array_size = point_count * sizeof( point_t ); printf( "Allocating %u bytes for point array (%u per point)\n", array_size, sizeof( point_t ) ); points = malloc( array_size ); // Read Points for( unsigned i = 0; i < point_count; ++i ) { //if ( feof( file ) ) if ( !fgets( buffer, 256, file ) ) { point_count = i; break; } //printf( "%s", buffer ); int read = sscanf( buffer, "(%lf, %lf)", &(points[i].x), &(points[i].y) ); //printf( "%i doubles read\n", read ); } // Print file load time end_time = clock(); printf( "\n%f seconds taken to load file\n", ((float)(end_time-start_time))/CLOCKS_PER_SEC ); start_time = end_time; // Close File fclose( file ); // Debug - Print out Points if ( point_count <= MAX_PRINT ) print_points( points, point_count ); else printf( "%u Points\nWay to many values to print!\n", point_count ); // Find and Print Closest Points point_t close1, close2; find_closest_points( points, point_count, &close1, &close2 ); printf( "Closest Points: (%lf,%lf) and (%lf,%lf)\n", close1.x, close1.y, close2.x, close2.y ); end_time = clock(); printf( "\n%f seconds finding closest\n", ((float)(end_time-start_time))/CLOCKS_PER_SEC ); // Show GUI Solution result = show_SDL_output(); } else { printf( "Please pass a text file argument when running program!\n" ); return 1; } // Free Memory if ( points != NULL ) free( points ); // Free SDL SDL_Quit(); // Return Success return result; }
int main() { point_set P; point centre; float radius; face f; int loc; if (read_points("test_geometry.p", &P) == -1) printf("\nFailed to read input points.\n"); else { print_points(stdout, &P); if (P.size >= 3) { printf("\ndistance(p1,p2) = %.3f", distance(&(P.base_point[0]), &(P.base_point[1]))); printf("\ndistance(p1,p3) = %.3f", distance(&(P.base_point[0]), &(P.base_point[2]))); printf("\ndistance(p2,p3) = %.3f\n", distance(&(P.base_point[1]), &(P.base_point[2]))); printf("\ncircumCircleRadius(p1,p2,p3) = %.3f\n", circumCircleRadius( &(P.base_point[0]), &(P.base_point[1]), &(P.base_point[2]))); if (circumCircleCentre(&(P.base_point[0]), &(P.base_point[1]), &(P.base_point[2]), ¢re)) printf("\ncircumCircleCentre(p1,p2,p3) = (%.3f,%.3f)", centre.x, centre.y); if (circumCircleCentreAndRadius(&(P.base_point[0]), &(P.base_point[1]), &(P.base_point[2]), ¢re, &radius)) printf( "\ncircumCircleCentreAndRadius(p1,p2,p3,center,rad) = (%.3f,%.3f), " "radius = %.3f\n", centre.x, centre.y, radius); f.point[0] = &(P.base_point[0]); f.point[1] = &(P.base_point[1]); loc = pointLocationRelativeToFace(&f, &(P.base_point[2])); printf( "\nPoint (%.3f, %.3f) is on the %s of the face [(%.3f, %.3f)(%.3f, %.3f)] (loc = %d)", P.base_point[2].x, P.base_point[2].y, loc ? (loc == 1 ? "right" : "left") : "top", f.point[0]->x, f.point[0]->y, f.point[1]->x, f.point[1]->y, loc); f.point[0] = &(P.base_point[1]); f.point[1] = &(P.base_point[2]); loc = pointLocationRelativeToFace(&f, &(P.base_point[0])); printf( "\nPoint (%.3f, %.3f) is on the %s of the face [(%.3f, %.3f)(%.3f, %.3f)] (loc = %d)", P.base_point[0].x, P.base_point[0].y, loc ? (loc == 1 ? "right" : "left") : "top", f.point[0]->x, f.point[0]->y, f.point[1]->x, f.point[1]->y, loc); f.point[0] = &(P.base_point[2]); f.point[1] = &(P.base_point[0]); loc = pointLocationRelativeToFace(&f, &(P.base_point[1])); printf( "\nPoint (%.3f, %.3f) is on the %s of the face [(%.3f, %.3f)(%.3f, %.3f)] (loc = %d)", P.base_point[1].x, P.base_point[1].y, loc ? (loc == 1 ? "right" : "left") : "top", f.point[0]->x, f.point[0]->y, f.point[1]->x, f.point[1]->y, loc); } } test_pointLocationRelativeToFace(); return 0; }