<commit_before>import numpy as np
from scipy import sparse
from affine import affine_transform
import warnings
from composite import smooth as smooth_composite

# class smooth_function(smooth_composite):
#     """
#     A container class for smooth_atom classes
#     """

#     # TODO? use a list for atoms instead of *atoms?
#     def __init__(self, *atoms, **keywords):
#         # why do we have this -- XXX
#         if not set(keywords.keys()).issubset(['coef', 'constant_term']):
#             warnings.warn('only keyword argument should be multiplier, "coef" and "constant", got %s' % `keywords`)

#         self.coef = 1
#         if keywords.has_key('coef'):
#             self.coef = keywords['coef']

#         self.constant_term = 0
#         if keywords.has_key('constant_term'):
#             self.constant_term = keywords['constant_term']

#         self.atoms = []
#         self.primal_shape = -1
#         for atom in atoms:
#             if self.primal_shape == -1:
#                 self.primal_shape = atom.primal_shape
#             else:
#                 if atom.primal_shape != self.primal_shape:
#                     raise ValueError("primal dimensions don't agree")
#             self.atoms.append(atom)
#         self.coefs = np.zeros(self.primal_shape)

#     def smooth_objective(self, x, mode='both', check_feasibility=False):
#         """
#         Evaluate a smooth function and/or its gradient

#         if mode == 'both', return both function value and gradient
#         if mode == 'grad', return only the gradient
#         if mode == 'func', return only the function value
#         """

#         if mode == 'func':
#             if len(self.atoms) > 1:
#                 v = 0.
#                 for atom in self.atoms:
#                     v += atom.smooth_objective(x, mode=mode)
#                 return self.scale(v) + self.constant_term
#             else:
#                 return self.scale(self.atoms[0].smooth_objective(x, mode=mode)) + self.constant_term
#         elif mode == 'grad':
#             if len(self.atoms) > 1:
#                 g = np.zeros(self.primal_shape)
#                 for atom in self.atoms:
#                     g += atom.smooth_objective(x, mode=mode)
#                 return self.scale(g)
#             else:
#                 return self.scale(self.atoms[0].smooth_objective(x, mode=mode))
#         elif mode == 'both':
#             if len(self.atoms) > 1:
#                 v = 0.
#                 g = np.zeros(self.primal_shape)
#                 for atom in self.atoms:
#                     output = atom.smooth_objective(x, mode=mode)
#                     v += output[0]
#                     g += output[1]
#                 return self.scale(v) + self.constant_term, self.scale(g)
#             else:
#                 v, g = self.atoms[0].smooth_objective(x, mode=mode)
#                 return self.scale(v) + self.constant_term, self.scale(g)
#         else:
#             raise ValueError("Mode specified incorrectly")


class smooth_atom(smooth_composite):

    """
    A class for representing a smooth function and its gradient
    """

    def __init__(self, primal_shape, coef=1, constant_term=0):
        self.constant_term = constant_term
        self.primal_shape = primal_shape
        self.coef = coef
        self.coefs = np.zeros(self.primal_shape)
        raise NotImplementedError

    def smooth_objective(self, x, mode='both', check_feasibility=False):
        raise NotImplementedError
    
    @classmethod
    def affine(cls, linear_operator, offset, coef=1, diag=False,
               constant_term=0):
        """
        Args and keywords passed to cls constructor along with
        l and primal_shape
        """
        atransform = affine_transform(linear_operator, offset, diag=diag)
        atom = cls(atransform.primal_shape, coef=coef, constant_term=constant_term)
        
        return affine_smooth(atom, atransform)

    @classmethod
    def linear(cls, linear_operator, coef=1, diag=False,
               constant_term=0):
        """
        Args and keywords passed to cls constructor along with
        l and primal_shape
        """
        atransform = affine_transform(linear_operator, None, diag=diag)
        atom = cls(atransform.primal_shape, coef=coef, constant_term=constant_term)
        
        return affine_smooth(atom, atransform)
<commit_msg>check to ensure coefs are non-negative<commit_after>import numpy as np
from scipy import sparse
from affine import affine_transform
import warnings
from composite import smooth as smooth_composite

