Exemplo n.º 1
0
    def __init__(*_coconut_match_to_args, **_coconut_match_to_kwargs):
        """Construct a new BlackBoxOptimizer. It is recommended to pass file=__file__."""
        _coconut_match_check = False
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_to_args) <= 2) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 0, "self" in _coconut_match_to_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 1, "file" in _coconut_match_to_kwargs)) == 1):
            _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("self")
            _coconut_match_temp_1 = _coconut_match_to_args[1] if _coconut.len(_coconut_match_to_args) > 1 else _coconut_match_to_kwargs.pop("file")
            _coconut_match_temp_2 = _coconut_match_to_kwargs.pop("tag") if "tag" in _coconut_match_to_kwargs else None
            _coconut_match_temp_3 = _coconut_match_to_kwargs.pop("protocol") if "protocol" in _coconut_match_to_kwargs else None
            if not _coconut_match_to_kwargs:
                self = _coconut_match_temp_0
                file = _coconut_match_temp_1
                tag = _coconut_match_temp_2
                protocol = _coconut_match_temp_3
                _coconut_match_check = True
        if not _coconut_match_check:
            raise _coconut_FunctionMatchError('match def __init__(self, file, *, tag=None, protocol=None):', _coconut_match_to_args)

        if not isinstance(file, Str):
            raise TypeError("file must be a string")
        self._file = norm_path(file)
        self._tag = tag

        if protocol is None:
# auto-detect protocol
            self._protocol = "json"
            if not os.path.exists(self.data_file):
                self._protocol = default_protocol
        else:
            self._protocol = protocol

        self.reload()
Exemplo n.º 2
0
 def _coconut_lambda_0(*_coconut_match_args,
                       **_coconut_match_kwargs):
     _coconut_match_check_0 = False
     _coconut_match_set_name_val = _coconut_sentinel
     _coconut_match_set_name_loss = _coconut_sentinel
     _coconut_FunctionMatchError = _coconut_get_function_match_error(
     )
     if _coconut.len(_coconut_match_args) == 1:
         if (_coconut.isinstance(
                 _coconut_match_args[0],
                 _coconut.abc.Sequence)) and (_coconut.len(
                     _coconut_match_args[0]) == 2):
             _coconut_match_set_name_val = _coconut_match_args[0][0]
             _coconut_match_set_name_loss = _coconut_match_args[0][
                 1]
             if not _coconut_match_kwargs:
                 _coconut_match_check_0 = True
     if _coconut_match_check_0:
         if _coconut_match_set_name_val is not _coconut_sentinel:
             val = _coconut_match_set_name_val
         if _coconut_match_set_name_loss is not _coconut_sentinel:
             loss = _coconut_match_set_name_loss
     if not _coconut_match_check_0:
         raise _coconut_FunctionMatchError(
             'best_val, min_loss = min(marginals, key=def ((val, loss)) -> loss)',
             _coconut_match_args)
     return loss
