Skip to content

Commit

Permalink
Rename attribtes, remove 'type' where not needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmtroffaes committed Sep 17, 2024
1 parent d5f222b commit aacedb1
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 262 deletions.
4 changes: 2 additions & 2 deletions cython/_cdd.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

from typing import SupportsFloat

NumberType = float
SupportsNumberType = SupportsFloat
Number = float
SupportsNumber = SupportsFloat

cdef extern from * nogil:
"#undef GMPRATIONAL"
Expand Down
6 changes: 3 additions & 3 deletions cython/_cddgmp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
from fractions import Fraction
from typing import Union

from cdd import LPObjType, LPSolverType, LPStatusType, RepType
from cdd import LPObj, LPSolver, LPStatus, Rep

NumberType = Fraction
SupportsNumberType = Union[Fraction, int]
Number = Fraction
SupportsNumber = Union[Fraction, int]

cdef extern from * nogil:
"#define GMPRATIONAL"
Expand Down
98 changes: 49 additions & 49 deletions cython/pycddlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ cdef class Matrix:
cdef dd_MatrixPtr dd_mat
# hack for annotation of properties
__annotations__ = dict(
array=Sequence[Sequence[NumberType]],
array=Sequence[Sequence[Number]],
lin_set=Set[int],
rep_type=RepType,
obj_type=LPObjType,
obj_func=Sequence[NumberType],
rep=Rep,
obj=LPObj,
obj_func=Sequence[Number],
)

@property
Expand Down Expand Up @@ -176,23 +176,23 @@ cdef class Matrix:
_set_set(self.dd_mat.linset, value)

@property
def rep_type(self):
def rep(self):
"""Representation:
inequalities (H-representation), generators (V-representation), or unspecified.
"""
return RepType(self.dd_mat.representation)
return Rep(self.dd_mat.representation)

@rep_type.setter
def rep_type(self, dd_RepresentationType value):
@rep.setter
def rep(self, dd_RepresentationType value):
self.dd_mat.representation = value

@property
def obj_type(self):
def obj(self):
"""Linear programming objective: maximize, minimize, or none."""
return LPObjType(self.dd_mat.objective)
return LPObj(self.dd_mat.objective)

@obj_type.setter
def obj_type(self, dd_LPObjectiveType value):
@obj.setter
def obj(self, dd_LPObjectiveType value):
self.dd_mat.objective = value

@property
Expand Down Expand Up @@ -227,7 +227,7 @@ cdef class Matrix:
def __reduce__(self):
return (
matrix_from_array,
(self.array, self.lin_set, self.rep_type, self.obj_type, self.obj_func),
(self.array, self.lin_set, self.rep, self.obj, self.obj_func),
)


