/*-------------- void matrix_mult() -------------- Inputs: struct matrix *a struct matrix *b Returns: a*b -> b */ void matrix_mult(struct matrix *a, struct matrix *b) { int row, col, times, sum; struct matrix* temp; temp = new_matrix(a->rows,b->cols); sum =0; for(row = 0; row <a->rows; row++){ for(col = 0; col <b->cols; col++){ for(times = 0; times < b->rows; times++){ sum = sum+ a->m[row][times] * b->m[times][col]; } temp -> m[row][col] = sum; sum = 0; } } b->m = temp->m; b->rows = temp->rows; b->cols = temp->cols; }
/*======== 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* coefs = new_matrix(4, 1); coefs->m[0][0] = p1; coefs->m[1][0] = p2; coefs->m[2][0] = p3; coefs->m[3][0] = p4; if (type == HERMITE_MODE) { matrix_mult(make_hermite(), coefs); } else { matrix_mult(make_bezier(), coefs); } return coefs; }
/*-------------- void matrix_mult() -------------- Inputs: struct matrix *a struct matrix *b Returns: a*b -> b */ void matrix_mult(struct matrix *a, struct matrix *b) { int r, c; struct matrix * tmp; tmp = new_matrix(4, 1); for (c=0; c < b->cols; c++) { for (r=0; r < 4; r++) tmp->m[r][0] = b->m[r][c]; for (r=0; r < 4; r++) b->m[r][c] = a->m[r][0] * tmp->m[0][0] + a->m[r][1] * tmp->m[1][0] + a->m[r][2] * tmp->m[2][0] + a->m[r][3] * tmp->m[3][0]; } free_matrix(tmp); }
/*======== struct matrix * make_bezier()) ========== Inputs: Returns: The correct 4x4 matrix that can be used to generate the coefiecients for a bezier curve ====================*/ struct matrix * make_bezier() { struct matrix * m = new_matrix(4, 4); ident(m); m->m[0][0] = -1; m->m[0][1] = 3; m->m[0][2] = -3; m->m[0][3] = 1; m->m[1][0] = 3; m->m[1][1] = -6; m->m[1][2] = 3; m->m[1][3] = 0; m->m[2][0] = -3; m->m[2][1] = 3; m->m[2][2] = 0; m->m[3][0] = 1; m->m[3][3] = 0; return m; }
Matrix matrix_add(Matrix a, Matrix b) { if(a.cols!=b.cols || a.rows!=b.rows){ printf("ERROR, matrix dimension mismatch.\n"); exit(EXIT_FAILURE); } int x, y; Matrix r; r = new_matrix(a.rows, a.cols); for (x=0; x<a.rows; x++){ for (y=0; y<b.cols; y++){ M(r, y, x) = M(a, y, x) + M(b, y, x); } } return r; }
void mnormalnew (header *hd) { header *st=hd,*result; double *m; int r,c; LONG k,n; hd=getvalue(hd); if (error) return; if (hd->type!=s_matrix || dimsof(hd)->r!=1 || dimsof(hd)->c!=2 || *(m=matrixof(hd))<0 || *m>=INT_MAX || *(m+1)<0 || *(m+1)>INT_MAX) wrong_arg_in("normal"); r=(int)*m; c=(int)*(m+1); result=new_matrix(r,c,""); if (error) return; m=matrixof(result); n=(LONG)c*r; for (k=0; k<n; k++) *m++=gasdev(); moveresult(st,result); }
/*======== 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 ) { int length = 1 / pow(step, 2); if((length % 2) == 1) length++; struct matrix * temp = new_matrix(4,length); generate_torus(temp, cx, cy, r1, r2, step); double x, y, z; int t; for(t=0;t<temp->lastcol; t++){ x = temp->m[0][t]; y = temp->m[1][t]; z = temp->m[2][t]; add_edge(points, x, y, z, x, y, z); } free_matrix(temp); }
struct matrix * make_hermite() { struct matrix * m = new_matrix(4, 4); ident(m); m->m[0][0] = 2; m->m[0][1] = -2; m->m[0][2] = 1; m->m[0][3] = 1; m->m[1][0] = -3; m->m[1][1] = 3; m->m[1][2] = -2; m->m[1][3] = -1; m->m[3][0] = 1; m->m[3][3] = 0; //printf("HERMITE\n"); //print_matrix(m); return m; }
/** * Returns new matrix, adding elements with the same index */ uint32_t* matrix_add(const uint32_t* matrix_a, const uint32_t* matrix_b) { uint32_t* result = new_matrix(); for(ssize_t i = 0; i < g_elements; ++i) { result[i] = matrix_a[i] + matrix_b[i]; } /* to do 1 0 0 1 1 1 0 1 + 1 0 => 1 1 1 2 4 4 5 6 3 4 + 4 4 => 7 8 */ return result; }
/** * Returns new matrix, multiplying scalar to each element */ uint32_t* scalar_mul(const uint32_t* matrix, uint32_t scalar) { uint32_t* result = new_matrix(); for(ssize_t i = 0; i < g_elements; ++i) { result[i] = matrix[i] * scalar; } /* to do 1 0 2 0 0 1 x 2 => 0 2 1 2 2 4 3 4 x 2 => 6 8 */ return result; }
/** * Returns new matrix, adding scalar to each element */ uint32_t* scalar_add(const uint32_t* matrix, uint32_t scalar) { uint32_t* result = new_matrix(); for(ssize_t i = 0; i < g_elements; ++i) { result[i] = matrix[i] + scalar; } /* to do 1 0 2 1 0 1 + 1 => 1 2 1 2 5 6 3 4 + 4 => 7 8 */ return result; }
void polyadd (header *hd) { header *st=hd,*hd1,*result; int c,c1,c2,i,r; double *m1,*m2,*mr; complex *mc1,*mc2,*mcr; interval *mi1,*mi2,*mir; hd1=next_param(st); equal_params_2(&hd,&hd1); if (error) return; getmatrix(hd,&r,&c1,&m1); if (r!=1) wrong_arg(); getmatrix(hd1,&r,&c2,&m2); if (r!=1) wrong_arg(); c=max(c1,c2); if (iscomplex(hd)) /* complex values */ { mc1=(complex *)m1; mc2=(complex *)m2; result=new_cmatrix(1,c,""); if (error) return; mcr=(complex *)matrixof(result); for (i=0; i<c; i++) { if (i>=c1) { c_copy(*mcr,*mc2); mcr++; mc2++; } else if (i>=c2) { c_copy(*mcr,*mc1); mcr++; mc1++; } else { c_add(*mc1,*mc2,*mcr); mc1++; mc2++; mcr++; } } } else if (isinterval(hd)) { mi1=(interval *)m1; mi2=(interval *)m2; result=new_imatrix(1,c,""); if (error) return; mir=(interval *)matrixof(result); for (i=0; i<c; i++) { if (i>=c1) { i_copy(*mir,*mi2); mir++; mi2++; } else if (i>=c2) { i_copy(*mir,*mi1); mir++; mi1++; } else { i_add(*mi1,*mi2,*mir); mi1++; mi2++; mir++; } } } else if (isreal(hd)) { result=new_matrix(1,c,""); if (error) return; mr=matrixof(result); for (i=0; i<c; i++) { if (i>=c1) { *mr++ = *m2++; } else if (i>=c2) { *mr++ = *m1++; } else { *mr++ = *m1++ + *m2++; } } } else wrong_arg(); moveresult(st,result); }
/*======== void add_sphere() ========== Inputs: struct matrix * points double cx double cy double r double step Returns: adds all the points for a sphere with center (cx, cy) and radius r. should call generate_sphere to create the necessary points jdyrlandweaver ====================*/ void add_sphere( struct matrix * points, double cx, double cy, double r, int step ) { struct matrix * temp; int lat, longt; int index; double x, y, z; int num_steps; num_steps = MAX_STEPS / step; temp = new_matrix( 4, num_steps * num_steps ); //generate the points on the sphere generate_sphere( temp, cx, cy, r, step ); int latStop, longStop, latStart, longStart; latStart = 0; latStop = num_steps; longStart = 0; longStop = num_steps; for ( lat = latStart; lat < latStop; lat++ ) { for ( longt = longStart; longt < longStop; longt++ ) { index = lat * num_steps + longt; add_polygon(points, temp->m[0][index], temp->m[1][index], temp->m[2][index], temp->m[0][index + 1], temp->m[1][index + 1], temp->m[2][index + 1], temp->m[0][index + num_steps+ 1], temp->m[1][index + num_steps+ 1], temp->m[2][index + num_steps+ 1]); add_polygon(points, temp->m[0][index], temp->m[1][index], temp->m[2][index], temp->m[0][index + num_steps + 1], temp->m[1][index + num_steps+ 1], temp->m[2][index + num_steps+ 1], temp->m[0][index + num_steps], temp->m[1][index + num_steps], temp->m[2][index + num_steps]); /*add_edge( points, temp->m[0][index], temp->m[1][index], temp->m[2][index], temp->m[0][index] + 1, temp->m[1][index] + 1, temp->m[2][index] );*/ }//end points only } free_matrix(temp); }
/*======== 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 ) { struct matrix *temp; temp = new_matrix(4,1); generate_torus(temp, cx, cy, r1, r2, step); int col; for(col = 0; col < temp->cols; col++) { add_edge(points, temp->m[0][col], temp->m[1][col], temp->m[2][col], temp->m[0][col], temp->m[1][col], temp->m[2][col]); } }
/** * Returns new matrix, multiplying the two matrices together */ uint32_t* matrix_mul(const uint32_t* matrix_a, const uint32_t* matrix_b) { uint32_t* result = new_matrix(); for (ssize_t c=0; c<g_elements; c++) { for (ssize_t first_row = (c/g_width) * g_width; first_row<g_elements;) { for(ssize_t second_column = (c%g_width); second_column<g_elements; second_column += g_width) { result[c] += (matrix_a[first_row++] * matrix_b[second_column]); } break; } } return result; }
/*-------------- void matrix_mult() -------------- Inputs: struct matrix *a struct matrix *b Returns: a*b -> b */ void matrix_mult(struct matrix *a, struct matrix *b) { int x; int y; int z; struct matrix * g; g = new_matrix(a->rows,b->cols); if(a->cols == b->rows){ for(x = 0; x < a->rows; x++){ for(y = 0; y < b->cols; y++){ g->m[x][y] = 0; for(z=0; z < a->cols; z++){ g->m[x][y] += ( (a->m[x][z]) * (b->m[z][y])); } } } } grow_matrix(a,b->cols); copy_matrix(g,a); }
/*======== void add_sphere() ========== Inputs: struct matrix * points double cx double cy double r double step Returns: adds all the points for a sphere with center (cx, cy) and radius r. should call generate_sphere to create the necessary points jdyrlandweaver ====================*/ void add_sphere( struct matrix * points, double cx, double cy, double r, double step ) { struct matrix * temp; int lat, longt; double ns; int steps; ns = 1.0 / step; steps = (int)ns; temp = new_matrix( 4, (steps) * (steps+1) ); //generate the points on the sphere generate_sphere( temp, cx, cy, r, step ); for(lat = 0; lat < steps; lat++){ for(longt = 0; longt < steps; longt++){ add_polygon(points, temp->m[0][(lat * (steps + 1)) + (longt % (steps + 1))], temp->m[1][(lat * (steps + 1)) + (longt % (steps + 1))], temp->m[2][(lat * (steps + 1)) + (longt % (steps + 1))], temp->m[0][(((lat + 1) % steps) * (steps + 1)) + ((longt + 1) % (steps + 1))], temp->m[1][(((lat + 1) % steps) * (steps + 1)) + ((longt + 1) % (steps + 1))], temp->m[2][(((lat + 1) % steps) * (steps + 1)) + ((longt + 1) % (steps + 1))], temp->m[0][(lat * (steps + 1)) + ((longt + 1) % (steps + 1))], temp->m[1][(lat * (steps + 1)) + ((longt + 1) % (steps + 1))], temp->m[2][(lat * (steps + 1)) + ((longt + 1) % (steps + 1))]); add_polygon(points, temp->m[0][(lat * (steps + 1)) + longt], temp->m[1][(lat * (steps + 1)) + longt], temp->m[2][(lat * (steps + 1)) + longt], temp->m[0][(((lat + 1) % steps) * (steps + 1)) + longt], temp->m[1][(((lat + 1) % steps) * (steps + 1)) + longt], temp->m[2][(((lat + 1) % steps) * (steps + 1)) + longt], temp->m[0][(((lat + 1) % steps) * (steps + 1)) + ((longt+1) % (steps + 1))], temp->m[1][(((lat + 1) % steps) * (steps + 1)) + ((longt+1) % (steps + 1))], temp->m[2][(((lat + 1) % steps) * (steps + 1)) + ((longt+1) % (steps + 1))]); } } }
int test_matrix_find_element(void) { Element *element_by_pos; Element *element_by_val; Matrix *matrix = new_matrix(); int res = fill_matrix(matrix, 20, 20); if(res != 0) { LOG_ERR("test matrix find element -> fill matrix error"); return res; } //find element by pos element_by_pos = matrix_find_by_pos(matrix, 3, 15); if(element_by_pos == NULL) { LOG_ERR("find element from matrix by pos"); return 1; } if(element_by_pos->value == 3*15) { LOG_SUCCESS("find element from matrix by pos"); } else { LOG_ERR("find element from matrix by pos"); return 1; } //find element by value element_by_val = matrix_find_by_val(matrix, 3*15); if(element_by_val == NULL) { LOG_ERR("find element from matrix by value"); return 1; } while(element_by_val != NULL) { if(element_by_val->row == 3 && element_by_val->col == 15) { LOG_SUCCESS("find element from matrix by value"); break; } else if(element_by_val->next == NULL){ LOG_ERR("find element from matrix by value"); return 1; } else { element_by_val = element_by_val->next; } } return 0; }
int mul_byte_by_matrix(uint8_t *result, const gf2matrix *m, uint8_t byte) { /** * 1. convert byte to a matrix (vector) * 2. multiply vector by m; obtain result matrix * 3. convert result matrix into byte array */ int i; int octets = get_rows(m) / 8; gf2matrix *vec = new_matrix(8, 1); byte2vector(vec, byte); gf2matrix *prod = mul_matrices(NULL, (gf2matrix *) m, vec); for (i = 0; i < octets; ++i) { vector2byte_offset(&result[i], prod, i * 8); } free_matrix(vec); free_matrix(prod); return 0; }
int main(int argc, char** argv) { //Parse commandline into globals do_cmd_line_parse(argc,argv); //Parse the PS file into line structs int num_lines = 0; struct Line * lines = ps_parse_to_lines(ps_file,&num_lines); //Do transforms on these line structs //Objects of your scene are scaled, then rotated //and finally translated in the world coordinates. lines = transform_lines(lines, num_lines, scale, rotation_cc, trans_x, trans_y); //Clip lines to window lines = clip_lines_to_window(lines, &num_lines, lower_bound_x, lower_bound_y,upper_bound_x,upper_bound_y); //Convert lines into list of points int num_points = 0; struct Point * points = lines_to_points(lines, num_lines, &num_points); //Allocate a blank 2d matrix to serve as the pixel map //Pixel map is sizes according to view window //Columns = X size, Rows = Y size struct Matrix2D pixel_map = new_matrix( upper_bound_x-lower_bound_x+1, upper_bound_y-lower_bound_y+1); //Write list of pixels in world coords into this pixel map //Deals with origin location pixel_map = write_points_to_pixel_map(points, num_points, pixel_map, lower_bound_x, upper_bound_y); //Pass pixel map in xpm write do_xpm_write(pixel_map); //Done return 0; }
void prod(int n1, int m1, int n2, int m2) { if (n2 != m1) { printf("Impossimatrix to multiply"); return ; } new_matrix(n1, m2, ptr_out3); int N = n1; int M = m2; //c[i][j] = a[i][k] * b[k][j] for (int i = 0; i < n1; ++i) { for (int j = 0; j < m2; ++j) { double x = 0; for (int k = 0; k < m1; ++k) { x += get_matrix(n1, m1, i, k, ptr_out1) * get_matrix(n2, m2, k, j, ptr_out2) ; } set_matrix(N, M, i, j, x); } } }
eta_file* new_eta_file(int lp_rows) { eta_file* ef; int i; ef = my_malloc(sizeof(eta_file)); ef->lp_rows = lp_rows; ef->k = 0; ef->P = my_malloc(lp_rows*sizeof(int)); ef->L = my_malloc(lp_rows*sizeof(eta_matrix*)); for (i = 0; i < lp_rows; i ++) { ef->L[i] = new_eta_matrix(lp_rows); } ef->Ls = my_malloc(lp_rows*ETA_MAX*sizeof(eta_singleton*)); for (i = 0; i < lp_rows*ETA_MAX; i ++) { ef->Ls[i] = new_eta_singleton(); } ef->L_d = my_malloc(lp_rows*sizeof(eta_matrix_d*)); for (i = 0; i < lp_rows; i ++) { ef->L_d[i] = new_eta_matrix_d(lp_rows); } ef->Ls_d = my_malloc(lp_rows*ETA_MAX*sizeof(eta_singleton_d*)); for (i = 0; i < lp_rows*ETA_MAX; i ++) { ef->Ls_d[i] = new_eta_singleton_d(); } ef->U = new_matrix(lp_rows, lp_rows); /* とりあえずこんだけ */ ef->U_d = (EXLPvector_d**)my_malloc(lp_rows*sizeof(EXLPvector_d*)); for (i = 0; i < lp_rows; i ++) ef->U_d[i] = new_vector_d(lp_rows); ef->Q = new_permutation_matrix(lp_rows); ef->R = new_permutation_matrix(lp_rows); return ef; }
// product of two matricies MATRIX matprod(MATRIX *m, MATRIX *n) { MATRIX mat=new_matrix(m->rows,n->cols); INDEX r, c, i; VALUE v=0; // validate matricies can be multiplied if (n->cols != n->rows) { fprintf(stderr, "matricies cannot be multiplied"); } else{ for(r=0; r<m->rows; r++){ for(c=0; c<n->cols; c++){ for(i=0; i<m->cols; i++){ v=v+get(m,r,i)*get(n,i,c); } set(&mat,r,c,v); } } } return mat; }
/*======== struct matrix * make_hermite()) ========== Inputs: Returns: The correct 4x4 matrix that can be used to generate the coefiecients for a hermite curve 03/16/12 14:36:19 jdyrlandweaver ====================*/ struct matrix * make_hermite() { struct matrix * m = new_matrix(4, 4); m->m[0][0] = 2; m->m[0][1] = -2; m->m[0][2] = 1; m->m[0][3] = 1; m->m[1][0] = -3; m->m[1][1] = 3; m->m[1][2] = -2; m->m[1][3] = -1; m->m[2][0] = 0; m->m[2][1] = 0; m->m[2][2] = 1; m->m[2][3] = 0; m->m[3][0] = 1; m->m[3][1] = 0; m->m[3][2] = 0; m->m[3][3] = 0; return m; }
/*======== 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 * m = new_matrix(1, 4); struct matrix * hermite = make_hermite(); struct matrix * bezier = make_bezier(); m->m[0][0] = p1; m->m[0][0] = p2; m->m[0][0] = p3; m->m[0][0] = p4; if (type == 0) { //Hermite matrix_mult( hermite, m ); } else { //Bezier matrix_mult( bezier, m ); } return m; }
/*======== struct matrix * make_bezier()) ========== Inputs: Returns: The correct 4x4 matrix that can be used to generate the coefiecients for a bezier curve 03/16/12 14:36:19 jdyrlandweaver ====================*/ struct matrix * make_bezier() { struct matrix * temp = new_matrix(4, 4); temp->m[0][0] = -1; temp->m[0][1] = 3; temp->m[0][2] = -3; temp->m[0][3] = 1; temp->m[1][0] = 3; temp->m[1][1] = -6; temp->m[1][2] = 3; temp->m[1][3] = 0; temp->m[2][0] = -3; temp->m[2][1] = 3; temp->m[2][2] = 0; temp->m[2][3] = 0; temp->m[3][0] = 1; temp->m[3][1] = 0; temp->m[3][2] = 0; temp->m[3][3] = 0; return temp; }
/*======== struct matrix * make_hermite()) ========== Inputs: Returns: The correct 4x4 matrix that can be used to generate the coefiecients for a hermite curve 03/16/12 14:36:19 jdyrlandweaver ====================*/ struct matrix * make_hermite() { struct matrix *hInv = new_matrix(4,4); hInv->m[0][0] = 2; hInv->m[0][1] = -2; hInv->m[0][2] = 1; hInv->m[0][3] = 1; hInv->m[1][0] = -3; hInv->m[1][1] = 3; hInv->m[1][2] = -2; hInv->m[1][3] = -1; hInv->m[2][0] = 0; hInv->m[2][1] = 0; hInv->m[2][2] = 1; hInv->m[2][3] = 0; hInv->m[3][0] = 1; hInv->m[3][1] = 0; hInv->m[3][2] = 0; hInv->m[3][3] = 0; return hInv; }
/*======== struct matrix * make_bezier()) ========== Inputs: Returns: The correct 4x4 matrix that can be used to generate the coefiecients for a bezier curve 03/16/12 14:36:19 jdyrlandweaver ====================*/ struct matrix * make_bezier() { struct matrix *b = new_matrix(4,4); b->m[0][0] = -1; b->m[0][1] = 3; b->m[0][2] = -3; b->m[0][3] = 1; b->m[1][0] = 3; b->m[1][1] = -6; b->m[1][2] = 3; b->m[1][3] = 0; b->m[2][0] = -3; b->m[2][1] = 3; b->m[2][2] = 0; b->m[2][3] = 0; b->m[3][0] = 1; b->m[3][1] = 0; b->m[3][2] = 0; b->m[3][3] = 0; return b; }
/*======== 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 * m = new_matrix(4, 4); if (type){ m->m[0][0]=p1; m->m[1][0]=p2; m->m[2][0]=p3; m->m[3][0]=p4; matrix_mult(make_bezier(),m); } else{ m->m[0][0]=p1; m->m[1][0]=p3; m->m[2][0]=p2-p1; m->m[3][0]=p4-p3; matrix_mult(make_hermite(),m); } m->lastcol=1; return m; }
Matrix matrix_cross(Matrix a, Matrix b) //3D column only { if((a.rows != 4) || (b.rows != 4)){ printf("%d %d\n", a.cols, b.cols); printf("Matrix error, cross product only works on 3D column vectors.\n"); exit(EXIT_FAILURE); } Matrix r; r = new_matrix(a.rows, a.cols); double data[4]; data[0] = a.t[1]*b.t[2] - a.t[2]*b.t[1]; data[1] = -1*(a.t[0]*b.t[2]) + (a.t[2]*b.t[0]); data[2] = a.t[0]*b.t[1] - a.t[1]*b.t[0]; data[3] = 1; memcpy(r.t, data, sizeof(data)); return r; }