Repository: scipy/scipy
File: scipy/optimize/tests/test__differential_evolution.py (Python)

Code before:
            # where N is the number of parameters,
            # S is the number of solution vectors to be examined,
            # and M is the number of constraint components
            return np.array([x[0] ** 2 + x[1],
                             x[0] ** 2 - x[1]])

        nlc = NonlinearConstraint(cons_f, [-1, -0.8500], [2, 2])
        pc = _ConstraintWrapper(nlc, [0.5, 1])
        assert np.size(pc.bounds[0]) == 2

        xs = [(0.5, 1), (0.5, 1.2), (1.2, 1.2), (0.1, -1.2), (0.1, 2.0)]
        vs = [(0, 0), (0, 0.1), (0.64, 0), (0.19, 0), (0.01, 1.14)]

        for x, v in zip(xs, vs):
            assert_allclose(pc.violation(x), v)

        # now check that we can vectorize the constraint wrapper
        assert_allclose(pc.violation(np.array(xs).T),
                        np.array(vs).T)
        assert pc.fun(np.array(xs).T).shape == (2, len(xs))
        assert pc.violation(np.array(xs).T).shape == (2, len(xs))
        assert pc.num_constr == 2
        assert pc.parameter_count == 2

    def test_matrix_linear_constraint(self):
        # gh20041 supplying a np.matrix to construct a LinearConstraint caused
        # _ConstraintWrapper to start returning constraint violations of the
        # wrong shape.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", PendingDeprecationWarning)
            matrix = np.matrix([[1, 1, 1, 1.],
                                [2, 2, 2, 2.]])
        lc = LinearConstraint(matrix, 0, 1)
        x0 = np.ones(4)
        cw = _ConstraintWrapper(lc, x0)
        # the shape of the constraint violation should be the same as the number
        # of constraints applied.
        assert cw.violation(x0).shape == (2,)

        # let's try a vectorised violation call.
        xtrial = np.arange(4 * 5).reshape(4, 5)
        assert cw.violation(xtrial).shape == (2, 5)

    @pytest.mark.fail_slow(20)
    def test_L1(self):
        # Lampinen ([5]) test problem 1

        def f(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            fun = np.sum(5*x[1:5]) - 5*x[1:5]@x[1:5] - np.sum(x[5:])
            return fun

Review comment (suggestion):
```suggestion
        # gh20041 supplying an np.matrix to construct a LinearConstraint caused
```

Code after:
            # where N is the number of parameters,
            # S is the number of solution vectors to be examined,
            # and M is the number of constraint components
            return np.array([x[0] ** 2 + x[1],
                             x[0] ** 2 - x[1]])

        nlc = NonlinearConstraint(cons_f, [-1, -0.8500], [2, 2])
        pc = _ConstraintWrapper(nlc, [0.5, 1])
        assert np.size(pc.bounds[0]) == 2

        xs = [(0.5, 1), (0.5, 1.2), (1.2, 1.2), (0.1, -1.2), (0.1, 2.0)]
        vs = [(0, 0), (0, 0.1), (0.64, 0), (0.19, 0), (0.01, 1.14)]

        for x, v in zip(xs, vs):
            assert_allclose(pc.violation(x), v)

        # now check that we can vectorize the constraint wrapper
        assert_allclose(pc.violation(np.array(xs).T),
                        np.array(vs).T)
        assert pc.fun(np.array(xs).T).shape == (2, len(xs))
        assert pc.violation(np.array(xs).T).shape == (2, len(xs))
        assert pc.num_constr == 2
        assert pc.parameter_count == 2

    def test_matrix_linear_constraint(self):
        # gh20041 supplying an np.matrix to construct a LinearConstraint caused
        # _ConstraintWrapper to start returning constraint violations of the
        # wrong shape.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", PendingDeprecationWarning)
            matrix = np.matrix([[1, 1, 1, 1.],
                                [2, 2, 2, 2.]])
        lc = LinearConstraint(matrix, 0, 1)
        x0 = np.ones(4)
        cw = _ConstraintWrapper(lc, x0)
        # the shape of the constraint violation should be the same as the number
        # of constraints applied.
        assert cw.violation(x0).shape == (2,)

        # let's try a vectorised violation call.
        xtrial = np.arange(4 * 5).reshape(4, 5)
        assert cw.violation(xtrial).shape == (2, 5)

    @pytest.mark.fail_slow(20)
    def test_L1(self):
        # Lampinen ([5]) test problem 1

        def f(x):
            x = np.hstack(([0], x))  # 1-indexed to match reference
            fun = np.sum(5*x[1:5]) - 5*x[1:5]@x[1:5] - np.sum(x[5:])
            return fun