int main() { matrix m, n, r, t; m = initmatrix(4,5); printf("matrix m:\n"); printMatrix(&m); n = initmatrix(4,4); unitMatrix(&n); printf("matrix n:\n"); printMatrix(&n); r = initmatrix(6,6); unitMatrix(&r); printf("matrix r:\n"); printMatrix(&r); t = initmatrix(6,6); // t = initMatrix(6,6); printf("enter row num: "); int rown, coln, val; scanf("%d", &rown); printf("\nenter col num: "); scanf("%d", &coln); printf("\nenter value: "); scanf("%d", &val); if (rown >= 6 || coln >= 6) { printf("row or col number out of bounds!\n"); return 1; } t.data[rown][coln] = val; //t.data[2][2] = 1; printf("matrix t:\n"); printMatrix(&t); }
static void nullifyMatrix() { int i, j; bool firstRow = false; bool firstCol = false; // first row for (i = 0; i < M; i++) if (a[0][i] == 0) { firstRow = true; break; } // first col for (i = 0; i < N; i++) if (a[i][0] == 0) { firstCol = true; break; } // rest of the matrix for (i = 1; i < N; i++) for (j = 1; j< M; j++) if (a[i][j] == 0) { a[i][0] = 0; a[0][j] = 0; } printf("D1:\n"); printMatrix(); // now we know which row and column to be nullify using information stored in previous step. // row first for (i = 1; i < N; i++) if (a[i][0] == 0) nullifyRow(i); // cols now for (j = 1; j < M; j++) if (a[0][j] == 0) nullifyCol(j); printf("D2:\n"); printMatrix(); // now first row: if (firstRow) nullifyRow(0); // now rist col: if (firstCol) nullifyCol(0); }
void matrixFill(int lines, int col, int mat[lines][col]){ int i, j; for(i = 0; i < lines; i++){ for(j = 0; j < col; j++){ printf("Digite o termo [%d][%d]: ", i + 1, j + 1); scanf("%d", &mat[i][j]); } } printMatrix(lines, col, mat); }
int main () { int i = 0; int j = 0; int k = 0; int l = 0; for(k=0; k<3; k++) { for(l=0; l<3; l++) { U[k][l] = 0; if(k==l) { U[k][l] = 1; } L[k][k] = M[k][k] - summation(k,-1, 0); for(i=k+1;i<3;i++) { L[i][k] = M[i][k] - summation(k, i, 1); } for(j=k+1;j<3;j++) { U[k][j] = (M[k][j] - summation(k, j, 2)) / L[k][k]; } } } printMatrix(U, 'M'); printMatrix(U, 'U'); printMatrix(L, 'L'); y_1 = B[0] / L[0][0]; y_2 = (B[1] - L[1][0] * y_1) / L[1][1]; y_3 = (B[2] - (L[2][0] * y_1) - (L[2][1] * y_2)) / L[2][2]; printf("y_1, y_2, y_3: %0.2f, %0.2f, %0.2f\n", y_1, y_2, y_3); x_3 = y_3; x_2 = y_2 - U[1][2] * x_3; x_1 = y_1 - U[0][1] * x_2 - U[0][2] * x_3; printf("x_1, x_2, x_3: %0.2f, %0.2f, %0.2f", x_1, x_2, x_3); return 0; }
void Field::printAll() { if (t > nextFrameTime) { nextFrameTime += algo::ftr().TMax() / algo::ftr().FramesCount(); printConsole(); printViews(); printMatrix(); printTimes(); } }
/* * Problems... * ------ * Computer problem 4.2 #1 */ void problem42_1() { // variables struct matrix a,ai,aai,im; struct lu lu; // setup a.size = NUM10; ai.size = NUM10; aai.size = NUM10; im.size = NUM10; // print printf("Problem 4.2 #1\n"); printf("Algorithm for inverting an nxn lower triangular matrix A.\n"); printf("Testing on matrix whose elements are aij = (i+j)^2 when i >= j.\n"); printf("Use n = 10. Form AA^-1 as test.\n"); // do the work a.m10 = loadMatrix42_1(); im = loadIdentityMatrix(NUM10); lu = luDecomposition(a); ai.m10 = evaluateLu(lu.lu10,im.m10); aai = multiplyMatrix(a,ai); // Original matrix printf("\nOriginal Matrix: A\n"); printMatrix(a); // Identity matrix printf("\nIdentity Matrix: I\n"); printMatrix(im); // LU decomposition printf("\nLU decomposition:\n"); printLu10(lu.lu10); // Inverted matrix printf("\nInverted Matrix: A^-1\n"); printMatrix(ai); // Product of A and A^-1 printf("\nProduct of AA^-1 Matrix\n"); printMatrix(aai); printf("\n"); return; }
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; }
int main (int argc, char * argv[]) { int i, j, k; // loop variables double * F, * G; // Create matrix pointers, G is verification matrix //srand(time(NULL)); // Seed randoms // Set size of DIM, default is 4097 from lab requirements if (argc == 1) { DIM = 4097; } else { DIM = atoi(argv[1]); } size_t memSize = DIM * DIM * sizeof(double); // Allocate memory for matrices F = (double *) malloc(memSize); G = (double *) malloc(memSize); if (F == NULL || G == NULL) fprintf(stderr, "Error allocating matrix\n"); // define thread hierarchy int nblocks = NBLOCKS; int tpb = TPB; // initialize with randoms in range [1.0 2.0) for (i = 0; i < DIM; i++) { for (j = 0; j < DIM; j++) { int temp = rand(); temp &= MASK_A; temp |= MASK_B; F[i * DIM + j] = *(float *) &temp; G[i * DIM + j] = *(float *) &temp; } } // Take beginning time time_t time1 = time(NULL); clock_t tick = clock(); // Perform matrix computations computeMatrix(F, DIM); // Take end time and calculate difference time_t time2 = time(NULL); tick = clock() - tick; double timeDiff = difftime(time2, time1); printf("elapsed time\t(clock): %d\n", (int) tick); printf("elapsed time\t (time): %.0f\n", timeDiff); printf("\ndiff: %d, total: %d\n", validateMatrix(F, G), DIM * DIM); printMatrix(F); return(0); }
void flipH(int n, int m, int mat[n][m]){ int i, j, a; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n/2; j++ ) { a = mat[i][(m-1)-j]; mat[i][(m-1)-j] = mat[i][j]; mat[i][j] = a; } } printMatrix(n, m, mat); }
Void Picture::printDescription( ) { LOG( "PART" ) << "Obraz " << SeqParams( )->getPicWidth( ) << " x " << SeqParams( )->getPicHeight( ) << std::endl; printMatrix( itsSamplesY, SeqParams( )->getPicWidth( ), SeqParams( )->getPicHeight( ), LOG( "PART" ) ); LOG_TAB( ); for( UInt i = 0; i < itsCTUs.size( ); ++i ) { if( itsCTUs[ i ] != nullptr ) itsCTUs[ i ]->printDescription( ); } LOG_UNTAB( ); }
void ecology_initialization(ode_set *des,FILE *in){ float tmp; time_t t; char buff[CHAR_BUFF_SIZE]; while( fscanf(in, " %s ", buff) != EOF){ if (!strcmp(buff, "CARRYING_CAPACITY")){ //reading carrying capacity fscanf(in,"%f ",&tmp); carrying_capacity = tmp; } else if (!strcmp(buff, "INTERACTION_MATRIX")){ //identifying the type of the interaction matrix fscanf(in, "%s ", buff); if (!strcmp(buff,"custom")){ im = safeMatrixAlloc(dim,dim); readMatrix(in, im, dim,dim); time(&t); printf("#Init random with clock seed: %d\n", (int) t); srand(t); } else if (!strcmp(buff,"uniform_random")){ int seed; fscanf(in, "%d", &seed); srand(seed); im = uniformRandomAlloc(dim,.7); } } else if (!strcmp(buff, "DUMP")){ //identifying the dumping method fscanf(in, "%s ", buff); if (!strcmp(buff,"state")){ des->data_dump = &ecology_dump; } else if (!strcmp(buff,"full")){ des->data_dump = &ecology_full_dump; } } } time(&t); printf("#Init random with clock seed: %d\n", (int) t); srand(t); generalized_simplex(dim,carrying_capacity*0.4,des->state); //displaying the matrix printMatrix(stdout, im, dim, dim); fclose(in); fflush(stdout); }
int main (void) { void printMatrix(int row,int col,int matrix[row][col]); void transposeMatrix(int row, int col, int matrix[row][col], int temp[col][row]); int matrix[3][4] = { {1,2,3,4}, {1,2,3,4}, {5,6,7,8} }; int temp[4][3]; printMatrix(3,4,matrix); transposeMatrix(3,4,matrix,temp); printMatrix(4,3,temp); return 0; }
int main() { ROW_t mat[] = { { 0.0F, 0.1F }, { 1.0F, 1.1F, 1.2F }, { 2.0F, 2.1F, 2.2F, 2.3F } }; int nRows = sizeof(mat) / sizeof(ROW_t); printMatrix( mat, nRows ); return 0; }
struct _matrix *perceptronLearn(float k, struct _matrix in, struct _matrix t, struct _matrix *w) { // Declare appropriate matrices int i, j; MATRIX *net_j; MATRIX *delta_w; delta_w = malloc(sizeof(MATRIX)); memset(delta_w, 0, sizeof(MATRIX)); delta_w->rows = w->rows; delta_w->cols = w->cols; delta_w->values = malloc(delta_w->rows * delta_w->cols); MATRIX *temp; // Calculate output net_j = perceptronOperate(in, w); // Print output and training matrix printf("\n\nLearn Output:\n"); printMatrix(*net_j); printf("\n\nTraining Matrix:\n"); printMatrix(t); // Caclulate error matrix for (i = 0; i < w->rows; i++) { for (j = 0; j < w->cols; j++) { delta_w->values[j + i * w->cols] = k * (t.values[j] - net_j->values[j]) * in.values[i]; // This might go wrong } } // Output error matrix printf("\n\nDelta_W Matrix:\n"); printMatrix(*delta_w); // Add to weight matrix, free and return temp = matrixAdd(*w, *delta_w); //w falues aren't changed because they are declared and handled in operator.c?? free(w); return temp; }
void printPlaceObject2(FILE *f, int length) { int start = fileOffset; int flags = readUInt8(f); int l; println("Depth: %i", readUInt16(f)); if(flags & PLACE_HASMOVE) println("Has move flag"); if(flags & PLACE_HASCHARACTER) println("Character ID: %i", readUInt16(f)); if(flags & PLACE_HASMATRIX) { println("Matrix:"); printMatrix(f); } if(flags & PLACE_HASCXFORM) { print("CXForm: "); printCXForm(f, true); putchar('\n'); } if(flags & PLACE_HASRATIO) println("Ratio: %i", readUInt16(f)); if(flags & PLACE_HASNAME) println("Name: %s", readString(f)); if(flags & PLACE_HASCLIP) println("ClipDepth: %i", readUInt16(f)); if(flags & PLACE_RESERVED) { println("Mystery number: %04x", readUInt16(f)); flags = readUInt16(f); println("Clip flags: %04x", flags); while((flags = readUInt16(f)) != 0) { println("Flags: %04x", flags); l = readUInt32(f); decompileAction(f, l, 0); } } dumpBytes(f, length-(fileOffset-start)); }
int main() { int n=7; int M[10][10]; memset(M,0,sizeof(M)); generateMagicSquare(M, n); printMatrix(M,n); }
/* Game Result*/ void displayResult(char box[]) { int x=gameOver(box); if(x==10) { printf("Player X won\n"); } else if(x==-10) { printf("Player O won\n"); } else { printf("....DRAW...\n"); } printMatrix(box); system("pause"); }
void makeYAxisRotation(CvMat* dst, double angle) { setHomography(dst, 1.0, 0.0, 0.0, 0.0, cos(angle), -1*sin(angle), 0.0, sin(angle), cos(angle)); if (DEBUG) { printf("Y: \n"); printMatrix(dst); } }
//测试函数 void TestPrintMatrix() { vector<vector<int>> matrix = { {1,2,3},{5,6,7},{9,10,11},{13,14,15} }; vector<int> ret; ret = printMatrix(matrix); int i = 0; while (i < ret.size()) { cout << ret[i++] << " "; } cout << endl; }
int main() { srand(time(0)); int m[N][N]; for (int row = 0; row < N; ++row) { for (int col = 0; col < N; ++col) { m[row][col] = (rand() % 5) - 2; } } printMatrix(m); findSubmatrixWithBiggestSum(m); return 0; }
int main(int argc,char **argv) { if(argc==3) { nRows=atoi(argv[1]);chunk_size=atoi(argv[2]); if(chunk_size&(chunk_size-1)!=0) { printf("\n Chunk_Size should be Power of 2\n"); exit(1); } } else if(argc==2) { nRows=atoi(argv[1]); printf("\nWe assuming chunk size as 4096\n");chunk_size=4096; } else if(argc==1) { printf("\n We assuming array size as 2*1024*1024 and Chunk Size as 2*1024*1024\n"); nRows=2*1024*1024; chunk_size=2*1024*1024; } nCols=nRows; //fillMatrix("./MatrixA"); printf("\n\n Matrix A is......\n\n"); // //fillMatrix("./MatrixB"); printf("\n\n Matrix B is ...........\n\n"); //printMatrix("./MatrixB"); //fillMatrix("./MatrixC"); printf("\n\n Matric C.....\n\n"); //printMatrix("./MatrixC"); nRows=nRows; nCols=nRows+padding;//appaanding 0's when rwo is not multiple of page size //printMatrix("./MatrixA"); struct timeval tv_start,tv_end; gettimeofday(&tv_start,NULL); map_Matrix(); gettimeofday(&tv_end,NULL); nCols=nCols-padding; padding=0; printMatrix("./MatrixC"); printf("\n\n-----------------------------------------------------------------------------------------------\n\n"); printf("\n \n time taken is %f sec ",(double)((tv_end.tv_sec-tv_start.tv_sec)+((tv_end.tv_usec-tv_start.tv_usec)/1000000))); return 0; }
int printUpperLeftBlock(double* a, int n, int m){ const int q=7; double* const temp = new double[q*q]; if (q<n){ for (int i=0; i<q; i++){ for (int j=0; j<q; j++){ temp[i*q+j]=getIJ(a, n, m, i, j); } } printMatrix(temp, q, q); } else{ for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ temp[i*n+j]=getIJ(a, n, m, i, j); } } printMatrix(temp, n, n); } delete[] temp; return 0; }
void invertMatrix(double matrix[MAX][MAX], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j + n] = 0; } } for (int i = 0; i < n; i++) { matrix[i][i + n] = 1; } printMatrix(matrix, n, 2*n); gaussElimination(matrix, n, n); }
int main(int argc, const char * argv[]) { int matrix[4][4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12}, {13, 14, 15, 16}}; printMatrix(matrix); matrix[2][1] = 0; findZeros(matrix); fillZeroes(matrix); return 0; }
int roundRobin(matrix m, matrix eigen_vectors, int size) { int iteration = 0; int i, j; while (getMaxOffDiagonal(m, size, &i, &j) > eps && iteration < max_iteration) { multiplyRotationMatrix(m, eigen_vectors, i, j, size); printf("\nIteration %d:", ++iteration); printf("\nMax off-diagonal element: %lf", getMaxOffDiagonal(m, size, &i, &j)); printf("\nAverage off-diagonal element: %lf", getAverageOffDiagonal(m, size)); printf("\nTrace of matrix: %lf\nMatrix", getTraceMatrix(m, size)); printMatrix(m, size); } return iteration; }
void motion(int velocidade) { int x, y = 0, aux = 1, count = 0; char matrix[LINHAS][COLUNAS]; //Config ncurses initscr(); noecho(); curs_set(FALSE); x = (LINHAS - 1)/2; while(aux <= 20){ //Subida da bola reduzida em fracoes while(y >= LINHAS - (LINHAS / aux)){ if(count % velocidade == 0){ y--; clearMatrix(matrix); matrix[x][y] = '@'; printMatrix(matrix); } count++; usleep(10000); } //Descida da bola ate tocar a borda inferior while(y < LINHAS - 2){ if(count % velocidade == 0) { y++; clearMatrix(matrix); matrix[x][y] = '@'; printMatrix(matrix); } count++; usleep(5000); } aux++; } usleep(500000); endwin(); }
static void update_prob( uFloat **P_pre, uFloat **R, uFloat **H, uFloat **P_post, uFloat **K ) { #ifdef PRINT_DEBUG printf( "ekf: updating prob\n" ); #endif #ifdef DIV_DEBUG printMatrix( "P", P_pre, STATE_SIZE, STATE_SIZE ); #endif #ifdef DIV_DEBUG printMatrix( "H", H, MEAS_SIZE, STATE_SIZE ); #endif #ifdef DIV_DEBUG printMatrix( "R", R, MEAS_SIZE, MEAS_SIZE ); #endif matMultTranspose( P_pre, H, temp_state_meas, STATE_SIZE, STATE_SIZE, MEAS_SIZE ); /* temp_state_meas = P_pre*H' */ matMult( H, temp_state_meas, temp_meas_meas, MEAS_SIZE, STATE_SIZE, MEAS_SIZE ); /* temp_meas_meas = (H*P_pre)*H' */ matAdd( temp_meas_meas, R, temp_meas_meas, MEAS_SIZE, MEAS_SIZE ); take_inverse( temp_meas_meas, temp_meas_2, MEAS_SIZE ); /* temp_meas_2 = inv( H*P_pre*H' + R) */ #ifdef DIV_DEBUG printMatrix( "1 / (HPH + R)", temp_meas_2, MEAS_SIZE, MEAS_SIZE ); #endif matMult( temp_state_meas, temp_meas_2, K, STATE_SIZE, MEAS_SIZE, MEAS_SIZE ); /* K = P_pre * H' * (inv( H*P_pre*H' + R))' */ #ifdef DIV_DEBUG printMatrix( "Kalman Gain", K, STATE_SIZE, MEAS_SIZE ); #endif matMult( H, P_pre, temp_meas_state, MEAS_SIZE, STATE_SIZE, STATE_SIZE ); /* temp_meas_state = H * P_pre */ matMult( K, temp_meas_state, temp_state_state, STATE_SIZE, MEAS_SIZE, STATE_SIZE ); /* temp_state_state = K*H*P_pre */ #ifdef PRINT_DEBUG printf( "ekf: updating prob 3\n" ); #endif matSub( P_pre, temp_state_state, P_post, STATE_SIZE, STATE_SIZE ); /* P_post = P_pre - K*H*P_pre */ #ifdef DIV_DEBUG printMatrix( "New P_post", P_post, STATE_SIZE, STATE_SIZE ); #endif }
void sub(int matrix1[MATSIZE][MATSIZE], int matrix2[MATSIZE][MATSIZE] ) { int diffM[MATSIZE][MATSIZE]; int i, j; for (i = 0; i < MATSIZE; i++) { for (j = 0; j < MATSIZE; j++) { diffM[i][j] = matrix1[i][j] - matrix2[i][j]; } } printMatrix(diffM); }
void main() { float colY[9], colH[9]; float y[3][3] = {{4, 5, 2}, {3, 9, 10}, {4, 5, 0.5}}; float h[3][3] = {{5, 6, 9}, {10, 12, 13}, {1, 1, 1}}; colVec(3, 3, y, colY); colVec(3, 3, h, colH); float totalSum = 48; float precision = 0.01; int length = 9; float X[9]; exactTotalSum(length, colY, colH, totalSum, precision, X); printMatrix(X, 9, 1); }
void testLUFactorisation(){ double **A, **L, **U; int n; n=3; A=allocateDoubleMatrix(n, n); L=allocateDoubleMatrix(n, n); U=allocateDoubleMatrix(n, n); A[0][0]=6.0; A[0][1]=0.0; A[0][2]=2.0; A[1][0]=24.0; A[1][1]=1.0; A[1][2]=8.0; A[2][0]=-12.0; A[2][1]=1.0; A[2][2]=-3.0; LUFactotisation(n,A,U,L); fprintf(stdout,"Matrix Factorisation U:\n"); printMatrix(U,n,n); fprintf(stdout,"Matrix Factorisation L:\n"); printMatrix(L,n,n); }