예제 #1
0
파일: main.c 프로젝트: etoestja/inf
int main(int argc, char** argv)
{
/*    printf("argc=%d\n", argc);
    int ss;
    for(ss = 0; ss < argc; ss++)
        printf("argv#%d=%s\n", ss, argv[ss]);
*/

    int N;
    scanf("%d", &N);
    matrix *M1 = malloc(sizeof(matrix)), *M2 = malloc(sizeof(matrix));
    mallocMatrix(M1, N, N);
    mallocMatrix(M2, N, N);

    fillMatrix(M1);

    #ifdef DEBUG
        fprintf(stderr, "A=\n");
        printMatrix(M1);
    #endif

    getInverseCopy(M1, M2, NULL);

    #ifdef DEBUG
        fprintf(stderr, "A^-1=\n");
        printMatrix(M2);
    #else
        if(argc == 1 || (argc >= 2 && argv[1][0] == 'i'))
        {
            printf("%d\n", M2->R);
            printMatrix(M2);
        }
    #endif

    matrix *M3 = malloc(sizeof(matrix));
    mallocMul(M1, M2, M3);
    mulMatrix(M1, M2, M3);

    #ifdef DEBUG
        fprintf(stderr, "A*A^-1=\n");
        printMatrix(M3);
    #else
        if(argc >= 2 && argv[1][0] == 'e')
        {
            printf("%d\n", M3->R);
            printMatrix(M3);
        }
    #endif

    freeMatrix(M1);
    freeMatrix(M2);

    return(0);
}
예제 #2
0
void QRGivensRotations(double ***A, double ***Q, double ***R) {

	double **G;
	int i, j;
	double c, s;

	mallocMatrix(&G);

	copyArray(R, A);
	setEye(Q);


	// Algorytm qr Givens rotations
	for(j=0; j<SIZE; j++) //kolumny
		for(i=SIZE - 1; i > j; i--) { //wiersze

			// #mozna zrownoleglic dwie instrukcje
			setEye(&G);
			givensRotation((*R)[i-1][j], (*R)[i][j], &c, &s);

			setMatrixG(&G, i, j, c, s);

			// #mozna zrownoleglic dwie instrukcje ponizej
			multiplyMatrixToSecondWithTransposition(&G, R);
			multiplyMatrixToFirst(Q, &G);

		}

//	printMatrix(&Q," Q ROZWIAZANIE");
//	printMatrix(&R," R ROZWIAZANIE");

	freeMatrix(&G);

	return;
}
예제 #3
0
파일: main.c 프로젝트: etoestja/inf
int main(int argc, char** argv)
{
/*    printf("argc=%d\n", argc);
    int ss;
    for(ss = 0; ss < argc; ss++)
        printf("argv#%d=%s\n", ss, argv[ss]);
*/
#ifdef USEFILES
    freopen("matrix.in", "r", stdin);
    freopen("matrix.out", "w", stdout);
#endif

    int N;
    scanf("%d", &N);
    matrix *M1 = malloc(sizeof(matrix)), *M2 = malloc(sizeof(matrix));
    mallocMatrix(M1, 2 * N, 2 * N);
    mallocMatrix(M2, 2 * N, 2 * N);

    fillMatrix(M1);

    #ifdef DEBUG
        fprintf(stderr, "A=\n");
        printMatrix(M1);
    #endif

    getInverseCopy(M1, M2, NULL);

    if(M2->R == 0 || M2->C == 0)
        return(1);

    printMatrixComplex(M2);

    freeMatrix(M1);
    freeMatrix(M2);

    return(0);
}
예제 #4
0
/** \brief Perform a convolution operation on image

  The bordering size/2 pixels of the image do not containg valid data.
  imgConvolute is quite memory efficient.
  Only (img->dimx+size)*size*sizeof(float) bytes are consumed by a
  temporary buffer. When processing multiple planes/frames/images with
  equal width and kernel size, efficiency can be improved further by
  allocating the temporary buffer yourself and calling imgConvoluteData
  directly.

  \param img image
  \param frame frame to perform operation on
  \param plane plane to perform operation on
  \param kernel convolution kernel
  \param size convolution kernel size
  \pre size must be smaller than img->dimx and img->dimy
  \return nonzero in case of (malloc) error
*/
int imgConvolute(IMG *img, int frame, int plane, float **kernel, int size)
{
  float **buffer;
  int y;

  /* Allocate memory for temporary buffer */
  buffer = mallocMatrix(img->dimx+size,size);
  if(!buffer) return 1;

  /* Convolute */
  imgConvoluteData(img->m[plane],buffer,frame,img->dimx, img->dimy,
                   kernel,size);

  /* Free buffer */
  for(y=0;y<size;++y) free(buffer[y]);
  free(buffer);
  return 0;
}
/* read PLN weights text file. 
Instead of reading from text file, the values can be directly coded into the structure */
void readPlnWeights(char *fname, struct PlnWeights *pln) {
        int cnt = 0;
        int n, m, k;
        
        FILE *fp = NULL;
        
        fp = fopen(fname, "r");
        if(!fp) {
            printf("Unable to open file to load PLN.\n");
            exit(0);
        }
        
        cnt = fscanf(fp, "PLN\n");
        
        cnt = fscanf(fp, "K\n%d\n", &pln->Nc);
        
        cnt = fscanf(fp, "N\n%d\n", &pln->N);

        cnt = fscanf(fp, "M\n%d\n", &pln->M);
        
        pln->distanceMeasure = mallocArray(pln->N);
        cnt = fscanf(fp, "DistanceMeasure\n");
        dprintf0("DistanceMeasure\n");
        for(n=0; n<pln->N; n++) {
            cnt = fscanf(fp, "%lf ", &pln->distanceMeasure[n]);
            dprintf("%e ", pln->distanceMeasure[n]);
        }
        dprintf0("\n");
        
        pln->inputMeans = mallocArray(pln->N);
        cnt = fscanf(fp, "InputMeans\n");
        dprintf0("InputMeans\n");
        for(n=0; n<pln->N; n++) {
            cnt = fscanf(fp, "%lf ", &pln->inputMeans[n]);
            dprintf("%e ", pln->inputMeans[n]);
        }
        dprintf0("\n");
        
        pln->inputStd = mallocArray(pln->N);
        cnt = fscanf(fp, "InputStd\n");
        dprintf0("InputStd\n");
        for(n=0; n<pln->N; n++) {
            cnt = fscanf(fp, "%lf ", &pln->inputStd[n]);
            dprintf("%e ", pln->inputStd[n]);
        }
        dprintf0("\n");
        
        pln->Nv = (int*)malloc(pln->Nc*sizeof(int));
        
        pln->clusterCenters = mallocMatrix(pln->Nc, pln->N);
        
        pln->R = (double***)malloc(pln->Nc*sizeof(double**));
        pln->C = (double***)malloc(pln->Nc*sizeof(double**));
        pln->W = (double***)malloc(pln->Nc*sizeof(double**));
        
        pln->Et = mallocMatrix(pln->Nc, pln->M);
        pln->lambda = mallocArray(pln->Nc);
        
        for(k=0; k<pln->Nc; k++) {
            int idx;
            cnt = fscanf(fp, "ClusterIndex\n%d\n", &idx);
            dprintf("Reading cluster %d\n", idx);
            if(k!=idx) {
                printf("Invalid file.\n");
                exit(0);
            }
            
            cnt = fscanf(fp, "Nv\n%d\n", &pln->Nv[k]);
            dprintf("Nv =  %d\n", pln->Nv[k]);
            
            cnt = fscanf(fp, "CenterVector\n");
            dprintf0("CenterVector\n");
            for(n=0; n<pln->N; n++) {
                cnt = fscanf(fp, "%lf ", &pln->clusterCenters[k][n]);
                dprintf("%e ", pln->clusterCenters[k][n]);
            }
            cnt = fscanf(fp, "\n");
            dprintf0("\n");
            
            pln->R[k] = mallocMatrix(pln->N+1, pln->N+1);
            dprintf0("R\n");
            cnt = fscanf(fp, "R\n");
            for(n=0; n<pln->N+1; n++) {
                for(m=0; m<pln->N+1; m++) {
                    cnt = fscanf(fp, "%lf ", &pln->R[k][n][m]);
                    dprintf("%e ", pln->R[k][n][m]);
                }
                cnt = fscanf(fp, "\n");
            }
            dprintf0("\n");
            
            pln->C[k] = mallocMatrix(pln->M, pln->N+1);
            cnt = fscanf(fp, "C\n");
            dprintf0("C\n");
            for(n=0; n<pln->M; n++) {
                for(m=0; m<pln->N+1; m++) {
                    cnt = fscanf(fp, "%lf ", &pln->C[k][n][m]);
                    dprintf("%e ", pln->C[k][n][m]);
                }
                cnt = fscanf(fp, "\n");
            }
            dprintf0("\n");
            
            pln->W[k] = mallocMatrix(pln->M, pln->N+1);
            cnt = fscanf(fp, "W\n");
            dprintf0("W\n");
            for(n=0; n<pln->M; n++) {
                for(m=0; m<pln->N+1; m++) {
                    cnt = fscanf(fp, "%lf ", &pln->W[k][n][m]);
                    dprintf("%e ", pln->W[k][n][m]);
                }
                cnt = fscanf(fp, "\n");
            }
            dprintf0("\n");
            
            cnt = fscanf(fp, "Et\n");
            dprintf0("Et\n");
            for(n=0; n<pln->M; n++) {
                cnt = fscanf(fp, "%lf ", &pln->Et[k][n]);
                dprintf("%e ", pln->Et[k][n]);
            }
            cnt = fscanf(fp, "\n");
            dprintf0("\n");
            
            cnt = fscanf(fp, "lambda\n%lf\n", &pln->lambda[k]);
            dprintf("lambda\n%lf\n", pln->lambda[k]);
        }
        
        cnt = fscanf(fp, "EOF\n");
        
        fclose(fp);
}
예제 #6
0
파일: main.c 프로젝트: etoestja/inf
void mallocCp(matrix* dest, matrix* src)
{
    mallocMatrix(dest, src->R, src->C);
}
예제 #7
0
파일: main.c 프로젝트: etoestja/inf
inline void mallocMul(matrix* A, matrix* B, matrix* C)
{
    mallocMatrix(C, A->R, B->C);
}