Exemplo n.º 3
0
def init_backend(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    """Create a backend object of the given name with the given data."""
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.sum((_coconut.len(_coconut_match_to_args) > 0, "name" in _coconut_match_to_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 1, "examples" in _coconut_match_to_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 2, "params" in _coconut_match_to_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("name")
        _coconut_match_temp_1 = _coconut_match_to_args[1] if _coconut.len(_coconut_match_to_args) > 1 else _coconut_match_to_kwargs.pop("examples")
        _coconut_match_temp_2 = _coconut_match_to_args[2] if _coconut.len(_coconut_match_to_args) > 2 else _coconut_match_to_kwargs.pop("params")
        args = _coconut_match_to_args[3:]
        _coconut_match_temp_3 = _coconut_match_to_kwargs.pop("attempt_to_update_backend") if "attempt_to_update_backend" in _coconut_match_to_kwargs else None
        name = _coconut_match_temp_0
        examples = _coconut_match_temp_1
        params = _coconut_match_temp_2
        attempt_to_update_backend = _coconut_match_temp_3
        options = _coconut_match_to_kwargs
        _coconut_match_check = True
    if not _coconut_match_check:
        raise _coconut_FunctionMatchError('match def init_backend(name, examples, params, *args, attempt_to_update_backend=None, **options):', _coconut_match_to_args)

    backend_cls = backend_registry[name]
    if attempt_to_update_backend is not None and isinstance(attempt_to_update_backend, backend_cls):
        updated_backend = attempt_to_update_backend.attempt_update(examples, params, *args, **options)
        if updated_backend is True:
            return attempt_to_update_backend
        elif isinstance(updated_backend, backend_cls):
            return updated_backend
        else:
            assert updated_backend is False, "invalid backend.attempt_update return value {_coconut_format_0} from {_coconut_format_1}".format(_coconut_format_0=(updated_backend), _coconut_format_1=(backend_cls))
    return backend_cls(examples, params, *args, **options)
Exemplo n.º 4
0
def forall_handle(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) <= 2) and (_coconut.sum(
        (_coconut.len(_coconut_match_to_args) > 0, "const"
         in _coconut_match_to_kwargs)) == 1) and (_coconut.sum(
             (_coconut.len(_coconut_match_to_args) > 1, "expr"
              in _coconut_match_to_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(
            _coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop(
                "const")
        _coconut_match_temp_1 = _coconut_match_to_args[1] if _coconut.len(
            _coconut_match_to_args) > 1 else _coconut_match_to_kwargs.pop(
                "expr")
        if not _coconut_match_to_kwargs:
            const = _coconut_match_temp_0
            expr = _coconut_match_temp_1
            _coconut_match_check = True
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError(
            "pattern-matching failed for "
            "'match def forall_handle(const, expr) = ForAll(const, expr)'"
            " in " +
            (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr)
             <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'match def forall_handle(const, expr) = ForAll(const, expr)'
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    return _coconut_tail_call(ForAll, const, expr)
Exemplo n.º 5
0
    def __init__(*_coconut_match_args, **_coconut_match_kwargs):
        """
        Construct a new BlackBoxOptimizer. You must either pass file=__file__ or
        both data_dir="/path/to/some/dir" and data_name="my_project_name".
        """
        _coconut_match_check_1 = False
        _coconut_match_set_name_self = _coconut_sentinel
        _coconut_match_set_name_data_dir = _coconut_sentinel
        _coconut_match_set_name_data_name = _coconut_sentinel
        _coconut_match_set_name_kwargs = _coconut_sentinel
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_args) <= 3) and (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "self" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "data_dir" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "data_name" in _coconut_match_kwargs)) == 1):
            _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("self")
            _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("data_dir")
            _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("data_name")
            if ((isinstance)(_coconut_match_temp_1, Str)) and ((isinstance)(_coconut_match_temp_2, Str)):
                _coconut_match_set_name_self = _coconut_match_temp_0
                _coconut_match_set_name_data_dir = _coconut_match_temp_1
                _coconut_match_set_name_data_name = _coconut_match_temp_2
                _coconut_match_set_name_kwargs = _coconut_match_kwargs
                _coconut_match_check_1 = True
        if _coconut_match_check_1:
            if _coconut_match_set_name_self is not _coconut_sentinel:
                self = _coconut_match_temp_0
            if _coconut_match_set_name_data_dir is not _coconut_sentinel:
                data_dir = _coconut_match_temp_1
            if _coconut_match_set_name_data_name is not _coconut_sentinel:
                data_name = _coconut_match_temp_2
            if _coconut_match_set_name_kwargs is not _coconut_sentinel:
                kwargs = _coconut_match_kwargs
        if not _coconut_match_check_1:
            raise _coconut_FunctionMatchError('addpattern def __init__(self, data_dir `isinstance` Str, data_name `isinstance` Str, **kwargs):', _coconut_match_args)

        self.__init__(os.path.join(data_dir, data_name), **kwargs)
Exemplo n.º 6
0
    def __init__(*_coconut_match_args, **_coconut_match_kwargs):
        _coconut_match_check_0 = False
        _coconut_match_set_name_self = _coconut_sentinel
        _coconut_match_set_name_file = _coconut_sentinel
        _coconut_match_set_name_tag = _coconut_sentinel
        _coconut_match_set_name_protocol = _coconut_sentinel
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_args) <= 2) and (_coconut.sum(
            (_coconut.len(_coconut_match_args) > 0, "self"
             in _coconut_match_kwargs)) == 1) and (_coconut.sum(
                 (_coconut.len(_coconut_match_args) > 1, "file"
                  in _coconut_match_kwargs)) == 1):
            _coconut_match_temp_2 = _coconut_match_kwargs.pop(
                "tag") if "tag" in _coconut_match_kwargs else None
            _coconut_match_temp_3 = _coconut_match_kwargs.pop(
                "protocol") if "protocol" in _coconut_match_kwargs else None
            _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(
                _coconut_match_args) > 0 else _coconut_match_kwargs.pop("self")
            _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(
                _coconut_match_args) > 1 else _coconut_match_kwargs.pop("file")
            _coconut_match_set_name_tag = _coconut_match_temp_2
            _coconut_match_set_name_protocol = _coconut_match_temp_3
            if (isinstance)(_coconut_match_temp_1, Str):
                _coconut_match_set_name_self = _coconut_match_temp_0
                _coconut_match_set_name_file = _coconut_match_temp_1
                if not _coconut_match_kwargs:
                    _coconut_match_check_0 = True
        if _coconut_match_check_0:
            if _coconut_match_set_name_self is not _coconut_sentinel:
                self = _coconut_match_set_name_self
            if _coconut_match_set_name_file is not _coconut_sentinel:
                file = _coconut_match_set_name_file
            if _coconut_match_set_name_tag is not _coconut_sentinel:
                tag = _coconut_match_set_name_tag
            if _coconut_match_set_name_protocol is not _coconut_sentinel:
                protocol = _coconut_match_set_name_protocol
        if not _coconut_match_check_0:
            raise _coconut_FunctionMatchError(
                'match def __init__(self, file `isinstance` Str, *, tag=None, protocol=None):',
                _coconut_match_args)

        self._backend_creation_counts = defaultdict(int)

        self._file = norm_path(file)
        self._tag = tag

        if protocol is None:
            # auto-detect protocol
            self.protocol = "json"
            if not os.path.exists(self.data_file):
                self.protocol = constants.default_protocol
        else:
            self.protocol = protocol

        self.reload()
Exemplo n.º 7
0
def mean(*_coconut_match_args, **_coconut_match_kwargs):
    _coconut_match_check_0 = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_args) == 1) and (_coconut.isinstance(_coconut_match_args[0], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[0]) >= 0):
        xs = _coconut.list(_coconut_match_args[0])
        if not _coconut_match_kwargs:
            _coconut_match_check_0 = True
    if not _coconut_match_check_0:
        raise _coconut_FunctionMatchError('match def mean([] + xs) =', _coconut_match_args)

    return sum(xs) / len(xs)