# class smooth_function(smooth_composite):
#     """
#     A container class for smooth_atom classes
#     """

#     # TODO? use a list for atoms instead of *atoms?
#     def __init__(self, *atoms, **keywords):
#         # why do we have this -- XXX
#         if not set(keywords.keys()).issubset(['coef', 'constant_term']):
#             warnings.warn('only keyword argument should be multiplier, "coef" and "constant", got %s' % `keywords`)

#         self.coef = 1
#         if keywords.has_key('coef'):
#             self.coef = keywords['coef']

#         self.constant_term = 0
#         if keywords.has_key('constant_term'):
#             self.constant_term = keywords['constant_term']

#         self.atoms = []
#         self.primal_shape = -1
#         for atom in atoms:
#             if self.primal_shape == -1:
#                 self.primal_shape = atom.primal_shape
#             else:
#                 if atom.primal_shape != self.primal_shape:
#                     raise ValueError("primal dimensions don't agree")
#             self.atoms.append(atom)
#         self.coefs = np.zeros(self.primal_shape)

#     def smooth_objective(self, x, mode='both', check_feasibility=False):
#         """
#         Evaluate a smooth function and/or its gradient

#         if mode == 'both', return both function value and gradient
#         if mode == 'grad', return only the gradient
#         if mode == 'func', return only the function value
#         """

#         if mode == 'func':
#             if len(self.atoms) > 1:
#                 v = 0.
#                 for atom in self.atoms:
#                     v += atom.smooth_objective(x, mode=mode)
#                 return self.scale(v) + self.constant_term
#             else:
#                 return self.scale(self.atoms[0].smooth_objective(x, mode=mode)) + self.constant_term
#         elif mode == 'grad':
#             if len(self.atoms) > 1:
#                 g = np.zeros(self.primal_shape)
#                 for atom in self.atoms:
#                     g += atom.smooth_objective(x, mode=mode)
#                 return self.scale(g)
#             else:
#                 return self.scale(self.atoms[0].smooth_objective(x, mode=mode))
#         elif mode == 'both':
#             if len(self.atoms) > 1:
#                 v = 0.
#                 g = np.zeros(self.primal_shape)
#                 for atom in self.atoms:
#                     output = atom.smooth_objective(x, mode=mode)
#                     v += output[0]
#                     g += output[1]
#                 return self.scale(v) + self.constant_term, self.scale(g)
#             else:
#                 v, g = self.atoms[0].smooth_objective(x, mode=mode)
#                 return self.scale(v) + self.constant_term, self.scale(g)
#         else:
#             raise ValueError("Mode specified incorrectly")


class smooth_atom(smooth_composite):

    """
    A class for representing a smooth function and its gradient
    """

    def __init__(self, primal_shape, coef=1, constant_term=0):
        self.constant_term = constant_term
        self.primal_shape = primal_shape
        self.coef = coef
        if coef < 0:
            raise ValueError('coefs must be nonnegative to ensure convexity (assuming all atoms are indeed convex)')
        self.coefs = np.zeros(self.primal_shape)
        raise NotImplementedError

    def smooth_objective(self, x, mode='both', check_feasibility=False):
        raise NotImplementedError
    
    @classmethod
    def affine(cls, linear_operator, offset, coef=1, diag=False,
               constant_term=0):
        """
        Args and keywords passed to cls constructor along with
        l and primal_shape
        """
        atransform = affine_transform(linear_operator, offset, diag=diag)
        atom = cls(atransform.primal_shape, coef=coef, constant_term=constant_term)
        
        return affine_smooth(atom, atransform)

    @classmethod
    def linear(cls, linear_operator, coef=1, diag=False,
               constant_term=0):
        """
        Args and keywords passed to cls constructor along with
        l and primal_shape
        """
        atransform = affine_transform(linear_operator, None, diag=diag)
        atom = cls(atransform.primal_shape, coef=coef, constant_term=constant_term)
        
        return affine_smooth(atom, atransform)

{'fraction_non_alphanumeric': 0.10617394216813665, 'fraction_numerical': 0.00457742547728034, 'mean_word_length': 1.9869956652217406, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3670615', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2578, 'n_words': 648}