查找当地最低当地、最低

2023-09-11 06:01:10 作者:原宿风i

在code以下正确找到当地最大的一个阵列,但未能找到当地最低。我已经做了网络搜索找到寻找最低的最佳方法,而且我觉得我用下面的正确的方法,根据这些搜索。但是,在下面的code,我还没有看到一些bug,很多时候会超过每行过几天的课程后。

变量STARTX和end​​X定义用户选择窗口,其中,code必须找到本地最小值/最大值。如果操作为STARTX和end​​X的值,你会看到,下面总是code中的最小值作为被选择窗口的第一个索引,这表明它不是通过指数迭代在窗口要搜索的输出最低限度。

任何人都可以找到错误,告诉我​​如何解决下面的code找到当地最低?

 类LocalMinMax {
静态双重[]分;
静态INT visiblePoints = 5000;
静态INT STARTX = 200;
静态INT endX = 700;

公共静态无效的主要(字串[] args){
    INT lastX2 = 0;
    INT =了maxWidth 800;
    双hstep =(双)了maxWidth / visiblePoints;
    INT =了maxHeight 400;
    分=新的双[visiblePoints]
    双最大值= Double.NEGATIVE_INFINITY;
    双分= Double.POSITIVE_INFINITY;
    INT minIndex = -1;
    INT maxIndex = -1;
    的for(int i = 0; I< visiblePoints;我++){
        分[I] =(双)((((Math.sin(0.009 * I))*(Math.cos(0.004 * I)))*(了maxHeight / 3)* 0.95)+(了maxHeight / 2 ));
        INT X2 =(int)的(I * hstep);
        如果(X2> = STARTX){
            INT sectionStartIndex =我;
            INT sectionEndIndex =(INT)(endX / hstep);
                对于(INT K = sectionStartIndex; K< sectionEndIndex; k ++){
                    如果(分> PTS [K]){
                        分= PTS [k]的;
                        minIndex = X2;
                        的System.out.println(minIndex,分钟,PTS [+ K +]:,x2是:+ minIndex +,+ MIN +,+ PTS [K] +,+ 2次);
                    }
                    如果(最大值&其中; PTS [K]){
                        最大= PTS [K]
                        maxIndex = X2;
                    }}}
        如果(lastX2!= X2){
            lastX2 = X2;
            如果(X2 == STARTX){
                INT宽度= endX  -  STARTX;
                的System.out.println(窗口:宽度,STARTX,endX是:+宽+,+ STARTX +,+ endX);
                }}}
    INT MAXVAL =(int)的最大值;
    INT MINVAL =(INT)分钟;
    的System.out.println(本地最大值:maxIndex,MAXVAL是:+ maxIndex +,+ MAXVAL);
    的System.out.println(LOCAL MIN:minIndex,MINVAL是:+ minIndex +,+ MINVAL);
    }}
 

解决方案

一种方法是扫描的后退的通过 sectionStartIndex..sectionEndIndex 窗口如下所示。这不是解决办法,但它会让你看到绿色的长方形上下移动,因为它跟踪的最低水平。

 的for(int K = sectionStartIndex; K< sectionEndIndex; k ++){
    INT J = sectionEndIndex  -  K;
    如果(分> PTS [J]){
        的System.out.println(minIndex,分,PTS [+ J +]:
            + minIndex +,+ MIN +,+ PTS [j]的);
        分= PTS [J]。
        minIndex = x2Count;
    }
    如果(最大值&其中; PTS [K]){
        最大= PTS [K]
        maxIndex = x2Count;
    }
}
 

附录:吊装数组初始化圈外是一种方法,如下图所示。注意使用除数的缩放操作。

更严重的问题是评估在一个域的功能,而在另一个采样它。我想分开的模型视图(鼠标坐标)(实数再由 psented $ P $双)。介绍的方法在两者之间的坐标转换,如 *规模* 函数看到的此处。让模型评价功能的鼠标移动;只有在被证明有必要优化。

