static void *mtx_row_new(t_symbol *s, int argc, t_atom *argv) { t_matrix *x = (t_matrix *)pd_new(mtx_row_class); int i, j, q; outlet_new(&x->x_obj, 0); inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("")); x->current_row=0; x->col=x->row=0; x->atombuffer=0; switch (argc) { case 0:break; case 1: i = atom_getfloat(argv); if (i<0)i=0; if(i)adjustsize(x, i, i); matrix_set(x, 0); break; case 2: i = atom_getfloat(argv++);if(i<0)i=0; j = atom_getfloat(argv++);if(j<0)j=0; if(i*j)adjustsize(x, i, j); matrix_set(x, 0); break; default: i = atom_getfloat(argv++);if(i<0)i=0; j = atom_getfloat(argv++);if(j<0)j=0; q = atom_getfloat(argv++);if(q<0)q=0; if(i*j)adjustsize(x, i, j); matrix_set(x, 0); x->current_row=q; } return (x); }
static void set_transformation_matrix(Display *dpy, Matrix *m, int offset_x, int offset_y, int screen_width, int screen_height) { /* offset */ int width = DisplayWidth(dpy, DefaultScreen(dpy)); int height = DisplayHeight(dpy, DefaultScreen(dpy)); /* offset */ float x = 1.0 * offset_x/width; float y = 1.0 * offset_y/height; /* mapping */ float w = 1.0 * screen_width/width; float h = 1.0 * screen_height/height; matrix_set_unity(m); matrix_set(m, 0, 2, x); matrix_set(m, 1, 2, y); matrix_set(m, 0, 0, w); matrix_set(m, 1, 1, h); #if DEBUG matrix_print(m); #endif }
static void matrix_set_unity(Matrix *m) { memset(m, 0, sizeof(m->m)); matrix_set(m, 0, 0, 1); matrix_set(m, 1, 1, 1); matrix_set(m, 2, 2, 1); }
static char * test_create_initial_board_from_file_with_empty_lines() { Matrix* board; Matrix* expected_matrix; char* filename; filename = "test_island/test_island_empty_lines.txt"; expected_matrix = matrix_create(2, 2); /* 1 2 3 4 */ matrix_set(expected_matrix, 0, 0, 1); matrix_set(expected_matrix, 0, 1, 2); matrix_set(expected_matrix, 1, 0, 3); matrix_set(expected_matrix, 1, 1, 4); board = create_initial_board_from_file(filename); /* print_board("board: ", board); print_board("expected: ", expected_matrix); */ mu_assert("error, expected_matrix != board_from_file", matrix_equal(board, expected_matrix)); return 0; }
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); } } }
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); }
int main(int argc, char** argv) { if (argc != 3) { fprintf(stderr, "Usage: %s <size> <n>\n", argv[0]); return 1; } int size = atoi(argv[1]); int n = atoi(argv[2]); printf("%d\n", n); matrix* m = matrix_new(size, size); int num_cells = size * size; int print_interval = num_cells / n; int i; timer* t = timer_new(); for (i = 0; i < num_cells; i++) { int next = i; int row = next / size; int col = next % size; matrix_set(m, row, col, (double) i); if (i % print_interval == 0) { timer_start(t); m = matrix_mmul(m, m); printf("%2f %lu\n", ((i + 0.0) / num_cells) * 100, timer_nsec(t)); } } matrix_free(m); return 0; }
static void *mtx_ones_new(t_symbol *s, int argc, t_atom *argv) { t_matrix *x = (t_matrix *)pd_new(mtx_ones_class); int col=0, row=0; outlet_new(&x->x_obj, 0); x->row = x->col = 0; x->atombuffer = 0; switch(argc) { case 0: break; case 1: col=row=atom_getfloat(argv); break; default: row=atom_getfloat(argv++); col=atom_getfloat(argv); } if(col<0) { col=0; } if(row<0) { row=0; } if (col*row) { x->atombuffer = (t_atom *)getbytes((col*row+2)*sizeof(t_atom)); setdimen(x, row, col); matrix_set(x, 1); } return (x); }
void kalman_gravity_demo() { // initialize the filter kalman_gravity_init(); mf16 *x = kalman_get_state_vector_uc(&kf); mf16 *z = kalman_get_observation_vector(&kfm); // filter! uint_fast16_t i; for (i = 0; i < MEAS_COUNT; ++i) { // prediction. kalman_predict_uc(&kf); // measure ... fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]); matrix_set(z, 0, 0, measurement); // update kalman_correct_uc(&kf, &kfm); } // fetch estimated g const fix16_t g_estimated = x->data[2][0]; const float value = fix16_to_float(g_estimated); assert(value > 9.7 && value < 10); }
static void blinker(void) { led_inv(RIGHT); led_inv(LEFT); #ifdef MATRIX for(uint8_t x = 0; x < 3; ++x) { for(uint8_t y = 0; y < 3; ++y) { matrix_set(x, y, ((x + y) & 1) ^ inv); } } inv = !inv; #endif wait_ms(500); if(button_clicked(RIGHT)) { motor_on(); } if(button_clicked(LEFT)) { motor_off(); } }
static void mtx_mulelement_matrix(t_mtx_binmtx *x, t_symbol *s, int argc, t_atom *argv) { int row=atom_getfloat(argv++); int col=atom_getfloat(argv++); t_atom *m; t_atom *m2 = x->m2.atombuffer+2; int n = argc-2; if (argc<2){ pd_error(x, "crippled matrix"); return; } if ((col<1)||(row<1)) { pd_error(x, "invalid dimensions"); return; } if (col*row>argc-2){ pd_error(x, "sparse matrix not yet supported : use \"mtx_check\""); return; } if (!(x->m2.col*x->m2.row)) { adjustsize(&x->m, row, col); matrix_set(&x->m, 0); outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer); return; } if ((col!=x->m2.col)||(row!=x->m2.row)){ pd_error(x, "matrix dimension do not match"); /* LATER SOLVE THIS */ return; } adjustsize(&x->m, row, col); m = x->m.atombuffer+2; while(n--){ t_float f = atom_getfloat(argv++)*atom_getfloat(m2++); SETFLOAT(m, f); m++; } outlet_anything(x->x_obj.ob_outlet, gensym("matrix"), argc, x->m.atombuffer); }
/** 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; }
/** 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 kalman_gravity_demo_lambda() { // initialize the filter kalman_gravity_init(); mf16 *x = kalman_get_state_vector(&kf); mf16 *z = kalman_get_observation_vector(&kfm); // forcibly increase uncertainty in every prediction step by ~20% (1/lambda^2) const fix16_t lambda = F16(0.9); // filter! uint_fast16_t i; for (i = 0; i < MEAS_COUNT; ++i) { // prediction. kalman_predict_tuned(&kf, lambda); // measure ... fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]); matrix_set(z, 0, 0, measurement); // update kalman_correct(&kf, &kfm); } // fetch estimated g const fix16_t g_estimated = x->data[2][0]; const float value = fix16_to_float(g_estimated); assert(value > 9.7 && value < 10); }
static void *matrix_new(t_symbol *s, int argc, t_atom *argv) { t_matrix *x = (t_matrix *)pd_new(matrix_class); int row, col; inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym("")); outlet_new(&x->x_obj, 0); x->atombuffer = 0; x->x_canvas = canvas_getcurrent(); switch (argc) { case 0: row = col = 0; break; case 1: if (argv->a_type == A_SYMBOL) { matrix_read(x, argv->a_w.w_symbol); return(x); } row = col = atom_getfloat(argv); break; default: row = atom_getfloat(argv++); col = atom_getfloat(argv++); } if(row*col) { adjustsize(x, row, col); matrix_set(x, 0); } return (x); }
static void *mtx_eye_new(t_symbol *s, int argc, t_atom *argv) { t_matrix *x = (t_matrix *)pd_new(mtx_eye_class); int col=0, row=0; outlet_new(&x->x_obj, 0); x->row = x->col = 0; x->atombuffer = 0; switch(argc) { case 0: break; case 1: col=row=atom_getfloat(argv); break; default: row=atom_getfloat(argv++); col=atom_getfloat(argv); } if(col<0)col=0; if(row<0)row=0; if (col*row){ int n = (col<row)?col:row; x->atombuffer = (t_atom *)getbytes((col*row+2)*sizeof(t_atom)); setdimen(x, row, col); matrix_set(x, 0); while(n--)SETFLOAT(x->atombuffer+2+n*(1+col), 1); } return (x); }
void matrix_repmat(Vector* v, size_t rows, size_t cols, Matrix** result) { matrix_create0(rows * v->size, cols, result, false); for (size_t i = 0; i < (*result)->rows; ++i) { for (size_t j = 0; j < (*result)->cols; ++j) { matrix_set(*result, i, j, IVector.at(v, i % v->size)); } } }
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_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_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); } } }
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; }
void matrix_diag_set_scalar(matrix_type * matrix , double value) { if (matrix->rows == matrix->columns) { int i; matrix_set(matrix , 0); for ( i=0; i < matrix->rows; i++) matrix->data[ GET_INDEX(matrix , i , i) ] = value; } else util_abort("%s: size mismatch \n",__func__); }
void matrix_diag_set(matrix_type * matrix , const double * diag) { if (matrix->rows == matrix->columns) { int i; matrix_set(matrix , 0); for ( i=0; i < matrix->rows; i++) matrix->data[ GET_INDEX(matrix , i , i) ] = diag[i]; } else util_abort("%s: size mismatch \n",__func__); }
void matrix_optimize(sp_matrix_t *m) { int i, size, redo; int *c; bitset_t *fullrow; size = MAX(m->maxcol, m->maxrow)+1; /* kill all double-entries (Mij and Mji are set) */ matrix_foreach(m, e) { double t_val; assert(e->row != e->col && "Root has itself as arg. Ok. But the arg (=root) will always have the same color as root"); t_val = matrix_get(m, e->col, e->row); if (fabs(t_val) > 1e-10) { matrix_set(m, e->col, e->row, 0); matrix_set(m, e->row, e->col, e->val + t_val); } }
int read_mat_matrix(Matrix* M, const char* file_name, const char* varname) { MATFile* matf = NULL; mxArray* Mx = NULL; int res; unsigned int i,j; double* data; assert(file_name); assert(M); assert(varname); /* let's open the file*/ matf = matOpen(file_name, "r" ); if(!matf) { return ERROR_IO; } /* let's read the variable varname */ Mx = matGetVariable(matf, varname); if (!Mx) { matClose(matf); return ERROR_IO_MAT; } /* let's close the file */ res = matClose(matf); assert(!res); /* check: the variable must be a 2D matrix!! */ if(mxGetNumberOfDimensions(Mx) > 2) { matClose(matf); return ERROR_IO_MAT; } /* let's create the Matrix */ M->n_rows = (unsigned int) mxGetM(Mx); M->n_cols = (unsigned int) mxGetN(Mx); res = create_matrix(M, M->n_rows, M->n_cols); if (res) { mxDestroyArray(Mx); return res; } /* copy the data from the mxArray. Remember that matlab save in column-order!! */ data = mxGetPr(Mx); for (j=0; j < M->n_cols; ++j) { for (i=0; i < M->n_rows; ++i) { matrix_set(M, i, j, (float) *data); ++data; } } /* destroy mxArray*/ mxDestroyArray(Mx); return 0; }
void matrix_init(size_t rows, size_t columns, const double* values, Matrix** out) { if (!rows || !columns) { errno = EINVAL; return; } matrix_create0(rows, columns, out, true); for (size_t i = 0; i < (*out)->rows; ++i) { for (size_t j = 0; j < (*out)->cols; ++j) { matrix_set(*out, i, j, values[j + (*out)->cols * i]); } } }
void matrix_initf(size_t rows, size_t columns, double (*f)(size_t, size_t), Matrix** out) { if (!rows || !columns) { errno = EINVAL; return; } matrix_create0(rows, columns, out, true); for (size_t i = 0; i < (*out)->rows; ++i) { for (size_t j = 0; j < (*out)->cols; ++j) { matrix_set(*out, i, j, f(i, j)); } } }
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)); } } }
static void matrix_size(t_matrix *x, t_symbol *s, int argc, t_atom *argv) { int col, row; switch(argc) { case 0: /* size */ if (x->row*x->col) outlet_list(x->x_obj.ob_outlet, gensym("size"), 2, x->atombuffer); break; case 1: row=atom_getfloat(argv); adjustsize(x, row, row); matrix_set(x, 0); break; default: row=atom_getfloat(argv++); col=atom_getfloat(argv); adjustsize(x, row, col); matrix_set(x, 0); } }
int mh_osc_set( const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data ) { int x = argv[0]->i; int y = argv[1]->i; ws2811_led_t colour = (ws2811_led_t)(argv[2]->i); matrix_set(x, y, colour); return 0; }