// Matrix multiply c = a * b, c can be a or b or overlap either decNumber *matrix_multiply(decNumber *r, const decNumber *a, const decNumber *b, const decNumber *c) { int arows, acols, brows, bcols; decNumber sum, s, t, u; int creg; int i, j, k; decimal64 result[MAX_DIMENSION]; decimal64 *rp = result; decimal64 *abase = matrix_decomp(a, &arows, &acols); decimal64 *bbase = matrix_decomp(b, &brows, &bcols); if (abase == NULL || bbase == NULL) return NULL; if (acols != brows) { err(ERR_MATRIX_DIM); return NULL; } creg = dn_to_int(c); if (matrix_descriptor(r, creg, arows, bcols) == 0) return NULL; busy(); for (i=0; i<arows; i++) for (j=0; j<bcols; j++) { decNumberZero(&sum); for (k=0; k<acols; k++) { matrix_get(&s, abase, i, k, acols); matrix_get(&t, bbase, k, j, bcols); dn_multiply(&u, &s, &t); dn_add(&sum, &sum, &u); } packed_from_number(rp++, &sum); } xcopy(get_reg_n(creg), result, sizeof(decimal64) * arows * bcols); return r; }
/** Multiplies two matrices. Stores the result in dest. * * @param a a Matrix pointer * @param b a Matrix pointer * @param dest pointer to a matrix in which to store the result, or NULL to allocate a new one * * @return dest, or a pointer to a new Matrix if dest is NULL */ Matrix* matrix_mult(Matrix *a, Matrix *b, Matrix *dest) { unsigned int i, j, k; scalar_t s; assert(a->cols == b->rows); if(!dest) dest = matrix_new(a->rows, b->cols); else assert(dest->rows == a->rows && dest->cols == b->cols) ; for(i=0; i < dest->rows; i++) { for(j=0; j < dest->cols; j++) { s = 0; for(k=0; k < a->cols; k++) s += (matrix_get(a,i,k) * matrix_get(b,k,j)); matrix_set(dest, i, j, s); } } return dest; }
/** Computes the determinant of a matrix * * @param m a Matrix pointer * * @return the determinant of m */ scalar_t matrix_det(Matrix *m) { Matrix *minor; unsigned int j; scalar_t rdet, sign; assert(MATRIX_IS_SQUARE(m)); if(m->cols == 2) { return ((matrix_get(m,0,0) * matrix_get(m,1,1)) - (matrix_get(m,0,1) * matrix_get(m,1,0))); } else { rdet = 0; for(j=0; j < m->cols; j++) { minor = matrix_minor(m, 0, j); sign = (j%2) ? S_LITERAL(1.0) : S_LITERAL(-1.0); rdet += sign * matrix_get(m, 0, j) * matrix_det(minor); matrix_free(minor); } return rdet; } return 0; }
bool matrix_equals(Matrix *m1, Matrix *m2) { int equals = m1->cols == m2->cols && m1->rows == m2->rows; for (size_t i = 0; equals && i < m1->rows; ++i) { for (size_t j = 0; equals && j < m1->cols; ++j) { equals = equals && matrix_get(m1, i, j) == matrix_get(m2, i, j); } } return equals; }
double accuracy(matrix_list_t* theta, matrix_t* X, matrix_t* y) { assert(theta->num == 2); matrix_t* theta_transpose, *temp, *temp2; theta_transpose = matrix_transpose(theta->matrix_list[0]); temp = matrix_prepend_col(X, 1.0); temp2 = matrix_multiply(temp, theta_transpose); matrix_t* h1 = matrix_sigmoid(temp2); free_matrix(theta_transpose); free_matrix(temp); free_matrix(temp2); theta_transpose = matrix_transpose(theta->matrix_list[1]); temp = matrix_prepend_col(h1, 1.0); temp2 = matrix_multiply(temp, theta_transpose); matrix_t* h2 = matrix_sigmoid(temp2); free_matrix(theta_transpose); free_matrix(temp); free_matrix(temp2); assert(h2->rows == 5000 && h2->cols == 10); matrix_t* p = matrix_constructor(1, 5000); int i, j; for(i = 0; i<h2->rows; i++) { double max = 0.0; unsigned char first = 1; for(j=0; j<h2->cols; j++) { if(matrix_get(h2, i, j) > max || first == 1) { vector_set(p, i, j); max = matrix_get(h2, i, j); first = 0; } } } double count = 0; for(i=0; i<5000; i++) { if(vector_get(y, i) == vector_get(p, i)) count = count + 1; } free_matrix(p); free_matrix(h1); free_matrix(h2); return count/5000; }
void matrix_hadamard(Matrix *m1, Matrix *m2, Matrix **result) { if (m1->cols != m2->cols || m1->rows != m2->rows) { errno = EDOM; return; } matrix_create0(m1->rows, m1->cols, result, false); for (size_t i = 0; i < m1->rows; ++i) { for (size_t j = 0; j < m1->cols; ++j) { matrix_set(*result, i, j, matrix_get(m1, i, j) * matrix_get(m2, i, j)); } } }
/* The easy way to compare is to compere the number of rows and columns */ int matrix_equal(Matrix* mat1, Matrix* mat2) { int i; int j; for (i = 0; i < mat1->number_of_rows; i++) { for (j = 0; j < mat1->number_of_columns; j++) { if (matrix_get(mat1, i, j) != matrix_get(mat2, i, j)) { return 0; } } } return 1; }
int matrix_compare(Matrix *a, Matrix *b) { unsigned int i, j; if(!MATRIX_CONGRUENT(a,b)) return -1; for(i=0; i < a->rows; i++) for(j=0; j < a->cols; j++) if ( S_NE(matrix_get(a, i, j), matrix_get(b, i, j))) return 1; return 0; }
void matrix_product(Matrix *m1, Matrix *m2, Matrix **result) { if (m1->cols != m2->rows) { errno = EDOM; return; } matrix_create0(m1->rows, m2->cols, result, false); for (size_t i = 0; i < m1->rows; ++i) { for (size_t j = 0; j < m2->cols; ++j) { double sum = 0; for (size_t k = 0; k < m2->rows; ++k) sum += matrix_get(m1, i, k) * matrix_get(m2, k, j); matrix_set(*result, i, j, sum); } } }
static void community_thread_body(void* params){ int root_i, root_j; // matrix iterators int i=-1,j; int k; uint8_t overlapping_value; unsigned long int rows_to_be_read; dsforest_t *thread_dsf; // cast the void* pointer to the struct used for passing us our parameters // and get them struct community_thread_data* ctdata = (struct community_thread_data*) params; dsforest_init(&thread_dsf, all_cliques->maximal_cliques_total); for(k=k_max; k >= 3; k--){ // get the first index in the window i = ctdata->start_from_row; // get the number of rows that must be read, given a k cliques_rows_to_be_read(all_cliques, k, &rows_to_be_read); if(i >= rows_to_be_read) continue; for (;matrix_row_in_window(m,i) == TRUE && i < rows_to_be_read; i+=NUM_THREADS) { root_i = dsforest_find(thread_dsf, i); j = i+1; for (; j < rows_to_be_read; j++) { // overlapping if(matrix_get(m, i, j, &overlapping_value) != 0){ printf("Error reading the matrix\n"); pthread_exit(NULL); } if (overlapping_value < k-1) continue; overlapping_value=0; if(matrix_set(m, i, j, overlapping_value) != 0){ printf("Error writing the matrix\n"); pthread_exit(NULL); } root_j = dsforest_find(thread_dsf, j); if(root_i != root_j){ // since after the union the root node of i, i.e. i_root, could be changed // we must verify if it has become a non-root node and update it accordingly. // i_root can change if it has been linked as son of j_root because of // a smaller rank dsforest_union(thread_dsf, root_i, root_j); if(!dsforest_is_root(thread_dsf, root_i)) root_i = dsforest_find(thread_dsf, i); } } } community_thread_epilogue(thread_dsf, k, rows_to_be_read); } dsforest_destroy(thread_dsf); pthread_exit(NULL); }
/********************************************************************** * get_piece_rating * * Check to see if this piece has already been classified. If it has * return that rating. Otherwise build the piece from the smaller * pieces, classify it, store the rating for later, and take the piece * apart again. **********************************************************************/ CHOICES get_piece_rating(MATRIX ratings, TBLOB *blobs, SEAMS seams, INT16 start, INT16 end, INT32 fx, STATE *this_state, STATE *best_state, INT32 pass, INT32 blob_index) { CHOICES choices; choices = matrix_get (ratings, start, end); if (choices == NOT_CLASSIFIED) { choices = classify_piece(blobs, seams, start, end, fx, this_state, best_state, pass, blob_index); matrix_put(ratings, start, end, choices); } return (choices); }
int main(int argc, char **argv) { FILE *swp; struct cache *c; struct matrix *m; FILE *matrix_shared; readargs(argc, argv); if ((swp = fopen(swapfile, "w+")) == NULL) error("cannot open swap file"); c = cache_create(&access_info, swp, CACHE_SIZE); /* Read traces. */ for (int i = 0; i < ntraces; i++) { FILE *trace; if ((trace = fopen(tracefiles[i], "r")) == NULL) error("cannot open trace file"); trace_read(c, trace, i); fclose(trace); fprintf(stderr, "\nFechado arquivo de trace da thread %d\n\n", i); } /* Flushe traces on swap file. */ cache_flush(c); /* Create communication matrix. */ fseek(swp, 0, SEEK_SET); m = matrix_create(QTD_THREADS, QTD_THREADS); fprintf(stderr, "\nMatriz criada\n"); matrix_generate(swp, m); if ((matrix_shared = fopen(outfile, "w")) == NULL) error("cannot open output file"); fprintf(stderr, "\nGravar matriz no arquivo\n"); for (int i = 0; i < ntraces; i++) { for(int j = 0; j < ntraces; j++) fprintf(matrix_shared, "%d;%d;%d\n", i, j, (int) matrix_get(m, i, j)); } /* House keeping. */ fclose(matrix_shared); fclose(swp); fprintf(stderr, "\n\n FIM!\n"); return (EXIT_SUCCESS); }
MAT *px_rows(const PERM *px, const MAT *A, MAT *out) { int i, j, m, n, px_i; MatrixReal **A_me, **out_me; MAT *matrix_get(int, int); if ( ! A || ! px ) error(E_NULL,"px_rows"); if ( px->size != A->m ) error(E_SIZES,"px_rows"); if ( A == out ) error(E_INSITU,"px_rows"); m = A->m; n = A->n; if ( ! out || out->m != (unsigned int)m || out->n != (unsigned int)n ) out = matrix_get(m,n); A_me = A->me; out_me = out->me; for ( i = 0; i < m; i++ ) { px_i = px->pe[i]; if ( px_i >= m ) error(E_BOUNDS,"px_rows"); for ( j = 0; j < n; j++ ) out_me[i][j] = A_me[px_i][j]; } return out; }
/** Creates the minor of a matrix by dropping the ith row and the jth column. * * @param m a pointer to a matrix * @param row the row number to drop, from 0 to m->rows-1 * @param col the column number to drop, from 0 to m->cols-1 * * @return the resulting minor matrix */ Matrix *matrix_minor(Matrix *m, const unsigned int row, const unsigned int col) { Matrix *out; unsigned int io, im, jo, jm; assert(row < m->rows && col < m->cols); #ifdef MATRIX_BY_ROW /* We'll need to just copy everything */ out = matrix_new(m->rows - 1, m->cols - 1); #else /* MATRIX_BY_VALUE */ out = (Matrix *) malloc(sizeof(Matrix)); out->flags = 0 | IS_CHILD; out->rows = m->rows - 1; out->cols = m->cols - 1; out->data = (scalar_t **) malloc(sizeof(scalar_t *) * out->rows * out->cols); #endif for(io=im=0; (io < out->rows) && (im < m->rows); io++, im++) { /* Skip column 'col' */ if(im == col) im++; for(jo=jm=0; (jo < out->cols) && (jm < m->cols); jo++, jm++) { /* Skip row 'row' */ if(jm == row) jm++; #ifdef MATRIX_BY_ROW matrix_set(out, io, jo, matrix_get(m, im, jm)); #else /* MATRIX_BY_VALUE */ matrix_set_ptr(out, io, jo, matrix_get_ptr(m, im, jm)); #endif } } return out; }
void matrix_transpose(Matrix *m, Matrix **result) { matrix_create0(m->cols, m->rows, result, false); for (size_t i = 0; i < m->rows; ++i) { for (size_t j = 0; j < m->cols; ++j) { matrix_set(*result, j, i, matrix_get(m, i, j)); } } }
void matrix_apply(Matrix* matrix, double (*f)(double), Matrix** result) { matrix_create0(matrix->rows, matrix->cols, result, false); for (size_t i = 0; i < matrix->rows; ++i) { for (size_t j = 0; j < matrix->cols; ++j) { matrix_set(*result, i, j, f(matrix_get(matrix, i, j))); } } }
void matrix_mul(Matrix *matrix, double factor, Matrix **result) { matrix_create0(matrix->rows, matrix->cols, result, false); for (size_t i = 0; i < matrix->rows; ++i) { for (size_t j = 0; j < matrix->cols; ++j) { matrix_set(*result, i, j, matrix_get(matrix, i, j) * factor); } } }
matrix_t matrix_add (matrix_t a, matrix_t b) { int i, j; matrix_t mret; if (!(mret = make_matrix (b->rn, b->cn))) return NULL; for (j = 0; j < b->cn; j++) { for (i = 0; i < b->rn; i++) { matrix_put (mret, i, j, matrix_get (a, i, j) + matrix_get (b, i, j)); } } return mret; }
bool Matrix_Copy_Real(gsl_matrix_complex *A, gsl_matrix *B){ if((A->size1==B->size1)&&(A->size2==B->size2)){ for(unsigned k=0;k<A->size1;k++) for(unsigned l=0;l<A->size2;l++) matrix_set(B,k,l,creal(matrix_get(A,k,l))); return true; } return false; }
/** Adds two matrices. Stores the result in dest. * * @param a a Matrix pointer * @param b a Matrix pointer * @param dest pointer to a matrix in which to store the result, or NULL to allocate a new one * * @return dest, or a pointer to a new Matrix if dest is NULL */ Matrix* matrix_add(Matrix *a, Matrix *b, Matrix *dest) { unsigned int i, j; assert(MATRIX_CONGRUENT(a, b)); if(!dest) dest = matrix_new(a->rows, a->cols); for(i=0; i < dest->rows; i++) { for(j=0; j < dest->cols; j++) { matrix_set(dest, i, j, matrix_get(a,i,j) + matrix_get(b,i,j)); } } return dest; }
/* zamien kolumny w matrix; zwraca 0 lub -1 dla bledu */ int matrix_swap_col (matrix_t a, int source, int target) { int i; int mwidth = (*a).cn; matrix_t m_tmp; if (source == target) return 0; if (!(m_tmp = make_matrix (1, mwidth))) return -1; for (i = 0; i < mwidth; i++) matrix_insert (m_tmp, 0, i, matrix_get (a, source, i)); for (i = 0; i < mwidth; i++) matrix_insert (a, source, i, matrix_get (a, target, i)); for (i = 0; i < mwidth; i++) matrix_insert (a, target, i, matrix_get (m_tmp, 0, i)); free_matrix (m_tmp); return 0; }
matrix *matrix_mmul(matrix* m1, matrix* m2) { ASSERT(matrix_num_cols(m1) == matrix_num_rows(m2)); matrix* m = matrix_new(matrix_num_rows(m1), matrix_num_cols(m2)); int row, col, i; for (row = 0; row < matrix_num_rows(m); row++) { for (col = 0; col < matrix_num_cols(m); col++) { double sum = 0; for (i = 0; i < matrix_num_cols(m1); i++) { sum += matrix_get(m1, row, i) * matrix_get(m2, i, col); } matrix_set(m, row, col, sum); } } return m; }
void Matrix_Print(gsl_matrix_complex *M){ double _Complex T; for(unsigned l=0;l<M->size1;l++){ for(unsigned k=0;k<M->size2;k++){ T=matrix_get(M,l,k); printf("%E %E*i\t",creal(T),cimag(T)); } printf("\n"); } }
void Matrix_Print(gsl_matrix *M){ double T; for(unsigned l=0;l<M->size1;l++){ for(unsigned k=0;k<M->size2;k++){ T=matrix_get(M,l,k); printf("%E\t",T); } printf("\n"); } }
matrix_t matrix_copy (matrix_t a) { int i, j; matrix_t m = NULL; m = make_matrix (a->rn, a->cn); for (j = 0; j < a->cn; j++) for (i = 0; i < a->rn; i++) matrix_put (m, i, j, matrix_get (a, i, j)); return m; }
/* zamien wiersze w matrix; zwraca 0 lub -1 dla bledu */ int matrix_swap_row (matrix_t a, int source, int target) { int i; int mwidth = (*a).rn; matrix_t m_tmp; if (source == target) return 0; if (!(m_tmp = make_matrix (mwidth, 1))) return -1; for (i = 0; i < mwidth; i++) matrix_insert (m_tmp, i, 0, matrix_get (a, i, source)); for (i = 0; i < mwidth; i++) matrix_insert (a, i, source, matrix_get (a, i, target)); for (i = 0; i < mwidth; i++) matrix_insert (a, i, target, matrix_get (m_tmp, i, 0)); free_matrix (m_tmp); return 0; }
MAT *m_inverse(const MAT *A, MAT *out) { unsigned int i; char MatrixTempBuffer[ 4000 ]; VEC *tmp = VNULL, *tmp2 = VNULL; MAT *A_cp = MNULL; PERM *pivot = PNULL; if ( ! A ) error(E_NULL,"m_inverse"); if ( A->m != A->n ) error(E_SQUARE,"m_inverse"); if ( ! out || out->m < A->m || out->n < A->n ) out = m_resize(out,A->m,A->n); if( SET_MAT_SIZE( A->m, A->n ) < 1000 ) mat_get( &A_cp, (void *)( MatrixTempBuffer + 2000 ), A->m, A->n ); else A_cp = matrix_get( A->m, A->n ); A_cp = m_copy( A, A_cp ); if( SET_VEC_SIZE( A->m ) < 1000 ) { vec_get( &tmp, (void *)MatrixTempBuffer, A->m ); vec_get( &tmp2, (void *)(MatrixTempBuffer + 1000), A->m ); } else { tmp = v_get( A->m ); tmp2 = v_get( A->m ); } if( SET_PERM_SIZE( A->m ) < 1000 ) { perm_get( &pivot, (void *)( MatrixTempBuffer + 3000 ), A->m ); } else { pivot = px_get( A->m ); } LUfactor(A_cp,pivot); //tracecatch_matrix(LUfactor(A_cp,pivot),"m_inverse"); for ( i = 0; i < A->n; i++ ){ v_zero(tmp); tmp->ve[i] = 1.0; LUsolve(A_cp,pivot,tmp,tmp2); //tracecatch_matrix(LUsolve(A_cp,pivot,tmp,tmp2),"m_inverse"); set_col(out,i,tmp2); } if( tmp != (VEC *)(MatrixTempBuffer ) ) // память выделялась, надо освободить V_FREE(tmp); if( tmp2 != (VEC *)(MatrixTempBuffer + 1000) ) // память выделялась, надо освободить V_FREE(tmp2); if( A_cp != (MAT *)(MatrixTempBuffer + 2000) ) // память выделялась, надо освободить M_FREE(A_cp); if( pivot != (PERM *)(MatrixTempBuffer + 3000) ) // память выделялась, надо освободить PX_FREE( pivot ); return out; }
static inline void matrix_fill_row(sp_matrix_t *m, int row, bitset_t *fullrow) { bitset_set(fullrow, row); matrix_foreach_in_col(m, row, e) { if (! bitset_is_set(fullrow, e->row)) { assert(0.0 == matrix_get(m, e->col, e->row)); matrix_set(m, e->col, e->row, e->val); matrix_set(m, e->row, e->col, 0.0); } } }
void matrix_display(Matrix* mat) { int i; int j; for (i = 0; i < mat->number_of_rows; i++) { for (j = 0; j < mat->number_of_columns; j++) { printf(" %5.5f ", matrix_get(mat, i, j)); } printf("\n"); } }
void matrix_display_integers(Matrix* mat) { int i; int j; for (i = 0; i < mat->number_of_rows; i++) { for (j = 0; j < mat->number_of_columns; j++) { printf(" %d ", (int) matrix_get(mat, i, j)); } printf("\n"); } }