本地:maxIndex,MAXVAL:200,326
本地:minIndex,MINVAL:200,79
51信用卡涉嫌暴力催收被查,网贷暴力催收何时休

 类LocalMinMax {

    静态双重[]分;
    静态INT visiblePoints = 5000;
    静态INT STARTX = 200;
    静态INT endX = 700;

    公共静态无效的主要(字串[] args){
        INT lastX2 = 0;
        INT =了maxWidth 800;
        双hstep =了maxWidth /(双)visiblePoints;
        INT =了maxHeight 400;
        分=新的双[visiblePoints]
        的for(int i = 0; I< pts.length;我++){
            分[I] =(((Math.sin(0.009 * I))*(Math.cos(0.004 * I)))
                *(了maxHeight / 3D)*​​ 0.95)+(了maxHeight / 2d中);
        }
        双最大值= Double.NEGATIVE_INFINITY;
        双分= Double.POSITIVE_INFINITY;
        INT minIndex = -1;
        INT maxIndex = -1;
        的for(int i = 0; I< visiblePoints;我++){
            INT X2 =(int)的(I * hstep);
            如果(X2> = STARTX){
                INT sectionStartIndex =我;
                INT sectionEndIndex =(INT)(endX / hstep);
                对于(INT K = sectionStartIndex; K< sectionEndIndex; k ++){
                    如果(分> PTS [K]){
                        分= PTS [k]的;
                        minIndex = X2;
                      //System.out.println("minIndex,分,PTS [+ K +],X2:
                      // + minIndex +,+ MIN +,+ PTS [K] +,+ 2次);
                    }
                    如果(最大值&其中; PTS [K]){
                        最大= PTS [K]
                        maxIndex = X2;
                      //System.out.println("maxIndex,最大,PTS [+ K +],X2:
                      // + maxIndex +,+ MAX +,+ PTS [K] +,+ 2次);
                    }
                }
            }
            如果(lastX2!= X2){
                lastX2 = X2;
                如果(X2 == STARTX){
                    INT宽度= endX  -  STARTX;
                }
            }
        }
        INT MAXVAL =(int)的最大值;
        INT MINVAL =(INT)分钟;
        的System.out.println(地方:maxIndex,MAXVAL:+ maxIndex +,+ MAXVAL);
        的System.out.println(地方:minIndex,MINVAL:+ minIndex +,+ MINVAL);
    }
}
 

The code below correctly finds the local maximum of an array, but it fails to find the local minimum. I have done web searches to find best methods for finding minima, and I think I am using the correct method below, based on those searches. But there is some bug in the code below that I am still not seeing, after going over each line many times over the course of a few days.

The variables startX and endX define the user-selected window in which the code must find the local min/max. If you manipulate the values for startX and endX, you will see that the code below always outputs the minimum as the first index of the selected window, which indicates that it is not iterating through the indices in the window to search for the minimum.

Can anyone find the bug and show me how to fix the code below to find the local minimum?

class LocalMinMax {
static double[] pts;
static int visiblePoints=5000;
static int startX = 200;
static int endX = 700;

public static void main (String[] args) {
    int lastX2 = 0;
    int maxWidth = 800;
    double hstep = (double) maxWidth / visiblePoints;
    int maxHeight = 400;
    pts = new double[visiblePoints];
    double max = Double.NEGATIVE_INFINITY;
    double min = Double.POSITIVE_INFINITY;
    int minIndex = -1;
    int maxIndex = -1;
    for (int i = 0; i < visiblePoints; i++){
        pts[i] = (double) ((((Math.sin(.009*i))*(Math.cos(.004*i))) * (maxHeight/3) * .95) + (maxHeight/2));
        int x2 = (int) (i * hstep);
        if(x2>=startX){
            int sectionStartIndex = i;
            int sectionEndIndex = (int)(endX/hstep);
                for(int k=sectionStartIndex;k<sectionEndIndex;k++){
                    if(min>pts[k]){
                        min = pts[k];
                        minIndex = x2;
                        System.out.println("minIndex, min, pts["+k+"]: , x2 are: "+minIndex+", "+min+", "+pts[k]+", "+x2);
                    }
                    if(max<pts[k]){
                        max = pts[k];
                        maxIndex = x2;
                    }}}
        if(lastX2!=x2){
            lastX2=x2;
            if(x2==startX){
                int width = endX - startX;
                System.out.println("WINDOW: width, startX, endX are: "+width+", "+startX+", "+endX);
                }}}
    int maxVal = (int)max;
    int minVal = (int)min;
    System.out.println("LOCAL MAX: maxIndex, maxVal are: "+maxIndex+", "+maxVal);
    System.out.println("LOCAL MIN: minIndex, minVal are: "+minIndex+", "+minVal);
    }}

