⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cluster1.c

📁 聚类算法全集以及内附数据集
💻 C
📖 第 1 页 / 共 4 页
字号:
  &&   (clset->cls[0].var != 1.0)) {    fputs(indent,        file); /* if nonstandard uniform radius */    fputs("radius   = ", file); /* write a radius indicator */    fprintf(file, "%g;\n", sqrt(clset->cls[0].var));  }                             /* print the radius */  /* --- print the normalization mode --- */  if ((clset->mode  != CLS_NONE)/* if not default parameters */  ||  (clset->noise != 0)       /* for the normalization mode */  ||  (clset->nrmps[0] != 1) || (clset->nrmps[1] != 0)) {    fputs(indent,        file); /* write the indentation and */    fputs("normmode = ", file); /* the normalization mode indicator */    fputs(nrmnames[clset->mode], file);    if ((clset->nrmps[0] != 1)  /* if not standard parameters, */    ||  (clset->nrmps[1] != 0)) /* print normalization parameters */      fprintf(file, "(%g,%g)", clset->nrmps[0], clset->nrmps[1]);    if (clset->noise != 0)      /* print noise cluster parameter */      fprintf(file, ", %g", clset->noise);    fputs(";\n", file);         /* terminate the normalization mode */  }                             /* (optional information) */  /* --- print cluster parameters --- */  fputs(indent,         file);  /* write the indentation and */  fputs("params   = {", file);  /* start the parameter section */  p = clset->cls;               /* traverse the clusters */  for (i = 0; i < clset->clscnt; p++, i++) {    if (i > 0)                  /* start a new output line */      fprintf(file, ",\n%s            ", indent);    fputs("{ [", file);         /* write the cluster center */    for (n = 0; n < clset->incnt-1; n++)      fprintf(file, "% g, ", p->ctr[n]);    fprintf(file, "% g]", p->ctr[n]);    if      (clset->type & CLS_COVARS) { /* if covariances are used */      for (n = 0; n < clset->incnt; n++){/* traverse the matrix rows */        fprintf(file, ",\n%s            [", indent);        for (k = 0; k < n; k++) /* traverse the matrix row */          fprintf(file, "% g, ", mat_get(p->cov, k, n));        fprintf(file, "% g]", mat_get(p->cov, n, n));      } }                       /* print the (normalized) covariances */    else if (clset->type & CLS_VARS) {   /* if variances are used */      fprintf(file, ",\n%s            ", indent);      fprintf(file, "  [% g", mat_get(p->cov, 0, 0));      for (n = 1; n < clset->incnt; n++)        fprintf(file, ", % g", mat_get(p->cov, n, n));      fputc(']', file); }       /* print a list of variances */    else if (clset->type & CLS_SIZE) {   /* if cluster size is used */      fprintf(file, ", [%g]", p->var);    }                           /* print the isotropic variance */    if (clset->type & CLS_WEIGHT)        /* if cluster weight is used */      fprintf(file, ", %g", p->wgt);     /* print the cluster weight */    fputs(" }", file);          /* terminate the cluster description */  }  fputs("};\n", file);          /* terminate the parameter section */  #ifdef CLS_EXTFN              /* if to compile extended functions */  if (clset->attset && !(mode & CLS_RBFNET))    fputs("};\n", file);        /* terminate the description */  #endif  return ferror(file) ? -1 : 0; /* return the write status */}  /* cls_desc() *//*----------------------------------------------------------------------  Cluster Set Parse Functions----------------------------------------------------------------------*/#ifdef CLS_PARSEstatic int _scales (CLSET *clset, SCAN *scan){                               /* --- get the input scalings */  assert(clset && scan);        /* check the function arguments */  if (clset->nst) return 0;     /* check whether scalings exist */  if ((sc_token(scan) != T_ID)  /* check whether 'scales' follows */  ||  (strcmp(sc_value(scan), "scales") != 0))    return 0;                   /* if not, abort the function */  clset->nst = nst_parse(scan, clset->incnt);  if (!clset->nst) return 1;    /* parse the scaling parameters */  clset->incnt = nst_dim(clset->nst);  return 0;                     /* get the number of dimensions */}  /* _scales() *//*--------------------------------------------------------------------*/static int _function (CLSET *clset, SCAN *scan){                               /* --- parse the function description */  const char *s;                /* buffer for token value */  double     p;                 /* buffer for a parameter */  if ((sc_token(scan) != T_ID)  /* check for "function" */  ||  (strcmp(sc_value(scan), "function") != 0))    return 0;                   /* if there is none, abort */  GET_TOK();                    /* consume the "function" keyword */  GET_CHR('=');                 /* consume '=' */  if (sc_token(scan) != T_ID) ERR_STR("cauchy");  s = sc_value(scan);           /* check for a function name */  if      (strcmp(s, "cauchy") == 0) clset->radfn = rf_cauchy;  else if (strcmp(s, "gauss")  == 0) clset->radfn = rf_gauss;  else ERR_STR("cauchy");       /* decode the function name */  GET_TOK();                    /* and consume it */  GET_CHR('(');                 /* consume '(' */  if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);  p = strtod(sc_value(scan), NULL);  if (p <= 0) ERROR(E_ILLNUM);  /* check for a number and */  clset->rfnps[0] = p;          /* get the first parameter, */  GET_TOK();                    /* i.e., the distance exponent */  GET_CHR(',');                 /* consume the separating ',' */  if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);  p = strtod(sc_value(scan), NULL);  if (p < 0) ERROR(E_ILLNUM);   /* check for a number and */  clset->rfnps[1] = p;          /* get the second parameter, */  GET_TOK();                    /* i.e., the scaling factor */  GET_CHR(')');                 /* consume the closing ')' */  if (sc_token(scan) == ',') {  /* if a comma follows */    GET_TOK();                  /* consume ',' */    if ((sc_token(scan) != T_ID)    ||  (strcmp(sc_value(scan), "normalized") != 0))      ERR_STR("normalized");    /* check for a normalization flag */    clset->type |= CLS_NORM;    /* set the normalization flag */    GET_TOK();                  /* in the cluster type and */  }                             /* consume the "normalized" keyword */  GET_CHR(';');                 /* consume the closing ';' */  return 0;                     /* return 'ok' */}  /* _function() *//*--------------------------------------------------------------------*/static int _radius (CLSET *clset, SCAN *scan){                               /* --- parse the uniform radius */  if ((sc_token(scan) != T_ID)  /* check for a radius specification */  ||  (strcmp(sc_value(scan), "radius") != 0))    return 0;                   /* if there is none, abort */  GET_TOK();                    /* consume the "radius" keyword */  GET_CHR('=');                 /* consume '=' */  if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);  clset->msd[1] = strtod(sc_value(scan), NULL);  if (clset->msd[1] <= 0) ERROR(E_ILLNUM);  GET_TOK();                    /* get and consume the eadius */  GET_CHR(';');                 /* consume the closing ';' */  return 0;                     /* return 'ok' */}  /* radius() *//*--------------------------------------------------------------------*/static int _norm (CLSET *clset, SCAN *scan){                               /* --- parse the normalization mode */  const char *s;                /* buffer for token value */  double     n;                 /* buffer for parameter */  if ((sc_token(scan) != T_ID)  /* check for "normmode" */  ||  (strcmp(sc_value(scan), "normmode") != 0)) {    clset->norm = CLS_NONE; return 0; }  GET_TOK();                    /* consume the "normmode" keyword */  GET_CHR('=');                 /* consume '=' */  if (sc_token(scan) != T_ID) ERR_STR("sum1");  s = sc_value(scan);           /* check for a norm. mode name */  if      (strcmp(s, nrmnames[CLS_NONE]) == 0) clset->mode = CLS_NONE;  else if (strcmp(s, nrmnames[CLS_SUM1]) == 0) clset->mode = CLS_SUM1;  else if (strcmp(s, nrmnames[CLS_MAX1]) == 0) clset->mode = CLS_MAX1;  else if (strcmp(s, nrmnames[CLS_HARD]) == 0) clset->mode = CLS_HARD;  else ERR_STR(nrmnames[CLS_SUM1]);  GET_TOK();                    /* decode the normalization mode */  if (sc_token(scan) == '(') {  /* if a paranthesis follows, */    GET_TOK();                  /* consume the '(' */    if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);    n = strtod(sc_value(scan), NULL);    if (n <= 0)                  ERROR(E_ILLNUM);    clset->nrmps[0] = n;        /* get the 1st normalization param. */    GET_TOK();                  /* and consume the number */    GET_CHR(',');               /* consume the separating comma */    if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);    n = strtod(sc_value(scan), NULL);    if (n <  0)                  ERROR(E_ILLNUM);    clset->nrmps[1] = n;        /* get the 2nd normalization param. */    GET_TOK();                  /* and consume the number */    GET_CHR(')');               /* consume the separating comma */  }  if (sc_token(scan) == ',') {  /* if a comma follows, */    GET_TOK();                  /* consume the ',' */    if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);    n = strtod(sc_value(scan), NULL);    clset->msd[0] = (n >= 0) ? n : clset->radfn(n*n, clset->rfnps);    clset->noise  = n;          /* get the noise cluster parameter, */    GET_TOK();                  /* compute the membership degree, */  }                             /* and consume the number */  GET_CHR(';');                 /* consume the closing ';' */  return 0;                     /* return 'ok' */}  /* _norm() *//*--------------------------------------------------------------------*/static int _params (CLSET *clset, SCAN *scan){                               /* --- parse a set of clusters */  int     i, k;                 /* loop variables */  int     err   = 0;            /* error status */  int     vsz   = 0;            /* size of cluster vector */  int     incnt = 0;            /* dimension of (input) data space */  CLUSTER *c;                   /* to access the clusters */  double  t, *v = NULL;         /* buffers (for reallocation) */  if ((sc_token(scan) != T_ID)  /* check for cluster parameters */  ||  (strcmp(sc_value(scan), "params") != 0))    ERR_STR("params");          /* if there are none, abort */  GET_TOK();                    /* consume the "params" keyword */  GET_CHR('=');                 /* consume '=' */  GET_CHR('{');                 /* consume '{' */  while (1) {                   /* cluster read loop */    GET_CHR('{');               /* consume '{' */    if (clset->clscnt >= vsz) { /* if the cluster vector is full */      vsz += (vsz > BLKSIZE) ? vsz >> 1 : BLKSIZE;      c = (CLUSTER*)realloc(clset->cls, vsz *sizeof(CLUSTER));      if (!c) ERROR(E_NOMEM);   /* allocate a new cluster vector */      clset->cls = c;           /* and set it */    }    GET_CHR('[');               /* consume opening '[' */    if (clset->incnt <= 0) {    /* if the dimension is yet unknown */      clset->incnt = 0;         /* initialize the dimension */      while (1) {               /* coordinate read loop */        if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);        if (clset->incnt >= incnt) { /* if the coord. vector is full */          incnt += (incnt > BLKSIZE) ? incnt >> 1 : BLKSIZE;          v = (double*)realloc(clset->vec, incnt *sizeof(double));          if (!v) ERROR(E_NOMEM);          clset->vec = v;       /* enlarge the coordinate vector */        }                       /* and set the new one */        clset->vec[clset->incnt++] = strtod(sc_value(scan), NULL);        GET_TOK();              /* get and consume the coordinate */        if (sc_token(scan) != ',') break;        GET_TOK();              /* if at end of coord. list, abort */      }                         /* the loop, otherwise consume ',' */    }    c = clset->cls +clset->clscnt++;    if (_cluster(c, clset->incnt) != 0)      ERROR(E_NOMEM);           /* initialize the cluster prototype */    mat_init(c->cov, MAT_UNIT, NULL);    mat_init(c->inv, MAT_UNIT, NULL);    mat_init(c->smp, MAT_ZERO, NULL);    if (incnt > 0) {            /* if the dimension was unknown */      vec_copy(c->ctr, clset->vec, clset->incnt);      incnt = 0; }              /* copy the cluster center */    else {                      /* if the dimension was known */      for (i = 0; i < clset->incnt; i++) {        if (i > 0) { GET_CHR(','); }  /* consume ',' */        if (sc_token(scan) != T_NUM) ERROR(E_NUMEXP);        c->ctr[i] = strtod(sc_value(scan), NULL);        GET_TOK();              /* traverse the dimensions and */      }                         /* get and consume the coordinates */    }                           /* of the center vector */    GET_CHR(']');               /* consume closing ']' */    i = 0;                      /* init. the parameter counter */    while (clset->clscnt <= 1){ /* if the cluster type is yet unknown */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -