@ Override public double membership ( double x ) { if ( Double . isNaN ( x ) ) { return Double . NaN ; } if ( xy . isEmpty ( ) ) { throw new RuntimeException ( "[discrete error] term is empty" ) ; } // ______________________ // / \ // / \ // ____________/ \____________ // x[0] x[n-1] // Pair first = xy . get ( 0 ) ; Pair last = xy . get ( xy . size ( ) - 1 ) ; if ( Op . isLE ( x , first . getX ( ) ) ) { return height * first . getY ( ) ; } if ( Op . isGE ( x , last . getX ( ) ) ) { return height * last . getY ( ) ; } Discrete . Pair value = new Discrete . Pair ( x , Double . NaN ) ; //Binary search will find a number greater than or equal to x int upper = Collections . binarySearch ( xy , value , ASCENDANTLY ) ; //if the upper bound is equal to x if ( upper >= 0 ) { return height * xy . get ( upper ) . y ; } //if the upper bound is not x, then binary search returns (-insertionPoint - 1) upper = Math . abs ( upper + 1 ) ; //and the lower bound int lower = upper - 1 ; //FuzzyLite.logger().log(Level.INFO, "x={0}\t[{1} , {2}]", new Object[]{x, lower, upper}); return height * Op . scale ( x , xy . get ( lower ) . getX ( ) , xy . get ( upper ) . getX ( ) , xy . get ( lower ) . getY ( ) , xy . get ( upper ) . getY ( ) ) ; }
Computes the membership function evaluated at x by using binary search to find the lower and upper bounds of x and then linearly interpolating the membership function between the bounds .