Expand Down Expand Up @@ -277,11 +277,11 @@ cdef _get_array_from_matrix(mytype **pp, _Shape shape):
# create matrix and wrap into Matrix class
# https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#instantiation-from-existing-c-c-pointers
def matrix_from_array(
array: Sequence[Sequence[SupportsNumberType]],
array: Sequence[Sequence[SupportsNumber]],
lin_set: Container[int] = (),
rep_type: RepType = RepType.UNSPECIFIED,
obj_type: LPObjType = LPObjType.NONE,
obj_func: Optional[Sequence[SupportsNumberType]] = None,
rep: Rep = Rep.UNSPECIFIED,
obj: LPObj = LPObj.NONE,
obj_func: Optional[Sequence[SupportsNumber]] = None,
) -> Matrix:
"""Construct a matrix with the given attributes.

Expand All @@ -297,8 +297,8 @@ def matrix_from_array(
try:
_set_matrix_from_array(dd_mat.matrix, shape, array)
_set_set(dd_mat.linset, lin_set)
dd_mat.representation = rep_type.value
dd_mat.objective = obj_type.value
dd_mat.representation = rep.value
dd_mat.objective = obj.value
if obj_func is not None:
if len(obj_func) != dd_mat.colsize:
raise ValueError(
Expand Down Expand Up @@ -336,7 +336,7 @@ def matrix_canonicalize(mat: Matrix) -> tuple[Set[int], Set[int]]:
cdef dd_ErrorType error = dd_NoError
cdef dd_boolean success
if mat.dd_mat.representation == dd_Unspecified:
raise ValueError("rep_type unspecified")
raise ValueError("rep unspecified")
success = dd_MatrixCanonicalize(
&mat.dd_mat, &impl_linset, &redset, &newpos, &error
)
Expand All @@ -360,13 +360,13 @@ cdef class LinProg:
"""A linear program: a set of inequalities and an objective function to optimize."""
cdef dd_LPPtr dd_lp
__annotations__ = dict(
array=Sequence[Sequence[NumberType]],
dual_solution=Sequence[tuple[int, NumberType]],
obj_type=LPObjType,
obj_value=NumberType,
primal_solution=Sequence[NumberType],
solver=LPSolverType,
status=LPStatusType,
array=Sequence[Sequence[Number]],
dual_solution=Sequence[tuple[int, Number]],
obj=LPObj,
obj_value=Number,
primal_solution=Sequence[Number],
solver=LPSolver,
status=LPStatus,
)

@property
Expand All @@ -388,23 +388,23 @@ cdef class LinProg:
return _get_array_from_matrix(self.dd_lp.A, shape)

@property
def obj_type(self):
def obj(self):
"""Whether we are minimizing or maximizing."""
return LPObjType(self.dd_lp.objective)
return LPObj(self.dd_lp.objective)

@obj_type.setter
def obj_type(self, dd_LPObjectiveType value):
@obj.setter
def obj(self, dd_LPObjectiveType value):
self.dd_lp.objective = value

@property
def solver(self):
"""The solver used when last solving the linear program."""
return LPSolverType(self.dd_lp.solver)
return LPSolver(self.dd_lp.solver)

@property
def status(self):
"""The status of the linear program, after solving."""
return LPStatusType(self.dd_lp.LPS)
return LPStatus(self.dd_lp.LPS)

@property
def obj_value(self):
Expand Down Expand Up @@ -451,7 +451,7 @@ cdef class LinProg:
self.dd_lp = NULL

def __reduce__(self):
return linprog_from_array, (self.array, self.obj_type)
return linprog_from_array, (self.array, self.obj)


cdef linprog_from_ptr(dd_LPPtr dd_lp):
Expand All @@ -468,19 +468,19 @@ def linprog_from_matrix(mat: Matrix) -> LinProg:
and its objective type must be set,
otherwise a :exc:`ValueError` is raised.
"""
# cddlib does not check if obj_type is valid
# cddlib does not check if obj is valid
if mat.dd_mat.objective != dd_LPmax and mat.dd_mat.objective != dd_LPmin:
raise ValueError("obj_type must be MIN or MAX")
raise ValueError("obj must be MIN or MAX")
# cddlib assumes H-representation
if mat.dd_mat.representation != dd_Inequality:
raise ValueError("rep_type must be INEQUALITY")
raise ValueError("rep must be INEQUALITY")
cdef dd_ErrorType error = dd_NoError
# note: dd_Matrix2LP never reports error... so ignore
return linprog_from_ptr(dd_Matrix2LP(mat.dd_mat, &error))