解决方案

One approach is to scan backward through the sectionStartIndex..sectionEndIndex window, as shown below. It's not the solution, but it will let you see the green rectangle move up and down as it tracks the minimum.

for (int k = sectionStartIndex; k < sectionEndIndex; k++) {
    int j = sectionEndIndex - k;
    if (min > pts[j]) {
        System.out.println("minIndex, min, pts[" + j + "]: "
            + minIndex + ", " + min + ", " + pts[j]);
        min = pts[j];
        minIndex = x2Count;
    }
    if (max < pts[k]) {
        max = pts[k];
        maxIndex = x2Count;
    }
}

Addendum: Hoisting the array initialization out of the loop is one approach, as shown below. Note the use of a double divisor in scaling operations.

The more serious problem is evaluating the function in one domain, while sampling it in another. I'd separate the view (mouse coordinates) from the model (real numbers represented by double). Introduce methods to transform coordinates between the two, such as the *scale* functions seen here. Let the model evaluate the function as the mouse moves; optimize only if proven necessary.

Local: maxIndex, maxVal: 200, 326
Local: minIndex, minVal: 200, 79

class LocalMinMax {

    static double[] pts;
    static int visiblePoints = 5000;
    static int startX = 200;
    static int endX = 700;

    public static void main(String[] args) {
        int lastX2 = 0;
        int maxWidth = 800;
        double hstep = maxWidth / (double) visiblePoints;
        int maxHeight = 400;
        pts = new double[visiblePoints];
        for (int i = 0; i < pts.length; i++) {
            pts[i] = (((Math.sin(.009 * i)) * (Math.cos(.004 * i)))
                * (maxHeight / 3d) * .95) + (maxHeight / 2d);
        }
        double max = Double.NEGATIVE_INFINITY;
        double min = Double.POSITIVE_INFINITY;
        int minIndex = -1;
        int maxIndex = -1;
        for (int i = 0; i < visiblePoints; i++) {
            int x2 = (int) (i * hstep);
            if (x2 >= startX) {
                int sectionStartIndex = i;
                int sectionEndIndex = (int) (endX / hstep);
                for (int k = sectionStartIndex; k < sectionEndIndex; k++) {
                    if (min > pts[k]) {
                        min = pts[k];
                        minIndex = x2;
                      //System.out.println("minIndex, min, pts[" + k + "], x2: "
                      //+ minIndex + ", " + min + ", " + pts[k] + ", " + x2);
                    }
                    if (max < pts[k]) {
                        max = pts[k];
                        maxIndex = x2;
                      //System.out.println("maxIndex, max, pts[" + k + "], x2: "
                      //+ maxIndex + ", " + max + ", " + pts[k] + ", " + x2);
                    }
                }
            }
            if (lastX2 != x2) {
                lastX2 = x2;
                if (x2 == startX) {
                    int width = endX - startX;
                }
            }
        }
        int maxVal = (int) max;
        int minVal = (int) min;
        System.out.println("Local: maxIndex, maxVal: " + maxIndex + ", " + maxVal);
        System.out.println("Local: minIndex, minVal: " + minIndex + ", " + minVal);
    }
}