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

📄 mathutil.java,v

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA,V
📖 第 1 页 / 共 4 页
字号:
    // if the length of the vectors are not equal error    //    /*    if (vec1.size() != vec2.size()) {        return Error::handle(name(), L"setPenalty", Error::ARG,    			 __FILE__, __LINE__);    }    */    // compute the dot product    //    for (int i = 0; i < vec1.size(); i++)    {      double val1 = ((Double)vec1.get(i)).doubleValue();      double val2 = ((Double)vec2.get(i)).doubleValue();      result += (val1 * val2);    }    // return the result    //    return result;  }  // method: almostEqual  //  // arguments:  //  Vector: (input) support vector  //  Vector: (input) input vector  //  // return: dot product  //  // this method evaluates the vector dot product  //  public static boolean almostEqual(Vector vec1, Vector vec2)  {    // compute the dot product    //    for (int i = 0; i < vec1.size(); i++)    {      double val1 = ((Double)vec1.get(i)).doubleValue();      double val2 = ((Double)vec2.get(i)).doubleValue();      if (!almostEqual(val1, val2))      {        return false;      }    }    // return the result    //    return true;  }  // method: almostEqual  //  // arguments:  //  Vector: (input) support vector  //  Vector: (input) input vector  //  // return: dot product  //  // this method evaluates the vector dot product  //  public static boolean almostEqual(Vector vec1, double val2)  {    // compute the dot product    //    for (int i = 0; i < vec1.size(); i++)    {      double val1 = ((Double)vec1.get(i)).doubleValue();      if (!almostEqual(val1, val2))      {        return false;      }    }    // return the result    //    return true;  }  // method: almostEqual  //  // arguments:  //  Vector: (input) support vector  //  Vector: (input) input vector  //  // return: dot product  //  // this method evaluates the vector dot product  //  public static boolean almostEqual(double val1, double val2)  {    // declare local variables    //    if (Math.abs(val1 - val2)      >= MIN_DIFF_RATE * (Math.abs(val1 + val2) + 0.01))    {      return false;    }    // return the result    //    return true;  }  // method: initDoubleVector  //  // arguments:  //    Vector vec: point from the cluster  // return   : none  //  // methods computes and returns the mean of the given cluster  //  static public void initDoubleVector(Vector vec_a, double value_a)  {    // loop over all elements    //    for (int i = 0; i < vec_a.size(); i++)    {      vec_a.set(i, new Double(value_a));    }  }  // method: doubleValue  //  // arguments:  //    Vector vec: point from the cluster  // return   : none  //  // methods computes and returns the mean of the given cluster  //  static public double doubleValue(Vector vec_a, int index_a)  {    Double value = (Double)vec_a.get(index_a);    return value.doubleValue();  }  // method: copyVector  //  // arguments:  //    Vector vec: point from the cluster  // return   : none  //  // methods computes and returns the mean of the given cluster  //  static public void copyVector(    Vector vec1_a,    Vector vec2_a,    int length_a,    int index1_a,    int index2_a)  {    // loop over all elements    //    for (int i = 0; i < length_a; i++)    {      vec1_a.set(i + index1_a, vec2_a.get(i + index2_a));    }  }  // method: withinClass  //  // arguments:  //    Data d: input data points  //    Matrix M: within class scatter matrix  //  // return   : none  //  // this method determines the within class scatter matrix  //  public static void withinClass(DataPoints data_a, double rx_a, double ry_a)  {    // declare local variables    //    int size = 0;    double x[] = null;    double y[] = null;    // declare the covariance object    //    Covariance cov = new Covariance();    // declare local matrices    //    Matrix M = new Matrix();    Matrix M1 = new Matrix();    Matrix M2 = new Matrix();    Matrix M3 = new Matrix();    Matrix M4 = new Matrix();    // set scales    double Xmin = OutputPanel.Xmin;    double Ymax = OutputPanel.Ymax;    // get the Rx and Ry ratio, seconds per pixel    //    double Rx = rx_a;    double Ry = ry_a;    // get the first data set size    //    size = data_a.dset1.size();    // initialize arrays to store the samples    //    x = new double[size];    y = new double[size];    // set up the initial random vectors i.e., the vectors of    // X and Y coordinate points form the display    //    for (int i = 0; i < size; i++)    {      MyPoint p = (MyPoint)data_a.dset1.elementAt(i);      x[i] = (p.x * Rx) + Xmin;      y[i] = Ymax - (p.y * Ry);    }    // compute the covariance matrix of the first data set    //    M1.row = M1.col = 2;    M1.Elem = new double[2][2];    M1.resetMatrix();    if (size > 0)    {      M1.Elem = cov.computeCovariance(x, y);    }    // get the second data set size    //    size = data_a.dset2.size();    // initialize arrays to store the samples    //    x = new double[size];    y = new double[size];    // set up the initial random vectors i.e., the vectors of    // X and Y coordinate points form the display    //    for (int i = 0; i < size; i++)    {      MyPoint p = (MyPoint)data_a.dset2.elementAt(i);      x[i] = (p.x * Rx) + Xmin;      y[i] = Ymax - (p.y * Ry);    }    // compute the covariance matrix of the second data set    //    M2.row = M2.col = 2;    M2.Elem = new double[2][2];    M2.resetMatrix();    if (size > 0)    {      M2.Elem = cov.computeCovariance(x, y);    }    // get the third data set size    //    size = data_a.dset3.size();    // initialize arrays to store the samples    //    x = new double[size];    y = new double[size];    // set up the initial random vectors i.e., the vectors of    // X and Y coordinate points form the display    //    for (int i = 0; i < size; i++)    {      MyPoint p = (MyPoint)data_a.dset3.elementAt(i);      x[i] = (p.x * Rx) + Xmin;      y[i] = Ymax - (p.y * Ry);    }    // compute the covariance matrix of the third data set    //    M3.row = M3.col = 2;    M3.Elem = new double[2][2];    M3.resetMatrix();    if (size > 0)    {      M3.Elem = cov.computeCovariance(x, y);    }    // get the fourth data set size    //    size = data_a.dset4.size();    // initialize arrays to store the samples    //    x = new double[size];    y = new double[size];    // set up the initial random vectors i.e., the vectors of    // X and Y coordinate points form the display    //    for (int i = 0; i < size; i++)    {      MyPoint p = (MyPoint)data_a.dset4.elementAt(i);      x[i] = (p.x * Rx) + Xmin;      y[i] = Ymax - (p.y * Ry);    }    // compute the covariance matrix of the fourth data set    //    M4.row = M4.col = 2;    M4.Elem = new double[2][2];    M4.resetMatrix();    if (size > 0)    {      M4.Elem = cov.computeCovariance(x, y);    }    // compute the within class scatter matrix    //    M.row = M.col = 2;    M.Elem = new double[2][2];    M.resetMatrix();    M.addMatrix(M1);    M.addMatrix(M2);    M.addMatrix(M3);    M.addMatrix(M4);  }  // method: betweenClass  //  // arguments:  //    Data d: input data points  //    Matrix M: between class scatter matrix  //  // return   : none  //  // this method determines the between class scatter matrix for  // the class independent linear discrimination algorithm  //  public static void betweenClass(    DataPoints d,    Matrix M,    double rx_a,    double ry_a)  {    // declare local variables    //    double xmean = 0.0;    double ymean = 0.0;    double xmean1 = 0.0;    double ymean1 = 0.0;    double xmean2 = 0.0;    double ymean2 = 0.0;    double xmean3 = 0.0;    double ymean3 = 0.0;    double xmean4 = 0.0;    double ymean4 = 0.0;    // declare local matrices    //    Matrix M1 = new Matrix();    Matrix T1 = new Matrix();    Matrix M2 = new Matrix();    Matrix T2 = new Matrix();    Matrix M3 = new Matrix();    Matrix T3 = new Matrix();    Matrix M4 = new Matrix();    Matrix T4 = new Matrix();    // declare the initial random variables    //    double transpose[][] = new double[2][1];    double mean[][] = new double[1][2];    // set scales     double Xmin = OutputPanel.Xmin;    double Ymax = OutputPanel.Ymax;    // get the Rx and Ry ratio, seconds per pixel    //    double Rx = rx_a;    double Ry = ry_a;    // initialize the between class matrix    //    M.row = M.col = 2;    M.Elem = new double[2][2];    M.resetMatrix();    // obtain the means of each individual class    //    if (d.dset1.size() > 0)    {      MyPoint p = (MyPoint)computePointMean(d.dset1);      xmean1 = (p.x * Rx) + Xmin;      ymean1 = Ymax - (p.y * Ry);    }    if (d.dset2.size() > 0)    {      MyPoint p = (MyPoint)computePointMean(d.dset2);      xmean2 = (p.x * Rx) + Xmin;      ymean2 = Ymax - (p.y * Ry);    }    if (d.dset3.size() > 0)    {      MyPoint p = (MyPoint)computePointMean(d.dset3);      xmean3 = (p.x * Rx) + Xmin;      ymean3 = Ymax - (p.y * Ry);    }    if (d.dset4.size() > 0)    {      MyPoint p = (MyPoint)computePointMean(d.dset4);      xmean4 = (p.x * Rx) + Xmin;      ymean4 = Ymax - (p.y * Ry);    }    // compute the global mean of all data sets    //    int samples = 0;    for (int i = 0; i < d.dset1.size(); samples++, i++)    {      MyPoint p2 = (MyPoint)d.dset1.elementAt(i);      xmean += (p2.x * Rx) + Xmin;      ymean += Ymax - (p2.y * Ry);    }    for (int i = 0; i < d.dset2.size(); samples++, i++)    {      MyPoint p2 = (MyPoint)d.dset2.elementAt(i);      xmean += (p2.x * Rx) + Xmin;      ymean += Ymax - (p2.y * Ry);    }    for (int i = 0; i < d.dset3.size(); samples++, i++)    {      MyPoint p2 = (MyPoint)d.dset3.elementAt(i);      xmean += (p2.x * Rx) + Xmin;      ymean += Ymax - (p2.y * Ry);    }    for (int i = 0; i < d.dset4.size(); samples++, i++)    {      MyPoint p2 = (MyPoint)d.dset4.elementAt(i);      xmean += (p2.x * Rx) + Xmin;      ymean += Ymax - (p2.y * Ry);    }    xmean /= samples;    ymean /= samples;    // compute the between class scatter contribution of the first set    //    if (d.dset1.size() > 0)    {      Matrix S = new Matrix();      mean[0][0] = xmean1 - xmean;      mean[0][1] = ymean1 - ymean;      M1.initMatrix(mean, 1, 2);      transpose[0][0] = xmean1 - xmean;      transpose[1][0] = ymean1 - ymean;      T1.initMatrix(transpose, 2, 1);      T1.multMatrix(M1, S);      M.addMatrix(S);    }    // compute the between class scatter contribution of the second set    //    if (d.dset2.size() > 0)    {      Matrix S = new Matrix();      mean[0][0] = xmean2 - xmean;      mean[0][1] = ymean2 - ymean;      M2.initMatrix(mean, 1, 2);      transpose[0][0] = xmean2 - xmean;      transpose[1][0] = ymean2 - ymean;      T2.initMatrix(transpose, 2, 1);      T2.multMatrix(M2, S);      M.addMatrix(S);    }    // compute the between class scatter contribution of the third set    //    if (d.dset3.size() > 0)    {      Matrix S = new Matrix();      mean[0][0] = xmean3 - xmean;      mean[0][1] = ymean3 - ymean;      M3.initMatrix(mean, 1, 2);      transpose[0][0] = xmean3 - xmean;      transpose[1][0] = ymean3 - ymean;      T3.initMatrix(transpose, 2, 1);      T3.multMatrix(M3, S);      M.addMatrix(S);    }    // compute the between class scatter contribution of the forth set    //    if (d.dset4.size() > 0)    {      Matrix S = new Matrix();      mean[0][0] = xmean4 - xmean;      mean[0][1] = ymean4 - ymean;      M4.initMatrix(mean, 1, 2);      transpose[0][0] = xmean4 - xmean;      transpose[1][0] = ymean4 - ymean;      T4.initMatrix(transpose, 2, 1);      T4.multMatrix(M4, S);      M.addMatrix(S);    }  }a987 3//// end of file@

⌨️ 快捷键说明

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