/* Modified: runyunz - Fall 2013 */ void nnsearch(TREENODE *subroot, VECTOR *vp, int count){ VECTOR *best; VECTOR **bests; int num; int i; if(!count){ count = 1; } bests = (VECTOR**)malloc(sizeof(VECTOR*) * count); bests[0] = NULL; #ifdef DEBUG printf("nn tree search was called with \n"); vecprint(vp); #endif best = rnnsearch(subroot, vp, (VECTOR *)NULL, 0, bests, count); if(best != NULL){ for(i = 0; i < count; i++){ if(bests[i] == NULL){ printf("Only %d nodes found\n", i); break; } printf("#%d nearest neighbor: ", i+1); vecprint(bests[i]); } } else{ printf("empty tree\n"); } }
void rrangesearch(TREENODE *subroot, VECTOR *vpLow, VECTOR *vpHigh, int level){ #ifdef DEBUG printf("recursive tree range search was called with \n"); vecprint(vpLow); vecprint(vpHigh); printf("level=%d\n", level); #endif /*** this part was disabled, for homework1 **/ int numdims; if( subroot != NULL ){ numdims = (subroot->pvec)->len; if( contains( vpLow, vpHigh, subroot->pvec ) ){ vecprint(subroot->pvec); } if( (vpLow->vec)[level] <= ((subroot->pvec)->vec)[level] ){ /* left branch can not be excluded */ rrangesearch( subroot->left, vpLow, vpHigh, (level+1)% numdims); } if( (vpHigh->vec)[level] > ((subroot->pvec)->vec)[level] ){ /* right branch can not be excluded */ /* notice the '>' as opposed to '>=' */ rrangesearch( subroot->right, vpLow, vpHigh, (level+1)% numdims); } } /*****/ return; }
void rangesearch(TREENODE *subroot, VECTOR *vpLow, VECTOR *vpHigh){ #ifdef DEBUG printf("tree range search was called with \n"); vecprint(vpLow); vecprint(vpHigh); #endif rrangesearch(subroot, vpLow, vpHigh, 0); }
int main() { int i, n; vector diag, sub1, sub2, sup1, sup2, b; printf("n = "); scanf("%d", &n); if (n < 3) return EXIT_FAILURE; diag = new_vector(n); sub1 = new_vector(n); sub2 = new_vector(n); sup1 = new_vector(n); sup2 = new_vector(n); b = new_vector(n); /* 例題. 正解はすべて 1 */ for (i = 0; i < n; i++) { diag[i] = 11; sub1[i] = 3; sub2[i] = 1; sup1[i] = 4; sup2[i] = 2; b[i] = 21; } b[0] = 17; b[n - 1] = 15; b[1] -= 1; b[n - 2] -= 2; gauss5(n, diag, sub1, sub2, sup1, sup2, b); vecprint(b, n, 8, "%9.6f"); printf("正解はすべて 1\n"); return EXIT_SUCCESS; }
TREENODE *insert(TREENODE *subroot, VECTOR *vp){ #ifdef DEBUG printf("tree insert was called with \n"); vecprint(vp); #endif return( rinsert(subroot, vp, 0) ); }
void nnsearch(TREENODE *subroot, VECTOR *vp, int count){ VECTOR **bestn = (VECTOR**)malloc(sizeof(VECTOR*) * count); NUMBER *bestn_d = (NUMBER*)malloc(sizeof(NUMBER) * count); NUMBER min_nd = HUGE; int max_id = 0; int cnt = 0; int i = 0; #ifdef DEBUG printf("nn tree search was called with \n"); vecprint(vp); #endif /* fetch count tree nodes first in case that there're less than count nodes */ getNNode(subroot, vp, count, bestn, &min_nd, &max_id, &cnt); rnnsearch(subroot, vp, 0, count, bestn, &min_nd, &max_id, &cnt); while(i < cnt) { bestn_d[i] = myvecdist2(vp, bestn[i]); i++; } if (cnt == 0) { printf("empty tree\n"); }else if(cnt < count) { printf("Only %d nodes found\n",cnt); } mergeSort(bestn, bestn_d, cnt); printf("nearest %d neighbors:\n",cnt); for(i = 0; i < cnt; i++){ vecprint(bestn[i]); printf("distance: %f\n", bestn_d[i]); } }
void rtprint(TREENODE *subroot, int level){ int i; if( subroot != NULL ){ rtprint(subroot->left, level+1); for(i=0; i<level;i++){ printf("\t"); } vecprint(subroot->pvec); rtprint(subroot->right, level+1); } }
void nnsearch(TREENODE *subroot, VECTOR *vp, int count){ VECTOR *best; best = rnnsearch(subroot, vp, (VECTOR *)NULL, 0); if( best != NULL){ printf("nearest neighbor: "); vecprint(best); }else{ printf("empty tree\n"); } }
void matrixprint( MATRIX *pmatrix){ int i, n; if( pmatrix == NULL ){ printf("empty matrix\n"); return; } assert( pmatrix->count >0 ); n = pmatrix->count; for(i=0; i<n; i++){ vecprint( matrixget( pmatrix, i) ); } }
void knnsearch(TREENODE *subroot, VECTOR *vp, int k, NUMBER radius) { heap *hp; assert(subroot != NULL); assert(vp->len != 0); hp = heap_alloc(k, vp->len); assert(hp != NULL); rknnsearch(subroot, vp, hp, radius, 0); printf("=========================\n\n"); printf("k nearest neighbor search: \n"); printf("target vector: "); vecprint(vp); heap_print(hp); /* Should print heap first */ heap_dealloc(hp); }
void rrangesearch(TREENODE *subroot, VECTOR *vpLow, VECTOR *vpHigh, int level){ /*** this part was disabled, for homework1 **/ int numdims; if( subroot != NULL ){ numdims = (subroot->pvec)->len; if( contains( vpLow, vpHigh, subroot->pvec ) ){ vecprint(subroot->pvec); } if( (vpLow->vec)[level] <= ((subroot->pvec)->vec)[level] ){ /* left branch can not be excluded */ rrangesearch( subroot->left, vpLow, vpHigh, (level+1)% numdims); } if( (vpHigh->vec)[level] > ((subroot->pvec)->vec)[level] ){ /* right branch can not be excluded */ /* notice the '>' as opposed to '>=' */ rrangesearch( subroot->right, vpLow, vpHigh, (level+1)% numdims); } } /*****/ return; }
int main() { int i, j, k, n; static matrix a, p; static vector d, e, temp; double s, t; printf("n = "); scanf("%d", &n); printf("乱数の種 (正の整数) = "); scanf("%ul", &seed); seed |= 1; a = new_matrix(n, n); p = new_matrix(n, n); d = new_vector(n); e = new_vector(n); temp = new_vector(n); for (i = 0; i < n; i++) for (j = 0; j <= i; j++) a[i][j] = a[j][i] = p[i][j] = p[j][i] = rnd() - rnd(); printf("A:\n"); matprint(a, n, 7, "%10.6f"); tridiagonalize(n, p, d, e); printf("d:\n"); vecprint(d, n, 5, "% -14g"); printf("e:\n"); vecprint(e, n - 1, 5, "% -14g"); for (i = 0; i < n; i++) { for (k = 0; k < n; k++) temp[k] = a[i][k]; for (j = 0; j < n; j++) { s = 0; for (k = 0; k < n; k++) s += temp[k] * p[j][k]; a[i][j] = s; } } for (j = 0; j < n; j++) { for (k = 0; k < n; k++) temp[k] = a[k][j]; for (i = 0; i < n; i++) { s = 0; for (k = 0; k < n; k++) s += p[i][k] * temp[k]; a[i][j] = s; } } s = 0; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (i == j) { t = a[i][j] - d[i]; s += t * t; } else if (i + 1 == j) { t = a[i][j] - e[i]; s += t * t; } else if (i == j + 1) { t = a[i][j] - e[j]; s += t * t; } else { t = a[i][j]; s += t * t; } printf("二乗平均誤差: %g\n", sqrt(s) / n); return EXIT_SUCCESS; }