def feasible(self, x): """Feasibility of the constraint. From the interface "Constraint". Parameters ---------- x : Numpy array. The variable to check for feasibility. Examples -------- >>> import numpy as np >>> from parsimony.functions.penalties import LInf >>> >>> np.random.seed(42) >>> x = np.random.rand(10, 1) * 2.0 - 1.0 >>> linf = LInf(c=0.618) >>> linf.feasible(x) False >>> linf.feasible(linf.proj(x)) True """ if self.penalty_start > 0: x_ = x[self.penalty_start:, :] else: x_ = x return maths.normInf(x_) <= self.c
def f(self, x): """Function value. From the interface "Function". Parameters ---------- x : Numpy array. The point at which to evaluate the function. Example ------- >>> import numpy as np >>> from parsimony.functions.penalties import LInf >>> import parsimony.utils.maths as maths >>> >>> np.random.seed(42) >>> x = np.random.rand(10, 1) >>> linf = LInf(l=1.1) >>> linf.f(x) - 1.1 * maths.normInf(x) 0.0 """ if self.penalty_start > 0: x_ = x[self.penalty_start:, :] else: x_ = x return self.l * (maths.normInf(x_) - self.c)
def proj(self, x): """The corresponding projection operator. From the interface "ProjectionOperator". Examples -------- >>> import numpy as np >>> from parsimony.functions.penalties import LInf >>> >>> np.random.seed(42) >>> x = np.random.rand(10, 1) * 2.0 - 1.0 >>> linf = LInf(c=0.618) >>> linf.proj(x) array([[-0.25091976], [ 0.618 ], [ 0.46398788], [ 0.19731697], [-0.618 ], [-0.618 ], [-0.618 ], [ 0.618 ], [ 0.20223002], [ 0.41614516]]) """ if self.penalty_start > 0: x_ = x[self.penalty_start:, :] else: x_ = x if maths.normInf(x_) <= self.c: return x y = np.copy(x_) y[y > self.c] = self.c y[y < -self.c] = -self.c # Put the unregularised variables back. if self.penalty_start > 0: y = np.vstack((x[:self.penalty_start, :], y)) return y