# Copyright 2021 BlackRock, Inc.
# 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.
from typing import cast

import numpy as np
import numpy.typing as npt


def levy_13(x1: float, x2: float) -> float:
    """https://www.sfu.ca/~ssurjano/levy13.html."""
    term_1 = np.sin(3 * np.pi * x1) ** 2
    term_2 = (x1 - 1) ** 2 * (1 + np.sin(3 * np.pi * x2) ** 2)
    term_3 = (x2 - 1) ** 2 * (1 + np.sin(2 * np.pi * x1) ** 2)
    return cast(float, term_1 + term_2 + term_3)


def levy_13_np(x: npt.NDArray[np.floating]) -> float:
    if len(x) != 2:
        raise AssertionError("Exactly 2 items expected")
    return levy_13(x[0], x[1])