Exemplo n.º 8
0
def mean(*_coconut_match_args, **_coconut_match_kwargs):
    """Compute the arithmetic mean of the given sequence."""
    _coconut_match_check_1 = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_args) == 1) and (_coconut.isinstance(_coconut_match_args[0], _coconut.abc.Iterable)):
        xs = _coconut_match_args[0]
        if not _coconut_match_kwargs:
            _coconut_match_check_1 = True
    if not _coconut_match_check_1:
        raise _coconut_FunctionMatchError('addpattern def mean(() :: xs) =', _coconut_match_args)

    return (mean)((tuple)(xs))
Exemplo n.º 9
0
    def attempt_update(*_coconut_match_args, **_coconut_match_kwargs):
        """Special method that allows fast updating of the backend."""
        _coconut_match_check_0 = False
        _coconut_match_set_name_self = _coconut_sentinel
        _coconut_match_set_name_examples = _coconut_sentinel
        _coconut_match_set_name_params = _coconut_sentinel
        _coconut_match_set_name_distribution = _coconut_sentinel
        _coconut_match_set_name_remove_erroring_algs = _coconut_sentinel
        _coconut_match_set_name__backend_store = _coconut_sentinel
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_args) <= 5) and (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "self" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "examples" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "params" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 3, "distribution" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 4, "remove_erroring_algs" in _coconut_match_kwargs)) <= 1):
            _coconut_match_temp_5 = _coconut_match_kwargs.pop("_backend_store") if "_backend_store" in _coconut_match_kwargs else _coconut_sentinel
            if _coconut_match_temp_5 is not _coconut_sentinel:
                _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("self")
                _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("examples")
                _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("params")
                _coconut_match_temp_3 = _coconut_match_args[3] if _coconut.len(_coconut_match_args) > 3 else _coconut_match_kwargs.pop("distribution")
                _coconut_match_temp_4 = _coconut_match_args[4] if _coconut.len(_coconut_match_args) > 4 else _coconut_match_kwargs.pop("remove_erroring_algs") if "remove_erroring_algs" in _coconut_match_kwargs else False
                _coconut_match_set_name__backend_store = _coconut_match_temp_5
                _coconut_match_set_name_self = _coconut_match_temp_0
                _coconut_match_set_name_examples = _coconut_match_temp_1
                _coconut_match_set_name_params = _coconut_match_temp_2
                _coconut_match_set_name_distribution = _coconut_match_temp_3
                _coconut_match_set_name_remove_erroring_algs = _coconut_match_temp_4
                if not _coconut_match_kwargs:
                    _coconut_match_check_0 = True
        if _coconut_match_check_0:
            if _coconut_match_set_name_self is not _coconut_sentinel:
                self = _coconut_match_set_name_self
            if _coconut_match_set_name_examples is not _coconut_sentinel:
                examples = _coconut_match_set_name_examples
            if _coconut_match_set_name_params is not _coconut_sentinel:
                params = _coconut_match_set_name_params
            if _coconut_match_set_name_distribution is not _coconut_sentinel:
                distribution = _coconut_match_set_name_distribution
            if _coconut_match_set_name_remove_erroring_algs is not _coconut_sentinel:
                remove_erroring_algs = _coconut_match_set_name_remove_erroring_algs
            if _coconut_match_set_name__backend_store is not _coconut_sentinel:
                _backend_store = _coconut_match_set_name__backend_store
        if not _coconut_match_check_0:
            raise _coconut_FunctionMatchError('match def attempt_update(self, examples, params, distribution, remove_erroring_algs=False, *, _backend_store):', _coconut_match_args)

        self.use_distribution(distribution, force=remove_erroring_algs != self.remove_erroring_algs)

        self.examples = examples
        self.params = params
        self.remove_erroring_algs = remove_erroring_algs
        self.backend_store = _backend_store

        self.select_new_backend()
        return True
