/* %F Consulta a celula que contem o focus. %i h : handle da matriz, %r string contendo a celula esta com o foco, na forma "%d:%d", onde o primeiro numero indica a linha e o segundo a coluna. "1:1" corresponde a celula do canto superior esquerdo. */ char *iupmatGetFocusPosition(Ihandle *h) { static char cell[100]; Tmat *mat=(Tmat*)matrix_data(h); if (mat) sprintf(cell,"%d:%d",mat_lin(mat)+1,mat_col(mat)+1); else return NULL; return cell; }
/* %F Retorna o valor de uma celula da matriz %i n : handle da matriz, lin,col : coordenadas da celula a ser consultada, (1,1) corresponde a celula do canto superior esquerdo. %o retorna o valor da celula. Used only by getattribute method. */ char *iupmatGetCell (Ihandle *n, int lin, int col) { Tmat *mat = (Tmat*)matrix_data(n); /* Se a celula nao existe, retorna NULL */ if ((lin < 0) || (col < 0) || (lin > mat_nl(mat)) || (col > mat_nc(mat))) return NULL; lin--; /* a celula superior esquerda e´ 1:1 para o usuario, */ col--; /* internamente ela e´ 0:0 */ if (lin==mat_lin(mat) && col==mat_col(mat) && iupCheck(mat_edtdatah(mat), "VISIBLE")==YES) return iupmatEditGetValue(mat); else return iupmatGetCellValue(n,lin,col); }
mat mars(Agraph_t* g, struct marsopts opts) { int i, j, n = agnnodes(g), k = MIN(n, MAX(opts.k, 2)), iter = 0; mat dij, u, u_trans, q, r, q_t, tmp, tmp2, z; double* s = (double*) malloc(sizeof(double)*k); double* ones = (double*) malloc(sizeof(double)*n); double* d; int* anchors = (int*) malloc(sizeof(int)*k); int* clusters = NULL; double change = 1, old_stress = -1; dij = mat_new(k, n); u = mat_new(n,k); tmp = mat_new(n,k); darrset(ones,n,-1); select_anchors(g, dij, anchors, k); if(opts.color) { for(i = 0; i < k; i++) { Agnode_t* anchor = get_node(anchors[i]); agset(anchor, "color", "red"); } } if(opts.power != 1) { clusters = graph_cluster(g,dij,anchors); } singular_vectors(g, dij, opts.power, u, s); vec_scalar_mult(s, k, -1); u_trans = mat_trans(u); d = mat_mult_for_d(u, s, u_trans, ones); for(i = 0; i < u->c; i++) { double* col = mat_col(u,i); double* b = inv_mul_ax(d,col,u->r); for(j = 0; j < u->r; j++) { tmp->m[mindex(j,i,tmp)] = b[j]; } free(b); free(col); } tmp2 = mat_mult(u_trans,tmp); for(i = 0; i < k; i++) { tmp2->m[mindex(i,i,tmp2)] += (1.0/s[i]); } q = mat_new(tmp2->r, tmp2->c); r = mat_new(tmp2->c, tmp2->c); qr_factorize(tmp2,q,r); q_t = mat_trans(q); if(opts.given) { z = get_positions(g, opts.dim); } else { z = mat_rand(n, opts.dim); } translate_by_centroid(z); if(opts.viewer) { init_viewer(g, opts.max_iter); append_layout(z); } old_stress = stress(z, dij, anchors, opts.power); while(change > EPSILON && iter < opts.max_iter) { mat right_side; double new_stress; if(opts.power == 1) { right_side = barnes_hut(z); } else { right_side = barnes_hut_cluster(z, dij, clusters, opts.power); } for(i = 0; i < opts.dim; i++) { double sum = 0; double* x; double* b = mat_col(right_side,i); for(j = 0; j < right_side->r; j++) { sum += b[j]; } x = inv_mul_full(d, b, right_side->r, u, u_trans, q_t, r); for(j = 0; j < z->r; j++) { z->m[mindex(j,i,z)] = x[j] - sum/right_side->r; } free(x); free(b); } adjust_anchors(g, anchors, k, z); update_anchors(z, dij, anchors, opts.power); translate_by_centroid(z); if(opts.viewer) { append_layout(z); } new_stress = stress(z, dij, anchors, opts.power); change = fabs(new_stress-old_stress)/old_stress; old_stress = new_stress; mat_free(right_side); iter++; } mat_free(dij); mat_free(u); mat_free(u_trans); mat_free(q); mat_free(r); mat_free(q_t); mat_free(tmp); mat_free(tmp2); free(s); free(ones); free(d); free(anchors); free(clusters); return z; }