void build_neural_net_from_cmds(int argc, char **argv) { int i, j; int num_layers; int *layer_specs; num_layers = atoi(argv[1]); layer_specs = (int *) malloc((num_layers + 1) * sizeof(int)); for (i = 0; i < num_layers + 1; i++) { layer_specs[i] = atoi(argv[i + 2]); } nn = (neural_net *) malloc(sizeof(neural_net)); nn->num_layers = num_layers; nn->layer_ptrs = (neural_layer **) malloc(num_layers * sizeof(neural_layer *)); for (i = 0; i < num_layers; i++) { nn->layer_ptrs[i] = (neural_layer *) malloc(sizeof(neural_layer)); } nn->layer_ptrs[0]->input = new_vector(layer_specs[0]); for (i = 0; i < num_layers - 1; i++) { nn->layer_ptrs[i]->output = new_vector(layer_specs[i + 1]); nn->layer_ptrs[i + 1]->input = nn->layer_ptrs[i]->output; } nn->layer_ptrs[num_layers - 1]->output = new_vector(layer_specs[num_layers]); nn->input = nn->layer_ptrs[0]->input; nn->output = nn->layer_ptrs[num_layers - 1]->output; for (i = 0; i < num_layers; i++) { nn->layer_ptrs[i]->r = new_vector(nn->layer_ptrs[i]->output->size); nn->layer_ptrs[i]->t = new_vector(nn->layer_ptrs[i]->output->size); nn->layer_ptrs[i]->s = new_vector(nn->layer_ptrs[i]->output->size); nn->layer_ptrs[i]->dL_ds_local = new_vector(nn->layer_ptrs[i]->output->size); nn->layer_ptrs[i]->dL_ds_global = new_vector(nn->layer_ptrs[i]->output->size); for (j = 0; j < nn->layer_ptrs[i]->t->size; j++) { nn->layer_ptrs[i]->t->data[j] = 1; } } for (i = 0; i < num_layers; i++) { nn->layer_ptrs[i]->w = new_matrix(nn->layer_ptrs[i]->output->size, nn->layer_ptrs[i]->input->size); fill_matrix_rand(nn->layer_ptrs[i]->w, -.5, .5); nn->layer_ptrs[i]->w_T = new_matrix(nn->layer_ptrs[i]->w->num_cols, nn->layer_ptrs[i]->w->num_rows); compute_matrix_transpose(nn->layer_ptrs[i]->w, nn->layer_ptrs[i]->w_T); } }
Vector Vector::operator+(const Vector rhs) { Vector new_vector(this->x, this->y); new_vector += rhs; return new_vector; }
int main(int argc, char *argv[]) { // Initialize randomness srand(time(NULL)); // 7 parameters are required. if (argc != 13) { fprintf(stderr, "Usage: %s obj-file sx sy sz tx ty tz rx ry rz focal-dist hiding\n", argv[0]); return 0; } unsigned int i, j; double sx = atof( argv[2] ); double sy = atof( argv[3] ); double sz = atof( argv[4] ); double tx = atof( argv[5] ); double ty = atof( argv[6] ); double tz = atof( argv[7] ); double rx = atof( argv[8] ); double ry = atof( argv[9] ); double rz = atof( argv[10] ); double fd = atof( argv[11] ); int hiding = atoi( argv[12] ); matrix_double scale = scale_by(sx, sy, sz); matrix_double tras = traslate(tx, ty, tz); matrix_double rot = rotate(rx, ry, rz); matrix_double proj = projection(fd); file_data vnf = readobj(argv[1]); // int p; // for (p = 0; p < vnf.vertices.num_elems; p++) { // point3d t = (vnf.vertices.data)[p]; // fprintf(stderr, "(%f,%f,%f)\n", t.x, t.y, t.z); // } // Join all the transformations in a single matrix // Operation order: Scaling, rotation, traslation and projection matrix_double t1 = product(proj, tras); matrix_double t2 = product(t1, rot); matrix_double t3 = product(t2, scale); apply_matrix(t3, vnf.vertices); dispose_matrix(&t3); dispose_matrix(&t2); dispose_matrix(&t1); dispose_matrix(&scale); dispose_matrix(&tras); dispose_matrix(&rot); dispose_matrix(&proj); // Projection sets w = z, so divide everything by w. for (i = 0; i < vnf.vertices.num_elems; i++) { point3d tmp = (vnf.vertices.data)[i]; tmp.x /= tmp.w; tmp.y /= tmp.w; tmp.z /= tmp.w; (vnf.vertices.data)[i] = tmp; } // The camera normal (taking advantage of perspective projection) vector camera_normal = new_vector( new_point3d(0.0, 0.0, 0.0, 1.0), new_point3d(0.0, 0.0, 1.0, 1.0) ); // The z-buffer double **zbuf = calloc(HEIGHT, sizeof *zbuf); for (i = 0; i < HEIGHT; i++) { zbuf[i] = calloc(WIDTH, sizeof **zbuf); for (j = 0; j < WIDTH; j++) zbuf[i][j] = DBL_MAX; } color c = new_color( 255, 255, 255 ); point center = { WIDTH >> 1, HEIGHT >> 1 }; int invertX = 0, invertY = 1; raster tmp = new_raster(WIDTH, HEIGHT, 3); for (i = 0; i < vnf.faces.num_elems; i++) { point3d v1 = (vnf.vertices.data)[((vnf.faces.data)[i].v1) - 1]; point3d v2 = (vnf.vertices.data)[((vnf.faces.data)[i].v2) - 1]; point3d v3 = (vnf.vertices.data)[((vnf.faces.data)[i].v3) - 1]; point pt[3] = { new_point(v1.x, v1.y), new_point(v2.x, v2.y), new_point(v3.x, v3.y) }; for (j = 0; j < 3; j++) pt[j] = raster_translate(pt[j], center, invertX, invertY); // Hiding faces using face normals if (hiding == 1) { vector va = new_vector( v1, v2 ); vector vb = new_vector( v1, v3 ); vector vn = vector_crossproduct( va, vb ); if ( abs(vector_angle(vn, camera_normal)) < 90.0 ) continue; } // Drawing the face for (j = 0; j < 3; j++) { int curr = j; int next = (j+1) % 3; point ps = pt[curr]; point pe = pt[next]; // Use z-buffering if allowed // if (hiding == 2) // put_line_z(tmp, new_line( ps, pe ), c, bresenham, zbuf, vertices[3][curr], vertices[3][next]); // else put_line(tmp, new_line( ps, pe ), c, bresenham); } // Filling the face using a random, eye-pleasing colour color cr = { ((rand() % 255) + 255) >> 1, ((rand() % 255) + 255) >> 1, ((rand() % 255) + 255) >> 1 }; // if (hiding == 2) // fill_face_z(tmp, pt[0], pt[1], pt[2], cr, zbuf, vertices[3][j], vertices[3][(j+1) % 3]); // elseelse fill_face(tmp, pt[0], pt[1], pt[2], cr); } raster_out(tmp); dispose_raster(tmp); for (i = 0; i < HEIGHT; i++) free(zbuf[i]); free(zbuf); free(vnf.vertices.data); free(vnf.faces.data); return 0; }
int main(int argc, char *argv[]) { test_case tests[] = { {"Dot with C code ", dot_c, flops_dot}, #if SSE {"Dot with SSE x 4 ", dot_sse, flops_dot}, {"Dot with SSE x 16 ", dot_sse_16, flops_dot}, #endif {"Distance with C code ", distance_c , flops_distance}, #if SSE {"Distance with SSE x 4 ", distance_sse, flops_distance}, {"Distance with SSE x 16 ", distance_sse_16, flops_distance}, #endif {NULL, NULL, NULL} }; float norm; float secs; int i; int n; float *results; float *vector1; float *vector2; int size = 32; int inc = 4; int nTimes; int nTests = sizeof(tests) /sizeof(test_case) - 1; norm = (float)sysconf(_SC_CLK_TCK); vector1 = new_vector(1<<MAXPOW2); vector2 = new_vector(1<<MAXPOW2); results = malloc(nTests * (MAXPOW2 - MINPOW2)*sizeof(float)); for(size=(1<<MINPOW2),n=0; size<(1<<MAXPOW2);size=size<<1,n++ ) { inc++; for(i=0; tests[i].desc; ++i) { float dot; int j; timestamp_t t0, t1; nTimes = (int)ceil( (MFLOPS*1e6F)/(float)tests[i].flops(size) ); t0 = get_timestamp(); for(j=0; j<nTimes; ++j) dot = tests[i].f(vector1, vector2, size); t1 = get_timestamp(); secs = (t1 - t0) / 1000000.0L; fprintf(stderr,"nTimes=%d %d: %s => (flops %f : time:%g us)\n", nTimes, size, tests[i].desc, (tests[i].flops(size)*nTimes)/secs/1e6F, secs); results[(n*nTests)+i]=(tests[i].flops(size)*nTimes)/secs/1e6F; } } for(n=0; n<(MAXPOW2 - MINPOW2); ++n) { printf("%d,\t", 1<<(MINPOW2+n)); for(i=0; i<nTests; ++i) { printf("%f,\t", results[(n*nTests)+i]); } printf("\n"); } free(vector1); free(vector2); free(results); return 0; }
void GraphDrawer::plotPolarisation(vector<MtzPtr> mtzs) { int count = 36; int divide = 360 / count; std::map<int, double> histogram; std::map<int, int> counts; for (int i = 0; i < mtzs.size(); i++) { for (int j = 0; j < mtzs[i]->reflectionCount(); j++) { if (!mtzs[i]->reflection(j)->betweenResolutions(1.8, 1.4)) continue; for (int k = 0; k < mtzs[i]->reflection(j)->millerCount(); k++) { MillerPtr miller = mtzs[i]->reflection(j)->miller(k); if (miller->getRawIntensity() < 0) continue; vec hkl = new_vector(miller->getH(), miller->getK(), miller->getL()); mtzs[i]->getMatrix()->multiplyVector(&hkl); double angle = cartesian_to_angle(hkl.h, hkl.k); double degrees = angle * 180 / M_PI; int category = (int)(degrees / divide) * divide; if (histogram.count(category) == 0) { histogram[category] = 0; counts[category] = 0; } histogram[category] += miller->getRawIntensity(); counts[category]++; } } } vector<double> xs, ys; GraphMap graphMap; graphMap["yMin"] = 0; graphMap["yMax"] = 200; graphMap["title"] = "Average raw intensity vs angle on detector"; graphMap["xTitle"] = "Angle on detector"; graphMap["yTitle"] = "Average raw intensity"; int num = 0; for (int i = 0; i <= 0; i++) { for (std::map<int, double>::iterator it = histogram.begin(); it != histogram.end(); it++) { if (i == 0) histogram[it->first] /= counts[it->first]; xs.push_back(it->first + (i * 360)); ys.push_back(histogram[it->first]); num++; } } std::cout << "Number of X values: " << num << std::endl; plot("polarisation", graphMap, xs, ys); }
void add_point(face_t *self, const char *point_string) { string_iterator_t *it; int *v; int *vt; int *vn; switch(nb_delimiters(point_string, SUB_DELIMITER)) { // f v1 v2 v3 [v*]* case 0: v = malloc(sizeof(int)); *v = atoi(point_string); add_to_vector(self->vertexes, v); break; // f v1/vt1 v2/vt2 v3/vt3 [v*/vt*]* case 1: it = new_string_iterator(point_string, SUB_DELIMITER); v = malloc(sizeof(int)); vt = malloc(sizeof(int)); if(self->textures == NULL) { self->textures = new_vector(MINIMAL_POINTS); } *v = atoi(it->current_string); add_to_vector(self->vertexes, v); next_string_iterator(it); *vt = atoi(it->current_string); add_to_vector(self->textures, vt); free_string_iterator(it); break; // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 [v*/vt*/vn*]* case 2: it = new_string_iterator(point_string, SUB_DELIMITER); v = malloc(sizeof(int)); vt = malloc(sizeof(int)); vn = malloc(sizeof(int)); if(self->textures == NULL) { self->textures = new_vector(MINIMAL_POINTS); } if(self->normals == NULL) { self->normals = new_vector(MINIMAL_POINTS); } *v = atoi(it->current_string); add_to_vector(self->vertexes, v); next_string_iterator(it); *vn = atoi(it->current_string); next_string_iterator(it); // case ?/?/? if(it->good) { *vt = *vn; add_to_vector(self->textures, vt); *vn = atoi(it->current_string); // case ?//? } else { free(vt); free_vector(self->textures); self->textures = NULL; } add_to_vector(self->normals, vn); free_string_iterator(it); break; } }
double* reconstruction(PSIRT* psirt) { int i=0; //for(i=0;i<n_particulas;i++) printf("\r\nDONE\tPARTICLE #%d STATUS: %d",i,particles[i]->status); // --- // 1. AJUSTAR ESCOPO // O universo de particulas esta inicialmente entre -1 e +1. // Ajustar de acordo com o fator de escala. // --- for (i = 0; i < psirt->n_particles; i++) { if ((psirt->particles[i]->location->x > -RECONSTRUCTION_SCALE_FACTOR) && (psirt->particles[i]->location->x < RECONSTRUCTION_SCALE_FACTOR) && (psirt->particles[i]->location->y > -RECONSTRUCTION_SCALE_FACTOR) && (psirt->particles[i]->location->y < RECONSTRUCTION_SCALE_FACTOR)) { psirt->particles[i]->location->x += RECONSTRUCTION_SCALE_FACTOR; psirt->particles[i]->location->y += RECONSTRUCTION_SCALE_FACTOR; psirt->particles[i]->location->x /= 2 * RECONSTRUCTION_SCALE_FACTOR; psirt->particles[i]->location->y /= 2 * RECONSTRUCTION_SCALE_FACTOR; } } // --- // 2. ESCALAR // Escalar particulas de acordo com resolucao. // --- Vector2D** scaled_particles = malloc(sizeof(Vector2D*) * psirt->n_particles); for (i = 0; i < psirt->n_particles; i++) { scaled_particles[i] = mult_constant(psirt->particles[i]->location, RES_X); //printf("\r\n ANTES: %f,%f \t DEPOIS: %f,%f",particles[i]->location->x, particles[i]->location->y,scaled_particles[i]->x,scaled_particles[i]->y ); } // --- // 3. CALCULAR INTENSIDADE DE CADA PIXEL // Para cada pixel, medir a distancia entre seu centro // e cada particula presente no sistema. // --- double *pixel_intensity = malloc(sizeof(double) * RES_X * RES_Y); for (i = 0; i < RES_X * RES_Y; i++) pixel_intensity[i] = 0.0; Vector2D* pixel = new_vector(0.0, 0.0); int pix_x = 0, pix_y = 0, part = 0; double iter_intensity = 0.0; for (pix_x = 0; pix_x < RES_X; pix_x++) { for (pix_y = 0; pix_y < RES_Y; pix_y++) { // atualiza pixel atual (centro: bias +0.5, +0.5) set_vector(pixel, pix_x + 0.5, pix_y + 0.5); int particula = -1; // distancia para cada particula for (part = 0; part < psirt->n_particles; part++) { if (psirt->particles[part]->status != DEAD) { // formato: x + ( y*RES_X ) double distance = vector_vector_distance(pixel, scaled_particles[part]); //!!!!!!!!!!!!!!!!! // if (particula == -1) particula = part; // // else if (part != particula) // { // if ( (scaled_particles[part]->x > scaled_particles[particula]->x) // & // ( (fabs(scaled_particles[part]->y - scaled_particles[particula]->y ) <= TRAJ_PART_THRESHOLD*RES_X ) ) ) // { // printf("\r\nParticula fixa: [%d] (%f,%f)\t comparada com [%d] (%f,%f)\t DIST = %f", // particula, scaled_particles[particula]->x, scaled_particles[particula]->y, // part, scaled_particles[part]->x, scaled_particles[part]->y, // distance); // } // } if (distance > PARTICLE_SIZE) { distance = 0.0; iter_intensity = 0.0; } else { distance = ((PARTICLE_SIZE - distance) / PARTICLE_SIZE); // distance= distance/PARTICLE_SIZE; // distance = 1-distance; iter_intensity = distance; //TODO interf. destrutiva? // iter_intensity = // (iter_intensity > SATURATION) ? SATURATION : iter_intensity; } if (iter_intensity > 0) pixel_intensity[pix_x + (pix_y * RES_X)] += iter_intensity; } } } } free(scaled_particles); free(pixel); double max = -1.0, min = 999999.0, delta = 0; for (i = 0; i < RES_X * RES_Y; i++) { if (pixel_intensity[i] > max) max = pixel_intensity[i]; else if (pixel_intensity[i] < min) min = pixel_intensity[i]; // if (i%RES_X==0) printf("\r\n"); // printf("%f ",pixel_intensity[i]); } delta = max - min; // normalize for (i = 0; i < RES_X * RES_Y; i++) { pixel_intensity[i] -= min; pixel_intensity[i] /= delta; } return pixel_intensity; }