Esempio n. 1
0
    def setup_class(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = Parameter()
Esempio n. 2
0
def solver(A, y, huber_param, w_bounds, y_bounds, w_radi, y_sample_weights, theta, gamma = 0.01):
    '''
    Presently, we perform Huber Regression on our data matrix, with our own
    specialized way of regularizing the weights, the results of the model fitting.
    
    If we currently do not have a huber parameter to use, we will let the method
    to optimize for one
    '''
     
    w_radi_sqrt = np.sqrt( w_radi )

    #bounds on weights
    wl = Constant( w_bounds[:, 0] )
    wu = Constant( w_bounds[:, 1] )
    
    #bounds on targets
    yl = Constant( y_bounds[:, 0] )
    yu = Constant( y_bounds[:, 1] )
    
    n = A.shape[0]
    m = A.shape[1]
    
    #weight variable
    w_var = Variable(m)
    
    y_var = A.dot(w_var)
    
    deviance = y_sample_weights.T * huber(y_var - y, M = huber_param)
    
    divergence = square( norm( multiply( w_radi_sqrt, ( w_var - 1.0 ) )  ) )
    
    constraints = [wl <= w_var, w_var <= wu, y_var <= yu, yl <= y_var, divergence <= ( theta ** 2.0 )]

    cost = (1.0 - gamma)*deviance + gamma*divergence 
    
    prob = Problem(Minimize(cost), constraints)

    prob.solve(solver = 'ECOS', abstol = 1e-4, reltol = 1e-3, feastol = 1e-4, max_iters = 1000, verbose = False )
        
    w_sol = np.array( w_var.value )

    #this still needs to be done since solver only satisfies constraints 
    #within a tolerance of 1e-4
    w_sol = np.clip(w_sol, wl.value, wu.value).ravel()
    
    w_sol = np.squeeze(np.asarray(w_sol))

    return w_sol, prob.status
Esempio n. 3
0
def cvxpy_solve_qp(P,
                   q,
                   G=None,
                   h=None,
                   A=None,
                   b=None,
                   initvals=None,
                   solver=None):
    """
    Solve a Quadratic Program defined as:

        minimize
            (1/2) * x.T * P * x + q.T * x

        subject to
            G * x <= h
            A * x == b

    calling a given solver using the CVXPY <http://www.cvxpy.org/> modelling
    language.

    Parameters
    ----------
    P : array, shape=(n, n)
        Primal quadratic cost matrix.
    q : array, shape=(n,)
        Primal quadratic cost vector.
    G : array, shape=(m, n)
        Linear inequality constraint matrix.
    h : array, shape=(m,)
        Linear inequality constraint vector.
    A : array, shape=(meq, n), optional
        Linear equality constraint matrix.
    b : array, shape=(meq,), optional
        Linear equality constraint vector.
    initvals : array, shape=(n,), optional
        Warm-start guess vector (not used).
    solver : string, optional
        Solver name in ``cvxpy.installed_solvers()``.

    Returns
    -------
    x : array, shape=(n,)
        Solution to the QP, if found, otherwise ``None``.
    """
    if initvals is not None:
        print("CVXPY: note that warm-start values are ignored by wrapper")
    n = q.shape[0]
    x = Variable(n)
    P = Constant(P)  # see http://www.cvxpy.org/en/latest/faq/
    objective = Minimize(0.5 * quad_form(x, P) + q * x)
    constraints = []
    if G is not None:
        constraints.append(G * x <= h)
    if A is not None:
        constraints.append(A * x == b)
    prob = Problem(objective, constraints)
    prob.solve(solver=solver)
    x_opt = array(x.value).reshape((n, ))
    return x_opt
Esempio n. 4
0
def cvxpy_solve_qp(P, q, G=None, h=None, A=None, b=None, initvals=None,
                   solver=None, verbose=False):
    """
    Solve a Quadratic Program defined as:

    .. math::

        \\begin{split}\\begin{array}{ll}
        \\mbox{minimize} &
            \\frac{1}{2} x^T P x + q^T x \\\\
        \\mbox{subject to}
            & G x \\leq h                \\\\
            & A x = h
        \\end{array}\\end{split}

    calling a given solver using the `CVXPY <http://www.cvxpy.org/>`_ modelling
    language.

    Parameters
    ----------
    P : array, shape=(n, n)
        Primal quadratic cost matrix.
    q : array, shape=(n,)
        Primal quadratic cost vector.
    G : array, shape=(m, n)
        Linear inequality constraint matrix.
    h : array, shape=(m,)
        Linear inequality constraint vector.
    A : array, shape=(meq, n), optional
        Linear equality constraint matrix.
    b : array, shape=(meq,), optional
        Linear equality constraint vector.
    initvals : array, shape=(n,), optional
        Warm-start guess vector (not used).
    solver : string, optional
        Solver name in ``cvxpy.installed_solvers()``.
    verbose : bool, optional
        Set to `True` to print out extra information.

    Returns
    -------
    x : array, shape=(n,)
        Solution to the QP, if found, otherwise ``None``.
    """
    if initvals is not None:
        print("CVXPY: note that warm-start values are ignored by wrapper")
    n = q.shape[0]
    x = Variable(n)
    P = Constant(P)  # see http://www.cvxpy.org/en/latest/faq/
    objective = Minimize(0.5 * quad_form(x, P) + q * x)
    constraints = []
    if G is not None:
        constraints.append(G * x <= h)
    if A is not None:
        constraints.append(A * x == b)
    prob = Problem(objective, constraints)
    prob.solve(solver=solver, verbose=verbose)
    x_opt = array(x.value).reshape((n,))
    return x_opt
Esempio n. 5
0
def targets_and_priorities(
        objectives: List[Union[Minimize, Maximize]],
        priorities,
        targets,
        limits=None,
        off_target: float = 1e-5) -> Union[Minimize, Maximize]:
    """Combines objectives with penalties within a range between target and limit.

    Each Minimize objective i has value

        priorities[i]*objectives[i] when objectives[i] >= targets[i]

        +infinity when objectives[i] > limits[i]

    Each Maximize objective i has value

        priorities[i]*objectives[i] when objectives[i] <= targets[i]

        +infinity when objectives[i] < limits[i]

    Args:
      objectives: A list of Minimize/Maximize objectives.
      priorities: The weight within the trange.
      targets: The start (end) of penalty for Minimize (Maximize)
      limits: The hard end (start) of penalty for Minimize (Maximize)
      off_target: Penalty outside of target.

    Returns:
      A Minimize/Maximize objective.
    """
    num_objs = len(objectives)
    new_objs: List[Union[Minimize, Maximize]] = []
    for i in range(num_objs):
        obj = objectives[i]
        sign = 1 if Constant.cast_to_const(priorities[i]).is_nonneg() else -1
        off_target *= sign
        if type(obj) == Minimize:
            expr = (priorities[i] - off_target) * atoms.pos(obj.args[0] -
                                                            targets[i])
            expr += off_target * obj.args[0]
            if limits is not None:
                expr += sign * indicator([obj.args[0] <= limits[i]])
            new_objs.append(expr)
        else:  # Maximize
            expr = (priorities[i] - off_target) * atoms.min_elemwise(
                obj.args[0], targets[i])
            expr += off_target * obj.args[0]
            if limits is not None:
                expr += sign * indicator([obj.args[0] >= limits[i]])
            new_objs.append(expr)
    obj_expr = sum(new_objs)
    if obj_expr.is_convex():
        return Minimize(obj_expr)
    else:
        return Maximize(obj_expr)
Esempio n. 6
0
    def setup_class(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = Parameter()
Esempio n. 7
0
class TestSign(BaseTest):
    """ Unit tests for the expression/sign class. """
    @classmethod
    def setup_class(self):
        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown = Variable()

    def test_add(self):
        self.assertEqual((self.pos + self.neg).sign, self.unknown.sign)
        self.assertEqual((self.neg + self.zero).sign, self.neg.sign)
        self.assertEqual((self.pos + self.pos).sign, self.pos.sign)
        self.assertEqual((self.unknown + self.zero).sign, self.unknown.sign)

    def test_sub(self):
        self.assertEqual((self.pos - self.neg).sign, self.pos.sign)
        self.assertEqual((self.neg - self.zero).sign, self.neg.sign)
        self.assertEqual((self.pos - self.pos).sign, self.unknown.sign)

    def test_mult(self):
        self.assertEqual((self.zero * self.pos).sign, self.zero.sign)
        self.assertEqual((self.unknown * self.pos).sign, self.unknown.sign)
        self.assertEqual((self.pos * self.neg).sign, self.neg.sign)
        self.assertEqual((self.pos * self.pos).sign, self.pos.sign)
        self.assertEqual((self.pos * self.pos).sign, self.pos.sign)
        self.assertEqual((self.neg * self.neg).sign, self.pos.sign)
        self.assertEqual((self.zero * self.unknown).sign, self.zero.sign)

    def test_neg(self):
        self.assertEqual((-self.zero).sign, self.zero.sign)
        self.assertEqual((-self.pos).sign, self.neg.sign)

    # Tests the is_positive and is_negative methods.
    def test_is_sign(self):
        assert self.pos.is_positive()
        assert not self.neg.is_positive()
        assert not self.unknown.is_positive()
        assert self.zero.is_positive()

        assert not self.pos.is_negative()
        assert self.neg.is_negative()
        assert not self.unknown.is_negative()
        assert self.zero.is_negative()

        assert self.zero.is_zero()
        assert not self.neg.is_zero()

        assert not (self.unknown.is_positive() or self.unknown.is_negative())
Esempio n. 8
0
def targets_and_priorities(objectives, priorities, targets, limits=None, off_target=1e-5):
    """Combines objectives with penalties within a range between target and limit.

    Args:
      objectives: A list of Minimize/Maximize objectives.
      priorities: The weight within the trange.
      targets: The start (end) of penalty for Minimize (Maximize)
      limits: The hard end (start) of penalty for Minimize (Maximize)
      off_target: Penalty outside of target.

    Returns:
      A Minimize/Maximize objective.
    """
    num_objs = len(objectives)
    new_objs = []
    for i in range(num_objs):
        obj = objectives[i]
        sign = 1 if Constant(priorities[i]).is_positive() else -1
        off_target *= sign
        if type(obj) == Minimize:
            expr = (priorities[i] - off_target)*atoms.pos(obj.args[0] - targets[i])
            expr += off_target*obj.args[0]
            if limits is not None:
                expr += sign*indicator([obj.args[0] <= limits[i]])
            new_objs.append(expr)
        else:  # Maximize
            expr = (priorities[i] - off_target)*atoms.min_elemwise(obj.args[0], targets[i])
            expr += off_target*obj.args[0]
            if limits is not None:
                expr += sign*indicator([obj.args[0] >= limits[i]])
            new_objs.append(expr)
    obj_expr = sum(new_objs)
    if obj_expr.is_convex():
        return Minimize(obj_expr)
    else:
        return Maximize(obj_expr)
Esempio n. 9
0
class TestCurvature(object):
    """ Unit tests for the expression/curvature class. """
    @classmethod
    def setup_class(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = Parameter()

    def test_add(self):
        assert_equals((self.const + self.cvx).curvature, self.cvx.curvature)
        assert_equals((self.unknown_curv + self.ccv).curvature,
                      self.unknown_curv.curvature)
        assert_equals((self.cvx + self.ccv).curvature,
                      self.unknown_curv.curvature)
        assert_equals((self.cvx + self.cvx).curvature, self.cvx.curvature)
        assert_equals((self.aff + self.ccv).curvature, self.ccv.curvature)

    def test_sub(self):
        assert_equals((self.const - self.cvx).curvature, self.ccv.curvature)
        assert_equals((self.unknown_curv - self.ccv).curvature,
                      self.unknown_curv.curvature)
        assert_equals((self.cvx - self.ccv).curvature, self.cvx.curvature)
        assert_equals((self.cvx - self.cvx).curvature,
                      self.unknown_curv.curvature)
        assert_equals((self.aff - self.ccv).curvature, self.cvx.curvature)

    def test_sign_mult(self):
        assert_equals((self.zero * self.cvx).curvature, self.const.curvature)
        assert_equals((self.neg * self.cvx).curvature, self.ccv.curvature)
        assert_equals((self.neg * self.ccv).curvature, self.cvx.curvature)
        assert_equals((self.neg * self.unknown_curv).curvature,
                      self.unknown_curv.curvature)
        assert_equals((self.pos * self.aff).curvature, self.aff.curvature)
        assert_equals((self.pos * self.ccv).curvature, self.ccv.curvature)
        assert_equals((self.unknown_sign * self.const).curvature,
                      self.const.curvature)
        assert_equals((self.unknown_sign * self.ccv).curvature,
                      self.unknown_curv.curvature)

    def test_neg(self):
        assert_equals((-self.cvx).curvature, self.ccv.curvature)
        assert_equals((-self.aff).curvature, self.aff.curvature)

    # Tests the is_affine, is_convex, and is_concave methods
    def test_is_curvature(self):
        assert self.const.is_affine()
        assert self.aff.is_affine()
        assert not self.cvx.is_affine()
        assert not self.ccv.is_affine()
        assert not self.unknown_curv.is_affine()

        assert self.const.is_convex()
        assert self.aff.is_convex()
        assert self.cvx.is_convex()
        assert not self.ccv.is_convex()
        assert not self.unknown_curv.is_convex()

        assert self.const.is_concave()
        assert self.aff.is_concave()
        assert not self.cvx.is_concave()
        assert self.ccv.is_concave()
        assert not self.unknown_curv.is_concave()
Esempio n. 10
0
 def setup_class(self):
     self.pos = Constant(1)
     self.neg = Constant(-1)
     self.zero = Constant(0)
     self.unknown = Variable()
Esempio n. 11
0
class TestCurvature(unittest.TestCase):
    """ Unit tests for the expression/curvature class. """
    def setUp(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = self.pos + self.neg

    def test_add(self):
        self.assertEqual((self.const + self.cvx).curvature, self.cvx.curvature)
        self.assertEqual((self.unknown_curv + self.ccv).curvature, UNKNOWN)
        self.assertEqual((self.cvx + self.ccv).curvature, UNKNOWN)
        self.assertEqual((self.cvx + self.cvx).curvature, self.cvx.curvature)
        self.assertEqual((self.aff + self.ccv).curvature, self.ccv.curvature)

    def test_sub(self):
        self.assertEqual((self.const - self.cvx).curvature, self.ccv.curvature)
        self.assertEqual((self.unknown_curv - self.ccv).curvature, UNKNOWN)
        self.assertEqual((self.cvx - self.ccv).curvature, self.cvx.curvature)
        self.assertEqual((self.cvx - self.cvx).curvature, UNKNOWN)
        self.assertEqual((self.aff - self.ccv).curvature, self.cvx.curvature)

    def test_sign_mult(self):
        self.assertEqual((self.zero * self.cvx).curvature, self.aff.curvature)
        self.assertEqual((self.neg * self.cvx).curvature, self.ccv.curvature)
        self.assertEqual((self.neg * self.ccv).curvature, self.cvx.curvature)
        self.assertEqual((self.neg * self.unknown_curv).curvature, QUASILINEAR)
        self.assertEqual((self.pos * self.aff).curvature, self.aff.curvature)
        self.assertEqual((self.pos * self.ccv).curvature, self.ccv.curvature)
        self.assertEqual((self.unknown_sign * self.const).curvature,
                         self.const.curvature)
        self.assertEqual((self.unknown_sign * self.ccv).curvature, UNKNOWN)

    def test_neg(self):
        self.assertEqual((-self.cvx).curvature, self.ccv.curvature)
        self.assertEqual((-self.aff).curvature, self.aff.curvature)

    # Tests the is_affine, is_convex, and is_concave methods
    def test_is_curvature(self):
        assert self.const.is_affine()
        assert self.aff.is_affine()
        assert not self.cvx.is_affine()
        assert not self.ccv.is_affine()
        assert not self.unknown_curv.is_affine()

        assert self.const.is_convex()
        assert self.aff.is_convex()
        assert self.cvx.is_convex()
        assert not self.ccv.is_convex()
        assert not self.unknown_curv.is_convex()

        assert self.const.is_concave()
        assert self.aff.is_concave()
        assert not self.cvx.is_concave()
        assert self.ccv.is_concave()
        assert not self.unknown_curv.is_concave()
Esempio n. 12
0
 def setUpClass(self) -> None:
     self.pos = Constant(1)
     self.neg = Constant(-1)
     self.zero = Constant(0)
     self.unknown = Variable()
Esempio n. 13
0
class TestCurvature(object):
    """ Unit tests for the expression/curvature class. """
    @classmethod
    def setup_class(self):
        self.cvx = Variable()**2
        self.ccv = Variable()**0.5
        self.aff = Variable()
        self.const = Constant(5)
        self.unknown_curv = log(Variable()**3)

        self.pos = Constant(1)
        self.neg = Constant(-1)
        self.zero = Constant(0)
        self.unknown_sign = Parameter()

    def test_add(self):
        assert_equals( (self.const + self.cvx).curvature, self.cvx.curvature)
        assert_equals( (self.unknown_curv + self.ccv).curvature, self.unknown_curv.curvature)
        assert_equals( (self.cvx + self.ccv).curvature, self.unknown_curv.curvature)
        assert_equals( (self.cvx + self.cvx).curvature, self.cvx.curvature)
        assert_equals( (self.aff + self.ccv).curvature, self.ccv.curvature)

    def test_sub(self):
        assert_equals( (self.const - self.cvx).curvature, self.ccv.curvature)
        assert_equals( (self.unknown_curv - self.ccv).curvature, self.unknown_curv.curvature)
        assert_equals( (self.cvx - self.ccv).curvature, self.cvx.curvature)
        assert_equals( (self.cvx - self.cvx).curvature, self.unknown_curv.curvature)
        assert_equals( (self.aff - self.ccv).curvature, self.cvx.curvature)

    def test_sign_mult(self):
        assert_equals( (self.zero* self.cvx).curvature, self.const.curvature)
        assert_equals( (self.neg*self.cvx).curvature, self.ccv.curvature)
        assert_equals( (self.neg*self.ccv).curvature, self.cvx.curvature)
        assert_equals( (self.neg*self.unknown_curv).curvature, self.unknown_curv.curvature)
        assert_equals( (self.pos*self.aff).curvature, self.aff.curvature)
        assert_equals( (self.pos*self.ccv).curvature, self.ccv.curvature)
        assert_equals( (self.unknown_sign*self.const).curvature, self.const.curvature)
        assert_equals( (self.unknown_sign*self.ccv).curvature, self.unknown_curv.curvature)

    def test_neg(self):
        assert_equals( (-self.cvx).curvature, self.ccv.curvature)
        assert_equals( (-self.aff).curvature, self.aff.curvature)

    # Tests the is_affine, is_convex, and is_concave methods
    def test_is_curvature(self):
        assert self.const.is_affine()
        assert self.aff.is_affine()
        assert not self.cvx.is_affine()
        assert not self.ccv.is_affine()
        assert not self.unknown_curv.is_affine()

        assert self.const.is_convex()
        assert self.aff.is_convex()
        assert self.cvx.is_convex()
        assert not self.ccv.is_convex()
        assert not self.unknown_curv.is_convex()

        assert self.const.is_concave()
        assert self.aff.is_concave()
        assert not self.cvx.is_concave()
        assert self.ccv.is_concave()
        assert not self.unknown_curv.is_concave()
Esempio n. 14
0
def gen_matrices(x_in, y_in, x_out, y_out, dx, dy, loworder=False):
    x_center = np.mean(x_in)
    y_center = np.mean(y_in)
    n_in = len(x_in)
    n_out = len(x_out)

    print("Size of the problem is " + str(n_in + n_out))

    deltax_in_in = x_in[..., np.newaxis] - x_in[np.newaxis,
                                                ...]  # should be x-x'
    deltax_out_in = x_out[..., np.newaxis] - x_in[np.newaxis,
                                                  ...]  # should be x-x'
    deltay_in_in = y_in[..., np.newaxis] - y_in[np.newaxis, ...]  # y - y'
    deltay_out_in = y_out[..., np.newaxis] - y_in[np.newaxis, ...]  # y - y'

    l2_in_plus_in_plus = (np.array(
        [deltax_in_in * dx - dx / 2.0,
         deltay_in_in * dy - dy / 2.0])**2).sum(axis=0)**0.5
    l2_in_plus_in_minus = (np.array(
        [deltax_in_in * dx - dx / 2.0,
         deltay_in_in * dy + dy / 2.0])**2).sum(axis=0)**0.5
    l2_in_minus_in_plus = (np.array(
        [deltax_in_in * dx + dx / 2.0,
         deltay_in_in * dy - dy / 2.0])**2).sum(axis=0)**0.5
    l2_in_minus_in_minus = (np.array(
        [deltax_in_in * dx + dx / 2.0,
         deltay_in_in * dy + dy / 2.0])**2).sum(axis=0)**0.5

    l2_out_plus_in_plus = (np.array(
        [deltax_out_in * dx - dx / 2.0,
         deltay_out_in * dy - dy / 2.0])**2).sum(axis=0)**0.5
    l2_out_plus_in_minus = (np.array(
        [deltax_out_in * dx - dx / 2.0,
         deltay_out_in * dy + dy / 2.0])**2).sum(axis=0)**0.5
    l2_out_minus_in_plus = (np.array(
        [deltax_out_in * dx + dx / 2.0,
         deltay_out_in * dy - dy / 2.0])**2).sum(axis=0)**0.5
    l2_out_minus_in_minus = (np.array(
        [deltax_out_in * dx + dx / 2.0,
         deltay_out_in * dy + dy / 2.0])**2).sum(axis=0)**0.5

    x_adjacency = sparse.csr_matrix((deltax_in_in == -1) *
                                    (deltay_in_in == 0) * -1 +
                                    (deltax_in_in == 1) *
                                    (deltay_in_in == 0) * 1)
    y_adjacency = sparse.csr_matrix((deltay_in_in == -1) *
                                    (deltax_in_in == 0) * -1 +
                                    (deltay_in_in == 1) *
                                    (deltax_in_in == 0) * 1)

    A_in_in_x = fxx(deltax_in_in * dx - dx / 2., deltay_in_in * dy - dy / 2.0, l2_in_plus_in_plus) - \
                fxx(deltax_in_in * dx - dx / 2., deltay_in_in * dy + dy / 2.0, l2_in_plus_in_minus) - \
                fxx(deltax_in_in * dx + dx / 2., deltay_in_in * dy - dy / 2.0, l2_in_minus_in_plus) + \
                fxx(deltax_in_in * dx + dx / 2., deltay_in_in * dy + dy / 2.0, l2_in_minus_in_minus)

    A_out_in_x = fxx(deltax_out_in * dx - dx / 2., deltay_out_in * dy - dy / 2.0, l2_out_plus_in_plus) - \
                 fxx(deltax_out_in * dx - dx / 2., deltay_out_in * dy + dy / 2.0, l2_out_plus_in_minus) - \
                 fxx(deltax_out_in * dx + dx / 2., deltay_out_in * dy - dy / 2.0, l2_out_minus_in_plus) + \
                 fxx(deltax_out_in * dx + dx / 2., deltay_out_in * dy + dy / 2.0, l2_out_minus_in_minus)

    D_in_in_x = fxy(deltax_in_in * dx - dx / 2., deltay_in_in * dy - dy / 2.0, l2_in_plus_in_plus) - \
                fxy(deltax_in_in * dx - dx / 2., deltay_in_in * dy + dy / 2.0, l2_in_plus_in_minus) - \
                fxy(deltax_in_in * dx + dx / 2., deltay_in_in * dy - dy / 2.0, l2_in_minus_in_plus) + \
                fxy(deltax_in_in * dx + dx / 2., deltay_in_in * dy + dy / 2.0, l2_in_minus_in_minus)

    D_out_in_x = fxy(deltax_out_in * dx - dx / 2., deltay_out_in * dy - dy / 2.0, l2_out_plus_in_plus) - \
                 fxy(deltax_out_in * dx - dx / 2., deltay_out_in * dy + dy / 2.0, l2_out_plus_in_minus) - \
                 fxy(deltax_out_in * dx + dx / 2., deltay_out_in * dy - dy / 2.0, l2_out_minus_in_plus) + \
                 fxy(deltax_out_in * dx + dx / 2., deltay_out_in * dy + dy / 2.0, l2_out_minus_in_minus)

    # u_y measurements

    A_in_in_y = fxx(deltay_in_in * dy - dy / 2.0, deltax_in_in * dx - dx / 2., l2_in_plus_in_plus) - \
                fxx(deltay_in_in * dy + dy / 2.0, deltax_in_in * dx - dx / 2., l2_in_plus_in_minus) - \
                fxx(deltay_in_in * dy - dy / 2.0, deltax_in_in * dx + dx / 2., l2_in_minus_in_plus) + \
                fxx(deltay_in_in * dy + dy / 2.0, deltax_in_in * dx + dx / 2., l2_in_minus_in_minus)

    A_out_in_y = fxx(deltay_out_in * dy - dy / 2.0, deltax_out_in * dx - dx / 2., l2_out_plus_in_plus) - \
                 fxx(deltay_out_in * dy + dy / 2.0, deltax_out_in * dx - dx / 2., l2_out_plus_in_minus) - \
                 fxx(deltay_out_in * dy - dy / 2.0, deltax_out_in * dx + dx / 2., l2_out_minus_in_plus) + \
                 fxx(deltay_out_in * dy + dy / 2.0, deltax_out_in * dx + dx / 2., l2_out_minus_in_minus)

    D_in_in_y = fxy(deltay_in_in * dy - dy / 2.0, deltax_in_in * dx - dx / 2., l2_in_plus_in_plus) - \
                fxy(deltay_in_in * dy + dy / 2.0, deltax_in_in * dx - dx / 2., l2_in_plus_in_minus) - \
                fxy(deltay_in_in * dy - dy / 2.0, deltax_in_in * dx + dx / 2., l2_in_minus_in_plus) + \
                fxy(deltay_in_in * dy + dy / 2.0, deltax_in_in * dx + dx / 2., l2_in_minus_in_minus)

    D_out_in_y = fxy(deltay_out_in * dy - dy / 2.0, deltax_out_in * dx - dx / 2., l2_out_plus_in_plus) - \
                 fxy(deltay_out_in * dy + dy / 2.0, deltax_out_in * dx - dx / 2., l2_out_plus_in_minus) - \
                 fxy(deltay_out_in * dy - dy / 2.0, deltax_out_in * dx + dx / 2., l2_out_minus_in_plus) + \
                 fxy(deltay_out_in * dy + dy / 2.0, deltax_out_in * dx + dx / 2., l2_out_minus_in_minus)

    if not loworder:

        B_in_in_x = x_in[..., np.newaxis] * A_in_in_x - fxxx(deltax_in_in - dx / 2., deltay_in_in - dy / 2.0,
                                                             l2_in_plus_in_plus) + \
                    fxxx(deltax_in_in - dx / 2., deltay_in_in + dy / 2.0, l2_in_plus_in_minus) + \
                    fxxx(deltax_in_in + dx / 2., deltay_in_in - dy / 2.0, l2_in_minus_in_plus) - \
                    fxxx(deltax_in_in + dx / 2., deltay_in_in + dy / 2.0, l2_in_minus_in_minus)

        B_out_in_x = x_out[..., np.newaxis] * A_out_in_x - fxxx(deltax_out_in - dx / 2., deltay_out_in - dy / 2.0,
                                                                l2_out_plus_in_plus) + \
                     fxxx(deltax_out_in - dx / 2., deltay_out_in + dy / 2.0, l2_out_plus_in_minus) + \
                     fxxx(deltax_out_in + dx / 2., deltay_out_in - dy / 2.0, l2_out_minus_in_plus) - \
                     fxxx(deltax_out_in + dx / 2., deltay_out_in + dy / 2.0, l2_out_minus_in_minus)

        C_in_in_x = y_in[..., np.newaxis] * A_in_in_x - fxxy(deltax_in_in - dx / 2., deltay_in_in - dy / 2.0,
                                                             l2_in_plus_in_plus) + \
                    fxxy(deltax_in_in - dx / 2., deltay_in_in + dy / 2.0, l2_in_plus_in_minus) + \
                    fxxy(deltax_in_in + dx / 2., deltay_in_in - dy / 2.0, l2_in_minus_in_plus) - \
                    fxxy(deltax_in_in + dx / 2., deltay_in_in + dy / 2.0, l2_in_minus_in_minus)

        C_out_in_x = y_out[..., np.newaxis] * A_out_in_x - fxxy(deltax_out_in - dx / 2., deltay_out_in - dy / 2.0,
                                                                l2_out_plus_in_plus) + \
                     fxxy(deltax_out_in - dx / 2., deltay_out_in + dy / 2.0, l2_out_plus_in_minus) + \
                     fxxy(deltax_out_in + dx / 2., deltay_out_in - dy / 2.0, l2_out_minus_in_plus) - \
                     fxxy(deltax_out_in + dx / 2., deltay_out_in + dy / 2.0, l2_out_minus_in_minus)

        E_in_in_x = x_in[..., np.newaxis] * D_in_in_x - fxyx(deltax_in_in - dx / 2., deltay_in_in - dy / 2.0,
                                                             l2_in_plus_in_plus) + \
                    fxyx(deltax_in_in - dx / 2., deltay_in_in + dy / 2.0, l2_in_plus_in_minus) + \
                    fxyx(deltax_in_in + dx / 2., deltay_in_in - dy / 2.0, l2_in_minus_in_plus) - \
                    fxyx(deltax_in_in + dx / 2., deltay_in_in + dy / 2.0, l2_in_minus_in_minus)

        E_out_in_x = x_out[..., np.newaxis] * D_out_in_x - fxyx(deltax_out_in - dx / 2., deltay_out_in - dy / 2.0,
                                                                l2_out_plus_in_plus) + \
                     fxyx(deltax_out_in - dx / 2., deltay_out_in + dy / 2.0, l2_out_plus_in_minus) + \
                     fxyx(deltax_out_in + dx / 2., deltay_out_in - dy / 2.0, l2_out_minus_in_plus) - \
                     fxyx(deltax_out_in + dx / 2., deltay_out_in + dy / 2.0, l2_out_minus_in_minus)

        F_in_in_x = y_in[..., np.newaxis] * D_in_in_x - fxyx(deltax_in_in - dx / 2., deltay_in_in - dy / 2.0,
                                                             l2_in_plus_in_plus) + \
                    fxyx(deltax_in_in - dx / 2., deltay_in_in + dy / 2.0, l2_in_plus_in_minus) + \
                    fxyx(deltax_in_in + dx / 2., deltay_in_in - dy / 2.0, l2_in_minus_in_plus) - \
                    fxyx(deltax_in_in + dx / 2., deltay_in_in + dy / 2.0, l2_in_minus_in_minus)

        F_out_in_x = y_out[..., np.newaxis] * D_out_in_x - fxyx(deltax_out_in - dx / 2., deltay_out_in - dy / 2.0,
                                                                l2_out_plus_in_plus) + \
                     fxyx(deltax_out_in - dx / 2., deltay_out_in + dy / 2.0, l2_out_plus_in_minus) + \
                     fxyx(deltax_out_in + dx / 2., deltay_out_in - dy / 2.0, l2_out_minus_in_plus) - \
                     fxyx(deltax_out_in + dx / 2., deltay_out_in + dy / 2.0, l2_out_minus_in_minus)


        B_in_in_y = y_in[..., np.newaxis] * A_in_in_y - fxxx(deltay_in_in - dy / 2.0, deltax_in_in - dx / 2.,
                                                             l2_in_plus_in_plus) + \
                    fxxx(deltay_in_in + dy / 2.0, deltax_in_in - dx / 2., l2_in_plus_in_minus) + \
                    fxxx(deltay_in_in - dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_plus) - \
                    fxxx(deltay_in_in + dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_minus)

        B_out_in_y = y_out[..., np.newaxis] * A_out_in_y - fxxx(deltay_out_in - dy / 2.0, deltax_out_in - dx / 2.,
                                                                l2_out_plus_in_plus) + \
                     fxxx(deltay_out_in + dy / 2.0, deltax_out_in - dx / 2., l2_out_plus_in_minus) + \
                     fxxx(deltay_out_in - dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_plus) - \
                     fxxx(deltay_out_in + dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_minus)

        C_in_in_y = x_in[..., np.newaxis] * A_in_in_y - fxxy(deltay_in_in - dy / 2.0, deltax_in_in - dx / 2.,
                                                             l2_in_plus_in_plus) + \
                    fxxy(deltay_in_in + dy / 2.0, deltax_in_in - dx / 2., l2_in_plus_in_minus) + \
                    fxxy(deltay_in_in - dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_plus) - \
                    fxxy(deltay_in_in + dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_minus)

        C_out_in_y = x_out[..., np.newaxis] * A_out_in_y - fxxy(deltay_out_in - dy / 2.0, deltax_out_in - dx / 2.,
                                                                l2_out_plus_in_plus) + \
                     fxxy(deltay_out_in + dy / 2.0, deltax_out_in - dx / 2., l2_out_plus_in_minus) + \
                     fxxy(deltay_out_in - dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_plus) - \
                     fxxy(deltay_out_in + dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_minus)

        E_in_in_y = y_in[..., np.newaxis] * D_in_in_y - fxyx(deltay_in_in - dy / 2.0, deltax_in_in - dx / 2.,
                                                             l2_in_plus_in_plus) + \
                    fxyx(deltay_in_in + dy / 2.0, deltax_in_in - dx / 2., l2_in_plus_in_minus) + \
                    fxyx(deltay_in_in - dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_plus) - \
                    fxyx(deltay_in_in + dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_minus)

        E_out_in_y = y_out[..., np.newaxis] * D_out_in_y - fxyx(deltay_out_in - dy / 2.0, deltax_out_in - dx / 2.,
                                                                l2_out_plus_in_plus) + \
                     fxyx(deltay_out_in + dy / 2.0, deltax_out_in - dx / 2., l2_out_plus_in_minus) + \
                     fxyx(deltay_out_in - dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_plus) - \
                     fxyx(deltay_out_in + dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_minus)

        F_in_in_y = x_in[..., np.newaxis] * D_in_in_y - fxyx(deltay_in_in - dy / 2.0, deltax_in_in - dx / 2.,
                                                             l2_in_plus_in_plus) + \
                    fxyx(deltay_in_in + dy / 2.0, deltax_in_in - dx / 2., l2_in_plus_in_minus) + \
                    fxyx(deltay_in_in - dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_plus) - \
                    fxyx(deltay_in_in + dy / 2.0, deltax_in_in + dx / 2., l2_in_minus_in_minus)

        F_out_in_y = x_out[..., np.newaxis] * D_out_in_y - fxyx(deltay_out_in - dy / 2.0, deltax_out_in - dx / 2.,
                                                                l2_out_plus_in_plus) + \
                     fxyx(deltay_out_in + dy / 2.0, deltax_out_in - dx / 2., l2_out_plus_in_minus) + \
                     fxyx(deltay_out_in - dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_plus) - \
                     fxyx(deltay_out_in + dy / 2.0, deltax_out_in + dx / 2., l2_out_minus_in_minus)

        G_in_in_xx = A_in_in_x + B_in_in_x + C_in_in_x
        G_in_in_xy = D_in_in_x + E_in_in_x + F_in_in_x
        G_out_in_xx = (A_out_in_x + B_out_in_x + C_out_in_x)
        G_out_in_xy = (D_out_in_x + E_out_in_x + F_out_in_x)

        G_in_in_yy = A_in_in_y + B_in_in_y + C_in_in_y
        G_in_in_yx = D_in_in_y + E_in_in_y + F_in_in_y
        G_out_in_yy = (A_out_in_y + B_out_in_y + C_out_in_y)
        G_out_in_yx = (D_out_in_y + E_out_in_y + F_out_in_y)

    else:
        G_in_in_xx = A_in_in_x
        G_in_in_xy = D_in_in_x
        G_out_in_xx = A_out_in_x
        G_out_in_xy = D_out_in_x

        G_in_in_yy = A_in_in_y
        G_in_in_yx = D_in_in_y
        G_out_in_yy = A_out_in_y
        G_out_in_yx = D_out_in_y

    Dx = sparse.csr_matrix((deltax_in_in == 0) * (deltay_in_in == 0) * -1 +
                           (deltax_in_in == 1) * (deltay_in_in == 0) * 1)
    rowsums = np.squeeze(np.asarray((Dx.sum(axis=1) != 0)))
    Dx[rowsums, :] = 0
    Dx.eliminate_zeros()
    Dx = Constant(Dx)

    Dy = sparse.csr_matrix((deltay_in_in == 0) * (deltax_in_in == 0) * -1 +
                           (deltay_in_in == 1) * (deltax_in_in == 0) * 1)
    rowsums = np.squeeze(np.asarray((Dy.sum(axis=1) != 0)))
    Dy[rowsums, :] = 0
    Dy.eliminate_zeros()
    Dy = Constant(Dy)

    return G_in_in_xx, G_in_in_xy, G_out_in_xx, G_out_in_xy, G_in_in_yy, G_in_in_yx, G_out_in_yy, G_out_in_yx, Dx, Dy