net.algart.math.functions.MaxFromTwoSelectedNumbersFunc Maven / Gradle / Ivy
Show all versions of algart Show documentation
/*
* The MIT License (MIT)
*
* Copyright (c) 2007-2024 Daniel Alievsky, AlgART Laboratory (http://algart.net)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.algart.math.functions;
/**
* Maximum from 2 arguments, selected by 1st argument:
* f(x0, x1, ..., xn-1) =
* max(xi+1, xj+1),
* i=(int)x[0]
(x0 cast to integer type),
* j=(i−1+indexShift
)%(n−1)+1,
* where indexShift
is an integer constant, passed to {@link #getInstance} method.
*
* More precisely, the {@link #get} method of this object performs the following actions:
*
*
* int k1 = (int)x[0] + 1;
* // it is supposed that always 1<=(int)x[0]<=x.length
* int k2 = k1 + indexShift;
* if (k2 >= x.length) {
* // it is supposed that indexShift<x.length-1
* k2 -= x.length - 1;
* }
* return x[k1] > x[k2] ? x[k1] : x[k2];
*
*
* If k1
or k2
index, calculated in such a way,
* is out of range 0..x.length-1
,
* this method throws IndexOutOfBoundsException
.
*
* This function can be useful for algorithms of non-maximum suppression, usually in combination with
* {@link Func#SELECT_FROM_8_DIRECTIONS_2D}, for example, in algorithms
* like Canny edge detector.
*
* This class is immutable and thread-safe:
* there are no ways to modify settings of the created instance.
*
* @author Daniel Alievsky
*/
public class MaxFromTwoSelectedNumbersFunc extends AbstractFunc implements Func {
private final int indexShift;
private MaxFromTwoSelectedNumbersFunc(int indexShift) {
if (indexShift < 0) {
throw new IllegalArgumentException("Negative index shift " + indexShift);
}
this.indexShift = indexShift;
}
/**
* Returns an instance of this class for the given index shift.
*
* @param indexShift the index shift (distance between compared numbers); must be non-negative.
* @return an instance of this class
* @throws IllegalArgumentException if indexShift<0
.
*/
public static MaxFromTwoSelectedNumbersFunc getInstance(int indexShift) {
return new MaxFromTwoSelectedNumbersFunc(indexShift);
}
public double get(double... x) {
int k1 = (int) x[0] + 1; // it is supposed that k1 < x.length
int k2 = k1 + indexShift;
if (k2 >= x.length) {
k2 -= x.length - 1;
}
return Math.max(x[k1], x[k2]);
}
/**
* Returns a brief string description of this object.
*
* @return a brief string description of this object.
*/
public String toString() {
return "max from 2 selected numbers f(x0,x1,...)=max(x[(int)x0+1],x[(int)x0+1+sh]), sh=" + indexShift;
}
}