<gh_stars>0
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_functions.ipynb (unless otherwise specified).

__all__ = ['Ifunction', 'himmelblau', 'eggholder', 'mc_cormick', 'holder_table', 'beale', 'saddle_point', 'Function']

# Internal Cell
from nbdev.showdoc import *
import numpy as np
import numpy as np
from mpl_toolkits import mplot3d
from matplotlib import pyplot as plt
import matplotlib.colors as colors

import jax.numpy as jnp

# Cell
class Ifunction:
    def __init__(self):
        pass

    def __call__(*args) -> np.ndarray:
        pass

    def min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 3) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y,z) coordinates of the minimum values
        """
        return self.coord(self._min())

    def coord(self, points: np.ndarray) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 3) with all the evaluations of the given
        k points of this function.
        The three values of the second dimension are the (x,y,z) coordinates of the minimum values
        """
        z = np.expand_dims(self(points[:, 0], points[:, 1]), axis=-1)
        return np.hstack((
            points,
            z
        ))

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        pass

# Cell
class himmelblau(Ifunction):
    def __call__(self, x: np.ndarray, y: np.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return (x**2+y-11)**2 + (x+y**2-7)**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3.0, 2.0],
            [-2.805118, 3.131312],
            [-3.779310, -3.283186],
            [3.584428, -1.848126]
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-5, 5],
            [-5, 5]
        ])

# Cell
class eggholder(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return -(y+47)*jnp.sin(jnp.sqrt(jnp.abs(x/2+(y+47)))) - x*jnp.sin(jnp.sqrt(jnp.abs(x-(y+47))))

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [512, 404.239],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-512, 512],
            [-512, 512]
        ])

# Cell
class mc_cormick(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return jnp.sin(x+y) + (x-y)**2-1.5*x+2.5*y+1

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [-0.54719, -1.54719],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-1.5, 4],
            [-3, 4]
        ])

# Cell
class holder_table(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return -jnp.abs(jnp.sin(x)*jnp.cos(y)*jnp.exp(jnp.abs(1-jnp.sqrt(x**2+y**2)/jnp.pi)))

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [8.05502, 9.66459],
            [8.05502, -9.66459],
            [-8.05502, 9.66459],
            [-8.05502, -9.66459],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-10, 10],
            [-10, 10]
        ])


# Cell
class beale(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return (1.5-x+x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3, 0.5],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-4, 4],
            [-4, 4]
        ])


# Cell
class saddle_point(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return x**2 - y**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3, 0.5],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-1.5, 1],
            [-1.5, 1]
        ])


# Cell
Function = {clazz.__name__: clazz() for clazz in Ifunction.__subclasses__()}
Function
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_functions.ipynb (unless otherwise specified).

__all__ = ['Ifunction', 'himmelblau', 'eggholder', 'mc_cormick', 'holder_table', 'beale', 'saddle_point', 'Function']

# Internal Cell
from nbdev.showdoc import *
import numpy as np
import numpy as np
from mpl_toolkits import mplot3d
from matplotlib import pyplot as plt
import matplotlib.colors as colors

import jax.numpy as jnp

# Cell
class Ifunction:
    def __init__(self):
        pass

    def __call__(*args) -> np.ndarray:
        pass

    def min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 3) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y,z) coordinates of the minimum values
        """
        return self.coord(self._min())

    def coord(self, points: np.ndarray) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 3) with all the evaluations of the given
        k points of this function.
        The three values of the second dimension are the (x,y,z) coordinates of the minimum values
        """
        z = np.expand_dims(self(points[:, 0], points[:, 1]), axis=-1)
        return np.hstack((
            points,
            z
        ))

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        pass

# Cell
class himmelblau(Ifunction):
    def __call__(self, x: np.ndarray, y: np.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return (x**2+y-11)**2 + (x+y**2-7)**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3.0, 2.0],
            [-2.805118, 3.131312],
            [-3.779310, -3.283186],
            [3.584428, -1.848126]
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-5, 5],
            [-5, 5]
        ])

# Cell
class eggholder(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return -(y+47)*jnp.sin(jnp.sqrt(jnp.abs(x/2+(y+47)))) - x*jnp.sin(jnp.sqrt(jnp.abs(x-(y+47))))

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [512, 404.239],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-512, 512],
            [-512, 512]
        ])

# Cell
class mc_cormick(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return jnp.sin(x+y) + (x-y)**2-1.5*x+2.5*y+1

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [-0.54719, -1.54719],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-1.5, 4],
            [-3, 4]
        ])

# Cell
class holder_table(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return -jnp.abs(jnp.sin(x)*jnp.cos(y)*jnp.exp(jnp.abs(1-jnp.sqrt(x**2+y**2)/jnp.pi)))

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [8.05502, 9.66459],
            [8.05502, -9.66459],
            [-8.05502, 9.66459],
            [-8.05502, -9.66459],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-10, 10],
            [-10, 10]
        ])


# Cell
class beale(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return (1.5-x+x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3, 0.5],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-4, 4],
            [-4, 4]
        ])


# Cell
class saddle_point(Ifunction):
    def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> np.ndarray:
        """
        Computes the given function
        """
        return x**2 - y**2

    def _min(self) -> np.ndarray:
        """
        Returns a np.array of the shape (k, 2) with all the minimum k points of this function.
        The two values of the second dimension are the (x,y) coordinates of the minimum values
        """
        return np.array([
            [3, 0.5],
        ])

    def domain(self) -> np.ndarray:
        """
        Returns the ((x_min, x_max), (y_min, y_max)) values where this function
        is of most interest
        """
        return np.array([
            [-1.5, 1],
            [-1.5, 1]
        ])


# Cell
Function = {clazz.__name__: clazz() for clazz in Ifunction.__subclasses__()}
Function
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_functions.ipynb (unless otherwise specified). # Internal Cell # Cell Returns a np.array of the shape (k, 3) with all the minimum k points of this function.         The two values of the second dimension are the (x,y,z) coordinates of the minimum values Returns a np.array of the shape (k, 3) with all the evaluations of the given         k points of this function.         The three values of the second dimension are the (x,y,z) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell Computes the given function Returns a np.array of the shape (k, 2) with all the minimum k points of this function.         The two values of the second dimension are the (x,y) coordinates of the minimum values Returns the ((x_min, x_max), (y_min, y_max)) values where this function         is of most interest # Cell