void init_matrices()
{
	clear_matrix(claim_matrix);
	clear_matrix(allocation_matrix);
	clear_matrix(difference_matrix);
	increment_row_vector(SYSTEM_RES, available_vector);

	return;
}
Example #2
0
/**
 * \brief
 * populate a matrix made of sub-matrices I (identity) and 0 (zero)
 * r is the size of the identity sub-matrix
 * |1 0 0 0|
 * |0 1 0 0|
 * |0 0 0 0|
 * |0 0 0 0|
 * In the above example r = 2
 */
static void init_IO_matrix(gf2matrix *m, int r)
{
	int i;
	clear_matrix(m);
	for (i = 0; i < r; ++i)
		set_bit_at(m, 1, i, i);
}
Example #3
0
// load a matrix from file "n" into matrix A
// returns 0: success, <0 failure
int load_matrix( char* n, matrix_t* A ) {
  assert( A != NULL );
  if ( n == NULL ) {
    fprintf( stderr, "input error: No input specified (-i)\n" );
    return 1; // failure
  }
  // make sure we don't have a memory leak
  clear_matrix(A);

  int ret;
  enum sparse_matrix_file_format_t ext;
  if (( ret = _identify_format_from_extension( n, &ext, 1 ) ) != 0 )
    return ret;

  switch(ext) {
    case MATRIX_MARKET:  ret = readmm( n, A, NULL ); break; // NULL = ignore comments
    case MATLAB:         ret = read_mat( n, A ); break;
    case HARWELL_BOEING: ret = 100; assert(0); break; // shouldn't be able to get here
  }
  if ( ret != 0 ) {
    fprintf( stderr, "input error: Failed to load matrix\n"); // TODO move these printouts to main...
    return ret;
  }

  if(A->sym == SM_UNSYMMETRIC)
    detect_matrix_symmetry( A );

  return 0; // success
}
int main(int argc, char * argv[]) {
    mtype_t * matrix = NULL ;
    int i, MAX_SIZE;
    double avg_time;
    support_init();
    
    MAX_ITERS = 100;
    
    if(argc==2)
        if(atoi(argv[1])>=2)
            MAX_SIZE = atoi(argv[1]);
        else{
        printf("The argument must be an integer and larger than or equal to 2");
        return -1;
        }
    else if(argc>2){
        printf("This program takes one or no arguments, you entered too many.");
        return -1;
    }
    else MAX_SIZE = 1024;
    for ( i = 2; i <= MAX_SIZE; i = i * 2 ) {
        // Allocate a matrix of size : buffer_side x buffer_side
        allocate_matrix (& matrix , i );
        // Clear / Initialize the matrix
        clear_matrix (matrix , i );
        
        avg_time = run_experiment_ij (matrix, 5.0, i);
        printf("The average time to do scalar multiplication ij on a %d x %d matrix is %lf \n", i, i, avg_time);
    }
    printf("------------------------------------------------------------------------------\n");
    for ( i = 2; i <= MAX_SIZE; i = i * 2 ) {
        // Allocate a matrix of size : buffer_side x buffer_side
        allocate_matrix (& matrix , i );
        // Clear / Initialize the matrix
        clear_matrix (matrix , i );
        
        avg_time = run_experiment_ji (matrix, 5.0, i);
        printf("The average time to do scalar multiplication ji on a %d x %d matrix is %lf \n", i, i, avg_time);
    }

    support_finalize();
    
    return 0;
}
Example #5
0
matrix * adjacent_matrix (int x, int y, int e, int *vec)
{
    matrix *adj = new_matrix(x,y);
    clear_matrix(adj);
    int i, j, k, n, i0, j0, i1, j1;
    int dx[4] = {0,0,-1,1};
    int dy[4] = {-1,1,0,0};
    for (i=0; i<2*e; i=i+2)
    {
        n = (vec[i]-1)*y + (vec[i+1]-1);
        for (j=0; j<adj->v; j++)
        {
            adj->data[n][j] = 0;
            adj->data[j][n] = 0;
        }
    }
    for (i=0; i<adj->v; i++)
    {
        adj->data[i][i] = 0;
        i0 = i/y;
        j0 = i%y;
        for (k=0; k<4; k++)
        {
            i1 = i0 + dx[k];
            j1 = j0 + dy[k];
            n = i1*y + j1;
            if (adj->data[i][n]!=0 && (i1>=0 && i1<x)&&(j1>=0 && j1<y)!=0)
            {
                adj->data[i][n] = 1;
            }
        }
        for (j=0; j<adj->v; j++)
        {
            if (adj->data[i][j]==-1)
            {
                adj->data[i][j] = 0;
            }
        }
    }
    return adj;
}
Example #6
0
/* all cpi and result must be aligned (have the same number of xforms,
   and have final xform in the same slot) */
