Repository: scipy/scipy
File: scipy/optimize/_differentialevolution.py (Python)

Code before:
    _ConstraintWrapper.fun and _ConstraintWrapper.violation can get sent
    arrays of shape (N, S) or (N,), where S is the number of vectors of shape
    (N,) to consider constraints for.
    """
    def __init__(self, constraint, x0):
        self.constraint = constraint

        if isinstance(constraint, NonlinearConstraint):
            def fun(x):
                x = np.asarray(x)
                return np.atleast_1d(constraint.fun(x))
        elif isinstance(constraint, LinearConstraint):
            def fun(x):
                if issparse(constraint.A):
                    A = constraint.A
                else:
                    A = np.atleast_2d(constraint.A)

                res = A.dot(x)
                # x either has shape (N, S) or (N)
                # (M, N) x (N, S) --> (M, S)
                # (M, N) x (N,)   --> (M,)
                # However, if (M, N) is a matrix then:
                # (M, N) * (N,)   --> (M, 1), we need this to be (M,)
                if x.ndim == 1 and res.ndim == 2:
                    # deal with case that constraint.A is a np.matrix
                    # see gh20041
                    res = np.asarray(res)[:, 0]

                return res
        elif isinstance(constraint, Bounds):
            def fun(x):
                return np.asarray(x)
        else:
            raise ValueError("`constraint` of an unknown type is passed.")

        self.fun = fun

        lb = np.asarray(constraint.lb, dtype=float)
        ub = np.asarray(constraint.ub, dtype=float)

        x0 = np.asarray(x0)

        # find out the number of constraints
        f0 = fun(x0)
        self.num_constr = m = f0.size
        self.parameter_count = x0.size

        if lb.ndim == 0:
            lb = np.resize(lb, m)
        if ub.ndim == 0:

Review comment (suggestion):
```suggestion
                    # deal with case that constraint.A is an np.matrix
```

Code after:
    _ConstraintWrapper.fun and _ConstraintWrapper.violation can get sent
    arrays of shape (N, S) or (N,), where S is the number of vectors of shape
    (N,) to consider constraints for.
    """
    def __init__(self, constraint, x0):
        self.constraint = constraint

        if isinstance(constraint, NonlinearConstraint):
            def fun(x):
                x = np.asarray(x)
                return np.atleast_1d(constraint.fun(x))
        elif isinstance(constraint, LinearConstraint):
            def fun(x):
                if issparse(constraint.A):
                    A = constraint.A
                else:
                    A = np.atleast_2d(constraint.A)

                res = A.dot(x)
                # x either has shape (N, S) or (N)
                # (M, N) x (N, S) --> (M, S)
                # (M, N) x (N,)   --> (M,)
                # However, if (M, N) is a matrix then:
                # (M, N) * (N,)   --> (M, 1), we need this to be (M,)
                if x.ndim == 1 and res.ndim == 2:
                    # deal with case that constraint.A is an np.matrix
                    # see gh20041
                    res = np.asarray(res)[:, 0]

                return res
        elif isinstance(constraint, Bounds):
            def fun(x):
                return np.asarray(x)
        else:
            raise ValueError("`constraint` of an unknown type is passed.")

        self.fun = fun

        lb = np.asarray(constraint.lb, dtype=float)
        ub = np.asarray(constraint.ub, dtype=float)

        x0 = np.asarray(x0)

        # find out the number of constraints
        f0 = fun(x0)
        self.num_constr = m = f0.size
        self.parameter_count = x0.size

        if lb.ndim == 0:
            lb = np.resize(lb, m)
        if ub.ndim == 0: