# Source code for qiskit_optimization.problems.linear_constraint

```# This code is part of a Qiskit project.
#
# (C) Copyright IBM 2019, 2023.
#
# obtain a copy of this license in the LICENSE.txt file in the root directory
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Linear Constraint."""

from typing import Union, List, Dict, Any

from numpy import ndarray
from scipy.sparse import spmatrix

from .constraint import Constraint, ConstraintSense
from .linear_expression import LinearExpression

[docs]class LinearConstraint(Constraint):
"""Representation of a linear constraint."""

# Note: added, duplicating in effect that in Constraint, to avoid issues with Sphinx
Sense = ConstraintSense

def __init__(
self,
name: str,
linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
sense: ConstraintSense,
rhs: float,
) -> None:
"""
Args:
name: The name of the constraint.
linear: The coefficients specifying the linear constraint.
sense: The sense of the constraint.
rhs: The right-hand-side of the constraint.
"""

@property
def linear(self) -> LinearExpression:
"""Returns the linear expression corresponding to the left-hand-side of the constraint.

Returns:
The left-hand-side linear expression.
"""
return self._linear

@linear.setter
def linear(
self,
linear: Union[ndarray, spmatrix, List[float], Dict[Union[str, int], float]],
) -> None:
"""Sets the linear expression corresponding to the left-hand-side of the constraint.
The coefficients can either be given by an array, a (sparse) 1d matrix, a list or a
dictionary.

Args:
linear: The linear coefficients of the left-hand-side.
"""

[docs]    def evaluate(self, x: Union[ndarray, List, Dict[Union[int, str], float]]) -> float:
"""Evaluate the left-hand-side of the constraint.

Args:
x: The values of the variables to be evaluated.

Returns:
The left-hand-side of the constraint given the variable values.
"""
return self.linear.evaluate(x)

def __repr__(self):
# pylint: disable=cyclic-import
from ..translators.prettyprint import expr2str, DEFAULT_TRUNCATE

lhs = expr2str(linear=self.linear, truncate=DEFAULT_TRUNCATE)
return f"<{self.__class__.__name__}: {lhs} {self.sense.label} {self.rhs} '{self.name}'>"

def __str__(self):
# pylint: disable=cyclic-import
from ..translators.prettyprint import expr2str

lhs = expr2str(linear=self.linear)
return f"{lhs} {self.sense.label} {self.rhs} '{self.name}'"
```