Exemple #1
0
double do_work(void) {
  double **a,           /* matrix A to be multiplied */
         **b,           /* matrix B to be multiplied */
         **c;           /* result matrix C */
  a = allocateMatrix(NRA, NCA);
  b = allocateMatrix(NCA, NCB);
  c = allocateMatrix(NRA, NCB);

  /*** Spawn a parallel region explicitly scoping all variables ***/

  initialize(a, NRA, NCA);
  initialize(b, NCA, NCB);
  initialize(c, NRA, NCB);

  compute(a, b, c, NRA, NCA, NCB);
  compute_interchange(a, b, c, NRA, NCA, NCB);

  double result = c[0][1];

  freeMatrix(a, NRA, NCA);
  freeMatrix(b, NCA, NCB);
  freeMatrix(c, NCA, NCB);

  return result;
}
Exemple #2
0
void matrixSquareRoot(Matrix* A, Matrix* S)
{
  //Approach is to do an SVD of A, then set S to V * sqrt(Sigma) * Vtransposed
  int i, j;

  Matrix* U = allocateMatrix(A->rows, A->columns);
  Matrix* Vtransposed = allocateMatrix(A->rows, A->columns);
  Matrix* Sigma = allocateMatrix(A->rows, A->columns);

  //First do an SVD
  singularValueDecomposition(A, U, Vtransposed, Sigma);

  //Now calculate sqrt(Sigma) * Vtransposed and stick it in U
  for(i = 0; i < Sigma->rows; i++)
  {
    for(j = 0; j < Sigma->columns; j++)
    {
      U->pointer[i + j * U->rows] = Vtransposed->pointer[i + j * Vtransposed->rows] * sqrt(Sigma->pointer[i + i * Sigma->rows]);
    }
  }

  multiplyMatrices(Vtransposed, U, S, 1, 0);

  freeMatrix(U);
  freeMatrix(Vtransposed);
  freeMatrix(Sigma);
}
Exemple #3
0
void levelSet3Dfast(MATRIXD im, MATRIX init_mask, int max_its, double E, double T, double alpha, 
					MATRIX *seg0, MATRIX *tmap, MATRIXD *phi, long **ls_vols, int offset){
	long dx = im.dx, dy = im.dy, dz = im.dz;
	int *px, *py, *pz, *qx, *qy, *qz, k, tmap_it = 1;
	MATRIX intTmp;
	MATRIX intTmp2 = allocateMatrix(dx,dy,dz);	// add a new temporary matrix
	MATRIXD dblTmp = allocateMatrixD(dx,dy,dz);	
	MATRIXD gradPhi = allocateMatrixD(dx,dy,dz);
	int minx, maxx, miny, maxy, minz, maxz;
	
	*phi = allocateMatrixD(dx,dy,dz);
	*seg0 = allocateMatrix(dx,dy,dz);
	*tmap = allocateMatrix(dx,dy,dz);
	*ls_vols = (long *)calloc(max_its,sizeof(long)); // initialized with 0
	
	initializeMatrix(tmap,0);
	initializeMatrix(seg0,0);		// initialized with 0
	initializeMatrix(&intTmp2,0);	// initialized with 0
	initializeGlobals4bwdist(dx,dy,dz,&px,&py,&pz,&qx,&qy,&qz,&intTmp);
	
	createSignedDistanceMap(init_mask,phi,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: intTmp, dblTmp

	findLimits(init_mask,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
	
	for (k = 1; k <= max_its; k++){
		calculateCurvature(*phi,&dblTmp,minx,maxx,miny,maxy,minz,maxz); // dblTmp keeps the curvatures
		calculateF(im,dblTmp,&dblTmp,E,T,alpha,minx,maxx,miny,maxy,minz,maxz); // first dblTmp (input): curvatures, second dblTmp (output): F values
		calculateGradPhi(*phi,dblTmp,&gradPhi,minx,maxx,miny,maxy,minz,maxz);
		evolveCurve(phi,dblTmp,gradPhi,minx,maxx,miny,maxy,minz,maxz);
		
		if (k % 50 == 0)
			reinitializeDistanceFunction(phi,init_mask,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: init_mask, intTmp, dblTmp
		
		(*ls_vols)[k-1] = selectNonpositive(*phi,&intTmp2,minx,maxx,miny,maxy,minz,maxz); // use the second temporarymatrix
		
		if (k == 1){
			selectNonpositive(*phi,seg0,minx,maxx,miny,maxy,minz,maxz);
			copyMatrixWithLimits(tmap,*seg0,minx,maxx,miny,maxy,minz,maxz);
			findLimits(*tmap,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
		}
		else if (k % 10 == 0){
			selectNonpositive(*phi,&intTmp2,minx,maxx,miny,maxy,minz,maxz);			// use the second temporarymatrix
			updateMaps(tmap,intTmp2,*seg0,tmap_it,minx,maxx,miny,maxy,minz,maxz);	// use the second temporarymatrix
			copyMatrixWithLimits(seg0,intTmp2,minx,maxx,miny,maxy,minz,maxz);		// use the second temporarymatrix
			tmap_it++;
			
			findLimits(*tmap,offset,&minx,&maxx,&miny,&maxy,&minz,&maxz);
		}
	}
	selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);
	//selectNonpositive(*phi,seg0,minx,maxx,miny,maxy,minz,maxz); ???? (consider this modification if there are more problems)
	
	freeMatrixD(gradPhi);
	freeMatrixD(dblTmp);
	freeMatrix(intTmp2);
	freeGlobals4bwdist(px,py,pz,qx,qy,qz,intTmp);
}
Exemple #4
0
/*---------------------------------------------------------------------------
 *
 * Compute matrix product using recursive tiling.
 *
 * Input
 *   int argc        - length of argv[] array
 *   char* argv[]    - pointer to command line parameter array
 *   int verbosity   - program verification: verbosity > 0 gives more output
 *   char* order     - string indicating loop order, e.g., "ijk" or "jki"
 *
 * Output
 *   double          - elapsed time for product computation
 */
double multiply_by_recursive_blocks( int argc, char* argv[], int verbosity,
                                     char* order )
{
    int rows, cols, mids, block_size;
    double **a, **b, **c;
    double t1, t2;
    double sec;
    double gflop_count;

    /*
     * process command line arguments
     */
    rows = atoi( argv[0] );
    mids = atoi( argv[1] );
    cols = atoi( argv[2] );
    block_size = atoi( argv[3] );
    gflop_count = 2.0 * rows * mids * cols / 1.0e9;

    if ( verbosity > 0 )
    {
        printf( "Recursive blocks(%3s): rows = %d, mids = %d, columns = %d\n",
                order, rows, mids, cols );
        printf( "block size = %d\n", block_size );
    }

    /*
     * allocate and initialize matrices
     */
    a = (double**) allocateMatrix( rows, mids );
    b = (double**) allocateMatrix( mids, cols );
    c = (double**) allocateMatrix( rows, cols );
    initialize_matrices( a, b, c, rows, cols, mids, verbosity );

    /*
     * compute product
     */
    t1 = wtime();
    mm_rec( c, a, b, 0, 0, 0, 0, 0, 0, rows, mids, cols, cols, block_size );
    t2 = wtime();
    sec = t2 - t1;

    if ( verbosity > 1 )
        printf( "checksum = %f\n", checksum( c, rows, cols ) );

    printf( "blocks(%3s): %6.3f secs %6.3f gflops ",
            order, sec, gflop_count / sec );
    printf( "( %5d x %5d x %5d ) ( %6d )\n", rows, mids, cols, block_size );

    /*
     * clean up
     */
    deallocateMatrix( a );
    deallocateMatrix( b );
    deallocateMatrix( c );

    return t2 - t1;
}
Exemple #5
0
void levelSet3D(MATRIXD im, MATRIX init_mask, int max_its, double E, double T, double alpha, 
				MATRIX *seg0, MATRIX *tmap, MATRIXD *phi, long **ls_vols){
	// some of these variables will be used as the local variables for other functions
	// they are defined outside (like global variables) because we want to decrease the overhead
	// corresponding to allocation and deallocation (since we have multiple calls for the same function
	long dx = im.dx, dy = im.dy, dz = im.dz;
	int *px, *py, *pz, *qx, *qy, *qz, k, tmap_it = 1;
	MATRIX intTmp;
	MATRIXD dblTmp = allocateMatrixD(dx,dy,dz);	
	MATRIXD gradPhi = allocateMatrixD(dx,dy,dz);
	
	*phi = allocateMatrixD(dx,dy,dz);
	*seg0 = allocateMatrix(dx,dy,dz);
	*tmap = allocateMatrix(dx,dy,dz);
	*ls_vols = (long *)calloc(max_its,sizeof(long)); // initialized with 0
	
	initializeMatrix(tmap,0);
	initializeGlobals4bwdist(dx,dy,dz,&px,&py,&pz,&qx,&qy,&qz,&intTmp);

	createSignedDistanceMap(init_mask,phi,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: intTmp, dblTmp

	for (k = 1; k <= max_its; k++){
		calculateCurvature(*phi,&dblTmp,0,dx-1,0,dy-1,0,dz-1); // dblTmp keeps the curvatures
		calculateF(im,dblTmp,&dblTmp,E,T,alpha,0,dx-1,0,dy-1,0,dz-1); // first dblTmp (input): curvatures, second dblTmp (output): F values
		calculateGradPhi(*phi,dblTmp,&gradPhi,0,dx-1,0,dy-1,0,dz-1);
		evolveCurve(phi,dblTmp,gradPhi,0,dx-1,0,dy-1,0,dz-1);
		
		if (k % 50 == 0)
			reinitializeDistanceFunction(phi,init_mask,px,py,pz,qx,qy,qz,intTmp,dblTmp); // temps: init_mask, intTmp, dblTmp
		
		(*ls_vols)[k-1] = selectNonpositive(*phi,&intTmp,0,dx-1,0,dy-1,0,dz-1);
		
		if (k == 1){
			selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);
			copyMatrix(tmap,*seg0);
		}
		else if (k % 10 == 0){
			selectNonpositive(*phi,&intTmp,0,dx-1,0,dy-1,0,dz-1);
			updateMaps(tmap,intTmp,*seg0,tmap_it,0,dx-1,0,dy-1,0,dz-1);
			copyMatrix(seg0,intTmp);
			tmap_it++;
		}
	}
	selectNonpositive(*phi,seg0,0,dx-1,0,dy-1,0,dz-1);

	freeMatrixD(gradPhi);
	freeMatrixD(dblTmp);
	freeGlobals4bwdist(px,py,pz,qx,qy,qz,intTmp);
}
void imfillSobelPrimitives(MATRIX *sb, int minx, int maxx, int miny, int maxy){
	MATRIX res = allocateMatrix(sb->row,sb->column);
	int maxNo, *reached, i, j;
	
	maxNo = connectedComponentsWithMask(*sb,0,minx,maxx,miny,maxy,0,FOUR,&res);
	reached = (int *)calloc(maxNo+1,sizeof(int));
	for (i = minx; i <= maxx; i++){
		if (res.data[i][miny] >= 0)
			reached[ res.data[i][miny] ] = 1;
		if (res.data[i][maxy] >= 0)
			reached[ res.data[i][maxy] ] = 1;
	}
	for (i = miny; i <= maxy; i++){
		if (res.data[minx][i] >= 0)
			reached[ res.data[minx][i] ] = 1;
		if (res.data[maxx][i] >= 0)
			reached[ res.data[maxx][i] ] = 1;
	}
	
	for (i = minx; i <= maxx; i++)
		for (j = miny; j <= maxy; j++)
			if ((sb->data[i][j] == 0) && (reached[ res.data[i][j] ] == 0))
				sb->data[i][j] = 1;
	free(reached);
	freeMatrix(res);
}
void eliminateVerticalSmallPrimitives(MATRIX *sb, int minx, int maxx, int miny, int maxy, int wthr){	// sb --> 0 or 1
	MATRIX res = allocateMatrix(sb->row,sb->column);
	int maxNo = connectedComponentsWithMask(*sb,1,minx,maxx,miny,maxy,0,FOUR,&res);
	int *topX = (int *)calloc(maxNo,sizeof(int));
	int *bottomX = (int *)calloc(maxNo,sizeof(int));
	int i, j;
	
	for (i = 0; i < maxNo; i++){
		topX[i] = maxx + 1;
		bottomX[i] = minx - 1;
	}
	for (i = minx; i <= maxx; i++)
		for (j = miny; j <= maxy; j++)
			if (res.data[i][j] >= 0){
				if (topX[ res.data[i][j] ] > i)
					topX[ res.data[i][j] ] = i;
				if (bottomX[ res.data[i][j] ] < i)
					bottomX[ res.data[i][j] ] = i;
			}
	for (i = 0; i < maxNo; i++){
		if (bottomX[i] - topX[i] <= wthr)
			bottomX[i] = -1;
	}
	for (i = minx; i <= maxx; i++)
		for (j = miny; j <= maxy; j++)
			if ((res.data[i][j] >= 0) && (bottomX[ res.data[i][j] ] == -1))
				sb->data[i][j] = 0;	
	
	free(topX);
	free(bottomX);
	freeMatrix(res);
}
void eliminateHorizontalSmallPrimitives(MATRIX *sb, int minx, int maxx, int miny, int maxy, int wthr){	// sb --> 0 or 1
	MATRIX res = allocateMatrix(sb->row,sb->column);
	int maxNo = connectedComponentsWithMask(*sb,1,minx,maxx,miny,maxy,0,FOUR,&res);
	int *leftY = (int *)calloc(maxNo,sizeof(int));
	int *rightY = (int *)calloc(maxNo,sizeof(int));
	int i, j;
	
	for (i = 0; i < maxNo; i++){
		leftY[i] = maxy + 1;
		rightY[i] = miny - 1;
	}
	for (i = minx; i <= maxx; i++)
		for (j = miny; j <= maxy; j++)
			if (res.data[i][j] >= 0){
				if (leftY[ res.data[i][j] ] > j)
					leftY[ res.data[i][j] ] = j;
				if (rightY[ res.data[i][j] ] < j)
					rightY[ res.data[i][j] ] = j;
			}
	for (i = 0; i < maxNo; i++){
		if (rightY[i] - leftY[i] <= wthr)
			rightY[i] = -1;
	}
	for (i = minx; i <= maxx; i++)
		for (j = miny; j <= maxy; j++)
			if ((res.data[i][j] >= 0) && (rightY[ res.data[i][j] ] == -1))
				sb->data[i][j] = 0;
	
	free(leftY);
	free(rightY);
	freeMatrix(res);
}
Exemple #9
0
Matrix::Matrix(const std::vector<int>& topNumbers, const std::vector<int>& leftNumbers)
{
    length = static_cast<int>(topNumbers.size()); // FIXME cast to int silences warning
    width = static_cast<int>(leftNumbers.size());
    allocateMatrix();
    initialize(topNumbers, leftNumbers);
}
Exemple #10
0
int multiplyMatrix(MatrixOfInt *matR, MatrixOfInt *matA, MatrixOfInt *matB){
	int i;
	int j;

	if(matA->col != matB->row){
		return FALSE;
	}

	matR->row = matA->row;
	matR->col = matB->col;
	if(!allocateMatrix(matR)){
		return FALSE;
	}
	for(i = 0; i < matR->row; i++){
		for(j = 0; j < matR->col; j++){
			//definindo cada um dos elementos
			//A linha atual tem a soma dos elementos de A
			//Vezes a soma dos elementos da coluna atual de B.

			*(*(matR->ptr + i) + j) = retornaMultiplicacaoDeLinhaComColunaDaOutra(matA, matB, i, j);

			//printf("Elemento [%d][%d] = %d * %d ",i, j, retornaSomaDeLinha(matA, i), retornaSomaDeColuna(matB, j));

		}
	}




    return TRUE;

}
Exemple #11
0
FLMSettings::FLMSettings()
{
    allocateMatrix();
    allocateSplines();

    mCorrMatrixLoaded = false;
    activeConfig = 0;
}
Exemple #12
0
int main(int argc, char *argv[]) {
  int i, j;
  int *p;
  pthread_t *threads;

  if (argc != 3) {
    printf("Usage: %s <size> <n>,  where size is dimension of square matrix, and n is number of threads\n", argv[0]);
    exit(1);
  }

  matrixSize = atoi(argv[1]);
  numThreads = atoi(argv[2]);

  a = allocateMatrix();
  b = allocateMatrix();
  c = allocateMatrix();
  
  for (i = 0; i < matrixSize; i++)
    for (j = 0; j < matrixSize; j++) {
      a[i][j] = i + j;
      b[i][j] = i + j;
    }

  if (matrixSize < 10) {  
    printMatrix(a);
    printMatrix(b);
  }

  // Allocate thread handles
  threads = (pthread_t *) malloc(numThreads * sizeof(pthread_t));

  // Create threads 
  for (i = 0; i < numThreads; i++) {
    p = (int *) malloc(sizeof(int));  // yes, memory leak, don't worry for now
    *p = i;
    pthread_create(&threads[i], NULL, worker, (void *)(p));
  }

  for (i = 0; i < numThreads; i++) {
    pthread_join(threads[i], NULL);
  }

  if (matrixSize < 10)   // print matrix if relatively small
    printMatrix(c); 
}
MATRIX createMatrixFromData(int **data, int row, int column){
    MATRIX res = allocateMatrix(row,column);
    int i, j;
    
    for (i = 0; i < row; i++)
        for (j = 0; j < column; j++)
            res.data[i][j] = data[i][j];
    
    return res;
}
MATRIX matrixTranspose(MATRIX M){
	MATRIX T = allocateMatrix(M.column,M.row);
	int i, j;
	
	for (i = 0; i < M.row; i++)
		for (j = 0; j < M.column; j++)
			T.data[j][i] = M.data[i][j];
	
	return T;
}
int main (int argc, char ** argv)
{
	if (argc != 3) 
	{
		printf("Use: %s <N> <T>,  onde 'N' é a dimensão da matriz unitária e 'T' é o número de threads.\n", argv[0]);
		return 1;
	}

	//Retira valores dos argumentos passados pela linha de comando
	matrix_size = atoi(argv[1]);
	number_of_threads = atoi(argv[2]);

	//Aloca espaço para variáveis e ponteiros para colunas
	A = allocateMatrix(matrix_size);

	//Gera matrizes A e B
	generateMatrix(A, matrix_size);

	//Imprime A e B (teste)
	if (matrix_size < 20)
		printMatrix(A, matrix_size, "A");

	//Ponteiro com threads
	pthread_t * p_threads;
	//Alocação de espaço para threads de acordo com o número de threads
	p_threads = (pthread_t *) malloc(number_of_threads * sizeof(pthread_t));


	//Inicializa o mutex
  	pthread_mutex_init(&mut, NULL);

	//Captura tempo inicial
	clock_gettime(CLOCK_MONOTONIC, &initial_time);

	//Criação de threads
	createThreads(p_threads, number_of_threads, matrix_size, A);

	//Join das threads
	joinThreads(p_threads);

	//Captura tempo final
	clock_gettime(CLOCK_MONOTONIC, &end_time);
  	
  	//imprime resultado
	printf("A soma de todos os termos é: %d\n", sum);

	//Calcula tempo final
	double elapsed = end_time.tv_sec - initial_time.tv_sec;
	elapsed += (end_time.tv_nsec - initial_time.tv_nsec) / 1000000000.0;
 	printf ("O tempo de processamento foi de: %f segundos\n", elapsed);   
	
	//Inicializa o mutex
  	pthread_mutex_destroy(&mut);  
	return 0;
}
void reallocateMatrix(MATRIX *M, long row, long column){
	int i, j, minrow, mincol;
	MATRIX tmp = allocateMatrix(M->row,M->column);

	copyMatrix(&tmp,*M);
	freeMatrix(*M);

	*M = allocateMatrix(row,column);
	initializeMatrix(M,0);

	if (tmp.row > row)			minrow = row;
	else						minrow = tmp.row;
	
	if (tmp.column > column)	mincol = column;
	else						mincol = tmp.column;
	
	for (i = 0; i < minrow; i++)
		for (j = 0; j < mincol; j++)
			M->data[i][j] = tmp.data[i][j];
	freeMatrix(tmp);
}
Exemple #17
0
void initializeGlobals4bwdist(int dx, int dy, int dz, int **px, int **py, int **pz, 
							  int **qx, int **qy, int **qz, MATRIX *labels){
	long cnt = dx * dy * dz;
	
	*px = (int *)calloc(cnt,sizeof(int));
	*py = (int *)calloc(cnt,sizeof(int));
	*pz = (int *)calloc(cnt,sizeof(int));
	*qx = (int *)calloc(cnt,sizeof(int));
	*qy = (int *)calloc(cnt,sizeof(int));
	*qz = (int *)calloc(cnt,sizeof(int));
	*labels = allocateMatrix(dx,dy,dz);	
}
int main()
{

    srand(time(NULL));
    Matrix maze = {NULL, 0, 0};
    Matrix fog = {NULL, 0, 0};
    int width = 50, height = 40;
    Position hero, exit;


    allocateMatrix(&maze, width, height);
    allocateMatrix(&fog, width, height);

    generateMaze(maze, &hero, &exit);
    generateFog(fog, hero);
    runGame(maze, fog, exit, hero);
    freeMatrix(&fog);
    freeMatrix(&maze);

    return 0;
}
Exemple #19
0
char * mm(void)
{

    int i, j, k;
    double **a, **b, **c;
    char *msg;
    msg = (char*)malloc(5*sizeof(char));
    a = allocateMatrix(NRA, NCA);
    b = allocateMatrix(NCA, NCB);
    c = allocateMatrix(NRA, NCB);

    initialize(a, NRA, NCA);
    initialize(b, NCA, NCB);
    initialize(c, NRA, NCB);


    for(i = 0; i < NRA; i++) {
      for(k = 0; k < NCA; k++) {
        for(j = 0; j < NCB; j++) {
          c[i][j] = a[i][k]*b[k][j];
        }
      }
    }

   printf("C[0:4,0:4]=\n");
   for(i = 0; i < 4; i++) {
     for(j = 0; j < 4; j++)
       printf("%f ", c[i][j]);
     printf("\n");
   }

    freeMatrix(a, NRA, NCA);
    freeMatrix(b, NCA, NCB);
    freeMatrix(c, NRA, NCB);

    msg = "done";

    return msg;
}
MATRIX addMatrix(MATRIX A, MATRIX B){
	long i, j;
	MATRIX M;

	if (A.row != B.row || A.column != B.column){
		printf("\nError: Matrix dimensions mismatch in add operation\n");
		exit(1);
	}
	M = allocateMatrix(A.row,A.column);
	for (i = 0; i < M.row; i++)
		for (j = 0; j < M.column; j++)
			M.data[i][j] = A.data[i][j] + B.data[i][j];
	return M;
}
int main() {
    int n        = 4;
    int **matrix = allocateMatrix(n);

    fillInMatrixValues(matrix);

    Submatrix submatrix = computeLargestSumSubmatrix(matrix, n);

    printf("Largest sum submatrix: \nx1 = %d\ny1 = %d\nx2 = %d\ny2 = %d\n", submatrix.startX, submatrix.startY, submatrix.stopX, submatrix.stopY);

    deallocateMatrix(matrix, n);

    return 0;
}
Exemple #22
0
double do_work(void) {
  double **a,           /* matrix A to be multiplied */
         **b,           /* matrix B to be multiplied */
         **c;           /* result matrix C */
  a = allocateMatrix(NRA, NCA);
  b = allocateMatrix(NCA, NCB);
  c = allocateMatrix(NRA, NCB);

  /*** Spawn a parallel region explicitly scoping all variables ***/

  initialize(a, NRA, NCA);
  initialize(b, NCA, NCB);
  initialize(c, NRA, NCB);

  compute(a, b, c, NRA, NCA, NCB);
#if defined(TAU_OPENMP)
  //if (omp_get_nested()) {
  compute_nested(a, b, c, NRA, NCA, NCB);
  //}
#endif
#ifdef TAU_MPI
  if (provided == MPI_THREAD_MULTIPLE) {
    printf("provided is MPI_THREAD_MULTIPLE\n");
  } else if (provided == MPI_THREAD_FUNNELED) {
    printf("provided is MPI_THREAD_FUNNELED\n");
  }
#endif /* TAU_MPI */
  compute_interchange(a, b, c, NRA, NCA, NCB);

  double result = c[0][1];

  freeMatrix(a, NRA, NCA);
  freeMatrix(b, NCA, NCB);
  freeMatrix(c, NCA, NCB);

  return result;
}
MATRIX form2DMatrix(MATRIX M, int row, int column){
	MATRIX newM;
	int i, j, c;

	if (row * column != M.row){
		printf("\nError: dimensions mismatch\n\n");
		exit(1);
	}
	newM = allocateMatrix(row,column);
	c = 0;
	for (i = 0; i < row; i++)
		for (j = 0; j < column; j++)
			newM.data[i][j] = M.data[c++][0];
	return newM;
}
int* generateMatrix(int row, int column, int noinit)
{
    int i, j, *m;
    m = allocateMatrix(row, column);
    if (!noinit) {
        printf("Generating matrix...\n");
        for (i=0; i<row; i++) {
            for (j=0; j<column; j++) {
                //            m[i][j] = rand()%10;
                //            *A(m,column,i,j) = rand()%10;
                *A(m,column,i,j) = j%10;
            }
        }
    }
    return m;
}
MATRIX readMatrix(char *filename){
	MATRIX M;
	long r, c, i, j;

	FILE *id = fopen(filename,"r");
	if (id == NULL){
		printf("Error: File %s does not exist...\n",filename);
		exit(1);
	}
	fscanf(id,"%ld%ld",&r,&c);
	M = allocateMatrix(r,c);
	for (i = 0; i < r; i++)
		for (j = 0; j < c; j++)
			fscanf(id,"%d",&(M.data[i][j]));
	fclose(id);
	return M;
}
int isMazeImposible(Matrix maze, Position exit)
{
    Matrix mark;
    allocateMatrix(&mark, maze.width, maze.height);
    fillMatrixWithValue(mark, 0);
    dfs(maze, mark, exit.x, exit.y);

    for(int i = 0; i < maze.height; ++i)
        for(int j = 0; j < maze.width; ++j)
            if(maze.matrix[i][j] == 0 && mark.matrix[i][j] == 0)
            {
                freeMatrix(&mark);
                return 1;
            }

    freeMatrix(&mark);
    return 0;
}
Exemple #27
0
int main(int argc, char **argv){
	
	int row = atoi(argv[1]);
	int col = atoi(argv[2]);

	int sum = 0;

	int **array = allocateMatrix(row, col);
	initMatrix(array, row, col);
	
	int i;
	for (i=0; i<col; i++) {
		sum+=array[row-1][i];
	}

	printf("The sum of the first number on each row is %d", sum);
	return 0;
}
int* loadMatrix(char *filename, int row, int column)
{
    int i, j, *m;
    
    FILE *fp = fopen(filename, "r");
    if (fp==NULL) {
        printf("Error opening %s for reading\n", filename);
        exit(1);
    }
    
    m = allocateMatrix(row, column);
    printf("Loading matrix...\n");
    for (i=0; i<row; i++) {
        for (j=0; j<column; j++) {
//            fscanf(fp, "%d", &m[i][j]);
            fscanf(fp, "%d", A(m,row,i,j));
        }
    }
    return m;
}
int* generateMatrix(int n, int noinit)
{
    int i, j, *m;
    m = allocateMatrix(n);

    if (!noinit) {
        printf("Generating matrix...\n");
        for (i=0; i<n; i++) {
            for (j=0; j<n; j++) {
                //            m[i][j] = rand()%10;
                //            *M(m,n,i,j) = rand()%10;
                // Modulo 10 so that each number is only 1 digit -
                // easy for displaying larger matrices on screen.
                *M(m,n,i,j) = j%10;
                //            *(m+i*N+j)= rand()%10;
            }
        }
    }
    return m;
}
float* loadMatrix(char *filename, int n)
{
    int i, j;
    float *m;
    
    FILE *fp = fopen(filename, "r");
    if (fp==NULL) {
        printf("Error opening %s for reading\n", filename);
        exit(1);
    }
    
    m = allocateMatrix(n);
    printf("Loading matrix...\n");
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
//            fscanf(fp, "%d", &m[i][j]);
            fscanf(fp, "%f", M(m,n,i,j));
        }
    }
    return m;
}