int main(void){ int tid; /*this task id */ int bufid, master, bytes, msgtag, source; char * graph_text = NULL; int **matrix; int degree, edges; int len; int i; int greycodeLength; tid = pvm_mytid(); /*enroll in pvm */ master = pvm_parent(); /*get the master id */ while (1){ /*Get the first graph */ pvm_initsend(PvmDataDefault); pvm_pkstr(""); /*Just put something in the buffer */ pvm_send(master, MSGREQTASK); /* Request a task */ bufid = pvm_recv(master, MSGTASK); /* Get the task */ pvm_bufinfo(bufid, &bytes, &msgtag, &source); /* Get the size of the text */ graph_text = malloc(bytes); pvm_upkstr(graph_text); /* Unpack the text */ len = strlen(graph_text); if(graph_text[len - 1] == '\n') graph_text[len - 1] = '\0'; matrix = create_matrix(graph_text, °ree); /*Generate the appropriate matrix */ populate_matrix(graph_text, matrix, &edges, degree); greycodeLength = 1 << degree; // Initialize code (where the greycode is stored) int * vals = calloc(2 * greycodeLength, sizeof(int)); int ** code = malloc(greycodeLength * sizeof(int *)); for(i = 0; i < greycodeLength; i++){ /*assign rows within the allocated space */ code[i] = vals + i*2; } /*** DO GREYCODE ALGORITHM ON THE MATRIX ***/ if(greycode(degree, matrix, code) == 1){ /*If we found a code */ pvm_initsend(PvmDataDefault); pvm_pkstr(graph_text); pvm_send(master, MSGCODE); } else{ /*Didnt find a code */ pvm_initsend(PvmDataDefault); pvm_pkstr(graph_text); pvm_send(master,MSGNOCODE); } destroy_matrix(matrix); /* Free the memory associated with the matrix */ destroy_matrix(code); free(graph_text); } pvm_exit(); exit(0); }
/* *description: the direct method of * best square approximation. */ Poly* bsa_direct(func f, double a, double b, size_t n) { size_t i, j; Poly* res = NULL; Matrix* H = create_matrix_n(n + 1); Matrix* D = create_vector(n + 1); Matrix* A; for (i = 0; i < H->m; i++) { for (j = 0; j < H->n; j++) { double h = pow(b, i + j + 1) - pow(a, i + j + 1); H->mem[i][j] = h / (i + j + 1); } } for (i = 0; i < D->m; i++) D->mem[i][0] = integration(f, a, b, 1e-6); A = me_gauss_elim(H, D, false); for (i = 0; i < A->m; i++) poly_add_inp(&res, create_poly(A->mem[i][0], i)); destroy_matrix(&H); destroy_matrix(&D); destroy_matrix(&A); return res; }
int main(void) { int** m; int num_cols, num_rows; srand (time(NULL)); printf("How many columns?: "); scanf("%d", &num_cols); printf("How many rows?: "); scanf("%d", &num_rows); m = create_matrix(num_cols, num_rows); if (NULL == m) { exit(-1); } random_int_matrix(m, num_cols, num_rows); print_matrix(m, num_cols, num_rows); int **transpose = create_matrix(num_rows, num_cols); if (NULL == transpose) { exit(-1); } transpose_matrix(m, transpose, num_cols, num_rows); print_matrix(transpose, num_rows, num_cols); destroy_matrix(m, num_rows); destroy_matrix(transpose, num_cols); return 0; }
int main(void) { FILE *fin = fopen( "matrix2.in", "r" ); if( fin == NULL ) { printf( "Error: could not open matrix2.in\n" ); exit( EXIT_FAILURE ); } Matrix *a = read_matrix( fin ); Matrix *b = read_matrix( fin ); fclose( fin ); Matrix *c = product_matrix( a, b ); FILE *output = fopen( "matrix2.out", "w" ); if( output == NULL ) { printf( "Error: could not open matrix2.out\n" ); exit( EXIT_FAILURE ); } print_matrix( output, c ); fclose( output ); destroy_matrix( a ); destroy_matrix( b ); destroy_matrix( c ); return 0; }
int main() { Matrix *A = make_matrix(3, 4); consecutive_matrix(A); printf("A\n"); print_matrix(A); Matrix *C = add_matrix_func(A, A); printf("A + A\n"); print_matrix(C); Matrix *B = make_matrix(4, 3); increment_matrix(B, 1); printf("B\n"); print_matrix(B); Matrix *D = mult_matrix_func(A, B); printf("D\n"); print_matrix(D); destroy_matrix(A); destroy_matrix(B); destroy_matrix(C); destroy_matrix(D); return 0; }
/******************************************************************************* * int LUP_decomposition(matrix_t A, matrix_t* L, matrix_t* U, matrix_t* P) * * LUP decomposition with partial pivoting *******************************************************************************/ int LUP_decomposition(matrix_t A, matrix_t* L, matrix_t* U, matrix_t* P){ int i, j, k, m, index; float s1, s2, temp; m = A.cols; destroy_matrix(L); destroy_matrix(U); destroy_matrix(P); matrix_t Lt, Ut, Pt; if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return -1; } if(A.cols != A.rows){ printf("ERROR: matrix is not square\n"); return -1; } Lt = create_identity_matrix(m); Ut = create_square_matrix(m); Pt = create_identity_matrix(m); for(i=0;i<m-1;i++){ index = i; for(j=i;j<m;j++){ if(fabs(A.data[j][i]) >= fabs(A.data[index][i])){ index = j; } } if(index != i){ for(j=0;j<m;j++){ temp = A.data[index][j]; A.data[index][j] = A.data[i][j]; A.data[i][j] = temp; temp = Pt.data[index][j]; Pt.data[index][j] = Pt.data[i][j]; Pt.data[i][j] = temp; } } } for(i=0;i<m;i++){ for(j=0;j<m;j++){ s1 = 0; s2 = 0; for(k=0;k<i;k++){ s1 += Ut.data[k][j] * Lt.data[i][k]; } for(k=0;k<j;k++){ s2 += Ut.data[k][j] * Lt.data[i][k]; } if(j>=i) Ut.data[i][j] = A.data[i][j] - s1; if(i>=j) Lt.data[i][j] = (A.data[i][j] - s2)/Ut.data[j][j]; } } *L = Lt; *U = Ut; *P = Pt; return 0; }
void multiplicar(int dimension, double *matriz1, double *matriz2) { matrix_t* matrizA; matrix_t* matrizB; matrix_t* producto; matrizA = (matrix_t*) create_matrix(dimension, dimension); matrizB = (matrix_t*) create_matrix(dimension, dimension); load_matrix(matrizA, matriz1); load_matrix(matrizB, matriz2); producto = (matrix_t*) matrix_multiply(matrizA, matrizB); print_matrix(stdout, producto); destroy_matrix(matrizA); destroy_matrix(matrizB); destroy_matrix(producto); }
int main(int argc, char *argv[]) { pclu_context *pclu = pclu_create_context(); printf("\n%s\n", pclu_context_info(pclu)); matrix *aa = create_random_matrix(SIZE, SIZE); matrix *bb = create_identity_matrix(SIZE, SIZE); #if PRINT_DATA printf("Matrix aa:\n"); print_matrix(aa); printf("\n"); printf("Matrix bb:\n"); print_matrix(bb); printf("\n"); #endif timer* tt1 = timer_alloc(); matrix *cc = create_matrix(SIZE, SIZE); matrix_multiply_cl(pclu, cc, aa, bb); double tm1 = timer_read(tt1); timer_free(tt1); printf("Matrix_multiply_cl took %.04f seconds.\n", tm1); #if PRINT_DATA printf("Matrix cc:\n"); print_matrix(cc); printf("\n"); #endif timer* tt = timer_alloc(); check_result(aa, cc); double ct = timer_read(tt); timer_free(tt); printf("Check result took %.04f seconds.\n", ct); destroy_matrix(aa); destroy_matrix(bb); pclu_destroy_context(pclu); return 0; }
/* PURPOSE: add matrix to the array of all matrices that have been created during program execution INPUTS: array of matrices, the new matrix to be added, and the number of matrices OUTPUTS: the position at which the array was added */ unsigned int add_matrix_to_array (Matrix_t** mats, Matrix_t* new_matrix, unsigned int num_mats) { //TODO ERROR CHECK INCOMING PARAMETERS if( mats == NULL ) { printf( "No array of matrices present.\n" ); return -1; } if( new_matrix == NULL ) { printf( "New matrix could not be made.\n" ); return -1; } if( num_mats < 1 ) { printf( "No array of matrices present.\n" ); return -1; } static long int current_position = 0; const long int pos = current_position % num_mats; if ( mats[pos] ) { destroy_matrix(&mats[pos]); } mats[pos] = new_matrix; current_position++; return pos; }
/*** * Purpose: Add a matrix to the list of matrices. Overwrites old * matrix if a matrix already exists in the desired location * Input: The list of matrices, * the new matrix, * the number of matrices in the list * Return: The positon of the new matrix in the list ***/ unsigned int add_matrix_to_array (Matrix_t** mats, Matrix_t* new_matrix, unsigned int num_mats) { // ERROR CHECK INCOMING PARAMETERS if(!mats){ printf("No list found\n"); return -1; } if(num_mats > 10 || num_mats < 0 ){ printf("Invalid number of matrices\n"); return -1; } if(!new_matrix){ printf("No matrix found"); return -1; } if(!new_matrix->data){ printf("No data found in new matrix\n"); return -1; } static long int current_position = 0; const long int pos = current_position % num_mats; if ( mats[pos] ) { destroy_matrix(&mats[pos]); } mats[pos] = new_matrix; current_position++; return pos; }
double determinant(Matrix *m){ Matrix *copy; unsigned int i, j; double det, factor; if(m == NULL) return -1; if(m->columns != m->rows) return -1; copy = clone(m); det = 1; /* reduce each of the rows to get a lower triangle */ for(i = 0; i < copy->columns; i++){ for(j = i + 1; j < copy->rows; j++){ if(copy->numbers[i][i] == 0) continue; factor = copy->numbers[i][j]/(copy->numbers[i][i]); reduce(copy, i, j, factor); } } for(i = 0; i < copy->columns; i++) det *= copy->numbers[i][i]; destroy_matrix(copy); return det; }
/******************************************************************************* * float matrix_determinant(matrix_t A) * * *******************************************************************************/ float matrix_determinant(matrix_t A){ int i,j,k; float ratio, det; if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return -1; } if (A.rows != A.cols){ printf("Error: Matrix is not square\n"); return -1; } matrix_t temp = duplicate_matrix(A); for(i=0;i<A.rows;i++){ for(j=0;j<A.rows;j++){ if(j>i){ ratio = temp.data[j][i]/temp.data[i][i]; for(k=0;k<A.rows;k++){ temp.data[j][k] = temp.data[j][k] - ratio * temp.data[i][k]; } } } } det = 1; //storage for determinant for(i=0;i<A.rows;i++) det = det*temp.data[i][i]; destroy_matrix(&temp); return det; }
/******************************************************************************* * matrix_t invert_matrix(matrix_t A) * * Invert Matrix function based on LUP decomposition and then forward and * backward substitution. *******************************************************************************/ matrix_t invert_matrix(matrix_t A){ int i,j,k,m; matrix_t L,U,P,D,temp; matrix_t out = create_empty_matrix(); if(!A.initialized){ printf("ERROR: matrix not initialized yet\n"); return out; } if(A.cols != A.rows){ printf("ERROR: matrix is not square\n"); return out; } if(matrix_determinant(A) == 0){ printf("ERROR: matrix is singular, not invertible\n"); return out; } m = A.cols; LUP_decomposition(A,&L,&U,&P); D = create_identity_matrix(m); temp = create_square_matrix(m); for(j=0;j<m;j++){ for(i=0;i<m;i++){ for(k=0;k<i;k++){ D.data[i][j] -= L.data[i][k] * D.data[k][j]; } } for(i=m-1;i>=0;i--){ // backwards.. last to first temp.data[i][j] = D.data[i][j]; for(k=i+1;k<m;k++){ temp.data[i][j] -= U.data[i][k] * temp.data[k][j]; } temp.data[i][j] = temp.data[i][j] / U.data[i][i]; } } // multiply by permutation matrix out = multiply_matrices(temp, P); // free allocation destroy_matrix(&temp); destroy_matrix(&L); destroy_matrix(&U); destroy_matrix(&P); destroy_matrix(&D); return out; }
/* *purpose: to destroy the remaining matrixes and complete the missing memory *input: an address to a pointer of type Matrix_t, and an unsigned integer for the number of matrixes *output:no return value */ void destroy_remaining_heap_allocations(Matrix_t **mats, unsigned int num_mats) { //TODO ERROR CHECK INCOMING PARAMETERS for (unsigned int i=0; i <num_mats; ++i) { destroy_matrix(&mats[i]); } // COMPLETE MISSING MEMORY CLEARING HERE }
int main(int argc, char* argv[]) { if (argc < 2) { printf("Parametros: [matriz]\n"); exit(1); } even_matrix = new_matrix_from_file(argv[1]); odd_matrix = clone_matrix(even_matrix); printf("Arquivo: %s\n", argv[1]); printf("Calculando [...]\n\n"); pthread_barrier_init(&barrier, NULL, N_THREADS); pthread_t *ptr_threads = (pthread_t *) malloc (N_THREADS * sizeof(pthread_t)); for (int i = 0; i < N_THREADS; i++) { Data * sub_matrix = malloc(sizeof(Data)); sub_matrix->lin_min = i * (even_matrix.lin / N_THREADS); sub_matrix->col_min = i * (even_matrix.col / N_THREADS); sub_matrix->lin_max = (i + 1) * (even_matrix.lin / N_THREADS) - 1; sub_matrix->col_max = (i + 1) * (even_matrix.col / N_THREADS) - 1; pthread_create(&ptr_threads[i], NULL, calc_sor, (void*) sub_matrix); } for (int i = 0; i < N_THREADS; i++) { pthread_join(ptr_threads[i], NULL); } pthread_barrier_destroy(&barrier); Matrix result = merge(even_matrix, odd_matrix); #ifdef TEST print_matrix(result); printf("\n"); #endif matrix_to_file(result, "result.txt"); printf("Resultado gravado no arquivo: result.txt\n"); destroy_matrix(even_matrix); destroy_matrix(odd_matrix); return 1; };
int main() { Matrix *A = init_matrix(3, 3); Matrix *B = init_matrix(3, 2); (A->values)[0][0] = 0; (A->values)[0][1] = 1; (A->values)[0][2] = 2; (A->values)[1][0] = 3; (A->values)[1][1] = 4; (A->values)[1][2] = 5; (A->values)[2][0] = 1; (A->values)[2][1] = 1; (A->values)[2][2] = 1; (B->values)[0][0] = 0; (B->values)[0][1] = 1; (B->values)[1][0] = 2; (B->values)[1][1] = 3; (B->values)[2][0] = 4; (B->values)[2][1] = 5; print_matrix(A); print_matrix(B); Matrix *C = mmult(A, B); print_matrix(C); destroy_matrix(A); destroy_matrix(B); destroy_matrix(C); return 0; }
static Aes shift_rows(OE oe, Aes aes) { Aes res = Aes_new(oe); MATRIX * SR = load_matrix(oe,(byte*)tbl_shift_rows,16,16); MATRIX * V = load_matrix(oe,aes->state,16,1); MATRIX * R = matrix_multiplication(SR,V); uint i = 0; destroy_matrix(SR); destroy_matrix(V); for(i = 0;i < 16;++i) { res->state[i] = matrix_getentry(R,i,0); } destroy_matrix(R); return res; }
/* * PURPOSE: free the memory of matrix list * INPUTS: * mats the matrix list * num_mats the number of matrix in the list * RETURN: void * If no errors occurred during destroy then return nothing * else print error message **/ void destroy_remaining_heap_allocations(Matrix_t **mats, unsigned int num_mats) { //TODO ERROR CHECK INCOMING PARAMETERS if(!(*mats)){ printf("matrix list is empty\n"); return; } // COMPLETE MISSING MEMORY CLEARING HERE for (int i = 0; i < num_mats; ++i){ destroy_matrix(&mats[i]); } }
MATRIX * pow3(MATRIX * SR) { MATRIX * T = 0, * T1 = 0; T = MM(SR,SR); T1 = MM(T,SR); destroy_matrix(T); return T1; }
static Aes mix_columns(OE oe, Aes aes) { Aes res = Aes_new(oe); MATRIX * MC = load_matrix(oe,(byte*)tbl_mix_columns,16,16); MATRIX * V = load_matrix(oe,aes->state,16,1); MATRIX * R = matrix_multiplication(MC,V); uint i = 0; destroy_matrix(MC); destroy_matrix(V); for(i = 0;i < 16;++i) { res->state[i] = matrix_getentry(R,i,0); } destroy_matrix(R); return res; }
static char *test_compare_matrices() { // By default newly create marices have all elements // equal to 0. So a and b should be equal. Matrix *a = create_matrix(2, 2); Matrix *b = create_matrix(2, 2); mu_assert("a != b", compare_matrices(a, b) == 1); // Let's modify their values a bit. a->value[1][1] = 2; b->value[0][0] = 1; // They should be not equal now mu_assert("a == b, should be different", compare_matrices(a, b) == 0); destroy_matrix(a); destroy_matrix(b); return 0; }
static char *test_add_matrices() { // We should create 2 empty matrices. If we add them we should get // an empty matrix. Matrix *a = create_matrix(2, 2); Matrix *b = create_matrix(2, 2); // We will store the result of the adition in a third matrix c. Matrix *c = create_matrix(2, 2); add_matrices(a, b, &c); // Since a and b are empty their result will an empty matrix too // so it makes sense to compare c against b or a. mu_assert("c != a or b", compare_matrices(a, c)); destroy_matrix(a); destroy_matrix(b); destroy_matrix(c); return 0; }
/******************************************************************************* * vector_t lin_system_solve_qr(matrix_t A, vector_t b) * * Gives a least-squares solution to the system AX=b for non-square A using QR. * * Ax=b * QRx=b * Rx=Q'b (because Q'Q=I) * then solve for x with gaussian elimination *******************************************************************************/ vector_t lin_system_solve_qr(matrix_t A, vector_t b){ vector_t xout = create_empty_vector(); vector_t temp; matrix_t Q,R; int i,k; if(!A.initialized || !b.initialized){ printf("ERROR: matrix or vector not initialized yet\n"); return xout; } // do QR decomposition if(QR_decomposition(A,&Q,&R)<0){ printf("failed to perform QR decomposition on A\n"); return xout; } // transpose Q matrix if(transpose_matrix(&Q)<0){ printf("ERROR: failed to transpose Q\n"); return xout; } // multiply through temp = matrix_times_col_vec(Q,b); destroy_matrix(&Q); // solve for x knowing R is upper triangular int nDim = R.cols; xout = create_vector(nDim); for(k=(nDim-1); k>=0; k--){ xout.data[k] = temp.data[k]; for(i=(k+1); i<nDim; i++){ xout.data[k] -= (R.data[k][i]*xout.data[i]); } xout.data[k] = xout.data[k] / R.data[k][k]; } destroy_matrix(&R); destroy_vector(&temp); return xout; }
static char *test_compute_inverse() { Matrix *a = create_matrix(3, 3); // The matrix b will contain the value of the a's inverse. Matrix *b = create_matrix(3, 3); b->value[0][0] = -1; b->value[0][1] = 0.5; b->value[0][2] = 0; b->value[1][0] = 0.5; b->value[1][1] = -1; b->value[1][2] = 0.5; b->value[2][0] = 0.3333333; b->value[2][1] = 0.5; b->value[2][2] = -0.333333; for(int i = 0, k = 1; i < 3; i++) { for(int j = 0; j < 3; j++, k++) { if(k != 5) { a->value[i][j] = k; } else { // The matrix that has all the values from 1 to 9 in spiral // has no inverse, so I replaced the 5 with an four. a->value[i][j] = 4; } } } a->determinant = get_determinant(a); compute_inverse(a); mu_assert("The invers is not equal to the matrix b", compare_matrices( a->inverse, b)); destroy_matrix(a); destroy_matrix(b); return 0; }
int main(void) { matrix_t* m = create_matrix(3, 3); load_value(m, 0, 0, 1.0); load_value(m, 0, 1, 2.0); load_value(m, 0, 2, 3.0); load_value(m, 1, 0, 4.0); load_value(m, 1, 1, 5.0); load_value(m, 1, 2, 6.1); load_value(m, 2, 0, 3.0); load_value(m, 2, 1, 2.0); load_value(m, 2, 2, 1.0); matrix_t* m1 = create_matrix(3, 3); load_value(m1, 0, 0, 1.0); load_value(m1, 0, 1, 0.0); load_value(m1, 0, 2, 0.0); load_value(m1, 1, 0, 0.0); load_value(m1, 1, 1, 1.0); load_value(m1, 1, 2, 0.0); load_value(m1, 2, 0, 0.0); load_value(m1, 2, 1, 0.0); load_value(m1, 2, 2, 1.0); print_matrix_std_o(m); printf("\n"); print_matrix_std_o(m1); printf("\n"); matrix_t* result = matrix_multiply(m, m1); print_matrix_std_o(result); destroy_matrix(m); destroy_matrix(m1); destroy_matrix(result); return EXIT_SUCCESS; }
void matrix_destroy(matrix_t *p1, matrix_t *p2, matrix_t *p3, matrix_t *p4, matrix_t *p5, matrix_t *p6, matrix_t *p7) { destroy_matrix(p1); destroy_matrix(p2); destroy_matrix(p3); destroy_matrix(p4); destroy_matrix(p5); destroy_matrix(p6); destroy_matrix(p7); }
static Aes shift_row_mix_cols(OE oe, Aes aes) { MATRIX * state = load_matrix(oe,aes->state,16,1); //MATRIX * SRMC = load_matrix(oe, (byte*)srmc, 16, 16 ); MATRIX * SR = load_matrix(oe, (byte*)tbl_shift_rows, 16, 16); MATRIX * MC = load_matrix(oe, (byte*)tbl_mix_columns, 16, 16); MATRIX * SRMC = matrix_multiplication(MC, SR); MATRIX * res = matrix_multiplication(SRMC,state); uint i = 0; Aes aesres = 0; destroy_matrix(SR); destroy_matrix(MC); destroy_matrix(SRMC); destroy_matrix(state); aesres = Aes_new(oe); for(i = 0;i < 16;++i) { aesres->state[i] = matrix_getentry(res,i,0); } destroy_matrix(res); return aesres; }
/* * Purpose: add_matrix_to_array adds a new matrix to the array of matrices * where a maximum of 10 matrices exist. Memory deallocation occurs * when more than 10 matrices are created * Input: This function takes in the matrix struct pointer, the new * matrix pointer, and the number of matrices * Return: It returns the posistion (or index) of the added matrix */ unsigned int add_matrix_to_array (Matrix_t** mats, Matrix_t* new_matrix, unsigned int num_mats) { if(!new_matrix || (num_mats < 0)) { return -1; } static long int current_position = 0; const long int pos = current_position % num_mats; if ( mats[pos] ) { destroy_matrix(&mats[pos]); } mats[pos] = new_matrix; current_position++; return pos; }
/* * PURPOSE: Add a matrix to an array of matrices * INPUTS: A matrix array mats which is to be added to, a matrix new_matrix which will be added to the array int num_mats current number of matrix in array * RETURN: position or index of array after operation **/ unsigned int add_matrix_to_array (Matrix_t** mats, Matrix_t* new_matrix, unsigned int num_mats) { //TODO ERROR CHECK INCOMING PARAMETERS if(!new_matrix) { return -1; } static long int current_position = 0; const long int pos = current_position % num_mats; if ( mats[pos] ) { destroy_matrix(&mats[pos]); } mats[pos] = new_matrix; current_position++; return pos; }
/* m1 x m2 */ Matrix *multiply(Matrix *m1, Matrix *m2){ Matrix *product, *trans; unsigned int i, j; if(m1 == NULL || m2 == NULL) return NULL; if(m1->columns != m2->rows) return NULL; trans = transpose(m1); product = constructor(m1->rows, m2->columns); for(i = 0; i < product->columns; i++){ for(j = 0; j < product->rows; j++){ product->numbers[i][j] = vector_multiply(trans->numbers[j], m2->numbers[i], product->rows); } } destroy_matrix(trans); return product; }