Exemplo n.º 10
0
def factorial(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) == 1) and (_coconut_match_to_args[0] == 0):
        if not _coconut_match_to_kwargs:
            _coconut_match_check = True
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def factorial(0) = 1'" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'def factorial(0) = 1'
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    return 1
Exemplo n.º 11
0
def forall_handle(*_coconut_match_args, **_coconut_match_kwargs):
    _coconut_match_check_2 = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_args) <= 2) and (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "const" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "expr" in _coconut_match_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("const")
        _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("expr")
        if not _coconut_match_kwargs:
            const = _coconut_match_temp_0
            expr = _coconut_match_temp_1
            _coconut_match_check_2 = True
    if not _coconut_match_check_2:
        raise _coconut_FunctionMatchError('match def forall_handle(const, expr) = ForAll(const, expr)', _coconut_match_args)

    return _coconut_tail_call(ForAll, const, expr)
Exemplo n.º 12
0
def quick_sort(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) == 1) and (_coconut.isinstance(_coconut_match_to_args[0], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_to_args[0]) == 0):
        if not _coconut_match_to_kwargs:
            _coconut_match_check = True
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def quick_sort([]) = []'" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'def quick_sort([]) = []'
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    return []
Exemplo n.º 13
0
def forall_handle(*_coconut_match_args, **_coconut_match_kwargs):
    _coconut_match_check_3 = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_args) <= 3) and (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "const" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 1, "prop" in _coconut_match_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_args) > 2, "expr" in _coconut_match_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("const")
        _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(_coconut_match_args) > 1 else _coconut_match_kwargs.pop("prop")
        _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(_coconut_match_args) > 2 else _coconut_match_kwargs.pop("expr")
        if not _coconut_match_kwargs:
            const = _coconut_match_temp_0
            prop = _coconut_match_temp_1
            expr = _coconut_match_temp_2
            _coconut_match_check_3 = True
    if not _coconut_match_check_3:
        raise _coconut_FunctionMatchError('match def forall_handle(const, prop, expr) =', _coconut_match_args)

    return _coconut_tail_call(ForAll, const, prop(const) >> expr)
Exemplo n.º 14
0
    def test_match_error_addpattern(*_coconut_match_to_args, **_coconut_match_to_kwargs):
        _coconut_match_check = False
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_to_args) <= 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 0, "x" in _coconut_match_to_kwargs)) == 1):
            _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("x")
            if (_coconut.isinstance(_coconut_match_temp_0, int)) and (not _coconut_match_to_kwargs):
                x = _coconut_match_temp_0
                _coconut_match_check = True
        if not _coconut_match_check:
            _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
            _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def test_match_error_addpattern(x is int): raise MatchError()'" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
            _coconut_match_err.pattern = 'def test_match_error_addpattern(x is int): raise MatchError()'
            _coconut_match_err.value = _coconut_match_to_args
            raise _coconut_match_err

        raise MatchError()
Exemplo n.º 15
0
def mean(*_coconut_match_args, **_coconut_match_kwargs):
    _coconut_match_check_0 = False
    _coconut_match_set_name_xs = _coconut_sentinel
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if _coconut.len(_coconut_match_args) == 1:
        if _coconut.isinstance(_coconut_match_args[0], _coconut.abc.Sequence):
            _coconut_match_temp_4 = _coconut.list(_coconut_match_args[0])
            _coconut_match_set_name_xs = _coconut_match_temp_4
            if not _coconut_match_kwargs:
                _coconut_match_check_0 = True
    if _coconut_match_check_0:
        if _coconut_match_set_name_xs is not _coconut_sentinel:
            xs = _coconut_match_set_name_xs
    if not _coconut_match_check_0:
        raise _coconut_FunctionMatchError('match def mean([] + xs) =',
                                          _coconut_match_args)

    return sum(xs) / len(xs)