void flam3_interpolate_n(flam3_genome *result, int ncp,
          flam3_genome *cpi, double *c, double stagger) {
   int i, j, k, numstd;
   
   if (flam3_palette_interpolation_hsv == cpi[0].palette_interpolation) {
   
      for (i = 0; i < 256; i++) {
         double t[3], s[4];
         s[0] = s[1] = s[2] = s[3] = s[4] = 0.0;
         
         for (k = 0; k < ncp; k++) {
            rgb2hsv(cpi[k].palette[i].color, t);
            for (j = 0; j < 3; j++)
               s[j] += c[k] * t[j];
            
            s[3] += c[k] * cpi[k].palette[i].color[3];
            s[4] += c[k] * cpi[k].palette[i].index;
            
         }
       
         hsv2rgb(s, result->palette[i].color);
         result->palette[i].color[3] = s[3];
         result->palette[i].index = s[4];
       
         for (j = 0; j < 4; j++) {
            if (result->palette[i].color[j] < 0.0)
               result->palette[i].color[j] = 0.0;
            if (result->palette[i].color[j] > 1.0)
               result->palette[i].color[j] = 1.0;
         }
         
         if (result->palette[i].index < 0.0)
            result->palette[i].index = 0.0;
         if (result->palette[i].index > 255.0)
            result->palette[i].index = 255.0;
      }
   } else {
      /* Sweep - not the best option for float indices */
      for (i = 0; i < 256; i++) {
         j = (i < (256 * c[0])) ? 0 : 1;
         result->palette[i] = cpi[j].palette[i];
      }
   }

   result->palette_index = flam3_palette_random;
   result->symmetry = 0;
   result->spatial_filter_select = cpi[0].spatial_filter_select;
   result->temporal_filter_type = cpi[0].temporal_filter_type;
   result->palette_mode = cpi[0].palette_mode;

   result->interpolation_type = cpi[0].interpolation_type;
   INTERP(brightness);
   INTERP(contrast);
   INTERP(highlight_power);
   INTERP(gamma);
   INTERP(vibrancy);
   INTERP(hue_rotation);
   INTERI(width);
   INTERI(height);
   INTERI(spatial_oversample);
   INTERP(center[0]);
   INTERP(center[1]);
   INTERP(rot_center[0]);
   INTERP(rot_center[1]);
   INTERP(background[0]);
   INTERP(background[1]);
   INTERP(background[2]);
   INTERP(pixels_per_unit);
   INTERP(spatial_filter_radius);
   INTERP(temporal_filter_exp);
   INTERP(temporal_filter_width);
   INTERP(sample_density);
   INTERP(zoom);
   INTERP(rotate);
   INTERI(nbatches);
   INTERI(ntemporal_samples);
   INTERP(estimator);
   INTERP(estimator_minimum);
   INTERP(estimator_curve);
   INTERP(gam_lin_thresh);
   
   /* Interpolate the chaos array */
   numstd = cpi[0].num_xforms - (cpi[0].final_xform_index >= 0);
   for (i=0;i<numstd;i++) {
      for (j=0;j<numstd;j++) {
         INTERP(chaos[i][j]);
         if (result->chaos[i][j]<0) result->chaos[i][j]=0;
         //chaos can be > 1
         //if (result->chaos[i][j]>1) result->chaos[i][j]=1.0;
      }
   }

   /* Interpolate each xform */
   for (i = 0; i < cpi[0].num_xforms; i++) {
   
      double csave[2];     
      double td;
      int all_id;
      int nx = cpi[0].num_xforms-(cpi[0].final_xform_index>=0);
      
      if (ncp==2 && stagger>0 && i!=cpi[0].final_xform_index) {
         csave[0] = c[0];
         csave[1] = c[1];
         c[0] = get_stagger_coef(csave[0],stagger,nx,i);
         c[1] = 1.0-c[0];
      }
      
      
      INTERP(xform[i].density);
      td = result->xform[i].density;
      result->xform[i].density = (td < 0.0) ? 0.0 : td;
      INTERP(xform[i].color);
      if (result->xform[i].color<0) result->xform[i].color=0;
      if (result->xform[i].color>1) result->xform[i].color=1;
      
      INTERP(xform[i].opacity);      
      INTERP(xform[i].color_speed);
      INTERP(xform[i].animate);
      INTERP(xform[i].blob_low);
      INTERP(xform[i].blob_high);
      INTERP(xform[i].blob_waves);
      INTERP(xform[i].pdj_a);
      INTERP(xform[i].pdj_b);
      INTERP(xform[i].pdj_c);
      INTERP(xform[i].pdj_d);
      INTERP(xform[i].fan2_x);
      INTERP(xform[i].fan2_y);
      INTERP(xform[i].rings2_val);
      INTERP(xform[i].perspective_angle);
      INTERP(xform[i].perspective_dist);
      INTERP(xform[i].julian_power);
      INTERP(xform[i].julian_dist);
      INTERP(xform[i].juliascope_power);
      INTERP(xform[i].juliascope_dist);
      INTERP(xform[i].radial_blur_angle);
      INTERP(xform[i].pie_slices);
      INTERP(xform[i].pie_rotation);
      INTERP(xform[i].pie_thickness);
      INTERP(xform[i].ngon_sides);
      INTERP(xform[i].ngon_power);
      INTERP(xform[i].ngon_circle);
      INTERP(xform[i].ngon_corners);
      INTERP(xform[i].curl_c1);
      INTERP(xform[i].curl_c2);
      INTERP(xform[i].rectangles_x);
      INTERP(xform[i].rectangles_y);
      INTERP(xform[i].amw_amp);
      INTERP(xform[i].disc2_rot);
      INTERP(xform[i].disc2_twist);
      INTERP(xform[i].super_shape_rnd);
      INTERP(xform[i].super_shape_m);
      INTERP(xform[i].super_shape_n1);
      INTERP(xform[i].super_shape_n2);
      INTERP(xform[i].super_shape_n3);
      INTERP(xform[i].super_shape_holes);
      INTERP(xform[i].flower_petals);
      INTERP(xform[i].flower_holes);
      INTERP(xform[i].conic_eccentricity);
      INTERP(xform[i].conic_holes);
      INTERP(xform[i].parabola_height);
      INTERP(xform[i].parabola_width);
      INTERP(xform[i].bent2_x);
      INTERP(xform[i].bent2_y);
      INTERP(xform[i].bipolar_shift);
      INTERP(xform[i].cell_size);
      INTERP(xform[i].cpow_r);
      INTERP(xform[i].cpow_i);
      INTERP(xform[i].cpow_power);
      INTERP(xform[i].curve_xamp);
      INTERP(xform[i].curve_yamp);
      INTERP(xform[i].curve_xlength);
      INTERP(xform[i].curve_ylength);
      INTERP(xform[i].escher_beta);
      INTERP(xform[i].lazysusan_x);
      INTERP(xform[i].lazysusan_y);
      INTERP(xform[i].lazysusan_twist);
      INTERP(xform[i].lazysusan_space);
      INTERP(xform[i].lazysusan_spin);
      INTERP(xform[i].modulus_x);
      INTERP(xform[i].modulus_y);
      INTERP(xform[i].oscope_separation);
      INTERP(xform[i].oscope_frequency);
      INTERP(xform[i].oscope_amplitude);
      INTERP(xform[i].oscope_damping);
      INTERP(xform[i].popcorn2_x);
      INTERP(xform[i].popcorn2_y);
      INTERP(xform[i].popcorn2_c);
      INTERP(xform[i].separation_x);
      INTERP(xform[i].separation_xinside);
      INTERP(xform[i].separation_y);
      INTERP(xform[i].separation_yinside);
      INTERP(xform[i].split_xsize);
      INTERP(xform[i].split_ysize);
      INTERP(xform[i].splits_x);
      INTERP(xform[i].splits_y);
      INTERP(xform[i].stripes_space);
      INTERP(xform[i].stripes_warp);
      INTERP(xform[i].wedge_angle);
      INTERP(xform[i].wedge_hole);
      INTERP(xform[i].wedge_count);
      INTERP(xform[i].wedge_swirl);
      INTERP(xform[i].wedge_julia_angle);
      INTERP(xform[i].wedge_julia_count);
      INTERP(xform[i].wedge_julia_power);
      INTERP(xform[i].wedge_julia_dist);
      INTERP(xform[i].wedge_sph_angle);
      INTERP(xform[i].wedge_sph_hole);
      INTERP(xform[i].wedge_sph_count);
      INTERP(xform[i].wedge_sph_swirl);
      INTERP(xform[i].whorl_inside);
      INTERP(xform[i].whorl_outside);
      INTERP(xform[i].waves2_scalex);
      INTERP(xform[i].waves2_scaley);
      INTERP(xform[i].waves2_freqx);
      INTERP(xform[i].waves2_freqy);

      for (j = 0; j < flam3_nvariations; j++)
         INTERP(xform[i].var[j]);

      if (flam3_inttype_log == cpi[0].interpolation_type) {
         double cxmag[4][2];  // XXX why only 4? should be ncp
         double cxang[4][2];
         double cxtrn[4][2];

         /* affine part */
         clear_matrix(result->xform[i].c);
         convert_linear_to_polar(cpi,ncp,i,0,cxang,cxmag,cxtrn);
         interp_and_convert_back(c, ncp, i, cxang, cxmag, cxtrn,result->xform[i].c);

         /* post part */
         all_id = 1;
         for (k=0; k<ncp; k++)
            all_id &= id_matrix(cpi[k].xform[i].post);
         
         clear_matrix(result->xform[i].post);
         if (all_id) {
            result->xform[i].post[0][0] = 1.0;
            result->xform[i].post[1][1] = 1.0;
         } else {
            convert_linear_to_polar(cpi,ncp,i,1,cxang,cxmag,cxtrn);
            interp_and_convert_back(c, ncp, i, cxang, cxmag, cxtrn,result->xform[i].post);
         }         
         
      } else {

         /* Interpolate c matrix & post */
         clear_matrix(result->xform[i].c);
         clear_matrix(result->xform[i].post);
         all_id = 1;
         for (k = 0; k < ncp; k++) {
            sum_matrix(c[k], cpi[k].xform[i].c, result->xform[i].c);
            sum_matrix(c[k], cpi[k].xform[i].post, result->xform[i].post);

            all_id &= id_matrix(cpi[k].xform[i].post);

         }
         if (all_id) {
            clear_matrix(result->xform[i].post);
            result->xform[i].post[0][0] = 1.0;
            result->xform[i].post[1][1] = 1.0;
         }
      }
      
      if (ncp==2 && stagger>0 && i!=cpi[0].final_xform_index) {
         c[0] = csave[0];
         c[1] = csave[1];
      }
      
   }
   
}
Example #7
0
/**
 * \brief
 * return the A(bitsxbits) matrix corresponding to the given rank
 * see Generating Large Non-Singular ...; Jmes Xiao, section 2.
 * @param p number of bits
 * @param r rank
 * @return a matrix A of dims (pxp)
 *
 * For a 2x2 we have:
 *  r=0    r=1    r=2
 * |1 0|  |1 1|  |0 1|
 * |0 1|  |1 0|  |1 1|
 *
 * In general:
 * 1. r = 0 A = I(p,p)
 * 2. r is even:
 * 		 	    | |0 1|                        |
 *              | |1 1|                        |
 *              |      ...(r/2 times           |
 *      A =     |          diagonally)         |
 *              |                              |
 *              |                   I(p-r,p-r) |
 *
 * 3. r is 1:
 * 		 	    | |1 1|           |
 *     A =      | |1 0|           |
 *              |      I(p-2,p-2) |
 *
 * 4. r is odd and > 1:
 *              | |1 1 1|                      |
 *              | |1 1 0|                      |
 *              | |1 0 0|                      |
 * 		 	    |       |0 1|                  |
 *     A =      |       |1 1|                  |
 *              |            ...(n times       |
 *              |            diagonally)       |
 *              |                              |
 *              |                 I(p-2n,p-2n) |
 *    where n = (r - 3) / 2
 */
