<commit_before>from cvxpy.atoms.elementwise.abs import abs
from cvxpy.atoms.elementwise.power import power
from cvxpy.expressions.variables.variable import Variable
from cvxpy.reductions.dcp2cone.atom_canonicalizers.abs_canon import abs_canon
from cvxpy.reductions.dcp2cone.atom_canonicalizers.power_canon import power_canon
from cvxpy.utilities.power_tools import gm_constrs
import numpy as np


def huber_canon(expr, args):
    M = expr.M
    x = args[0]
    shape = expr.shape
    n = Variable(*shape)
    s = Variable(*shape)

    # n**2 + 2*M*|s|
    power_expr = power(n, 2)
    n2, constr_sq = power_canon(power_expr, power_expr.args)
    abs_expr = abs(s)
    abs_s, constr_abs = abs_canon(abs_expr, abs_expr.args)
    obj = n2 + 2 * M * abs_s

    # x == s + n
    constraints = constr_sq + constr_abs
    constraints.append(x == s + n)
    return obj, constraints<commit_msg>TODO about recursion in atom_canonicalizers<commit_after>from cvxpy.atoms.elementwise.abs import abs
from cvxpy.atoms.elementwise.power import power
from cvxpy.expressions.variables.variable import Variable
from cvxpy.reductions.dcp2cone.atom_canonicalizers.abs_canon import abs_canon
from cvxpy.reductions.dcp2cone.atom_canonicalizers.power_canon import power_canon
from cvxpy.utilities.power_tools import gm_constrs
import numpy as np


def huber_canon(expr, args):
    M = expr.M
    x = args[0]
    shape = expr.shape
    n = Variable(*shape)
    s = Variable(*shape)

    # n**2 + 2*M*|s|
    # TODO(akshayka): Make use of recursion inherent to canonicalization
    # process and just return a power / abs expressions for readability sake
    power_expr = power(n, 2)
    n2, constr_sq = power_canon(power_expr, power_expr.args)
    abs_expr = abs(s)
    abs_s, constr_abs = abs_canon(abs_expr, abs_expr.args)
    obj = n2 + 2 * M * abs_s

    # x == s + n
    constraints = constr_sq + constr_abs
    constraints.append(x == s + n)
    return obj, constraints
{'fraction_non_alphanumeric': 0.09025641025641026, 'fraction_numerical': 0.009230769230769232, 'mean_word_length': 3.8775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '372299', 'n_tokens_mistral': 709, 'n_tokens_neox': 694, 'n_words': 168}