Source code for tick.prox.prox_positive
# License: BSD 3 clause
# -*- coding: utf8 -*-
import numpy as np
from .base import Prox
from .build.prox import ProxPositiveDouble as _ProxPositiveDouble
from .build.prox import ProxPositiveFloat as _ProxPositiveFloat
__author__ = 'Stephane Gaiffas'
dtype_map = {
np.dtype("float64"): _ProxPositiveDouble,
np.dtype("float32"): _ProxPositiveFloat
}
[docs]class ProxPositive(Prox):
"""Projection operator onto the half-space of vectors with
non-negative entries
Parameters
----------
range : `tuple` of two `int`, default=`None`
Range on which the prox is applied. If `None` then the prox is
applied on the whole vector
Attributes
----------
dtype : `{'float64', 'float32'}`
Type of the arrays used.
"""
[docs] def __init__(self, range: tuple = None, positive: bool = False):
Prox.__init__(self, range)
self._prox = self._build_cpp_prox("float64")
def _call(self, coeffs: np.ndarray, step: object, out: np.ndarray):
self._prox.call(coeffs, step, out)
[docs] def value(self, coeffs: np.ndarray):
"""
Returns the projected ``coeffs``
Parameters
----------
coeffs : `numpy.ndarray`, shape=(n_coeffs,)
Vector to be projected
Returns
-------
output : `float`
Returns 0 (as this is a projection)
"""
return self._prox.value(coeffs)
def _build_cpp_prox(self, dtype_or_object_with_dtype):
self.dtype = self._extract_dtype(dtype_or_object_with_dtype)
prox_class = self._get_typed_class(dtype_or_object_with_dtype,
dtype_map)
if self.range is None:
return prox_class(0.)
else:
return prox_class(0., self.range[0], self.range[1])