# Copyright 2019 Francesco Ceccon
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=invalid-name
"""Floating point arithmetic."""
import numpy as np
import math

make_number = float
inf = np.inf
zero = float(0.0)
pi = np.pi
isnan = lambda n: np.isnan(n)
isinf = lambda n: np.isinf(n)
float_int = {float, int}


def _declare_unary_function(name, fun):
    # skip rounding mode since we don't round
    globals()[name] = lambda n, _: float(fun(n))


def _declare_binary_function(name, fun):
    # skip rounding mode since we don't round
    globals()[name] = lambda x, y, _: float(fun(x, y))


_UNARY_FUNCTIONS = [
    ('sqrt', 'sqrt'), ('log', 'log'), ('log10', 'log10'), ('exp', 'exp'), ('sin', 'sin'),
    ('asin', 'arcsin'), ('cos', 'cos'), ('acos', 'arccos'),
    ('tan', 'tan'), ('atan', 'arctan')
]
for fun_name, fun in _UNARY_FUNCTIONS:
    _declare_unary_function(fun_name, getattr(np, fun))


_BINARY_FUNCTIONS = ['power']
for fun in _BINARY_FUNCTIONS:
    _declare_binary_function(fun, getattr(np, fun))


def down(f):
    """Perform computation rounding down."""
    return f()


def up(f):
    """Perform computation rounding down."""
    return f()


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-6, abs_eps=1e-8):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))

c1e6028c56920dda75c9f6e5825e34feb91807ba
<mask token>


def down(f):
    """Perform computation rounding down."""
    return f()


<mask token>


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-06, abs_eps=1e-08):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))

<mask token>


def _declare_unary_function(name, fun):
    globals()[name] = lambda n, _: float(fun(n))


def _declare_binary_function(name, fun):
    globals()[name] = lambda x, y, _: float(fun(x, y))


<mask token>


def down(f):
    """Perform computation rounding down."""
    return f()


def up(f):
    """Perform computation rounding down."""
    return f()


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-06, abs_eps=1e-08):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))

<mask token>


def _declare_unary_function(name, fun):
    globals()[name] = lambda n, _: float(fun(n))


def _declare_binary_function(name, fun):
    globals()[name] = lambda x, y, _: float(fun(x, y))


<mask token>
for fun_name, fun in _UNARY_FUNCTIONS:
    _declare_unary_function(fun_name, getattr(np, fun))
<mask token>
for fun in _BINARY_FUNCTIONS:
    _declare_binary_function(fun, getattr(np, fun))


def down(f):
    """Perform computation rounding down."""
    return f()


def up(f):
    """Perform computation rounding down."""
    return f()


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-06, abs_eps=1e-08):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))

<mask token>
make_number = float
inf = np.inf
zero = float(0.0)
pi = np.pi
isnan = lambda n: np.isnan(n)
isinf = lambda n: np.isinf(n)
float_int = {float, int}


def _declare_unary_function(name, fun):
    globals()[name] = lambda n, _: float(fun(n))


def _declare_binary_function(name, fun):
    globals()[name] = lambda x, y, _: float(fun(x, y))


_UNARY_FUNCTIONS = [('sqrt', 'sqrt'), ('log', 'log'), ('log10', 'log10'), (
    'exp', 'exp'), ('sin', 'sin'), ('asin', 'arcsin'), ('cos', 'cos'), (
    'acos', 'arccos'), ('tan', 'tan'), ('atan', 'arctan')]
for fun_name, fun in _UNARY_FUNCTIONS:
    _declare_unary_function(fun_name, getattr(np, fun))
_BINARY_FUNCTIONS = ['power']
for fun in _BINARY_FUNCTIONS:
    _declare_binary_function(fun, getattr(np, fun))


def down(f):
    """Perform computation rounding down."""
    return f()


def up(f):
    """Perform computation rounding down."""
    return f()


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-06, abs_eps=1e-08):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))

# Copyright 2019 Francesco Ceccon
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=invalid-name
"""Floating point arithmetic."""
import numpy as np
import math

make_number = float
inf = np.inf
zero = float(0.0)
pi = np.pi
isnan = lambda n: np.isnan(n)
isinf = lambda n: np.isinf(n)
float_int = {float, int}


def _declare_unary_function(name, fun):
    # skip rounding mode since we don't round
    globals()[name] = lambda n, _: float(fun(n))


def _declare_binary_function(name, fun):
    # skip rounding mode since we don't round
    globals()[name] = lambda x, y, _: float(fun(x, y))


_UNARY_FUNCTIONS = [
    ('sqrt', 'sqrt'), ('log', 'log'), ('log10', 'log10'), ('exp', 'exp'), ('sin', 'sin'),
    ('asin', 'arcsin'), ('cos', 'cos'), ('acos', 'arccos'),
    ('tan', 'tan'), ('atan', 'arctan')
]
for fun_name, fun in _UNARY_FUNCTIONS:
    _declare_unary_function(fun_name, getattr(np, fun))


_BINARY_FUNCTIONS = ['power']
for fun in _BINARY_FUNCTIONS:
    _declare_binary_function(fun, getattr(np, fun))


def down(f):
    """Perform computation rounding down."""
    return f()


def up(f):
    """Perform computation rounding down."""
    return f()


def min_(*args):
    """Returns the minimum."""
    return min(a for a in args if not isnan(a))


def max_(*args):
    """Return the maximum."""
    return max(a for a in args if not isnan(a))


def almosteq(a, b, rel_eps=1e-6, abs_eps=1e-8):
    """Floating point equality check between `a` and `b`."""
    if type(a) in float_int and type(b) in float_int:
        return math.isclose(a, b, rel_tol=rel_eps, abs_tol=abs_eps)
    else:
        return np.isclose(a, b, rtol=rel_eps, atol=abs_eps)


def almostgte(a, b):
    """Return True if a >= b."""
    return np.all(np.logical_or(a > b, almosteq(a, b)))


def almostlte(a, b):
    """Return True if a <= b."""
    return np.all(np.logical_or(a < b, almosteq(a, b)))
