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

📄 cluster.c

📁 聚类分析的源码集
💻 C
📖 第 1 页 / 共 5 页
字号:
        for (i1 = l; i1 <= k1; i1++)        { i = i1 + 1;          g = rv1[i];          y = w[i];          h = s * g;          g = c * g;          z = sqrt(f*f+h*h);          rv1[i1] = z;          c = f / z;          s = h / z;          f = x * c + g * s;          g = -x * s + g * c;          h = y * s;          y = y * c;          for (j = 0; j < n; j++)          { x = v[j][i1];            z = v[j][i];            v[j][i1] = x * c + z * s;            v[j][i] = -x * s + z * c;          }          z = sqrt(f*f+h*h);          w[i1] = z;          /* rotation can be arbitrary if z is zero */          if (z!=0.0)          { c = f / z;            s = h / z;          }          f = c * g + s * y;          x = -s * g + c * y;          for (j = 0; j < m; j++)          { y = u[j][i1];            z = u[j][i];            u[j][i1] = y * c + z * s;            u[j][i] = -y * s + z * c;          }        }        rv1[l] = 0.0;        rv1[k] = f;        w[k] = x;      }    }  }  free(rv1);  return;}/* ********************************************************************* */staticdouble euclid (int n, double** data1, double** data2, int** mask1, int** mask2,  const double weight[], int index1, int index2, int transpose) /*Purpose=======The euclid routine calculates the weighted Euclidean distance between tworows or columns in a matrix.Arguments=========n      (input) intThe number of elements in a row or column. If transpose==0, then n is the numberof columns; otherwise, n is the number of rows.data1  (input) double arrayThe data array containing the first vector.data2  (input) double arrayThe data array containing the second vector.mask1  (input) int arrayThis array which elements in data1 are missing. If mask1[i][j]==0, thendata1[i][j] is missing.mask2  (input) int arrayThis array which elements in data2 are missing. If mask2[i][j]==0, thendata2[i][j] is missing.weight (input) double array, dimension( n )The weights that are used to calculate the distance.index1     (input) intIndex of the first row or column.index2     (input) intIndex of the second row or column.transpose (input) intIf transpose==0, the distance between two rows in the matrix is calculated.Otherwise, the distance between two columns in the matrix is calculated.============================================================================*/{ double result = 0.;  double tweight = 0;  int i;  if (transpose==0) /* Calculate the distance between two rows */  { for (i = 0; i < n; i++)    { if (mask1[index1][i] && mask2[index2][i])      { double term = data1[index1][i] - data2[index2][i];        result = result + weight[i]*term*term;        tweight += weight[i];      }    }  }  else  { for (i = 0; i < n; i++)    { if (mask1[i][index1] && mask2[i][index2])      { double term = data1[i][index1] - data2[i][index2];        result = result + weight[i]*term*term;        tweight += weight[i];      }    }  }  if (!tweight) return 0; /* usually due to empty clusters */  result /= tweight;  result *= n;  return result;}/* ********************************************************************* */staticdouble harmonic(int n, double** data1, double** data2, int** mask1, int** mask2,  const double weight[], int index1, int index2, int transpose) /*Purpose=======The harmonic routine calculates the weighted Euclidean distance between tworows or columns in a matrix, adding terms for the different dimensionsharmonically, i.e. summing the inverse and taking the inverse of the total.Arguments=========n      (input) intThe number of elements in a row or column. If transpose==0, then n is the numberof columns; otherwise, n is the number of rows.data1  (input) double arrayThe data array containing the first vector.data2  (input) double arrayThe data array containing the second vector.mask1  (input) int arrayThis array which elements in data1 are missing. If mask1[i][j]==0, thendata1[i][j] is missing.mask2  (input) int arrayThis array which elements in data2 are missing. If mask2[i][j]==0, thendata2[i][j] is missing.weight (input) double array, dimension( n )The weights that are used to calculate the distance.index1     (input) intIndex of the first row or column.index2     (input) intIndex of the second row or column.transpose (input) intIf transpose==0, the distance between two rows in the matrix is calculated.Otherwise, the distance between two columns in the matrix is calculated.============================================================================*/{ double result = 0.;  double tweight = 0;  int i;  if (transpose==0) /* Calculate the distance between two rows */  { for (i = 0; i < n; i++)    { if (mask1[index1][i] && mask2[index2][i])      { const double term = data1[index1][i] - data2[index2][i];        if (term==0) return 0;        result = result + weight[i]/(term*term);        tweight += weight[i];      }    }  }  else  { for (i = 0; i < n; i++)    { if (mask1[i][index1] && mask2[i][index2])      { const double term = data1[i][index1] - data2[i][index2];        if (term==0) return 0;        result = result + weight[i]/(term*term);        tweight += weight[i];      }    }  }  if (!tweight) return 0; /* usually due to empty clusters */  result /= tweight;  result *= n;  result = 1. / result;  return result;}/* ********************************************************************* */staticdouble cityblock (int n, double** data1, double** data2, int** mask1,  int** mask2, const double weight[], int index1, int index2, int transpose)/*Purpose=======The cityblock routine calculates the weighted "City Block" distance betweentwo rows or columns in a matrix. City Block distance is defined as theabsolute value of X1-X2 plus the absolute value of Y1-Y2 plus..., which isequivalent to taking an "up and over" path.Arguments=========n      (input) intThe number of elements in a row or column. If transpose==0, then n is the numberof columns; otherwise, n is the number of rows.data1  (input) double arrayThe data array containing the first vector.data2  (input) double arrayThe data array containing the second vector.mask1  (input) int arrayThis array which elements in data1 are missing. If mask1[i][j]==0, thendata1[i][j] is missing.mask2  (input) int arrayThis array which elements in data2 are missing. If mask2[i][j]==0, thendata2[i][j] is missing.weight (input) double array, dimension( n )The weights that are used to calculate the distance.index1     (input) intIndex of the first row or column.index2     (input) intIndex of the second row or column.transpose (input) intIf transpose==0, the distance between two rows in the matrix is calculated.Otherwise, the distance between two columns in the matrix is calculated.============================================================================ */{ double result = 0.;  double tweight = 0;  int i;  if (transpose==0) /* Calculate the distance between two rows */  { for (i = 0; i < n; i++)    { if (mask1[index1][i] && mask2[index2][i])      { double term = data1[index1][i] - data2[index2][i];        result = result + weight[i]*fabs(term);        tweight += weight[i];      }    }  }  else  { for (i = 0; i < n; i++)    { if (mask1[i][index1] && mask2[i][index2])      { double term = data1[i][index1] - data2[i][index2];        result = result + weight[i]*fabs(term);        tweight += weight[i];      }    }  }  if (!tweight) return 0; /* usually due to empty clusters */  result /= tweight;  result *= n;  return result;}/* ********************************************************************* */staticdouble correlation (int n, double** data1, double** data2, int** mask1,  int** mask2, const double weight[], int index1, int index2, int transpose)/*Purpose=======The correlation routine calculates the weighted Pearson distance between tworows or columns in a matrix. We define the Pearson distance as one minus thePearson correlation.This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold(e.g., choose b = a + c).Arguments=========n      (input) intThe number of elements in a row or column. If transpose==0, then n is the numberof columns; otherwise, n is the number of rows.data1  (input) double arrayThe data array containing the first vector.data2  (input) double arrayThe data array containing the second vector.mask1  (input) int arrayThis array which elements in data1 are missing. If mask1[i][j]==0, thendata1[i][j] is missing.mask2  (input) int arrayThis array which elements in data2 are missing. If mask2[i][j]==0, thendata2[i][j] is missing.weight (input) double array, dimension( n )The weights that are used to calculate the distance.index1     (input) intIndex of the first row or column.index2     (input) intIndex of the second row or column.transpose (input) intIf transpose==0, the distance between two rows in the matrix is calculated.Otherwise, the distance between two columns in the matrix is calculated.============================================================================*/{ double result = 0.;  double sum1 = 0.;  double sum2 = 0.;  double denom1 = 0.;  double denom2 = 0.;  double tweight = 0.;  if (transpose==0) /* Calculate the distance between two rows */  { int i;    for (i = 0; i < n; i++)    { if (mask1[index1][i] && mask2[index2][i])      { double term1 = data1[index1][i];        double term2 = data2[index2][i];        double w = weight[i];        sum1 += w*term1;        sum2 += w*term2;        result += w*term1*term2;        denom1 += w*term1*term1;        denom2 += w*term2*term2;        tweight += w;      }    }  }  else  { int i;    for (i = 0; i < n; i++)    { if (mask1[i][index1] && mask2[i][index2])      { double term1 = data1[i][index1];        double term2 = data2[i][index2];        double w = weight[i];        sum1 += w*term1;        sum2 += w*term2;        result += w*term1*term2;        denom1 += w*term1*term1;        denom2 += w*term2*term2;        tweight += w;      }    }  }  if (!tweight) return 0; /* usually due to empty clusters */  result -= sum1 * sum2 / tweight;  denom1 -= sum1 * sum1 / tweight;  denom2 -= sum2 * sum2 / tweight;  if (denom1 <= 0) return 1; /* include '<' to deal with roundoff errors */  if (denom2 <= 0) return 1; /* include '<' to deal with roundoff errors */  result = result / sqrt(denom1*denom2);  result = 1. - result;  return result;}/* ********************************************************************* */staticdouble acorrelation (int n, double** data1, double** data2, int** mask1,  int** mask2, const double weight[], int index1, int index2, int transpose)/*Purpose=======The acorrelation routine calculates the weighted Pearson distance between tworows or columns, using the absolute value of the correlation.This definition yields a semi-metric: d(a,b) >= 0, and d(a,b) = 0 iff a = b.but the triangular inequality d(a,b) + d(b,c) >= d(a,c) does not hold(e.g., choose b = a + c).Arguments=========n      (input) intThe number of elements in a row or column. If transpose==0, then n is the numberof columns; otherwise, n is the number of rows.data1  (input) double arrayThe data array containing the first vector.data2  (input) double arrayThe data array containing the second vector.mask1  (input) int arrayThis array which elements in data1 are missing. If mask1[i][j]==0, thendata1[i][j] is missing.mask2  (input) int arrayThis array which elements in data2 are missing. If mask2[i][j]==0, thendata2[i][j] is missing.weight (input) double array, dimension( n )The weights that are used to calculate the distance.index1     (input) intIndex of the first row or column.index2     (input) intIndex of the second row or column.transpose (input) intIf transpose==0, the distance between two rows in the matrix is calculated.Otherwise, the distance between two columns in the matrix is calculated.============================================================================*/{ double result = 0.;  double sum1 = 0.;  double sum2 = 0.;  double denom1 = 0.;  double denom2 = 0.;  double tweight = 0.;  if (transpose==0) /* Calculate the distance between two rows */  { int i;    for (i = 0; i < n; i++)    { if (mask1[index1][i] && mask2[index2][i])

⌨️ 快捷键说明

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