static gf2matrix *make_A_matrix(int p, int r)
{
	gf2matrix *a;
	if (r == 0) {
		a = make_identity_matrix(p);
	}
	else if (r == 1) {
		gf2matrix *I = make_identity_matrix(p - 2);
		gf2matrix *block = new_matrix(2, 2);
		scalar2matrix(block, 0xE, 4);
		a = new_matrix(p, p);
		clear_matrix(a);
		copy_matrix_to_offset(a, I, 2, 2);
		copy_matrix_to_offset(a, block, 0, 0);
		free_matrix(block);
		free_matrix(I);
	}
	else if ((r % 2) == 0) {
		int i;
		gf2matrix *I = NULL;
		gf2matrix *block = new_matrix(2, 2);
		if (p > r)
			I = make_identity_matrix(p - r);
		scalar2matrix(block, 0xE, 4);
		a = new_matrix(p, p);
		clear_matrix(a);
		if (I)
			copy_matrix_to_offset(a, I, r, r);
		for (i = 0; i < r; i += 2)
			copy_matrix_to_offset(a, block, i, i);
		free_matrix(block);
		free_matrix(I);
	}
	else /*  r is odd and > 1 */
	{
		int i;
		int n;
		gf2matrix *I = NULL;
		gf2matrix *block = NULL;
		n = (r - 3) / 2;
		if (n = 0) {
			a = make_identity_matrix(p);
		}
		else {
			int p_2n = p - (2 * n);
			block = new_matrix(2, 2);
			if (p > r)
				I = make_identity_matrix(p - r);
			scalar2matrix(block, 0xE, 4);
			a = new_matrix(p, p);
			clear_matrix(a);
			if (I)
				copy_matrix_to_offset(a, I, r, r);
			for (i = 0; i < n; ++i) {
				int offset = (i * 2) + 3;
				copy_matrix_to_offset(a, block, offset, offset);
			}
			set_bit_at(a, 1, 0, 0);
			set_bit_at(a, 1, 0, 1);
			set_bit_at(a, 1, 0, 2);
			set_bit_at(a, 1, 1, 1);
			set_bit_at(a, 1, 1, 0);
			set_bit_at(a, 1, 2, 0);
		}
		free_matrix(block);
		free_matrix(I);
	}
	/* print_matrix(a, "A:"); */
	return a;
}
Example #8
0
int readmm(char const *const filename, matrix_t *const A, char** comments) {
  // references:
  // [1] http://math.nist.gov/MatrixMarket/formats.html
  // [2] The Matrix Market Exchange Formats: Initial Design,
  //     R. F. Boisvert, R. Pozo, K. Remington,
  //     Applied and Computational Mathematics Division, NIST
  //     http://math.nist.gov/MatrixMarket/reports/MMformat.ps

  // MatrixMarket format notes:
  // sparse coordinate format: COO
  // dense format: column-oriented (DCOL)
  //   TODO to express a preference for DROW format, set that in the input matrix, 'A'
  // type: real, complex, integer, pattern
  // symmetry: general, symmetric, skew-symmetric, Hermitian
  //   for symmetric types only the *lower triangular* portion is stored
  //   for skew-symmetric matrices, diagonal is zero, so left out too

  // require "%%MatrixMarket " as first 15 characters
  // "%%MatrixMarket <object> <format> [qualifier ...]"
  // object could be vector, matrix, directed graph
  // format type is the storage format (coordinate, array)
  // qualifiers are fields, symmetry, etc
  // data is one entry per line
  // additional restrictions:
  //  * lines are limited 1024 characters
  //  * blanks lines can appear in any line after the first
  //  * data is separated by one or more "blanks" (spaces?)
  //  * real data is floating-point decimal, optionally use E-format exponential
  //  * all indices are 1-based
  //  * text is case-insensitive

  // expect sparse:
  // "%%MatrixMarket matrix coordinate <datatype> <sym>"
  // "%<comments>" -- zero or more lines
  // "  <rows> <columns> <non-zeros>"
  // "  <row> <col> <real>" -- indices are base-1, not base-0 REAL
  // "  <row> <col> <real> <imag>" -- indices are base-1, not base-0 COMPLEX


  // expect dense:
  // "%%MatrixMarket matrix array <type> <sym>"
  // "%<comments>" -- zero or more lines
  // "<rows> <columns>"
  // "<real>" -- REAL, column major order
  // "<real> <imag>" -- COMPLEX

  // additional restrictions: (kind of common-sense)
  //  * for coordinate AND array, "Hermitian" matrix types can only be "complex"
  //  * pattern matrices can only be coordinate format, and general or symmetric

  // <format> = coordinate, array
  // <datatype> = real, integer, complex, pattern
  // <sym> = general, symmetric, skew-symmetric, hermitian

  // Symmetry
  // general: no symmetry
  // symmetric A(i,j) = A(j,i)       (on or below diagonal) (square matrices)
  // skew-symmetric A(i,j) = -A(j,i)       (below diagonal) (square matrices)
  // hermitian      A(i,j) = A(j,i)* (on or below diagonal) (square matrices)

  // extensions:
  // * structured comments
  // * format specializations (pretty printing?)
  // * new object and format types
  //    <object> = graphs, grids, vectors?
  //    <format> = elemental (FEM), band, Toeplitz? (50% less storage or keen interest)

  // example structured comments
  //  "%%Harwell-Boeing collection"
  //  "%"
  //  "%HB FILE_NAME   abcd.mtx"
  //  "%HB KEY         MMEXMPL1"
  //  "%HB OBJECT      matrix"
  //  "%HB FORMAT      coordinate"
  //  "%HB QUALIFIRES  real general"
  //  "%HB DESCRIPTION Unsymmetric matrix from example"
  //  "%HB CONTRIBUTOR R. Boisvert ([email protected])"
  //  "%HB DATE        June 24, 1996"
  //  "%HB PRECISION   4"
  //  "%HB DATA_LINES  9"
  //  "%"
  //  "% some more comments"

  // TODO could have verbose documentation about the format inside the file as comments

  FILE* f = fopen(filename, "r");
  if(f == NULL)
    return -2;


  int object, format, datatype, symmetry, rows, cols, nz;
  int ret;
  // NULL = ignoring comments
  ret = readmm_header(f, &object, &format, &datatype, &symmetry, &rows, &cols, &nz, NULL);
  if(ret < 0) {
    fclose(f);
    return ret -10;
  }
  // output object    is -1 unrecognized, 0 matrix
  //        format    is -1 unrecognized, 0 array, 1 coordinate
  //        datatype  is -1 unrecognized, 0 real, 1 integer, 2 complex, 3 pattern
  //        symmetry  is -1 unrecognized, 0 general, 1 symmetric, 2 skew-symmetric, 3 hermitian
  //        rows, cols, nz are dimensions of the matrix
  //        comments are malloc-ed copy of the comments from the file

  // check we got the expected object type == matrix
  if(object != 0) {
    fclose(f);
    return -3;
  }

  clear_matrix(A);
  A->m = rows;
  A->n = cols;
  A->nz = nz;
  A->base = FIRST_INDEX_ONE;
  A->location = LOWER_TRIANGULAR; // TODO test for this at the end and report error
  switch(format) {
    case 0: // array
      ret = readmm_data_dense(f, A, datatype, symmetry, rows, cols, nz);
      break;
    case 1: // COO
      ret = readmm_data_sparse(f, A, datatype, symmetry, rows, cols, nz);
      break;
    default:
      ret = -4;
  }

  fclose(f);
  return ret;
}
Example #9
0
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 );
}  
Example #10
0
File: f4.cpp Project: pzinn/M2
void F4GB::do_spairs()
{
  if (hilbert && hilbert->nRemainingExpected() == 0)
    {
      if (M2_gbTrace >= 1)
        fprintf(stderr, "-- skipping degree...no elements expected in this degree\n");
      return;
    }
  reset_matrix();
  reset_syz_matrix();
  clock_t begin_time = clock();

  n_lcmdups = 0;
  make_matrix();

  if (M2_gbTrace >= 5) {
    fprintf(stderr, "---------\n");
    show_matrix();
    fprintf(stderr, "---------\n");
  }

  clock_t end_time = clock();
  clock_make_matrix += end_time - begin_time;
  double nsecs = static_cast<double>(end_time - begin_time);
  nsecs /= CLOCKS_PER_SEC;
  if (M2_gbTrace >= 2)
    fprintf(stderr, " make matrix time = %f\n", nsecs);

  if (M2_gbTrace >= 2)
    H.dump();

  begin_time = clock();
  gauss_reduce(true);
  end_time = clock();
  clock_gauss += end_time - begin_time;

  //  fprintf(stderr, "---------\n");
  //  show_matrix();
  //  fprintf(stderr, "---------\n");

  nsecs = static_cast<double>(end_time - begin_time);
  nsecs /= CLOCKS_PER_SEC;
  if (M2_gbTrace >= 2)
    {
      fprintf(stderr, " gauss time          = %f\n", nsecs);

      fprintf(stderr, " lcm dups            = %ld\n", n_lcmdups);
      if (M2_gbTrace >= 5)
        {
          fprintf(stderr, "---------\n");
          show_matrix();
          fprintf(stderr, "---------\n");
          show_syz_matrix();
          //  show_new_rows_matrix();
        }
    }
  new_GB_elements();
  int ngb = INTSIZE(gb);
  if (M2_gbTrace >= 1) {
    fprintf(stderr, " # GB elements   = %d\n", ngb);
    if (M2_gbTrace >= 5) show_gb_array(gb);
    if (using_syz)
      fprintf(stderr, " # syzygies      = %ld\n", static_cast<long>(syz_basis.size()));
    if (M2_gbTrace >= 5) show_syz_basis();
  }

  clear_matrix();
  clear_syz_matrix();
}
Example #11
0
// broadcast a matrix A to all nodes in the MPI communicator 'comm'
// MPI node 'root' intially holds the original matrix
// afterwards, all nodes hold the whole matrix 'A'
// return: MPI_SUCCESS on success, MPI_ERR_* on failure (see Mpi_Bcast)
int matrix_bcast(matrix_t* A, int root, MPI_Comm comm) {
  assert(A != NULL);

  // get communicator info
  int ret;
  int myrank, size;
  ret = MPI_Comm_rank(comm, &myrank);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Comm_size(comm, &size);
  assert(ret == MPI_SUCCESS);
  if(size <= 1)
    return ret; // nothing to do

  // check our root node is within the communicator
  assert(root < size); // root to big for communicator
  if(myrank == root) {
    assert(A->format == SM_CSC); // can only handle CSC format for now
    assert(A->data_type == REAL_DOUBLE); // con only handle double
  }

  // make sure we aren't going to lose any memory
  if(myrank != root)
    clear_matrix(A);

  // send round one: sizes for malloc
  // TODO this could be more efficient if sending all three, then waiting for completion
  // TODO return an error rather than aborting
  ret = MPI_Bcast(&(A->m),         1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->n),         1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->nz),        1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->base),      1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->format),    1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->sym),       1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->location),  1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(&(A->data_type), 1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);

  // allocate memory where required
  if(myrank != root) {
    // TODO refactor: this could be a generic matrix_realloc(A, m_new, n_new, nz_new)
    // assuming CSC format
    A->ii = malloc(A->nz * sizeof(unsigned int)); // row indices
    assert(A->ii != NULL); // TODO malloc error
    A->jj = malloc(((A->n)+1) * sizeof(unsigned int)); // col ptrs
    assert(A->jj != NULL); // TODO malloc error
    A->dd = malloc(A->nz * sizeof(double)); // data
    assert(A->dd != NULL); // TODO malloc error
  }

  // send round two: data
  ret = MPI_Bcast(A->ii, A->nz, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(A->jj, (A->n) +1, MPI_INT, root, comm);
  assert(ret == MPI_SUCCESS);
  ret = MPI_Bcast(A->dd, A->nz, MPI_DOUBLE, root, comm);
  assert(ret == MPI_SUCCESS);

  return ret;
}
Example #12
0
/*======== 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 = 100;
  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, w, h, d,step,r1,r2, cx,cy,r;
   
    
    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, "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, "box", strlen(line)) == 0 ){
      printf("%s", line);
      fgets(line, 255, f);
      printf("%s", line);
      sscanf(line, "%lf %lf %lf %lf %lf %lf ", &x, &y, &z, &w, &h, &d);
      add_box( pm, x,y,z,w,h,d);
    }
    else if (strncmp(line, "sphere", strlen(line)) == 0 ){
      printf("%s", line);
      fgets(line, 255, f);
      printf("%s\n", line);
      sscanf(line, "%lf %lf %lf %lf ", &cx, &cy, &r, &step);
      add_sphere( pm, cx,  cy, r, step);
    }
    else if (strncmp(line, "torus", strlen(line)) == 0 ){
      //printf("%s", line);
      fgets(line, 255, f);
      //printf("%s", line);
      sscanf(line, "%lf %lf %lf %lf %lf ", &cx, &cy, &r1, &r2, &step);
      add_torus( pm, cx,cy,r1,r2,step);
    } 
    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, "quit", strlen(line)) == 0 ) {
      return;
    }
    else if ( strncmp(line, "#", 1 ) == 0 ){
      printf(" Comment \n");
    }
    else if ( strncmp(line, "clear", strlen(line)) == 0 ){
      clear_matrix( pm );
      printf("Clear? What's that?\n");
    }
    else {
      printf("Invalid command: [%s]\n", line);
      //printf("%c\n", line[0]);
    }
  }
  
  free_matrix(tmp);
  fclose(f);
  //printf("END PARSE\n");
}