int main() { int operation; unsigned timestamp; unsigned prev_checksum; unsigned new_checksum; matrix matrix1; matrix matrix2; matrix matrix3; char line[1024]; while(!feof(stdin)) { fgets(line, sizeof(line), stdin); } if(sscanf(line, "timestamp: 0x%x, operation: %d, checksum: 0x%x", ×tamp, &operation, &prev_checksum) == 0) { fprintf(stderr, "[oracle] Invalid input\n"); exit(1); } srand(timestamp); printf("[Starting oracle]\n"); printf("timestamp: 0x%04x, ", timestamp); printf("operation: %d, ", operation); printf("checksum: 0x%04x\n", prev_checksum); /* init matrices */ printf("initializing matrices\n"); matrix_init(&matrix1); matrix_init(&matrix2); switch(operation) { case 0: /* add matrices */ printf("adding VM matrices ");fflush(stdout); matrix_add(&matrix3, &matrix1, &matrix2); break; case 1: /* multiply matrices */ printf("multiplying matrices ");fflush(stdout); matrix_mult(&matrix3, &matrix1, &matrix2); break; default: break; } new_checksum = matrix_checksum(&matrix3); printf("new checksum: 0x%04x [%s]\n", new_checksum, new_checksum == prev_checksum ? "ok" : "ko"); return 0; }
/** * ECEF vector to ENU vector * <http://www.navipedia.net/index.php/Transformations_between_ECEF_and_ENU_coordinates> * * @param v A vector in ECEF coordinates * @param lon A vector in ECEF coordinates * @param lat A vector in ECEF coordinates */ vec ECEF2ENU(vec v, double lon, double lat) { mat3 T; double slon = sin(lon); double clon = cos(lon); double slat = sin(lat); double clat = cos(lat); vec enu; T.m.x1 = -slon; T.m.y1 = clon; T.m.z1 = 0; T.m.x2 = -slat*clon; T.m.y2 = -slat*slon; T.m.z2 = clat; T.m.x3 = clat*clon; T.m.y3 = clat*slon; T.m.z3 = slat; enu = matrix_mult(T, v); return enu; }
/*======== struct matrix * generate_curve_coefs() ========== Inputs: double p1 double p2 double p3 double p4 int type Returns: A matrix containing the values for a, b, c and d of the equation at^3 + bt^2 + ct + d for the curve defined by p1, p2, p3 and p4. Type determines whether the curve is bezier or hermite ====================*/ struct matrix * generate_curve_coefs( double p1, double p2, double p3, double p4, int type) { struct matrix * g = new_matrix(4, 1); g->m[0][0] = p1; g->m[1][0] = p2; g->m[2][0] = p3; g->m[3][0] = p4; struct matrix *c; if (type == HERMITE_MODE) { c = make_hermite(); } if (type == BEZIER_MODE) { c = make_bezier(); } matrix_mult(c, g); return g; }
void renderer_validate_for_mask_rendering(struct renderer *renderer, struct pipe_surface *dst, const struct matrix *modelview) { struct matrix mvp = renderer->projection; /* will be used in POLYGON_STENCIL and POLYGON_FILL */ matrix_mult(&mvp, modelview); renderer_set_mvp(renderer, &mvp); renderer_set_target(renderer, dst, renderer->g3d.fb.zsbuf, VG_FALSE); renderer_set_blend(renderer, ~0); renderer_set_fs(renderer, RENDERER_FS_WHITE); /* set internal dirty flags (hacky!) */ renderer->dirty = FRAMEBUFFER_DIRTY | BLEND_DIRTY; }
/** * Prepare the renderer for OpenVG pipeline. */ void renderer_validate_for_shader(struct renderer *renderer, const struct pipe_sampler_state **samplers, struct pipe_sampler_view **views, VGint num_samplers, const struct matrix *modelview, void *fs, const void *const_buffer, VGint const_buffer_len) { struct matrix mvp = renderer->projection; /* will be used in POLYGON_STENCIL and POLYGON_FILL */ matrix_mult(&mvp, modelview); renderer_set_mvp(renderer, &mvp); renderer_set_custom_fs(renderer, fs, samplers, views, num_samplers, const_buffer, const_buffer_len); }
/** * @test This tests a 90 degree CW turn around the Z axis */ char * coord_transform_test2() { vec v = {0,1,0}; vec axis = {0,0,-Pi/2.0}; // 90 deg CW about Z axis vec a; mat3 M; M = axis_angle_to_rotation_matrix(axis); a = matrix_mult(M, v); char * err = "\n (-) Error: coord_transform_test2()\n (+) Failed rotation\n"; mu_assert(err, ( (a.component[0] == 1 ) && (a.component[1] < 1e-16 ) && (a.component[1] < 1e-16 ))); return 0; }
void serial_lu(double **matrix, int dim) { int procs; int rank; MPI_Comm_size(MPI_COMM_WORLD, &procs); MPI_Comm_rank(MPI_COMM_WORLD, &rank); double **L = create_zero_matrix(dim); if(rank==0) { printf("A=\n"); print_matrix(dim,matrix); } int i,j,k; // initialize diagonal of L to 1 for(i=0;i<dim;i++) { L[i][i] = 1; } // columns for(j=0;j<dim;j++) { // rows for(i=j+1;i<dim;i++) { double ratio = matrix[i][j]/matrix[j][j]; L[i][j] = ratio; for(k=0;k<dim;k++) { matrix[i][k] -= ratio*matrix[j][k]; } } } //Print serial results for proc 0 only (computation done on all) if(rank == 0) { printf("L=\n"); print_matrix(dim,L); printf("U=\n"); print_matrix(dim,matrix); printf("L*U=\n"); print_matrix(dim,matrix_mult(L,matrix,dim)); } free_matrix(dim,L); free_matrix(dim,matrix); }
int main() { /* Перемножение матриц 1024*1024 Переход к обычному уумножению при разных значениях k: k = 16 : 3.532507 k = 32 : 3.174222 k = 64 : 3.139104 k = 128 : 3.255240 k = 256 : 3.902180 k = 512 : 5.226510 */ int** a, **b, **c, **d; int n = 1024, k = 16; a = matrix_new(n); b = matrix_new(n); matrix_fill(a, n); matrix_fill(b, n); printf("Starting:\n"); clock_t t = clock(); c = matrix_mult(a, b, n); t = clock()-t; printf("Обычное умножение - %f\n", ((double)t/CLOCKS_PER_SEC)); printf("Начало перемножения методом Штрассена \n"); t = clock(); d = strassen(a, b, n, k); t = clock()-t; printf("Умножение методом Штрассена - %f\n", ((double)t/CLOCKS_PER_SEC)); return 0; }
struct matrix * generate_curve_coefs( double p1, double p2, double p3, double p4, int type) { struct matrix *m; struct matrix *a = new_matrix(4,1); a->lastcol = 1; if(type==0){//hermite m = make_hermite(); a->m[0][0] = p1;//p0 a->m[1][0] = p3;//p1 a->m[2][0] = p2;//r0 a->m[3][0] = p4;//r1 }else{//bezier m = make_bezier(); a->m[0][0] = p1; a->m[1][0] = p2; a->m[2][0] = p3; a->m[3][0] = p4; } matrix_mult(m,a); return a; }
/** * A transformation from window coordinates to paint coordinates. */ VGboolean vg_get_paint_matrix(struct vg_context *ctx, const struct matrix *paint_to_user, const struct matrix *user_to_surface, struct matrix *mat) { struct matrix tmp; /* get user-to-paint matrix */ memcpy(mat, paint_to_user, sizeof(*paint_to_user)); if (!matrix_invert(mat)) return VG_FALSE; /* get surface-to-user matrix */ memcpy(&tmp, user_to_surface, sizeof(*user_to_surface)); if (!matrix_invert(&tmp)) return VG_FALSE; matrix_mult(mat, &tmp); return VG_TRUE; }
/** * @test This tests the creation of a 90 degree rotation matrix, but does not * do the actual rotation. */ char * coord_transform_test1() { vec v = {0,1,0}; vec a; mat3 M; // 90deg CW M.x1 = 0; M.y1 = 1; M.z1 = 0; M.x2 = -1; M.y2 = 0; M.z2 = 0; M.x3 = 0; M.y3 = 0; M.z3 = 1; a = matrix_mult(M, v); char * err = "\n (-) Error: coord_transform_test1()\n (+) Failed rotation matrix creation\n"; mu_assert(err, ( (a.component[0] == 1 ) && (a.component[1] == 0 ) && (a.component[2] == 0 ))); return 0; }
//T2 inversion based on whittall mod t2 fit code void T2inversion(const double * T2, const double * R, const double * I, const double * time, const double alpha, double * T2dist, double * syn_data, const int size, const double stdev, const int nT2) { const int k=nT2+1; int aa=((size+(k-3))*k); int bb=size*k; double* L=new double[aa]; double* L0=new double[bb]; for(unsigned int i=0; i<size;i++) { for(unsigned int j=0; j<nT2;j++) { //using row-wise mapping index (i,j) to nlines x k matrix L[j+i*k]=exp(-time[i]/T2[j])/stdev; L0[j+i*k]=exp(-time[i]/T2[j]); } L[k+i*k]=1/stdev; L0[k+i*k]=1; } for(unsigned int ii=size; ii<(size+(k-3));ii++) { for(unsigned int jj=0; jj<nT2;jj++) { if((ii-size)==jj) {L[jj+ii*k]=1*alpha;} else if ((ii-size)==jj-1){L[jj+ii*k]=-2*alpha;} else if ((ii-size)==jj-2){L[jj+ii*k]=1*alpha;} else {L[jj+ii*k]=0;} } } // m=NNLS(L,R); perform non-negative least-squares on L and R // std::cout<<(size+k-3)*k<<std::endl; NNLS(L,R,size,nT2,T2dist); //syn_data=L0*m matrix_mult(syn_data,L0,T2dist,size,k,k,1); //end of function }
/*======== struct matrix * generate_curve_coefs() ========== Inputs: double p1 double p2 double p3 double p4 int type Returns: A matrix containing the values for a, b, c and d of the equation at^3 + bt^2 + ct + d for the curve defined by p1, p2, p3 and p4. Type determines whether the curve is bezier or hermite 03/16/12 14:42:46 jdyrlandweaver ====================*/ struct matrix * generate_curve_coefs( double p1, double p2, double p3, double p4, int type) { struct matrix * k = new_matrix(4,4); struct matrix * m = new_matrix(4, 1); if(type == 0){ //hermite m->m[0][0] = p1; m->m[1][0] = p2; m->m[2][0] = p3 - p1; m->m[3][0] = p4 - p2; k = make_hermite(); } else if(type == 1){ //bezier m->m[0][0] = p1; m->m[1][0] = p3; m->m[2][0] = p2; m->m[3][0] = p4; k = make_bezier(); } matrix_mult(k, m); free_matrix(k); return m; }
int main() { float ans[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { x[i][j] = j + 1; y[i][j] = j + 1; ret[i][j] = 0; ans[i][j] = 0; } } struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); matrix_mult(x, y, ans); clock_gettime(CLOCK_MONOTONIC, &end); double ref_time = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec) / BIL; clock_gettime(CLOCK_MONOTONIC, &start); for (int i = 0; i < NUMTHREADS; i++) { threads[i].i = i; pthread_create(&threads[i].thread, NULL, matrix_mult_worker, &threads[i]); } for (int i = 0; i < NUMTHREADS; i++) { pthread_join(threads[i].thread, NULL); } clock_gettime(CLOCK_MONOTONIC, &end); double threaded = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec) / BIL; float transpose[SIZE][SIZE]; matrix_transpose(y, transpose); clock_gettime(CLOCK_MONOTONIC, &start); matrix_mult_simd(x, y, ans); clock_gettime(CLOCK_MONOTONIC, &end); double simd = end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec) / BIL; printf("Nonthreaded: %f\nThreaded: %f (%.2fx faster)\nSIMD: %f (%.2fx faster)\n", ref_time, threaded, ref_time/threaded, simd, ref_time/simd); }
/* mycovwt calculates the estimated weighted covariance matrix based on the implementation in R. The algorithm roughly follows these steps: 1) wt <- wt/sum(wt) 2) x <- sqrt(wt) * (x - t(array(center, dim(x)[2:1]))) 3) t(x) %*% x 'x' is a matrix with 'm' rows and 'n' columns in vector form, while 'wt' is a vector if weights for each obervation with length 'm'. 'center' is a vector of length 'size' specifying the centers to be used when computing covariances. Resulting covariance matrix stored in 'rv'. */ void mycovwt(double *x, int *m, int *n, double *wt, double *center, int *size, double *rv){ double sum = 0; int row = *m; int col = *n; int i; for (i = 0; i < row; i++){ sum += wt[i]; } for (i = 0; i < row; i++){ wt[i] /= sum; wt[i] = sqrt(wt[i]); } // same operation in R as rep_len(center, row*col) double arr[row*col]; for (i = 0; i < row*col; i++){ arr[i] = center[i % *size]; } double arr_t[row*col]; // tranpose with 'col' as the row length and 'row' as the col length // because the sweep function in R (for teigen situations) uses // 'array(center, dim(x)[2:1])' which gives a resulting matrix with // the row and column dimensions of x reversed. transpose(arr, &col, &row, arr_t); for (i = 0; i < row*col; i++){ x[i] = (x[i] - arr_t[i]) * wt[i%row]; } //use arr_t to hold transpose of x transpose(x, &row, &col, arr_t); //multiply the transpose of x by x matrix_mult(arr_t, x, col, row, col, rv); }
int inv_test(u8 * in, u8 * inv, u8 * sav, int n) { memcpy(sav, in, n * n); if (gf_invert_matrix(in, inv, n)) { printf("Given singular matrix\n"); print_matrix(sav, n); return -1; } matrix_mult(inv, sav, in, n); if (is_ident(in, n)) { printf("fail\n"); print_matrix(sav, n); print_matrix(inv, n); print_matrix(in, n); return -1; } putchar('.'); return 0; }
/*======== struct matrix * generate_curve_coefs() ========== Inputs: double p1 double p2 double p3 double p4 int type Returns: A matrix containing the values for a, b, c and d of the equation at^3 + bt^2 + ct + d for the curve defined by p1, p2, p3 and p4. Type determines whether the curve is bezier or hermite ====================*/ struct matrix * generate_curve_coefs( double p1, double p2, double p3, double p4, int type) { struct matrix *B= new_matrix(4,1); B->m[0][0]=p1; B->m[0][1]=p2; B->m[0][2]=p3; B->m[0][3]=p4; struct matrix *A; if (type){//zero means hermite,1 is bezier A =make_bezier(); } else{ A = make_hermite(); } matrix_mult(A,B); return B; //now Matrtix B is in the form /* [a] [b] [c] [d] */ }
Matrix pers(Matrix a, double alpha, double zn, double zf) { if(zn>zf){ printf("Error, zn must be less then zf."); exit(EXIT_FAILURE); } double s, c, q; Matrix r, pers; r = new_matrix(4, 4); pers = new_matrix(4, 4); s = sin(rad(alpha)/2.0); c = cos(rad(alpha)/2.0); q = s/(1-(zn/zf)); double elem[16] = {c, 0, 0, 0, 0, c, 0, 0, 0, 0, q, s, 0, 0, -q*zn, 0}; pers.t = elem; r = matrix_mult(pers, a); return r; }
/*======== void add_torus() ========== Inputs: struct matrix * points double cx double cy double r1 double r2 double step Returns: adds all the points required to make a torus with center (cx, cy) and radii r1 and r2. should call generate_torus to create the necessary points 03/22/12 13:34:03 jdyrlandweaver ====================*/ void add_torus( struct matrix * points, double cx, double cy, double r1, double r2, double step ) { double x, y, z; double t, p; for (p = 0; p < 1.001; p+= step) { for (t = 0; t < 1.001; t+= step) { x = r1*cos(t*2*M_PI) + cx; y = cos(p*2*M_PI)*(r1*sin(t*2*M_PI) + r2) + cy; z = cos(p*2*M_PI)*(r1*sin(t*2*M_PI) + r2); add_edge(points, x, y, z, x, y, z); } } struct matrix *rotY = new_matrix(4, 4); rotY = make_rotY(2*M_PI*60/360); struct matrix *rotX = new_matrix(4, 4); rotX = make_rotX(2*M_PI*30/360); matrix_mult(rotX, points); //matrix_mult(rotY, points); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguments (x0, y0, z0, x1, y1, z1) b: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) h: add a bezier to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) c: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) m: add a sphere to the edge matrix - takes 3 arguments (cx, cy, r) d: add a torus to the edge matrix - takes 4 arguments (cx, cy, r1, r2) p: add a rectangular prism to the edge matrix - takes 6 arguments (x, y, z, width, height, depth) w: clear the current edge matrix - takes 0 arguments i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) 03/08/12 16:22:10 jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 0; g.green = 255; g.blue = 255; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); char c; double x, y, z, x1, y1, z1, x2, y2, x3, y3, x4, y4; c = line[0]; switch (c) { case 'l': // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'p': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); break; case 'm': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'd': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 0.05); //printf( "%lf %lf %lf\n", x, y, z); break; case 'c': fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); break; case 'b': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 'h': fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); break; case 's': //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 't': //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); break; case 'x': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); break; case 'y': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); break; case 'z': //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); break; case 'i': ident(transform); break; case 'a': //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); break; case 'v': /* clear_screen(s); draw_lines(pm, s, g); display(s); break; */ // Second version for triangles: clear_screen(s); draw_polygons(pm, s, g); display(s); break; case 'w': pm->lastcol = 0; break; case 'g': fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_polygons(pm, s, g); save_extension(s, line); break; case 'q': return; case '#': break; default: printf("Invalid command\n"); break; } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
void calc_Dprime(matrix D, matrix P, int p, int q) { matrix Dprime = matrix_copy(D); double d_pq = MatrixGET(D, p, q), d_pp = MatrixGET(D, p, p), d_qq = MatrixGET(D, q, q); double theta = (d_qq - d_pp) / (2 * d_pq), // (11.1.8) t = sign(theta) / (fabs(theta) + sqrt(pow(theta,2.0)+1.0)), // (11.1.10) c = 1.0 / sqrt(pow(t,2.0)+1.0), // (11.1.11) s = t * c, // (11.1.12) tau = s / (1.0 + c); // (11.1.18) // set to 0 (11.1.13) MatrixSET(Dprime, p, q, 0); MatrixSET(Dprime, q, p, 0); MatrixSET(Dprime, p, p, d_pp - t*d_pq); // (11.1.14) MatrixSET(Dprime, q, q, d_qq + t*d_pq); // (11.1.15) // (11.1.16) int r; for (r = 0; r < D.N; r++) { if (r != p && r != q) { double d_rp = MatrixGET(D, r, p), d_rq = MatrixGET(D, r, q); double dPrime_rp = d_rp - s * (d_rq + tau * d_rp); // D symmetric => also D' MatrixSET(Dprime, r, p, dPrime_rp); MatrixSET(Dprime, p, r, dPrime_rp); } } // (11.1.17) for (r = 0; r < D.N; r++) { if (r != p && r != q) { double d_rq = MatrixGET(D, r, q), d_rp = MatrixGET(D, r, p); double dPrime_rq = d_rq + s * (d_rp - tau * d_rq); // symmetric again MatrixSET(Dprime, r, q, dPrime_rq); MatrixSET(Dprime, q, r, dPrime_rq); } } matrix_copy_to(Dprime, D); // Transformtion matrix P // pq-rotation matrix with values c & s matrix P_pq = unity_matrix(D.N); MatrixSET(P_pq, p, p, c); MatrixSET(P_pq, p, q, s); MatrixSET(P_pq, q, p, -1.0*s); MatrixSET(P_pq, q, q, c); matrix P_n = matrix_mult(P, P_pq); matrix_copy_to(P_n, P); }
void clsLinearModelEM::CalculateSlopeInterceptEstimates() { MATRIX *wx = matrix_mult(mobj_Weights, mobj_X) ; if (wx == NULL) { ///PrintMatrix(wx, "c:\\test1.csv") ; } MATRIX *xprime_wx = matrix_mult(mobj_XTranspose, wx) ; if (xprime_wx == NULL) { //PrintMatrix(xprime_wx, "c:\\test1.csv") ; } MATRIX *inv_xprime_wx = matrix_invert(xprime_wx) ; if (inv_xprime_wx == NULL) { //PrintMatrix(inv_xprime_wx, "c:\\test1.csv") ; } MATRIX *wy = matrix_mult(mobj_Weights, mobj_Y) ; if (wy == NULL) { //PrintMatrix(wy, "c:\\test1.csv") ; } MATRIX *xprime_wy = matrix_mult(mobj_XTranspose, wy) ; if (xprime_wy == NULL) { //PrintMatrix(xprime_wy, "c:\\test1.csv") ; } MATRIX *beta = matrix_mult(inv_xprime_wx, xprime_wy) ; if (beta == NULL) { //PrintMatrix(beta, "c:\\test1.csv") ; } double **betaPtr = (double **) beta->ptr ; mdbl_slope = betaPtr[0][0] ; mdbl_intercept = betaPtr[1][0] ; int numPoints = mobj_X->rows ; double maxDiff = -1*DBL_MAX ; double minDiff = DBL_MAX ; double **ptrMatrixX = (double **) mobj_X->ptr ; double **ptrMatrixY = (double **) mobj_Y->ptr ; double maxY = -1 * DBL_MAX ; for (int index = 0 ; index < numPoints ; index++) { double diff = ptrMatrixY[index][0] - (mdbl_slope * ptrMatrixX[index][0] + mdbl_intercept) ; if (diff > maxDiff) maxDiff = diff ; if (diff < minDiff) minDiff = diff ; if (ptrMatrixY[index][0] > maxY) maxY = ptrMatrixY[index][0] ; } mdbl_unif_u = 1.0 / (maxDiff - minDiff) ; mdbl_unif_u = 1.0 / maxY ; matrix_free(wx) ; matrix_free(xprime_wx) ; matrix_free(inv_xprime_wx) ; matrix_free(wy) ; matrix_free(xprime_wy) ; matrix_free(beta) ; }
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"); }
/*======== void my_main() ========== Inputs: Returns: This is the main engine of the interpreter, it should handle most of the commadns in mdl. If frames is not present in the source (and therefore num_frames is 1, then process_knobs should be called. If frames is present, the enitre op array must be applied frames time. At the end of each frame iteration save the current screen to a file named the provided basename plus a numeric string such that the files will be listed in order, then clear the screen and reset any other data structures that need it. Important note: you cannot just name your files in regular sequence, like pic0, pic1, pic2, pic3... if that is done, then pic1, pic10, pic11... will come before pic2 and so on. In order to keep things clear, add leading 0s to the numeric portion of the name. If you use sprintf, you can use "%0xd" for this purpose. It will add at most x 0s in front of a number, if needed, so if used correctly, and x = 4, you would get numbers like 0001, 0002, 0011, 0487 05/17/12 09:41:35 jdyrlandweaver ====================*/ void my_main( int polygons ) { int i, f, j; double step; double xval, yval, zval, knob_value; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; struct vary_node **knobs; struct vary_node *vn; char frame_name[128]; num_frames = 1; step = 5; f=0; g.red = 0; g.green = 255; g.blue = 255; first_pass(); knobs = second_pass(); for(f = 0;f<num_frames; f++){ struct vary_node *curr = (struct vary_node *)calloc(1, sizeof(struct vary_node)); curr = knobs[f]; while(curr){ printf("%s : %f\n", curr->name, curr->value); curr = curr->next; } } for(f=0;f<num_frames;f++){ s = new_stack(); tmp = new_matrix(4, 4); transform = new_matrix(4, 4); if(num_frames>1){ vn = knobs[f]; while(vn){ SYMTAB *symb = lookup_symbol(vn->name); set_value(symb, vn->value); vn = vn->next; } } //print_knobs(); for (i=0;i<lastop;i++) { SYMTAB *v; switch (op[i].opcode) { case SPHERE: //printf("Add sphere\n"); add_sphere( tmp,op[i].op.sphere.d[0], //cx op[i].op.sphere.d[1], //cy op[i].op.sphere.d[2], //cz op[i].op.sphere.r, step); //apply the current top origin matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case TORUS: //printf("Add Torus\n"); add_torus( tmp, op[i].op.torus.d[0], //cx op[i].op.torus.d[1], //cy op[i].op.torus.d[2], //cz op[i].op.torus.r0, op[i].op.torus.r1, step); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case BOX: //printf("Add box\n"); add_box( tmp, op[i].op.box.d0[0], op[i].op.box.d0[1], op[i].op.box.d0[2], op[i].op.box.d1[0], op[i].op.box.d1[1], op[i].op.box.d1[2]); matrix_mult( s->data[ s->top ], tmp ); draw_polygons( tmp, t, g ); tmp->lastcol = 0; break; case LINE: //printf("Line\n"); add_edge( tmp, op[i].op.line.p0[0], op[i].op.line.p0[1], op[i].op.line.p0[1], op[i].op.line.p1[0], op[i].op.line.p1[1], op[i].op.line.p1[1]); draw_lines( tmp, t, g ); tmp->lastcol = 0; break; case MOVE: //printf("Move\n"); //get the factors xval = op[i].op.move.d[0]; yval = op[i].op.move.d[1]; zval = op[i].op.move.d[2]; v = op[i].op.move.p; if(v){ xval = xval * v->s.value; yval = yval * v->s.value; zval = zval * v->s.value; } //printf("x: %f y: %f z: %f\n", xval, yval, zval); transform = make_translate( xval, yval, zval ); //multiply by the existing origin matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case SCALE: //printf("Scale\n"); xval = op[i].op.scale.d[0]; yval = op[i].op.scale.d[1]; zval = op[i].op.scale.d[2]; v = op[i].op.scale.p; if(v){ //printf("I'm not null Scale\n"); xval *= v->s.value; yval *= v->s.value; zval *= v->s.value; } transform = make_scale( xval, yval, zval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case ROTATE: //printf("Rotate\n"); xval = op[i].op.rotate.degrees * ( M_PI / 180 ); v = op[i].op.rotate.p; if(v){ xval *= v->s.value; } //get the axis if ( op[i].op.rotate.axis == 0 ) transform = make_rotX( xval ); else if ( op[i].op.rotate.axis == 1 ) transform = make_rotY( xval ); else if ( op[i].op.rotate.axis == 2 ) transform = make_rotZ( xval ); matrix_mult( s->data[ s->top ], transform ); //put the new matrix on the top copy_matrix( transform, s->data[ s->top ] ); free_matrix( transform ); break; case PUSH: //printf("Push\n"); push( s ); break; case POP: //printf("Pop\n"); pop( s ); break; case SAVE: //printf("Save\n"); save_extension( t, op[i].op.save.p->name ); break; case DISPLAY: //printf("Display\n"); display( t ); break; } } if(num_frames>1){ sprintf (frame_name, "%s%03d.png", name, f); save_extension(t, frame_name); } clear_screen(t); free_stack(s); free_matrix(tmp); } //free_stack( s ); //free_matrix( tmp ); //free_matrix( transform ); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; g.red = 255; g.green = 0; g.blue = 255; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; double width, height, depth, radius, radius1, radius2; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { //printf("BEZIER\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "box", strlen(line)) == 0 ) { //printf("BOX\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &width, &height, &depth); add_box(pm, x, y, z, width, height, depth); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "sphere", strlen(line)) == 0 ) { //printf("SPHERE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &radius); add_sphere(pm, x, y, radius, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "torus", strlen(line)) == 0 ) { //printf("TORUS\n");ds fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &radius1, &radius2 ); add_torus(pm, x, y, radius1, radius2, 0.01); //printf( "%lf %lf %lf\n", x, y, z); } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { //printf("SCALE\n"); fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); // line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); matrix_mult(tmp, transform); //print_matrix(transform); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); matrix_mult(tmp, transform); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { clear_screen(s); draw_lines(pm, s, g); display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); draw_lines(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; clear_screen(s); } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if (strncmp(line, "#", strlen(1)) == 0){ } else { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: line: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) circle: add a circle to the edge matrix - takes 3 arguments (cx, cy, r) hermite: add a hermite curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) bezier: add a bezier curve to the edge matrix - takes 8 arguments (x0, y0, x1, y1, x2, y2, x3, y3) sphere: add a sphere to the edge matrix - takes 3 arguemnts (cx, cy, r) torus: add a torus to the edge matrix - takes 4 arguemnts (cx, cy, r1, r2) box: add a rectangular prism to the edge matrix - takes 6 arguemnts (x, y, z, width, height, depth) clear: clear the currnt edge matrix - takes 0 arguments ident: set the transform matrix to the identity matrix - scale: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) translate: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) xrotate: create an x-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) yrotate: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) zrotate: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) apply: apply the current transformation matrix to the edge matrix display: draw the lines of the edge matrix to the screen display the screen save: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) quit: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { FILE *f; char line[256]; struct matrix * tmp; double angle; color g; struct stack * STACK = new_stack(); g.red = 0; g.green = 255; g.blue = 0; clear_screen(s); if ( strcmp(filename, "stdin") == 0 ) f = stdin; else f = fopen(filename, "r"); while ( fgets(line, 255, f) != NULL ) { line[strlen(line)-1]='\0'; //printf(":%s:\n",line); double x, y, z, x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4; if ( strncmp(line, "line", strlen(line)) == 0 ) { // printf("LINE!\n"); fgets(line, 255, f); // printf("\t%s", line); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_edge(pm, x, y, z, x1, y1, z1); // printf( "%lf %lf %lf %lf %lf %lf\n", x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "circle", strlen(line)) == 0 ) { //printf("CIRCLE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_circle(pm, x, y, z, 0.01); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "bezier", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, BEZIER_MODE ); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "hermite", strlen(line)) == 0 ) { //printf("HERMITE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); add_curve(pm, x1, y1, x2, y2, x3, y3, x4, y4, 0.01, HERMITE_MODE ); //printf( "%lf %lf %lf\n", x, y, z); matrix_mult( STACK->data[ STACK->top], pm); draw_lines( pm, s, g); pm->lastcol = 0; } else if ( strncmp(line, "box", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf %lf %lf", &x, &y, &z, &x1, &y1, &z1); add_box(pm, x, y, z, x1, y1, z1); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if (strncmp(line, "sphere", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); add_sphere(pm, x, y, z, 10); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if (strncmp(line, "torus", strlen(line)) == 0 ) { fgets(line, 255, f); sscanf(line, "%lf %lf %lf %lf", &x, &y, &z, &z1); add_torus(pm, x, y, z, z1, 10); matrix_mult( STACK->data[ STACK->top ], pm); draw_polygons( pm, s, g ); pm->lastcol = 0; } else if ( strncmp(line, "scale", strlen(line)) == 0 ) { fgets(line, 255, f); //line[strlen(line)-1]='\0'; sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_scale(x, y, z); //matrix_mult(tmp, transform); //print_matrix(transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "translate", strlen(line)) == 0 ) { //printf("TRANSLATE\n"); fgets(line, 255, f); sscanf(line, "%lf %lf %lf", &x, &y, &z); tmp = make_translate(x, y, z); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "xrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotX( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "yrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotY( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "zrotate", strlen(line)) == 0 ) { //printf("ROTATE!\n"); fgets(line, 255, f); sscanf(line, "%lf", &angle); angle = angle * (M_PI / 180); tmp = make_rotZ( angle); //matrix_mult(tmp, transform); //matrix_mult( tmp, STACK->data[ STACK->top ] ); matrix_mult( STACK->data[ STACK->top ], tmp ); copy_matrix( tmp, STACK->data[ STACK->top ] ); } else if ( strncmp(line, "ident", strlen(line)) == 0 ) { ident(transform); } else if ( strncmp(line, "apply", strlen(line)) == 0 ) { //printf("APPLY!\n"); //print_matrix( transform ); // print_matrix(pm); matrix_mult(transform, pm); } else if ( strncmp(line, "print", strlen(line)) == 0) { print_matrix( STACK->data[ STACK->top ] ); } else if ( strncmp(line, "display", strlen(line)) == 0 ) { display(s); } else if ( strncmp(line, "save", strlen(line)) == 0 ) { fgets(line, 255, f); // line[strlen(line)-1] = '\0'; //clear_screen(s); //draw_polygons(pm, s, g); save_extension(s, line); } else if ( strncmp(line, "clear", strlen(line)) == 0 ) { pm->lastcol = 0; } else if ( strncmp(line, "quit", strlen(line)) == 0 ) { return; } else if ( strncmp(line, "push", strlen(line)) == 0 ) { push( STACK ); //seg fault printf("Pushed\n"); } else if ( strncmp(line, "pop", strlen(line)) == 0 ) { pop( STACK ); printf("Popped\n"); } else if ( line[0] != '#' ) { printf("Invalid command\n"); } } free_matrix(tmp); fclose(f); //printf("END PARSE\n"); }
main() { int i, j, k; FILE *f; double *X; X = (double*)malloc(isnp*ndof*sizeof(double)); printf("reading input file \n"); f = fopen("snapshot_u1", "r"); k = -1; for (j=0; j<isnp; j++) { for (i=0; i<ndof; i++) { k++; fscanf(f,"%lf \n",&X[k]); } } fclose(f); printf("finished reading input file \n"); double **A; int row; // allocate an "array of arrays" of int A = (double**)malloc( ndof* sizeof(double*) ) ; for(row = 0; row < ndof; row++ ) { A[row] = (double*)malloc( isnp*sizeof(double) ) ; } k = -1; for (i=0; i<ndof; i++) { for (j=0; j<isnp; j++) { k++; A[i][j] = X[k]; } } free(X); double *w; w = (double*)malloc( isnp* sizeof(double*) ) ; double **v; v = (double**)malloc( isnp* sizeof(double*) ) ; for(row = 0; row < isnp; row++ ) { v[row] = (double*)malloc( isnp*sizeof(double) ) ; } dsvd(A,ndof,isnp,w,v); printf("done with svd \n"); f = fopen ("w.dat", "w+"); for (i=0; i<isnp; i++) { fprintf(f, "%d %f\n", i,w[i]); } fclose(f); //-------------------------------- // check for orthonormality double **AT; // allocate an "array of arrays" of int AT = (double**)malloc( isnp* sizeof(double*) ) ; for(row = 0; row < isnp; row++ ) { AT[row] = (double*)malloc( ndof*sizeof(double) ) ; } for (i=0; i<ndof; i++) { for (j=0; j<isnp; j++) { AT[j][i] = A[i][j]; } } double **Iden; // allocate an "array of arrays" of int Iden = (double**)malloc( isnp* sizeof(double*) ) ; for(row = 0; row < isnp; row++ ) { Iden[row] = (double*)malloc( isnp*sizeof(double) ) ; } matrix_mult(isnp,ndof,isnp,AT,A,Iden); printf("Identity matrix? \n"); double s1 = 0.0; double s2 = 0.0; for (i=0; i<isnp; i++) { for (j=0; j<isnp; j++) { if(i == j) s1 += Iden[i][j]; else s2 += Iden[i][j]; } } s1 /= (double)(isnp); s2 /= (double)(isnp*(isnp-1)); printf("sum of diagonals = %f \n", s1); printf("sum of off-diagonals = %f \n", s2); //------------------------------------ }
void my_main( int polygons ) { int i; double step; double xval, yval, zval; struct matrix *transform; struct matrix *tmp; struct stack *s; screen t; color g; s = new_stack(); tmp = new_matrix(4, 1000); clear_screen( t ); for (i=0;i<lastop;i++) { switch (op[i].opcode) { //simple cases //i realized that you already defined these constants in another //file after looking at this for 30 min -_- case POP: pop(s); break; case PUSH: push(s); break; //move,scale,rotate case MOVE: transform = make_translate(op[i].op.move.d[0],op[i].op.move.d[1], op[i].op.move.d[2]); matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; case ROTATE: //there are 3 rotations possible, SO SWITCH-CEPTION! switch((int)op[i].op.rotate.axis) { case ROT_X: transform = make_rotX(op[i].op.rotate.degrees); break; case ROT_Y: transform = make_rotY(op[i].op.rotate.degrees); break; case ROT_Z: transform = make_rotZ(op[i].op.rotate.degrees); break; } matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; case SCALE: transform = make_scale(op[i].op.scale.d[0],op[i].op.scale.d[1], op[i].op.scale.d[2]); matrix_mult(transform,s->data[s->top]); free_matrix(transform); break; //box,sphere,torus case BOX: add_box(tmp,op[i].op.box.d0[0],op[i].op.box.d0[1],op[i].op.box.d0[2], op[i].op.box.d1[0],op[i].op.box.d1[1],op[i].op.box.d1[2]); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); free_matrix(tmp); //reset tmp=new_matrix(4,1000); break; case SPHERE: add_sphere(tmp,op[i].op.sphere.d[0],op[i].op.sphere.d[1], op[i].op.sphere.d[2],op[i].op.sphere.r,0.01); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); free_matrix(tmp); //reset tmp=new_matrix(4,1000); break; case TORUS: add_torus(tmp,op[i].op.torus.d[0],op[i].op.torus.d[1],op[i].op.torus.d[2], op[i].op.torus.r0,op[i].op.torus.r1,0.01); matrix_mult(s->data[s->top],tmp); draw_polygons(tmp,t,g); free_matrix(tmp); //reset tmp=new_matrix(4,1000); break; //line case LINE: add_edge(tmp,op[i].op.line.p0[0],op[i].op.line.p0[1],op[i].op.line.p0[2], op[i].op.line.p1[0],op[i].op.line.p1[1],op[i].op.line.p1[2]); matrix_mult(s->data[s->top],tmp); draw_lines(tmp,t,g); free_matrix(tmp); tmp=new_matrix(4,1000);//RESET break; //EVERYTIN else case SAVE: save_extension(t,op[i].op.save.p->name); break; case DISPLAY: display(t); break; default: break; } } }
/*======== void parse_file () ========== Inputs: char * filename struct matrix * transform, struct matrix * pm, screen s Returns: Goes through the file named filename and performs all of the actions listed in that file. The file follows the following format: Every command is a single character that takes up a line Any command that requires arguments must have those arguments in the second line. The commands are as follows: l: add a line to the edge matrix - takes 6 arguemnts (x0, y0, z0, x1, y1, z1) i: set the transform matrix to the identity matrix - s: create a scale matrix, then multiply the transform matrix by the scale matrix - takes 3 arguments (sx, sy, sz) t: create a translation matrix, then multiply the transform matrix by the translation matrix - takes 3 arguments (tx, ty, tz) x: create an x-axis rotation matrix,bv then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) y: create an y-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) z: create an z-axis rotation matrix, then multiply the transform matrix by the rotation matrix - takes 1 argument (theta) a: apply the current transformation matrix to the edge matrix v: draw the lines of the edge matrix to the screen display the screen g: draw the lines of the edge matrix to the screen save the screen to a file - takes 1 argument (file name) q: end parsing See the file script for an example of the file format IMPORTANT MATH NOTE: the trig functions int math.h use radian mesure, but us normal humans use degrees, so the file will contain degrees for rotations, be sure to conver those degrees to radians (M_PI is the constant for PI) jdyrlandweaver ====================*/ void parse_file ( char * filename, struct matrix * transform, struct matrix * pm, screen s) { double args [6]; char command [25]; color c; c.red = 255; c.blue = 0; c.green = 0; FILE *f = fopen(filename, "r"); if(f == NULL) { printf("cannot find file\n"); exit(1); } while(fgets(command, 25, f)){ if(command[0] == 'l') { fgets(command, 25, f); sscanf(command, "%lf %lf %lf %lf %lf %lf", &args[0], &args[1], &args[2], &args[3], &args[4], &args[5]); add_edge(pm, args[0], args[1], args[2], args[3], args[4], args[5]); } else if (command[0] == 'i') { ident(transform); } else if (command[0] == 's') { fgets(command, 25, f); sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]); matrix_mult(make_scale(args[0], args[1], args[2]), transform); } else if (command[0] == 't') { fgets(command, 25, f); sscanf(command, "%lf %lf %lf", &args[0], &args[1], &args[2]); struct matrix *m; m = make_translate(args[0], args[1], args[2]); matrix_mult(m, transform); } else if (command[0] == 'x') { fgets(command, 25, f); sscanf(command, "%lf", &args[0]); struct matrix *m; m = make_rotX(args[0]); matrix_mult(m, transform); } else if (command[0] == 'y') { fgets(command, 25, f); sscanf(command, "%lf", &args[0]); struct matrix *m; m = make_rotY(args[0]); matrix_mult(m, transform); } else if (command[0] == 'z') { fgets(command, 25, f); sscanf(command, "%lf", &args[0]); struct matrix *m; m = make_rotZ(args[0]); matrix_mult(m, transform); } else if (command[0] == 'a') { matrix_mult(transform, pm); } else if (command[0] == 'v') { draw_lines(pm, s, c); } else if (command[0] == 'g') { draw_lines(pm, s, c); fgets(command, 25, f); int i = strlen(command); command[i] = '\0'; save_extension(s, command); } else if (command[0] == 'q') { exit(1); } } }
void airsar_to_latlon(meta_parameters *meta, double xSample, double yLine, double height, double *lat, double *lon) { if (!meta->airsar) asfPrintError("airsar_to_latlon() called with no airsar block!\n"); const double a = 6378137.0; // semi-major axis const double b = 6356752.3412; // semi-minor axis const double e2 = 0.00669437999014; // ellipticity const double e12 = 0.00673949674228; // second eccentricity // we try to cache the matrices needed for the computation // this makes sure we don't reuse the cache incorrectly (i.e., on // data (=> an airsar block) which doesn't match what we cached for) static meta_airsar *cached_airsar_block = NULL; // these are the cached transformation parameters static matrix *m = NULL; static double ra=-999, o1=-999, o2=-999, o3=-999; if (!m) m = matrix_alloc(3,3); // only needs to be done once // if we aren't calculating with the exact same airsar block, we // need to recalculate the transformation block int recalc = !cached_airsar_block || cached_airsar_block->lat_peg_point != meta->airsar->lat_peg_point || cached_airsar_block->lon_peg_point != meta->airsar->lon_peg_point || cached_airsar_block->head_peg_point != meta->airsar->head_peg_point; if (recalc) { // cache airsar block, so we can be sure we're not reusing // the stored data incorrectly if (cached_airsar_block) free(cached_airsar_block); cached_airsar_block = meta_airsar_init(); *cached_airsar_block = *(meta->airsar); asfPrintStatus("Calculating airsar transformation parameters...\n"); // now precalculate data double lat_peg = meta->airsar->lat_peg_point*D2R; double lon_peg = meta->airsar->lon_peg_point*D2R; double head_peg = meta->airsar->head_peg_point*D2R; double re = a / sqrt(1-e2*sin(lat_peg)*sin(lat_peg)); double rn = (a*(1-e2)) / pow(1-e2*sin(lat_peg)*sin(lat_peg), 1.5); ra = (re*rn) / (re*cos(head_peg)*cos(head_peg)+rn*sin(head_peg)*sin(head_peg)); matrix *m1, *m2; m1 = matrix_alloc(3,3); m2 = matrix_alloc(3,3); m1->coeff[0][0] = -sin(lon_peg); m1->coeff[0][1] = -sin(lat_peg)*cos(lon_peg); m1->coeff[0][2] = cos(lat_peg)*cos(lon_peg); m1->coeff[1][0] = cos(lon_peg); m1->coeff[1][1] = -sin(lat_peg)*sin(lon_peg); m1->coeff[1][2] = cos(lat_peg)*sin(lon_peg); m1->coeff[2][0] = 0.0; m1->coeff[2][1] = cos(lat_peg); m1->coeff[2][2] = sin(lat_peg); m2->coeff[0][0] = 0.0; m2->coeff[0][1] = sin(head_peg); m2->coeff[0][2] = -cos(head_peg); m2->coeff[1][0] = 0.0; m2->coeff[1][1] = cos(head_peg); m2->coeff[1][2] = sin(head_peg); m2->coeff[2][0] = 1.0; m2->coeff[2][1] = 0.0; m2->coeff[2][2] = 0.0; o1 = re*cos(lat_peg)*cos(lon_peg)-ra*cos(lat_peg)*cos(lon_peg); o2 = re*cos(lat_peg)*sin(lon_peg)-ra*cos(lat_peg)*sin(lon_peg); o3 = re*(1-e2)*sin(lat_peg)-ra*sin(lat_peg); matrix_mult(m,m1,m2); matrix_free(m1); matrix_free(m2); } // Make sure we didn't miss anything assert(ra != -999 && o1 != -999 && o2 != -999 && o3 != -999); //------------------------------------------------------------------ // Now the actual computation, using the cached matrix etc // convenience aliases double c0 = meta->airsar->cross_track_offset; double s0 = meta->airsar->along_track_offset; double ypix = meta->general->y_pixel_size/meta->general->line_scaling; double xpix = meta->general->x_pixel_size/meta->general->sample_scaling; // radar coordinates double c_lat = (xSample*xpix+c0)/ra; double s_lon = (yLine*ypix+s0)/ra; //height += meta->airsar->gps_altitude; // radar coordinates in WGS84 double t1 = (ra+height)*cos(c_lat)*cos(s_lon); double t2 = (ra+height)*cos(c_lat)*sin(s_lon); double t3 = (ra+height)*sin(c_lat); double c1 = m->coeff[0][0]*t1 + m->coeff[0][1]*t2 + m->coeff[0][2]*t3; double c2 = m->coeff[1][0]*t1 + m->coeff[1][1]*t2 + m->coeff[1][2]*t3; double c3 = m->coeff[2][0]*t1 + m->coeff[2][1]*t2 + m->coeff[2][2]*t3; // shift into local Cartesian coordinates double x = c1 + o1;// + 9.0; double y = c2 + o2;// - 161.0; double z = c3 + o3;// - 179.0; // local Cartesian coordinates into geographic coordinates double d = sqrt(x*x+y*y); double theta = atan2(z*a, d*b); *lat = R2D*atan2(z+e12*b*sin(theta)*sin(theta)*sin(theta), d-e2*a*cos(theta)*cos(theta)*cos(theta)); *lon = R2D*atan2(y, x); }