Exemplo n.º 16
0
    def angle(*_coconut_match_to_args, **_coconut_match_to_kwargs):
        _coconut_match_check = False
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_to_args) <= 2) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 0, "self" in _coconut_match_to_kwargs)) == 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 1, "other" in _coconut_match_to_kwargs)) == 1):
            _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("self")
            _coconut_match_temp_1 = _coconut_match_to_args[1] if _coconut.len(_coconut_match_to_args) > 1 else _coconut_match_to_kwargs.pop("other")
            if (_coconut.isinstance(_coconut_match_temp_1, vector)) and (not _coconut_match_to_kwargs):
                self = _coconut_match_temp_0
                other = _coconut_match_temp_1
                _coconut_match_check = True
        if not _coconut_match_check:
            _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
            _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def angle(self, other is vector) = math.acos(self.unit() * other.unit())'" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
            _coconut_match_err.pattern = 'def angle(self, other is vector) = math.acos(self.unit() * other.unit())'
            _coconut_match_err.value = _coconut_match_to_args
            raise _coconut_match_err

        return _coconut_tail_call(math.acos, self.unit() * other.unit())
Exemplo n.º 17
0
def factorial(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    """Compute n! where n is an integer >= 0."""
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) <= 1) and (_coconut.sum((_coconut.len(_coconut_match_to_args) > 0, "n" in _coconut_match_to_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("n")
        if (_coconut.isinstance(_coconut_match_temp_0, int)) and (not _coconut_match_to_kwargs):
            n = _coconut_match_temp_0
            _coconut_match_check = True
    if _coconut_match_check and not (n > 0):
        _coconut_match_check = False
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def factorial(n is int if n > 0) ='" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'def factorial(n is int if n > 0) ='
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    return n * factorial(n - 1)
Exemplo n.º 18
0
    def __sub__(*_coconut_match_to_args, **_coconut_match_to_kwargs):
        """Subtract one vector from another."""
        _coconut_match_check = False
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_to_args) == 2) and ("self" not in _coconut_match_to_kwargs) and (_coconut.isinstance(_coconut_match_to_args[1], vector)):
            _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(_coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop("self")
            other_pts = _coconut_match_to_args[1][0:]
            if not _coconut_match_to_kwargs:
                self = _coconut_match_temp_0
                _coconut_match_check = True
        if _coconut_match_check and not (len(other_pts) == len(self.pts)):
            _coconut_match_check = False
        if not _coconut_match_check:
            _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
            _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def __sub__(self, vector(*other_pts)                 if len(other_pts) == len(self.pts)) ='" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
            _coconut_match_err.pattern = 'def __sub__(self, vector(*other_pts)                 if len(other_pts) == len(self.pts)) ='
            _coconut_match_err.value = _coconut_match_to_args
            raise _coconut_match_err

        return _coconut_tail_call((vector), *map(_coconut_minus, self.pts, other_pts))
Exemplo n.º 19
0
def quick_sort(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    """Sort the input sequence using the quick sort algorithm."""
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) == 1) and (_coconut.isinstance(_coconut_match_to_args[0], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_to_args[0]) >= 1):
        tail = _coconut.list(_coconut_match_to_args[0][1:])
        head = _coconut_match_to_args[0][0]
        if not _coconut_match_to_kwargs:
            _coconut_match_check = True
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError("pattern-matching failed for " "'def quick_sort([head] + tail) ='" " in " + (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr) <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'def quick_sort([head] + tail) ='
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    left = [x for x in tail if x < head]
    right = [x for x in tail if x >= head]

# Test cases:
    return quick_sort(left) + [head] + quick_sort(right)
Exemplo n.º 20
0
 def _coconut_lambda_2(*_coconut_match_args, **_coconut_match_kwargs):
     _coconut_match_check_2 = False
     _coconut_match_set_name_val = _coconut_sentinel
     _coconut_match_set_name_loss = _coconut_sentinel
     _coconut_match_set_name_N = _coconut_sentinel
     _coconut_FunctionMatchError = _coconut_get_function_match_error()
     if (_coconut.len(_coconut_match_args) == 2) and ("val" not in _coconut_match_kwargs) and (_coconut.isinstance(_coconut_match_args[1], _coconut.abc.Sequence)) and (_coconut.len(_coconut_match_args[1]) == 2):
         _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(_coconut_match_args) > 0 else _coconut_match_kwargs.pop("val")
         _coconut_match_set_name_loss = _coconut_match_args[1][0]
         _coconut_match_set_name_N = _coconut_match_args[1][1]
         if not _coconut_match_kwargs:
             _coconut_match_set_name_val = _coconut_match_temp_0
             _coconut_match_check_2 = True
     if _coconut_match_check_2:
         if _coconut_match_set_name_val is not _coconut_sentinel:
             val = _coconut_match_temp_0
         if _coconut_match_set_name_loss is not _coconut_sentinel:
             loss = _coconut_match_args[1][0]
         if _coconut_match_set_name_N is not _coconut_sentinel:
             N = _coconut_match_args[1][1]
     if not _coconut_match_check_2:
         raise _coconut_FunctionMatchError('ns = marginals |> starmap$(def (val, (loss, N)) -> N) |> np.asarray', _coconut_match_args)
     return N
Exemplo n.º 21
0
def exists_handle(*_coconut_match_to_args, **_coconut_match_to_kwargs):
    _coconut_match_check = False
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.len(_coconut_match_to_args) <= 3) and (_coconut.sum(
        (_coconut.len(_coconut_match_to_args) > 0, "const"
         in _coconut_match_to_kwargs)) == 1) and (_coconut.sum(
             (_coconut.len(_coconut_match_to_args) > 1, "prop"
              in _coconut_match_to_kwargs)) == 1) and (_coconut.sum(
                  (_coconut.len(_coconut_match_to_args) > 2, "expr"
                   in _coconut_match_to_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_to_args[0] if _coconut.len(
            _coconut_match_to_args) > 0 else _coconut_match_to_kwargs.pop(
                "const")
        _coconut_match_temp_1 = _coconut_match_to_args[1] if _coconut.len(
            _coconut_match_to_args) > 1 else _coconut_match_to_kwargs.pop(
                "prop")
        _coconut_match_temp_2 = _coconut_match_to_args[2] if _coconut.len(
            _coconut_match_to_args) > 2 else _coconut_match_to_kwargs.pop(
                "expr")
        if not _coconut_match_to_kwargs:
            const = _coconut_match_temp_0
            prop = _coconut_match_temp_1
            expr = _coconut_match_temp_2
            _coconut_match_check = True
    if not _coconut_match_check:
        _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
        _coconut_match_err = _coconut_FunctionMatchError(
            "pattern-matching failed for "
            "'match def exists_handle(const, prop, expr) ='"
            " in " +
            (_coconut_match_val_repr if _coconut.len(_coconut_match_val_repr)
             <= 500 else _coconut_match_val_repr[:500] + "..."))
        _coconut_match_err.pattern = 'match def exists_handle(const, prop, expr) ='
        _coconut_match_err.value = _coconut_match_to_args
        raise _coconut_match_err

    return _coconut_tail_call(Exists, const, prop(const) & expr)
Exemplo n.º 22
0
    async def async_map_4(*_coconut_match_to_args, **_coconut_match_to_kwargs):
        _coconut_match_check = False
        _coconut_FunctionMatchError = _coconut_get_function_match_error()
        if (_coconut.len(_coconut_match_to_args)
                == 1) and (_coconut.isinstance(
                    _coconut_match_to_args[0], _coconut.abc.Sequence)) and (
                        _coconut.len(_coconut_match_to_args[0]) >= 1):
            iters = _coconut.list(_coconut_match_to_args[0][1:])
            func = _coconut_match_to_args[0][0]
            if not _coconut_match_to_kwargs:
                _coconut_match_check = True
        if not _coconut_match_check:
            _coconut_match_val_repr = _coconut.repr(_coconut_match_to_args)
            _coconut_match_err = _coconut_FunctionMatchError(
                "pattern-matching failed for "
                "'match async def async_map_4([func] + iters) = parallel_map(func, *iters)'"
                " in " + (_coconut_match_val_repr
                          if _coconut.len(_coconut_match_val_repr) <= 500 else
                          _coconut_match_val_repr[:500] + "..."))
            _coconut_match_err.pattern = 'match async def async_map_4([func] + iters) = parallel_map(func, *iters)'
            _coconut_match_err.value = _coconut_match_to_args
            raise _coconut_match_err

        return parallel_map(func, *iters)
Exemplo n.º 23
0
Arquivo: util.py Projeto: evhub/bbopt
def _init_backend(*_coconut_match_args, **_coconut_match_kwargs):
    """Create a backend object with the given data (backend can be backend name or class)."""
    _coconut_match_check_0 = False
    _coconut_match_set_name_backend_cls = _coconut_sentinel
    _coconut_match_set_name_examples = _coconut_sentinel
    _coconut_match_set_name_params = _coconut_sentinel
    _coconut_match_set_name_args = _coconut_sentinel
    _coconut_match_set_name__attempt_to_update_backend = _coconut_sentinel
    _coconut_match_set_name__on_new_backend = _coconut_sentinel
    _coconut_match_set_name_options = _coconut_sentinel
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.sum((_coconut.len(_coconut_match_args) > 0, "backend_cls"
                      in _coconut_match_kwargs)) == 1) and (_coconut.sum(
                          (_coconut.len(_coconut_match_args) > 1, "examples"
                           in _coconut_match_kwargs)) == 1) and (_coconut.sum(
                               (_coconut.len(_coconut_match_args) > 2, "params"
                                in _coconut_match_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(
            _coconut_match_args) > 0 else _coconut_match_kwargs.pop(
                "backend_cls")
        _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(
            _coconut_match_args) > 1 else _coconut_match_kwargs.pop("examples")
        _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(
            _coconut_match_args) > 2 else _coconut_match_kwargs.pop("params")
        _coconut_match_set_name_args = _coconut_match_args[3:]
        _coconut_match_temp_3 = _coconut_match_kwargs.pop(
            "_attempt_to_update_backend"
        ) if "_attempt_to_update_backend" in _coconut_match_kwargs else None
        _coconut_match_temp_4 = _coconut_match_kwargs.pop(
            "_on_new_backend"
        ) if "_on_new_backend" in _coconut_match_kwargs else None
        _coconut_match_set_name_backend_cls = _coconut_match_temp_0
        _coconut_match_set_name_examples = _coconut_match_temp_1
        _coconut_match_set_name_params = _coconut_match_temp_2
        _coconut_match_set_name__attempt_to_update_backend = _coconut_match_temp_3
        _coconut_match_set_name__on_new_backend = _coconut_match_temp_4
        _coconut_match_set_name_options = _coconut_match_kwargs
        _coconut_match_check_0 = True
    if _coconut_match_check_0:
        if _coconut_match_set_name_backend_cls is not _coconut_sentinel:
            backend_cls = _coconut_match_temp_0
        if _coconut_match_set_name_examples is not _coconut_sentinel:
            examples = _coconut_match_temp_1
        if _coconut_match_set_name_params is not _coconut_sentinel:
            params = _coconut_match_temp_2
        if _coconut_match_set_name_args is not _coconut_sentinel:
            args = _coconut_match_args[3:]
        if _coconut_match_set_name__attempt_to_update_backend is not _coconut_sentinel:
            _attempt_to_update_backend = _coconut_match_temp_3
        if _coconut_match_set_name__on_new_backend is not _coconut_sentinel:
            _on_new_backend = _coconut_match_temp_4
        if _coconut_match_set_name_options is not _coconut_sentinel:
            options = _coconut_match_kwargs
    if not _coconut_match_check_0:
        raise _coconut_FunctionMatchError(
            'match def _init_backend(backend_cls, examples, params, *args, _attempt_to_update_backend=None, _on_new_backend=None, **options):',
            _coconut_match_args)

    backend_examples = examples[:]
    backend_params = params.copy()

    new_backend = None
    if isinstance(_attempt_to_update_backend, backend_cls):
        updated_backend = _attempt_to_update_backend.attempt_update(
            backend_examples, backend_params, *args, **options)
        if updated_backend is True:
            new_backend = _attempt_to_update_backend
        elif isinstance(updated_backend, backend_cls):
            new_backend = updated_backend
        else:
            assert updated_backend is False, "invalid {_coconut_format_0}.attempt_update return value: {_coconut_format_1}".format(
                _coconut_format_0=(backend_cls),
                _coconut_format_1=(updated_backend))

    if new_backend is None:
        assert not _attempt_to_update_backend or isinstance(
            _attempt_to_update_backend, Backend
        ), "invalid backend to attempt update on: {_coconut_format_0}".format(
            _coconut_format_0=(_attempt_to_update_backend))
        new_backend = backend_cls(backend_examples, backend_params, *args,
                                  **options)
        if _on_new_backend is not None:
            _on_new_backend(new_backend)

    return new_backend
Exemplo n.º 24
0
Arquivo: util.py Projeto: evhub/bbopt
def get_backend(*_coconut_match_args, **_coconut_match_kwargs):
    """Create a backend object, attempting to update a backend from backend_store."""
    _coconut_match_check_1 = False
    _coconut_match_set_name_backend_store = _coconut_sentinel
    _coconut_match_set_name_backend = _coconut_sentinel
    _coconut_match_set_name_examples = _coconut_sentinel
    _coconut_match_set_name_params = _coconut_sentinel
    _coconut_match_set_name_args = _coconut_sentinel
    _coconut_match_set_name__current_backend = _coconut_sentinel
    _coconut_match_set_name__on_new_backend = _coconut_sentinel
    _coconut_match_set_name_options = _coconut_sentinel
    _coconut_FunctionMatchError = _coconut_get_function_match_error()
    if (_coconut.sum(
        (_coconut.len(_coconut_match_args) > 0, "backend_store"
         in _coconut_match_kwargs)) == 1) and (_coconut.sum(
             (_coconut.len(_coconut_match_args) > 1, "backend"
              in _coconut_match_kwargs)) == 1) and (_coconut.sum(
                  (_coconut.len(_coconut_match_args) > 2, "examples"
                   in _coconut_match_kwargs)) == 1) and (_coconut.sum(
                       (_coconut.len(_coconut_match_args) > 3, "params"
                        in _coconut_match_kwargs)) == 1):
        _coconut_match_temp_0 = _coconut_match_args[0] if _coconut.len(
            _coconut_match_args) > 0 else _coconut_match_kwargs.pop(
                "backend_store")
        _coconut_match_temp_1 = _coconut_match_args[1] if _coconut.len(
            _coconut_match_args) > 1 else _coconut_match_kwargs.pop("backend")
        _coconut_match_temp_2 = _coconut_match_args[2] if _coconut.len(
            _coconut_match_args) > 2 else _coconut_match_kwargs.pop("examples")
        _coconut_match_temp_3 = _coconut_match_args[3] if _coconut.len(
            _coconut_match_args) > 3 else _coconut_match_kwargs.pop("params")
        _coconut_match_set_name_args = _coconut_match_args[4:]
        _coconut_match_temp_4 = _coconut_match_kwargs.pop(
            "_current_backend"
        ) if "_current_backend" in _coconut_match_kwargs else None
        _coconut_match_temp_5 = _coconut_match_kwargs.pop(
            "_on_new_backend"
        ) if "_on_new_backend" in _coconut_match_kwargs else None
        _coconut_match_set_name_backend_store = _coconut_match_temp_0
        _coconut_match_set_name_backend = _coconut_match_temp_1
        _coconut_match_set_name_examples = _coconut_match_temp_2
        _coconut_match_set_name_params = _coconut_match_temp_3
        _coconut_match_set_name__current_backend = _coconut_match_temp_4
        _coconut_match_set_name__on_new_backend = _coconut_match_temp_5
        _coconut_match_set_name_options = _coconut_match_kwargs
        _coconut_match_check_1 = True
    if _coconut_match_check_1:
        if _coconut_match_set_name_backend_store is not _coconut_sentinel:
            backend_store = _coconut_match_temp_0
        if _coconut_match_set_name_backend is not _coconut_sentinel:
            backend = _coconut_match_temp_1
        if _coconut_match_set_name_examples is not _coconut_sentinel:
            examples = _coconut_match_temp_2
        if _coconut_match_set_name_params is not _coconut_sentinel:
            params = _coconut_match_temp_3
        if _coconut_match_set_name_args is not _coconut_sentinel:
            args = _coconut_match_args[4:]
        if _coconut_match_set_name__current_backend is not _coconut_sentinel:
            _current_backend = _coconut_match_temp_4
        if _coconut_match_set_name__on_new_backend is not _coconut_sentinel:
            _on_new_backend = _coconut_match_temp_5
        if _coconut_match_set_name_options is not _coconut_sentinel:
            options = _coconut_match_kwargs
    if not _coconut_match_check_1:
        raise _coconut_FunctionMatchError(
            'match def get_backend(backend_store, backend, examples, params, *args, _current_backend=None, _on_new_backend=None, **options):',
            _coconut_match_args)

    if isinstance(backend, type) and issubclass(backend, Backend):
        backend_cls = backend
    else:
        backend_cls = backend_registry[backend]
        assert issubclass(
            backend_cls, Backend
        ), "invalid backend class for {_coconut_format_0}: {_coconut_format_1}".format(
            _coconut_format_0=(backend), _coconut_format_1=(backend_cls))

    store_ind = None
    attempt_to_update_backend = _current_backend
    for i, (stored_args, stored_options,
            stored_backend) in enumerate(backend_store[backend_cls]):
        attempt_to_update_backend = stored_backend
        if stored_args == args and stored_options == options:
            store_ind = i
            break

    if backend_cls.request_backend_store:
        init_options = options.copy()
        init_options["_backend_store"] = _make_safe_backend_store(
            backend_store, (attempt_to_update_backend, ))
    else:
        init_options = options

    new_backend = _init_backend(
        backend_cls,
        examples,
        params,
        *args,
        _attempt_to_update_backend=attempt_to_update_backend,
        _on_new_backend=_on_new_backend,
        **init_options)

    if store_ind is None:
        backend_store[backend_cls].append((args, options, new_backend))
    else:
        backend_store[backend_cls][store_ind] = (args, options, new_backend)

    return new_backend