abs max Algorithm

The Absolute Maximum (Abs Max) Algorithm is a numerical optimization technique used to find the maximum value of a function within a specified interval. The algorithm works by evaluating the function at different points within the interval and comparing the absolute values of these points. The point with the highest absolute value is considered the absolute maximum. This algorithm is particularly useful in mathematical optimization problems, engineering, and computer science, where the objective is to find the maximum or minimum value of a real-valued function. The Abs Max Algorithm can be implemented using various approaches such as brute force, which involves dividing the interval into smaller sub-intervals and comparing the absolute values of the function at each sub-interval's endpoint. Another approach is to use calculus to find the critical points (i.e., points where the derivative of the function is zero or undefined) within the interval and evaluate the function at these points along with the interval's endpoints. The point with the highest absolute value among these critical points and endpoints is considered the absolute maximum. However, it is essential to note that the Abs Max Algorithm is only applicable to continuous functions, as discontinuities in the function may lead to erroneous results.
from typing import List


def abs_max(x: List[int]) -> int:
    """
    >>> abs_max([0,5,1,11])
    11
    >>> abs_max([3,-10,-2])
    -10
    """
    j = x[0]
    for i in x:
        if abs(i) > abs(j):
            j = i
    return j


def abs_max_sort(x):
    """
    >>> abs_max_sort([0,5,1,11])
    11
    >>> abs_max_sort([3,-10,-2])
    -10
    """
    return sorted(x, key=abs)[-1]


def main():
    a = [1, 2, -11]
    assert abs_max(a) == -11
    assert abs_max_sort(a) == -11


if __name__ == "__main__":
    main()

LANGUAGE:

DARK MODE: