void euler_cromer(const data* dat, output_function output) { int i; double dt = dat->eta * delta_t_factor; vector *a = (vector*) malloc((dat->N)*sizeof(vector)), *a_1 = (vector*) malloc((dat->N)*sizeof(vector)), *r = init_r(dat), *v = init_v(dat); double time = 0; output(time, dt, dat, r, v); while (time < dat->t_max) { accelerations(dat, r, a); adots(dat, r, v, a_1); for (i = 0; i < dat->N; i++) { vector v_neu = vector_add(v[i], scalar_mult(dt, a[i])), r_neu = vector_add(r[i], scalar_mult(0.5 * dt, vector_add(v[i], v_neu))); r[i] = r_neu; v[i] = v_neu; } dt = delta_t(dat, a, a_1); time += dt; output(time, dt, dat, r, v); } free(a); free(r); free(v); }
/*********************************************************************** * Compute the complexity of a motif as a number between 0 and 1. * * Motif complexity is the average K-L distance between the "motif * background distribution" and each column of the motif. The motif * background is just the average distribution of all the columns. The * K-L distance, which measures the difference between two * distributions, is the same as the information content: * * \sum_i p_i log(p_i/f_i) * * This value increases with increasing complexity. ***********************************************************************/ double compute_motif_complexity (MOTIF_T* a_motif) { double return_value; ARRAY_T* motif_background; // Mean emission distribution. int num_rows; int i_row; int num_cols; int i_col; num_cols = get_alph_size(ALPH_SIZE); num_rows = get_num_rows(a_motif->freqs); // Compute the mean emission distribution. motif_background = get_matrix_col_sums(a_motif->freqs); scalar_mult(1.0 / (double)num_rows, motif_background); // Compute the K-L distance w.r.t. the background. return_value = 0; for (i_row = 0; i_row < num_rows; i_row++) { ARRAY_T* this_emission = get_matrix_row(i_row, a_motif->freqs); for (i_col = 0; i_col < num_cols; i_col++) { ATYPE this_item = get_array_item(i_col, this_emission); ATYPE background_item = get_array_item(i_col, motif_background); // Use two logs to avoid handling divide-by-zero as a special case. return_value += this_item * (my_log(this_item) - my_log(background_item)); } } free_array(motif_background); return(return_value / (double)num_rows); }
void verlet(const data* dat, output_function output) { int i; const double dt = dat->eta * delta_t_factor; // konstant vector *a = (vector*) malloc((dat->N)*sizeof(vector)), *a_1 = (vector*) malloc((dat->N)*sizeof(vector)), *r_p = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n-1), not initialized *r = init_r(dat), // r_(n) *r_n = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n+1), not initialized *v = init_v(dat); // v_(n) double time = 0; output(time, dt, dat, r, v); // set initial data accelerations(dat, r, a); // a_0 adots(dat, r, v, a_1); for (i = 0; i < dat->N; i++) { // set r_(-1) r_p[i] = vector_add(r[i], vector_add(scalar_mult(dt, v[i]), scalar_mult(0.5 * dt * dt, a[i]))); // set r_1 r_n[i] = vector_add(scalar_mult(2, r[i]), vector_add(scalar_mult(-1, r_p[i]), scalar_mult(dt * dt, a[i]))); } //time += dt; // regular timesteps while (time < dat->t_max) { accelerations(dat, r_n, a); // a_n+1 (gets shifted to a_n) for (i = 0; i < dat->N; i++) { // shift indexes n+1 -> n r_p[i] = r[i]; r[i] = r_n[i]; r_n[i] = vector_add(scalar_mult(2, r[i]), vector_add(scalar_mult(-1, r_p[i]), scalar_mult(dt * dt, a[i]))); v[i] = scalar_mult(0.5 / dt, vector_diff(r_n[i], r_p[i])); } adots(dat, r, v, a_1); time += dt; output(time, dt, dat, r, v); } free(a); free(r_p); free(r); free(r_n); free(v); }
int main() { screen s; color c; c.red = 0; c.green = 255; c.blue = 255; int i, j; for( i=0; i<XRES; i++) for ( j=0; j<YRES; j++) { /* c.red = random() % (MAX_COLOR + 1); c.green = random() % (MAX_COLOR + 1); c.blue = random() % (MAX_COLOR + 1); */ c.red= 190; c.blue =150; c.green=175; plot( s, c, i, j); } c.blue=255; c.red =0; c.green = 150; printf("Matrix Testing Area. Please stand well out of range of the matrix that is about to happen.\n"); printf("Making a 3X6 matrix.\n\n"); struct matrix* testing = new_matrix(3,6); add_edge(testing, 1, 11, 1, 2, 12, 1); add_edge(testing, 3, 13, 1, 4, 14, 1); add_edge(testing, 5, 15, 1, 6, 16, 1); printf("Points made. Print test.\n"); print_matrix(testing); printf("Scalar Multiplication Test:\n"); scalar_mult(3, testing); print_matrix(testing); printf("Identity test. Time to face the facts.\n"); struct matrix* identity = new_matrix(6, 6); ident(identity); print_matrix(identity); printf("Testing matrix multiplication. Prepare your eyes. Multiplying by the identity matrix now.\n"); matrix_mult(testing, identity); print_matrix(identity); drawPicture(s, c); display( s ); save_ppm(s, "image" ); save_extension(s, "image.jpg"); }
static expr *rexp3(int critical) { expr *e, *f; int64_t v; e = expr0(critical); if (!e) return NULL; while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT || i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) { int j = i; i = scan(scpriv, tokval); f = expr0(critical); if (!f) return NULL; e = add_vectors(e, scalar_mult(f, -1L, false)); switch (j) { case TOKEN_EQ: case TOKEN_NE: if (is_unknown(e)) v = -1; /* means unknown */ else if (!is_really_simple(e) || reloc_value(e) != 0) v = (j == TOKEN_NE); /* unequal, so return true if NE */ else v = (j == TOKEN_EQ); /* equal, so return true if EQ */ break; default: if (is_unknown(e)) v = -1; /* means unknown */ else if (!is_really_simple(e)) { nasm_error(ERR_NONFATAL, "`%s': operands differ by a non-scalar", (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j == TOKEN_GE ? ">=" : ">")); v = 0; /* must set it to _something_ */ } else { int64_t vv = reloc_value(e); if (vv == 0) v = (j == TOKEN_LE || j == TOKEN_GE); else if (vv > 0) v = (j == TOKEN_GE || j == TOKEN_GT); else /* vv < 0 */ v = (j == TOKEN_LE || j == TOKEN_LT); } break; } if (v == -1) e = unknown_expr(); else e = scalarvect(v); } return e; }
void leap_frog(const data* dat, output_function output) { int i; double dt = dat->eta * delta_t_factor; vector *a = (vector*) malloc((dat->N)*sizeof(vector)), *a_1 = (vector*) malloc((dat->N)*sizeof(vector)), *r_p = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n+1/2), not initialized *r = init_r(dat), // r_(n+1) *r_n = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n+3/2), not initialized *v = init_v(dat); // v_(n+1) double time = 0; output(time, dt, dat, r, v); // initial step for (i = 0; i < dat->N; i++) // set r_(1/2) r_n[i] = vector_add(r[i], scalar_mult(0.5 * dt, v[i])); // regular timesteps while (time < dat->t_max) { // a_(n+1/2) accelerations(dat, r_n, a); adots(dat, r_n, v, a_1); for (i = 0; i < dat->N; i++) { // store previous values as r_(n+1/2) r_p[i] = r_n[i]; // v_(n+1) vector_add_to(&v[i], scalar_mult(dt, a[i])); // r_(n+3/2) vector_add_to(&r_n[i], scalar_mult(dt, v[i])); // build r_(n+1) r[i] = scalar_mult(0.5, vector_add(r_p[i], r_n[i])); } dt = delta_t(dat, a, a_1); time += dt; output(time, dt, dat, r, v); } free(a); free(r_p); free(r); free(r_n); free(v); }
/*********************************************************************** * Make an array have variance one by dividing by the standard * deviation. ***********************************************************************/ void variance_one_array (ARRAY_T* array) { ATYPE variance; variance = array_variance(array); /* Avoid divide by zero. */ if (variance == 0.0) { fprintf(stderr, "Warning: variance of zero.\n"); } else { scalar_mult(1.0 / sqrt(array_variance(array)), array); } }
void drawPicture(screen s, color c){ struct matrix* picture = new_matrix(3, 5); int startx = 45; int starty = 400; int counter = 1; int sizec = 0.5; int locationc = 50; while(counter != 9){ free_matrix(picture); picture = new_matrix(3,5); startx+= locationc; //starty = starty + 25*(7-counter); add_edge(picture, startx, starty, 1, startx+50, starty, 1); add_edge(picture, startx+50, starty, 1, startx+90, starty-40, 1); add_edge(picture, startx+90, starty-40, 1, startx+90, starty-90, 1); add_edge(picture, startx+90, starty-90, 1, startx+50, starty-130, 1); add_edge(picture, startx+50, starty-130, 1, startx, starty-130, 1); add_edge(picture, startx, starty-130, 1, startx-40, starty-90,1); add_edge(picture, startx-40, starty-90,1, startx-40,starty-40,1); add_edge(picture, startx-40, starty-40,1, startx, starty, 1); scalar_mult(sizec +(counter *0.10), picture); locationc+= 5; draw_lines(picture, s,c); counter++; } //print_matrix(picture); //scalar_mult(1.5, picture); /* scalar_mult(0.5, picture); matrix_mult(transformers, picture); draw_lines(picture, s, c); */ }
void calculate_intensity(struct vector *c, struct constants *k, color ambient, struct light *light, struct vector *p, struct vector *n, struct vector *v) { /* I = Ke + Ia Ka + Ip Kd (->L * ->N) + Ip Ks [(2->N(->L * ->N)- ->L) * ->V] */ double dotd, dots; struct vector l, tmp; l = *p; subtract_vectors(&l, &light->l); normalize(&l); dotd = dot(&l, n); if (dotd < 0) { dotd = dots = 0; goto add_terms; } tmp = *n; scalar_mult(2 * dotd, &tmp); subtract_vectors(&tmp, &l); dots = dot(&tmp, v); if (dots < 0) dots = 0; add_terms: ctov(c, k->e); ctov(&tmp, ambient); tmp.x *= k->r[Ka]; tmp.y *= k->g[Ka]; tmp.z *= k->b[Ka]; add_vectors(c, &tmp); ctov(&tmp, light->c); tmp.x *= k->r[Kd] * dotd + k->r[Ks] * dots; tmp.y *= k->g[Kd] * dotd + k->g[Kd] * dots; tmp.z *= k->b[Kd] * dotd + k->b[Ks] * dots; add_vectors(c, &tmp); }
/*************************************************************************** * Set the sequence weights according to an external file. * * If the filename is "none," "internal," or NULL, then the weights are * set uniformly. ***************************************************************************/ void set_sequence_weights (char* weight_filename, // Name of file containing sequence weights. int num_seqs, // Number of sequences. SEQ_T** sequences) // The sequences. { ARRAY_T* weights; FILE * weight_file; int i_seq; /* Allocate the weights. */ weights = allocate_array(num_seqs); /* Set uniform weights if no file was supplied. */ if ((weight_filename == NULL) || (strcmp(weight_filename, "none") == 0) || (strcmp(weight_filename, "internal") == 0)) { init_array(1.0, weights); } /* Read the weights from a file. */ else { if (open_file(weight_filename, "r", FALSE, "weight", "weights", &weight_file) == 0) exit(1); read_array(weight_file, weights); fclose(weight_file); /* Normalize the weights so they add to the total number of sequences. */ normalize(0.0, weights); scalar_mult(num_seqs, weights); } /* Assign the weights to the sequences. */ for (i_seq = 0; i_seq < num_seqs; i_seq++) { (sequences[i_seq])->weight = get_array_item(i_seq, weights); } /* Free the weights. */ free_array(weights); }
/*********************************************************************** * Normalize the elements of an array to sum to 1.0. ***********************************************************************/ void normalize (ATYPE close_enough, /* If the total is close to 1.0, don't bother. */ ARRAY_T* array) { ATYPE total; /* Compute the sum of the elements in the array. */ total = array_total(array); /* Only normalize if we're relatively far from 1.0. */ if (almost_equal(total, 1.0, close_enough)) { return; } /* If there's nothing in the array, then return a uniform distribution. */ if (total == 0.0) { init_array(1.0 / (ATYPE)get_array_length(array), array); return; } /* Divide by the total. */ scalar_mult(1.0 / total, array); }
int main() { screen s; color c; c.red = 0; c.green = 255; c.blue = 255; int i, j; for( i=0; i<XRES; i++) for ( j=0; j<YRES; j++) { c.red = random() % (MAX_COLOR + 1); c.green = random() % (MAX_COLOR + 1); c.blue = random() % (MAX_COLOR + 1); plot( s, c, i, j); } struct matrix * points = new_matrix(4, 4); add_point(points, 50, 250, 0); add_point(points, 70, 250, 0); add_edge(points, 30, 100, 0, 300, 400, 0); draw_lines(points, s, c); print_matrix(points); printf("\n"); scalar_mult(2.0, points); printf("\n"); ident(points); printf("\n"); display( s ); save_ppm(s, "picture" ); save_extension(s, "picture.jpg"); }
/*Modifica le velocità delle particelle coinvolte nella collisione*/ void switch_speeds(){ int j; int x,y; double temp_r_diff[N]; /* Vettore differenza temporaneo per le 9 immagini*/ double v_diff[N]; double rdiff[2]={0,0}; /*Vero vettore differenza*/ /* temp_r_diff = R0 - R1 * v_diff = V0 _ V1 */ double min = DBL_MAX; double tmp_dbl; particle_s temp_part; double v_temp; for ( x= -1; x < 2 ; x++){ for ( y = -1; y<2 ; y++){ temp_part = particleList[index_collision[1]]; temp_part.position[0] += x; temp_part.position[1] += y; diff(particleList[index_collision[0]].position,temp_part.position, temp_r_diff); /*vettore differenza salvato in temp_r_diff*/ tmp_dbl = scalar_prod(temp_r_diff,temp_r_diff) ; if ( tmp_dbl < min){ min = tmp_dbl; for ( j= 0; j<N; j++){ rdiff[j] = temp_r_diff[j]; } } } } diff( particleList[index_collision[0]].speed, particleList[index_collision[1]].speed, v_diff); scalar_mult( 1/(sqrt(scalar_prod(rdiff,rdiff))), rdiff); v_temp = scalar_prod(v_diff,rdiff); for ( j = 0 ; j < N ; j++){ particleList[index_collision[0]].speed[j] -= v_temp*rdiff[j]; particleList[index_collision[1]].speed[j] += v_temp*rdiff[j]; } }
static expr *expr4(int critical) { expr *e, *f; e = expr5(critical); if (!e) return NULL; while (i == '+' || i == '-') { int j = i; i = scan(scpriv, tokval); f = expr5(critical); if (!f) return NULL; switch (j) { case '+': e = add_vectors(e, f); break; case '-': e = add_vectors(e, scalar_mult(f, -1L, false)); break; } } return e; }
int main() { int i, j; screen s; color c; struct matrix *edges; struct matrix *transform; struct matrix *identity; edges = new_matrix(4, 4); transform = new_matrix(4, 4); identity = new_matrix(4, 4); /* Different Matrices for Testing Different Matrix Operations struct matrix *tester; struct matrix *tester2; tester2 = new_matrix(4, 2); tester = new_matrix(4, 4); clear_matrix( edges ); printf("Edge Matrix:\n"); print_matrix( edges ); clear_matrix( transform ); printf("Transform Matrix:\n"); print_matrix( transform ); ////////////////////////////////// Initializations of Tester Matrices ////////////////////////////////// counter = 0; for (i = 0; i < tester->rows; i++ ) { for (j = 0; j < tester->cols; j++ ) { tester->m[i][j] = counter; counter += 1; } } printf("Tester Matrix:\n"); print_matrix( tester ); counter = 0; for (i = 0; i < tester2->rows; i++ ) { for (j = 0; j < tester2->cols; j++ ) { tester2->m[i][j] = 1; } } printf("Tester2 Matrix:\n"); print_matrix( tester2 ); printf("\n\n"); /////////////////////////////// IDENTITY printf("Testing Identity: Identity Matrix\n"); print_matrix( identity ); SCALAR MULTIPLICATION printf("Testing Scalar Multiplication ( 2.5 * Tester )\n"); scalar_mult( mult, tester ); print_matrix( tester ); MATRIX MULTIPLICATION printf("Testing Matrix Multiplication With Identity => SAME\n"); matrix_mult( identity, tester ); print_matrix( tester ); printf("Testing Matrix Multiplication With (4x4) and (4x2)\n"); matrix_mult( tester, tester2 ); print_matrix( tester2 ); printf("\n\n"); printf("Tester Before\n"); clear_matrix( tester ); print_matrix( tester ); ADDING POINTS printf("Adding Point to tester\n"); add_point( tester, 1, 2, 3); print_matrix( tester ); printf("\nAdding Point to tester again\n"); add_point( tester, 4, 5, 6); add_point( tester, 7, 8, 9); add_point( tester, 10, 11, 12); add_point( tester, 13, 14, 15); print_matrix( tester ); ADDING EDGES printf("Adding Edge to Tester\n"); add_edge( tester, 1, 1, 1, 2, 2, 2 ); add_edge( tester, 3, 3, 3, 4, 4, 4 ); add_edge( tester, 5, 5, 5, 6, 6, 6 ); print_matrix( tester ); free_matrix( tester ); free_matrix( tester2 ); ident( identity ); int x0, y0, z0, x1, y1, z1; z0 = 0; z1 = 0; */ c.red = MAX_COLOR; c.green = MAX_COLOR; c.blue = MAX_COLOR; clear_screen(s); //SCALAR MULT (DILATION) clear_matrix(edges); clear_matrix(transform); add_edge( edges, 1,1,0, 6,1,0 ); add_edge( edges, 6,1,0, 6,6,0 ); add_edge( edges, 6,6,0, 1,6,0 ); add_edge( edges, 1,6,0, 1,1,0 ); draw_lines(edges,s,c); for(i = 0; i < 55; i++){ scalar_mult(1.09, edges); c.red = c.red - 4; draw_lines(edges,s,c); } //TRANSLATE clear_matrix(edges); clear_matrix(transform); c.red = MAX_COLOR; add_edge( edges, 0,YRES,0, 0,YRES-25,0 ); add_edge( edges, 0,YRES-25,0, 25,YRES-25,0 ); add_edge( edges, 25,YRES-25,0, 25,YRES,0 ); add_edge( edges, 25,YRES,0, 0,YRES,0 ); draw_lines(edges,s,c); for(i = 0; i < 20; i++){ transform = make_translate( 25, -25, 0 ); matrix_mult( transform, edges); c.green = c.green - 4; draw_lines(edges,s,c); } //ROTATION clear_matrix(edges); clear_matrix(transform); c.green = MAX_COLOR; add_edge( edges, XRES,0,0, XRES,25,0 ); add_edge( edges, XRES,25,0, XRES-25,25,0 ); add_edge( edges, XRES-25,25,0, XRES-25,0,0 ); add_edge( edges, XRES-25,0,0, XRES,0,0 ); draw_lines(edges,s,c); for(i = 0; i < 20; i++){ transform = make_rotZ( 4.5 ); matrix_mult( transform, edges); c.blue = c.blue - 4; draw_lines(edges,s,c); } //Central Flower clear_matrix(edges); clear_matrix(transform); c.blue = MAX_COLOR; c.red = 0; c.green = 0; add_edge( edges, 200,200,0, 200,300,0 ); add_edge( edges, 200,300,0, 300,300,0 ); add_edge( edges, 300,300,0, 300,200,0 ); add_edge( edges, 300,200,0, 200,200,0 ); draw_lines(edges,s,c); for(i = 0; i < 36; i++){ transform = make_rotZ( 10 ); matrix_mult( transform, edges); transform = make_translate( 50, -50, 0); matrix_mult( transform, edges); draw_lines(edges,s,c); } //NAME clear_matrix(edges); clear_matrix(transform); c.blue = MAX_COLOR; c.red = MAX_COLOR; c.green = MAX_COLOR; add_edge( edges, 250,0,0, 250,25,0 );//M add_edge( edges, 250,25,0, 263,0,0 ); add_edge( edges, 263,0,0, 275,25,0 ); add_edge( edges, 275,25,0, 275,0,0 ); add_edge( edges, 280,0,0, 293,25,0 );//A add_edge( edges, 293,25,0, 305,0,0 ); add_edge( edges, 287,13,0, 299,13,0 ); add_edge( edges, 310,0,0, 325,25,0 );//Y add_edge( edges, 318,13,0, 305,25,0 ); add_edge( edges, 330,0,0, 343,25,0 );//A add_edge( edges, 343,25,0, 355,0,0 ); add_edge( edges, 337,13,0, 349,13,0 ); add_edge( edges, 360,0,0, 360,25,0 );//N add_edge( edges, 360,25,0, 385,0,0 ); add_edge( edges, 385,0,0, 385,25,0 ); add_edge( edges, 390,0,0, 390,25,0 );//K add_edge( edges, 390,13,0, 408,25,0 ); add_edge( edges, 395,14,0, 408,0,0 ); draw_lines(edges,s,c); save_extension(s, "matrix.png" ); display(s); free_matrix( transform ); free_matrix( edges ); }
myCamera::myCamera(float posx, float posy, float posz, float atx, float aty, float atz, float upx, float upy, float upz, int t) { move_speed = 0; wave = 0; isSprinting = false; sprintTime = 100; type = t; fovy = 65.0; aspect = 1.0; zNear = .1; zFar = 200.0; top = 5; bottom = -5; left = -5; right = 5; pos[0] = posx; pos[1] = posy; pos[2] = posz; pos[3] = 1.0; look[0] = atx; look[1] = aty; look[2] = atz; look[3] = 1.0; up[0] = upx; up[1] = upy; up[2] = upz; GLfloat* r = normalize(look); w[0] = -r[0]; w[1] = -r[1]; w[2] = -r[2]; delete[] r; GLfloat up_w = dot_product(up, w); // mult_w = (up dot w) * w GLfloat *mult_w = scalar_mult(up_w, w); // up_sub_mult_w = up - (up dot w)*w; GLfloat* up_sub_mult_w = vector_subtract(up, mult_w); r = normalize(up_sub_mult_w); v[0] = r[0]; v[1] = r[1]; v[2] = r[2]; delete [] r; r = cross_product(v, w); u[0] = r[0]; u[1] = r[1]; u[2] = r[2]; generateT(); generateR(); generateS(); generateM(); delete [] mult_w; delete [] up_sub_mult_w; delete [] r; }
double angle(vect a, vect b) { return scalar_mult(a, b) / (norm(a) * norm(b)); }
static expr *expr5(int critical) { expr *e, *f; e = expr6(critical); if (!e) return NULL; while (i == '*' || i == '/' || i == '%' || i == TOKEN_SDIV || i == TOKEN_SMOD) { int j = i; i = scan(scpriv, tokval); f = expr6(critical); if (!f) return NULL; if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) || !(is_simple(f) || is_just_unknown(f)))) { nasm_error(ERR_NONFATAL, "division operator may only be applied to" " scalar values"); return NULL; } if (j != '*' && !is_unknown(f) && reloc_value(f) == 0) { nasm_error(ERR_NONFATAL, "division by zero"); return NULL; } switch (j) { case '*': if (is_simple(e)) e = scalar_mult(f, reloc_value(e), true); else if (is_simple(f)) e = scalar_mult(e, reloc_value(f), true); else if (is_just_unknown(e) && is_just_unknown(f)) e = unknown_expr(); else { nasm_error(ERR_NONFATAL, "unable to multiply two " "non-scalar objects"); return NULL; } break; case '/': if (is_just_unknown(e) || is_just_unknown(f)) e = unknown_expr(); else e = scalarvect(((uint64_t)reloc_value(e)) / ((uint64_t)reloc_value(f))); break; case '%': if (is_just_unknown(e) || is_just_unknown(f)) e = unknown_expr(); else e = scalarvect(((uint64_t)reloc_value(e)) % ((uint64_t)reloc_value(f))); break; case TOKEN_SDIV: if (is_just_unknown(e) || is_just_unknown(f)) e = unknown_expr(); else e = scalarvect(((int64_t)reloc_value(e)) / ((int64_t)reloc_value(f))); break; case TOKEN_SMOD: if (is_just_unknown(e) || is_just_unknown(f)) e = unknown_expr(); else e = scalarvect(((int64_t)reloc_value(e)) % ((int64_t)reloc_value(f))); break; } } return e; }
int main() { //Basic Matrix Math struct matrix *a; struct matrix *b; a=new_matrix(4,4); b=new_matrix(4,2); printf("Identity matrix:\n"); ident(a); print_matrix(a); b->m[0][0]=1; b->m[0][1]=2; b->m[1][0]=3; b->m[1][1]=4; b->m[2][0]=5; b->m[2][1]=6; b->m[3][0]=7; b->m[3][1]=8; printf("Matrix #2:\n"); print_matrix(b); printf("Scalar Multiplication by 2:\n"); scalar_mult(2, b); print_matrix(b); printf("New Matrix #1:\n"); a->m[2][1]=3; a->m[0][3]=2; print_matrix(a); printf("Matrix Multiplication:\n"); matrix_mult(a, b); print_matrix(b); printf("Adding points/edges:\n"); struct matrix *d; d = new_matrix(3, 3); add_point(d, 200,400,70); add_point(d, 200,0,7); print_matrix(d); printf("\n"); add_edge(d, 300,500,100,300,100,134); add_edge(d, 100,500,100,100,100,134); add_edge(d, 400,00,100,400,400,134); print_matrix(d); printf("\n"); screen s; color c; c.red = 200; c.green = 100; c.blue = 250; int i, j; for( i=0; i<XRES; i++) for ( j=0; j<YRES; j++) { plot( s, c, i, j); } c.red=0; c.green=200; c.blue=200; draw_lines(d, s, c); display( s ); save_ppm(s, "image" ); save_extension(s, "image.jpg"); }
expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv, int *fwref, int critical, struct eval_hints *hints) { expr *e; expr *f = NULL; hint = hints; if (hint) hint->type = EAH_NOHINT; if (critical & CRITICAL) { critical &= ~CRITICAL; bexpr = rexp0; } else bexpr = expr0; scan = sc; scpriv = scprivate; tokval = tv; opflags = fwref; if (tokval->t_type == TOKEN_INVALID) i = scan(scpriv, tokval); else i = tokval->t_type; while (ntempexprs) /* initialize temporary storage */ nasm_free(tempexprs[--ntempexprs]); e = bexpr(critical); if (!e) return NULL; if (i == TOKEN_WRT) { i = scan(scpriv, tokval); /* eat the WRT */ f = expr6(critical); if (!f) return NULL; } e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */ if (f) { expr *g; if (is_just_unknown(f)) g = unknown_expr(); else { int64_t value; begintemp(); if (!is_reloc(f)) { nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); return NULL; } value = reloc_seg(f); if (value == NO_SEG) value = reloc_value(f) | SEG_ABS; else if (!(value & SEG_ABS) && !(value % 2) && critical) { nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT"); return NULL; } addtotemp(EXPR_WRT, value); g = finishtemp(); } e = add_vectors(e, g); } return e; }
int main() { int row, col; int i, j; struct matrix * m; struct matrix * point; struct matrix * n; screen s; color c; m = new_matrix(5,5); for(row = 0; row<m->rows; row++){ for(col = 0; col<m->cols; col++){ m->m[row][col] = row; } } n = new_matrix(5,5); for(row = 0; row<n->rows; row++){ for(col = 0; col<n->cols; col++){ n->m[row][col] = row; } } printf("Print original second matrix\n"); print_matrix(n); matrix_mult(m,n); printf("multiplied matrix\n"); print_matrix(n); printf("orignal first matrix\n"); print_matrix(m); scalar_mult(5.0,m); printf("first matrix after scalar multiplication\n"); print_matrix(m); ident(m); printf("first matrix after iden function\n"); print_matrix(m); point = new_matrix(3,5); printf("original point matrix\n"); print_matrix(point); printf("print point matrix after add point is call\n"); print_matrix(point); //add_edge(point,0,0,0,350,500,0); printf("print point matrix after add edge is call\n"); print_matrix(point); //large star add_point(point,0,0,0); add_point(point,250,500,0); add_edge(point,250,500,0,500,0,0); add_edge(point,0,350,0,500,0,0); add_edge(point,0,350,0,500,350,0); add_edge(point,0,0,0,500,350,0); //medium star add_edge(point,175,350,0,250,175,0); add_edge(point,250,175,0,325,350,0); add_edge(point,325,350,0,130,260,0); add_edge(point,130,260,0,370,260,0); add_edge(point,370,260,0,175,350,0); //small star add_edge(point,250,315,0,212,260,0); add_edge(point,250,315,0,288,260,0); add_edge(point,288,260,0,200,293,0); add_edge(point,200,293,0,300,293,0); add_edge(point,300,293,0,212,260,0); c.red = 122; c.green = 218; c.blue = 225; clear_screen(s); for( i=0; i<XRES; i++){ for ( j=0; j<YRES; j++){ //c.red = random() % (MAX_COLOR + 1); //c.green = random() % (MAX_COLOR + 1); //c.blue = random() % (MAX_COLOR + 1); plot( s, c, i, j); } } c.green = 40; draw_lines(point,s,c); display( s ); save_ppm(s, "image" ); save_extension(s, "image.png"); free(m); free(n); free(point); }
void runge_kutta(const data* dat, output_function output) { int i; double dt = dat->eta * delta_t_factor; vector *r = init_r(dat), *v = init_v(dat), *a = (vector*) malloc((dat->N)*sizeof(vector)), *a_1 = (vector*) malloc((dat->N)*sizeof(vector)); // temporary r vector for acceleration calculations vector *rt = (vector*) malloc((dat->N)*sizeof(vector)); vector *a1 = (vector*) malloc((dat->N)*sizeof(vector)), *a2 = (vector*) malloc((dat->N)*sizeof(vector)), *a3 = (vector*) malloc((dat->N)*sizeof(vector)), *a4 = (vector*) malloc((dat->N)*sizeof(vector)); vector *r1 = (vector*) malloc((dat->N)*sizeof(vector)), *r2 = (vector*) malloc((dat->N)*sizeof(vector)), *r3 = (vector*) malloc((dat->N)*sizeof(vector)), *r4 = (vector*) malloc((dat->N)*sizeof(vector)); vector *v1 = (vector*) malloc((dat->N)*sizeof(vector)), *v2 = (vector*) malloc((dat->N)*sizeof(vector)), *v3 = (vector*) malloc((dat->N)*sizeof(vector)), *v4 = (vector*) malloc((dat->N)*sizeof(vector)); double time = 0; while (time < dat->t_max) { accelerations(dat, r, a1); for (i = 0; i < dat->N; i++) { v1[i] = scalar_mult(dt, a1[i]); r1[i] = scalar_mult(dt, v[i]); // temp r for a2 rt[i] = vector_add(r[i], scalar_mult(0.5, r1[i])); } accelerations(dat, rt, a2); for (i = 0; i < dat->N; i++) { v2[i] = scalar_mult(dt, a2[i]); r2[i] = scalar_mult(dt, vector_add(v[i], scalar_mult(0.5, v1[i]))); // temp r for a3 rt[i] = vector_add(r[i], scalar_mult(0.5, r2[i])); } accelerations(dat, rt, a3); for (i = 0; i < dat->N; i++) { v3[i] = scalar_mult(dt, a3[i]); r3[i] = scalar_mult(dt, vector_add(v[i], scalar_mult(0.5, v2[i]))); // temp r for a3 rt[i] = vector_add(r[i], scalar_mult(0.5, r3[i])); } accelerations(dat, rt, a4); for (i = 0; i < dat->N; i++) { v4[i] = scalar_mult(dt, a4[i]); r4[i] = scalar_mult(dt, vector_add(v[i], v3[i])); // calculate v_(n+1) and r_(n+1) vector_add_to(&v[i], vector_add(scalar_mult(1.0/6.0, v1[i]), vector_add(scalar_mult(1.0/3.0, v2[i]), vector_add(scalar_mult(1.0/3.0, v3[i]), scalar_mult(1.0/6.0, v4[i]))))); vector_add_to(&r[i], vector_add(scalar_mult(1.0/6.0, r1[i]), vector_add(scalar_mult(1.0/3.0, r2[i]), vector_add(scalar_mult(1.0/3.0, r3[i]), scalar_mult(1.0/6.0, r4[i]))))); } // increase time adots(dat, r, v, a_1); dt = delta_t(dat, a1, a_1); // a1 = a(t_n), a_1 = a'(t_n) time += dt; output(time, dt, dat, r, v); } free(r); free(v); free(a); free(a_1); free(rt); free(r1); free(r2); free(r3); free(r4); free(v1); free(v2); free(v3); free(v4); free(a1); free(a2); free(a3); free(a4); }
void hermite_iterated(const data* dat, output_function output, int iterations) { int i,j; double dt = dat->eta * delta_t_factor; vector *a = (vector*) malloc((dat->N)*sizeof(vector)), // a_n, not initialized *a_p = (vector*) malloc((dat->N)*sizeof(vector)), // a_(n+1)^p, not initialized *a_1 = (vector*) malloc((dat->N)*sizeof(vector)), // a_1_n, not initialized *a_1_p = (vector*) malloc((dat->N)*sizeof(vector)), // a_1_(n+1)^p, not initialized *r = init_r(dat), // r_n *r_p = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n+1)^p, not initialized *v = init_v(dat), // v_n *v_p = (vector*) malloc((dat->N)*sizeof(vector)); // v_(n+1)^p, not initialized double time = 0; accelerations(dat, r, a); adots(dat, r, v, a_1); dt = delta_t(dat, a, a_1); output(time, dt, dat, r, v); while (time < dat->t_max) { // prediction step for (i = 0; i < dat->N; i++) { v_p[i] = vector_add(v[i], vector_add(scalar_mult(dt, a[i]), scalar_mult(0.5 * dt * dt, a_1[i]))); r_p[i] = vector_add(r[i], vector_add(scalar_mult(dt, v[i]), vector_add(scalar_mult(0.5 * dt * dt, a[i]), scalar_mult(dt * dt * dt / 6.0, a_1[i])))); } // iteration of correction step for (j = 0; j < iterations; j++) { // predict a, a_1 accelerations(dat, r_p, a_p); adots(dat, r_p, v_p, a_1_p); for (i = 0; i < dat->N; i++) { // correction steps -> overwrite "prediction" variables for convenience, // will get written in r,v after iteration is done v_p[i] = vector_add(v[i], vector_add(scalar_mult(dt / 2.0, vector_add(a_p[i], a[i])), scalar_mult(dt * dt / 12.0, vector_diff(a_1_p[i], a_1[i])))); r_p[i] = vector_add(r[i], vector_add(scalar_mult(dt / 2.0, vector_add(v_p[i], v[i])), scalar_mult(dt * dt / 12.0, vector_diff(a_p[i], a[i])))); } } // apply iterated correction steps for (i = 0; i < dat->N; i++) { r[i] = r_p[i]; v[i] = v_p[i]; } time += dt; // a/a_1 values for next step accelerations(dat, r, a); adots(dat, r, v, a_1); // new dt dt = delta_t(dat, a, a_1); output(time, dt, dat, r, v); } free(a); free(a_p); free(a_1); free(a_1_p); free(r); free(r_p); free(v); free(v_p); }
void hermite(const data* dat, output_function output) { int i; double dt = dat->eta * delta_t_factor; vector *a = (vector*) malloc((dat->N)*sizeof(vector)), // a_n, not initialized *a_p = (vector*) malloc((dat->N)*sizeof(vector)), // a_(n+1)^p, not initialized *a_1 = (vector*) malloc((dat->N)*sizeof(vector)), // a_1_n, not initialized *a_1_p = (vector*) malloc((dat->N)*sizeof(vector)), // a_1_(n+1)^p, not initialized *a_2 = (vector*) malloc((dat->N)*sizeof(vector)), // a_(n+1)^(2), not initialized *a_3 = (vector*) malloc((dat->N)*sizeof(vector)), // a_(n+1)^(3), not initialized *r = init_r(dat), // r_n *r_p = (vector*) malloc((dat->N)*sizeof(vector)), // r_(n+1)^p, not initialized *v = init_v(dat), // v_n *v_p = (vector*) malloc((dat->N)*sizeof(vector)); // v_(n+1)^p, not initialized double time = 0; accelerations(dat, r, a); adots(dat, r, v, a_1); dt = delta_t(dat, a, a_1); output(time, dt, dat, r, v); while (time < dat->t_max) { // prediction step for (i = 0; i < dat->N; i++) { v_p[i] = vector_add(v[i], vector_add(scalar_mult(dt, a[i]), scalar_mult(0.5 * dt * dt, a_1[i]))); r_p[i] = vector_add(r[i], vector_add(scalar_mult(dt, v[i]), vector_add(scalar_mult(0.5 * dt * dt, a[i]), scalar_mult(dt * dt * dt / 6.0, a_1[i])))); } // predict a^p, a_1^p accelerations(dat, r_p, a_p); adots(dat, r_p, v_p, a_1_p); for (i = 0; i < dat->N; i++) { // predict 2nd and 3rd derivations a_2[i] = vector_add(scalar_mult(2 * (-3) / dt / dt, vector_diff(a[i], a_p[i])), scalar_mult(2 * (-1) / dt, vector_add(scalar_mult(2, a_1[i]), a_1_p[i]))); a_3[i] = vector_add(scalar_mult(6 * 2 / dt / dt / dt, vector_diff(a[i], a_p[i])), scalar_mult(6 / dt / dt, vector_add(a_1[i], a_1_p[i]))); // correction steps v[i] = vector_add(v_p[i], vector_add(scalar_mult(dt * dt * dt / 6.0, a_2[i]), scalar_mult(dt * dt * dt / 24.0, a_3[i]))); r[i] = vector_add(r_p[i], vector_add(scalar_mult(dt * dt * dt * dt / 24.0, a_2[i]), scalar_mult(dt * dt * dt * dt * dt / 120.0, a_3[i]))); } time += dt; // a/a_1 values for next step accelerations(dat, r, a); adots(dat, r, v, a_1); // new dt dt = delta_t_(dat, a, a_1, a_2, a_3); output(time, dt, dat, r, v); } free(a); free(a_p); free(a_1); free(a_1_p); free(r); free(r_p); free(v); free(v_p); }
void qp(double *x_out, int *iter, double *gt, double *bt) { /* define data */ double q[300]= {0}; double q1[180]= {0}; double q2[120]= {0}; double l[180] = {0}; double u[180] = {0}; double tmp_var_p[180] = {0}; double tmp_var_p2[180] = {0}; double arg_prox_h[180] = {0}; double lambda[180] = {0}; double y[180] = {0}; double x[180] = {0}; double lambda_old[180] = {0}; double v[180] = {0}; double v_old[180] = {0}; double tmp_var_n[180] = {0}; double tmp_var_n2[180] = {0}; double tmp_var_nm[300] = {0}; double tmp_var_nm2[300] = {0}; double rhs[300] = {0}; int jj = 0; double cond = -1; double theta = 1; double theta_old = 1; mat_vec_mult_sparse(&G,gt,q1); mat_vec_mult_sparse(&B,bt,q2); copy_vec_part((double *) &Lb,l,180); copy_vec_part((double *) &Ub,u,180); while ((jj < 2000) && (cond < 0)) { jj++; copy_vec_part_negate(v,tmp_var_p,180); mat_vec_mult_diag(&CT,tmp_var_p,tmp_var_n); vec_sub(tmp_var_n,q1,tmp_var_n,180); stack_vec(tmp_var_n,q2,rhs,180,120); perm_fwdsolve(&L,p,rhs,tmp_var_nm); mat_vec_mult_sparse(&Dinv,tmp_var_nm,tmp_var_nm2); backsolve_perm(<,p,tmp_var_nm2,tmp_var_nm); copy_vec_part(tmp_var_nm,x,180); mat_vec_mult_diag(&C,x,tmp_var_p); vec_add(v,tmp_var_p,tmp_var_p,180); copy_vec_part(tmp_var_p,arg_prox_h,180); clip(tmp_var_p,l,u,180); mat_vec_mult_diag(&Einv,tmp_var_p,y); copy_vec_part(lambda,lambda_old,180); vec_sub(arg_prox_h,tmp_var_p,lambda,180); vec_sub(lambda,lambda_old,tmp_var_p,180); theta_old = theta; theta = (1+sqrt(1+4*pow(theta_old,2)))/2; scalar_mult((theta_old-1)/theta,tmp_var_p,180); copy_vec_part(v,v_old,180); vec_add(tmp_var_p,lambda,v,180); if (mod(jj,10) == 0) { cond = check_stop_cond_FGM(&Einv,lambda,lambda_old,tmp_var_p,tmp_var_p2,180,0.001); } restart(lambda,lambda_old,v,v_old,tmp_var_p,tmp_var_p2,180); } copy_vec_part(x,x_out,180); *iter = jj; }
int main() { screen s; color c; // TEST: add_point struct matrix *a; a = (struct matrix *)new_matrix(3,3); add_point(a,1,0,2); // TEST: print_matrix printf("\nAdding one point to matrix A:\n"); print_matrix(a); // TEST: add_edge add_edge(a,4,5,3,2,2,2); printf("Adding an edge:\n"); print_matrix(a); // TEST: scalar multiplication scalar_mult(5,a); printf("Scalar multiplication by 5:\n"); print_matrix(a); scalar_mult(0.2,a); printf("Scalar multiplication by 0.2, returning to original matrix:\n"); print_matrix(a); // TEST: matrix matrix multiplication struct matrix *b; b = (struct matrix *)new_matrix(3,1); add_point(b,1,2,3); printf("Matrix B:\n"); print_matrix(b); matrix_mult(a,b); printf("Matrix multiplication of A with B:\n"); print_matrix(a); // TEST: turning a matrix into the identity matrix struct matrix *d; d = (struct matrix *)new_matrix(3,3); add_edge(d,3,4,5,8.2,9.1,2); add_point(d,5,2,1); printf("Matrix C:\n"); print_matrix(d); ident(d); printf("Matrix C after being made into the 3 x 3 identity matrix:\n"); print_matrix(d); /* struct matrix *m; m = (struct matrix *)new_matrix(3,3); m->m[0][0] = 1; m->m[0][1] = 2; m->m[0][2] = 5; m->m[1][0] = 3; m->m[1][1] = 4; m->m[1][2] = 6; m->m[2][0] = 7; m->m[2][1] = 8; m->m[2][2] = 9; */ /* struct matrix *n; n = (struct matrix *)new_matrix(3,4); n->m[0][0] = 1; n->m[1][0] = 2; n->m[2][0] = 1; n->m[0][1] = 1; n->m[1][1] = 0; n->m[2][1] = 1; n->m[0][2] = 1; n->m[1][2] = 2; n->m[2][2] = 1; n->m[0][3] = 1; n->m[1][3] = 1; n->m[2][3] = 0; */ /* struct matrix *p; p = (struct matrix *)new_matrix(3,5); p->m[0][0] = 1; //print_matrix(p); //print_matrix(matrix_mult(m,n)); */ /* struct matrix *q; q = (struct matrix *)new_matrix(3,2); add_point(q,0,0,0); add_point(q,100,0,0); struct matrix *t; t = (struct matrix *)new_matrix(3,3); t->m[0][0] = cos(M_PI / 6); t->m[1][0] = sin(M_PI / 6); t->m[0][1] = -1 * sin(M_PI / 6); t->m[1][1] = cos(M_PI / 6); t->lastcol = 2; */ /*add_edge(t,cos( M_PI / 6),sin( M_PI / 6), 0, -1 * sin(M_PI / 6), cos(M_PI / 6), 0); */ /* struct matrix *u; u = (struct matrix *)new_matrix(3,2); add_edge(u,0,0,0,100,0,0); int i; for(i = 0; i < 11; i++){ matrix_mult(t,q); add_edge(u,q->m[0][0],q->m[1][0],q->m[2][0], q->m[0][1],q->m[1][1],q->m[2][1]);} int j,k; float f; for(j = 0; j < u->rows; j++){ for(k = 0; k < u->cols; k++){ f = u->m[j][k]; u->m[j][k] = (int)f + 250;} } print_matrix(t); printf("%f\n", cos(M_PI / 6)); */ /* struct matrix *r; r = (struct matrix *)new_matrix(3,2); r->m[0][0] = 0; r->m[0][1] = 0; r->m[1][0] = 50; r->m[1][1] = 50; print_matrix(m); matrix_mult(m,q); print_matrix(m); */ /* int i,j; c.red = 0; c.green = 0; c.blue = 255; for(i = 0; i < XRES; i++){ for(j = 0; j < YRES; j++){ plot(s,c,i,j); } } c.red = 0; c.green = 0; c.blue = 0; draw_lines(q,s,c); */ /* int i, j; for( i=0; i<XRES; i++) for ( j=0; j<YRES; j++) { c.red = random() % (MAX_COLOR + 1); c.green = random() % (MAX_COLOR + 1); c.blue = random() % (MAX_COLOR + 1); plot( s, c, i, j); } */ c.red = 170; c.green = 240; c.blue = 30; int i,j; for(i = 0; i < XRES; i++){ for(j = 0; j < YRES; j++){ plot(s,c,i,j); } } // TEST: draw lines in edge matrix c.red = 0; c.green = 0; c.blue = 0; struct matrix *e; e = (struct matrix *)new_matrix(3,2); //add_point(e,0,0,0); //add_point(e,250,250,0); //draw_lines(e,s,c); for(i = 0; i < 25; i++){ add_edge(e,250+5*i,250+5*i,0,500-5*i,250+5*i,0); add_edge(e,500-5*i,250+5*i,0,500-5*i,500-5*i,0); //add_edge(e,500-5*i,500-5*i,0,250+5*i,250+5*i,0); } for(i = 0; i < 25; i++){ add_edge(e,5*i,5*i,0,250-5*i,5*i,0); add_edge(e,250-5*i,5*i,0,250-5*i,250-5*i,0); //add_edge(e,250-5*i,250-5*i,0,5*i,5*i,0); } for(i = 0; i < 25; i++){ add_edge(e,125+5*i,125+5*i,0,125+5*i,375-5*i,0); add_edge(e,125+5*i,375-5*i,0,375-5*i,375-5*i,0); //add_edge(e,375-5*i,375-5*i,0,125+5*i,125+5*i,0); } add_edge(e,0,0,0,125,125,0); add_edge(e,375,375,0,500,500,0); draw_lines(e,s,c); display( s ); save_ppm(s, "image" ); save_extension(s, "image.jpg"); }
bool two_points_to_one_side(point fst, point snd, point start, point end) { vect side = normalize(end - start); return greaterOrEqual(scalar_mult((side * normalize(fst - start)), (side * normalize(snd - start))), 0); }
point plane_ray_find_intersection(vect _normal, double d, vect a, point x_0) { long double n_x_0 = scalar_mult(_normal, x_0); long double n_a = scalar_mult(_normal, a); double t = (d - n_x_0) / n_a; return x_0 + a * t; }
static expr *expr6(int critical) { int32_t type; expr *e; int32_t label_seg; int64_t label_ofs; int64_t tmpval; bool rn_warn; char *scope; switch (i) { case '-': i = scan(scpriv, tokval); e = expr6(critical); if (!e) return NULL; return scalar_mult(e, -1L, false); case '+': i = scan(scpriv, tokval); return expr6(critical); case '~': i = scan(scpriv, tokval); e = expr6(critical); if (!e) return NULL; if (is_just_unknown(e)) return unknown_expr(); else if (!is_simple(e)) { nasm_error(ERR_NONFATAL, "`~' operator may only be applied to" " scalar values"); return NULL; } return scalarvect(~reloc_value(e)); case '!': i = scan(scpriv, tokval); e = expr6(critical); if (!e) return NULL; if (is_just_unknown(e)) return unknown_expr(); else if (!is_simple(e)) { nasm_error(ERR_NONFATAL, "`!' operator may only be applied to" " scalar values"); return NULL; } return scalarvect(!reloc_value(e)); case TOKEN_IFUNC: { enum ifunc func = tokval->t_integer; i = scan(scpriv, tokval); e = expr6(critical); if (!e) return NULL; if (is_just_unknown(e)) return unknown_expr(); else if (!is_simple(e)) { nasm_error(ERR_NONFATAL, "function may only be applied to" " scalar values"); return NULL; } return scalarvect(eval_ifunc(reloc_value(e), func)); } case TOKEN_SEG: i = scan(scpriv, tokval); e = expr6(critical); if (!e) return NULL; e = segment_part(e); if (!e) return NULL; if (is_unknown(e) && critical) { nasm_error(ERR_NONFATAL, "unable to determine segment base"); return NULL; } return e; case TOKEN_FLOATIZE: return eval_floatize(tokval->t_integer); case TOKEN_STRFUNC: return eval_strfunc(tokval->t_integer); case '(': i = scan(scpriv, tokval); e = bexpr(critical); if (!e) return NULL; if (i != ')') { nasm_error(ERR_NONFATAL, "expecting `)'"); return NULL; } i = scan(scpriv, tokval); return e; case TOKEN_NUM: case TOKEN_STR: case TOKEN_REG: case TOKEN_ID: case TOKEN_INSN: /* Opcodes that occur here are really labels */ case TOKEN_HERE: case TOKEN_BASE: case TOKEN_DECORATOR: begintemp(); switch (i) { case TOKEN_NUM: addtotemp(EXPR_SIMPLE, tokval->t_integer); break; case TOKEN_STR: tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn); if (rn_warn) nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long"); addtotemp(EXPR_SIMPLE, tmpval); break; case TOKEN_REG: addtotemp(tokval->t_integer, 1L); if (hint && hint->type == EAH_NOHINT) hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE; break; case TOKEN_ID: case TOKEN_INSN: case TOKEN_HERE: case TOKEN_BASE: /* * If !location.known, this indicates that no * symbol, Here or Base references are valid because we * are in preprocess-only mode. */ if (!location.known) { nasm_error(ERR_NONFATAL, "%s not supported in preprocess-only mode", (i == TOKEN_HERE ? "`$'" : i == TOKEN_BASE ? "`$$'" : "symbol references")); addtotemp(EXPR_UNKNOWN, 1L); break; } type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */ if (i == TOKEN_BASE) { label_seg = in_abs_seg ? abs_seg : location.segment; label_ofs = 0; } else if (i == TOKEN_HERE) { label_seg = in_abs_seg ? abs_seg : location.segment; label_ofs = in_abs_seg ? abs_offset : location.offset; } else { if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) { scope = local_scope(tokval->t_charptr); if (critical == 2) { nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined", scope,tokval->t_charptr); return NULL; } else if (critical == 1) { nasm_error(ERR_NONFATAL, "symbol `%s%s' not defined before use", scope,tokval->t_charptr); return NULL; } else { if (opflags) *opflags |= OPFLAG_FORWARD; type = EXPR_UNKNOWN; label_seg = NO_SEG; label_ofs = 1; } } if (opflags && is_extern(tokval->t_charptr)) *opflags |= OPFLAG_EXTERN; } addtotemp(type, label_ofs); if (label_seg != NO_SEG) addtotemp(EXPR_SEGBASE + label_seg, 1L); break; case TOKEN_DECORATOR: addtotemp(EXPR_RDSAE, tokval->t_integer); break; } i = scan(scpriv, tokval); return finishtemp(); default: nasm_error(ERR_NONFATAL, "expression syntax error"); return NULL; } }