def linprog_from_array(
array: Sequence[Sequence[SupportsNumberType]], obj_type: LPObjType
array: Sequence[Sequence[SupportsNumber]], obj: LPObj
) -> LinProg:
"""Construct a linear program from *array*.

Expand All @@ -489,11 +489,11 @@ def linprog_from_array(

.. versionadded:: 3.0.0
"""
if obj_type != dd_LPmax and obj_type != dd_LPmin:
raise ValueError("obj_type must be MIN or MAX")
if obj != dd_LPmax and obj != dd_LPmin:
raise ValueError("obj must be MIN or MAX")
cdef _Shape shape = _array_shape(array)
cdef dd_LPPtr dd_lp = dd_CreateLPData(
obj_type, NUMBER_TYPE, shape.numrows, shape.numcols
obj, NUMBER_TYPE, shape.numrows, shape.numcols
)
if dd_lp == NULL:
raise MemoryError
Expand All @@ -506,7 +506,7 @@ def linprog_from_array(


def linprog_solve(
lp: LinProg, solver: LPSolverType = LPSolverType.DUAL_SIMPLEX
lp: LinProg, solver: LPSolver = LPSolver.DUAL_SIMPLEX
) -> None:
"""Solve the linear program *lp* using *solver*."""
cdef dd_ErrorType error = dd_NoError
Expand All @@ -519,13 +519,13 @@ cdef class Polyhedron:
"""Representation of a polyhedron."""
cdef dd_PolyhedraPtr dd_poly
__annotations__ = dict(
rep_type=RepType,
rep=Rep,
)

@property
def rep_type(self):
def rep(self):
"""Representation type of the input."""
return RepType(self.dd_poly.representation)
return Rep(self.dd_poly.representation)

def __str__(self):
cdef libc.stdio.FILE *pfile
Expand All @@ -549,7 +549,7 @@ cdef polyhedron_from_ptr(dd_PolyhedraPtr dd_poly):


def polyhedron_from_matrix(
mat: Matrix, row_order_type: Optional[RowOrderType] = None
mat: Matrix, row_order_type: Optional[RowOrder] = None
) -> Polyhedron:
"""Run the double description method to convert *mat* into a polyhedron,
using *row_order_type* if specified.
Expand All @@ -562,7 +562,7 @@ def polyhedron_from_matrix(
mat.dd_mat.representation != dd_Inequality
and mat.dd_mat.representation != dd_Generator
):
raise ValueError("rep_type must be INEQUALITY or GENERATOR")
raise ValueError("rep must be INEQUALITY or GENERATOR")
cdef dd_ErrorType error = dd_NoError
if row_order_type is None:
dd_poly = dd_DDMatrix2Poly(mat.dd_mat, &error)
Expand Down Expand Up @@ -663,7 +663,7 @@ def fourier_elimination(mat: Matrix) -> Matrix:
.. versionadded:: 3.0.0
"""
if mat.dd_mat.representation != dd_Inequality:
raise ValueError("rep_type must be INEQUALITY")
raise ValueError("rep must be INEQUALITY")
cdef dd_ErrorType error = dd_NoError
cdef dd_MatrixPtr dd_mat = dd_FourierElimination(mat.dd_mat, &error)
if error != dd_NoError:
Expand All @@ -686,7 +686,7 @@ def block_elimination(mat: Matrix, col_set: Container[int]) -> Matrix:
.. versionadded:: 3.0.0
"""
if mat.dd_mat.representation != dd_Inequality:
raise ValueError("rep_type must be INEQUALITY")
raise ValueError("rep must be INEQUALITY")
cdef set_type dd_colset = NULL
cdef dd_MatrixPtr dd_mat = NULL
cdef dd_ErrorType error = dd_NoError
Expand Down
14 changes: 7 additions & 7 deletions cython/pyenums.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
from enum import IntEnum


class _AdjacencyTestType(IntEnum):
class _AdjacencyTest(IntEnum):
COMBINATORIAL = dd_Combinatorial
ALGEBRAIC = dd_Algebraic


class _NumberType(IntEnum):
class _Number(IntEnum):
UNKNOWN = dd_Unknown
REAL = dd_Real
RATIONAL = dd_Rational
INTEGER = dd_Integer


class RepType(IntEnum):
class Rep(IntEnum):
"""Type of representation.
Use :attr:`INEQUALITY` for H-representation
and :attr:`GENERATOR` for V-representation.
Expand All @@ -44,7 +44,7 @@ class RepType(IntEnum):
GENERATOR = dd_Generator


class RowOrderType(IntEnum):
class RowOrder(IntEnum):
"""Type of row order to use in the double description method."""
MAX_INDEX = dd_MaxIndex
MIN_INDEX = dd_MinIndex
Expand Down Expand Up @@ -83,20 +83,20 @@ class _CompStatus(IntEnum):
REGION_EMPTY = dd_RegionEmpty


class LPObjType(IntEnum):
class LPObj(IntEnum):
"""Type of objective for a linear program."""
NONE = dd_LPnone
MAX = dd_LPmax
MIN = dd_LPmin


class LPSolverType(IntEnum):
class LPSolver(IntEnum):
"""Type of solver for a linear program."""
CRISS_CROSS = dd_CrissCross
DUAL_SIMPLEX = dd_DualSimplex


class LPStatusType(IntEnum):
class LPStatus(IntEnum):
"""Status of a linear program."""
UNDECIDED = dd_LPSundecided
OPTIMAL = dd_Optimal
Expand Down
10 changes: 5 additions & 5 deletions docs/source/cdd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ Enums

.. autoclass has trouble finding attributes of enums, so list explicitly
.. autoclass:: LPObjType(value)
.. autoclass:: LPObj(value)
:show-inheritance:

.. attribute::
NONE
MAX
MIN

.. autoclass:: LPSolverType(value)
.. autoclass:: LPSolver(value)
:show-inheritance:

.. attribute::
CRISS_CROSS
DUAL_SIMPLEX

.. autoclass:: LPStatusType(value)
.. autoclass:: LPStatus(value)
:show-inheritance:

.. attribute::
Expand All @@ -38,15 +38,15 @@ Enums
UNBOUNDED
DUAL_UNBOUNDED

.. autoclass:: RepType(value)
.. autoclass:: Rep(value)
:show-inheritance:

.. attribute::
UNSPECIFIED
INEQUALITY
GENERATOR

.. autoclass:: RowOrderType(value)
.. autoclass:: RowOrder(value)
:show-inheritance:

.. attribute::
Expand Down
Loading

0 comments on commit aacedb1

Please sign in to comment.