Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make matroid copy and deepcopy simply return self #37670

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/sage/matroids/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from .minor_matroid import MinorMatroid
from .dual_matroid import DualMatroid
from .rank_matroid import RankMatroid
from .circuits_matroid import CircuitsMatroid
from .circuit_closures_matroid import CircuitClosuresMatroid
from .basis_matroid import BasisMatroid
from .linear_matroid import LinearMatroid, RegularMatroid, BinaryMatroid, TernaryMatroid, QuaternaryMatroid
Expand Down
40 changes: 0 additions & 40 deletions src/sage/matroids/basis_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1161,46 +1161,6 @@ cdef class BasisMatroid(BasisExchangeMatroid):
else:
return rich_to_bool(op, 1)

def __copy__(self):
"""
Create a shallow copy.

EXAMPLES::

sage: from sage.matroids.advanced import *
sage: M = BasisMatroid(matroids.catalog.Vamos())
sage: N = copy(M) # indirect doctest
sage: M == N
True
"""
N = BasisMatroid(M=self)
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo=None):
"""
Create a deep copy.

.. NOTE::

Identical to shallow copy for BasisMatroid class.

EXAMPLES::

sage: from sage.matroids.advanced import *
sage: M = BasisMatroid(matroids.catalog.Vamos())
sage: N = deepcopy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
False
"""
if memo is None:
memo = {}
N = BasisMatroid(M=self)
N.rename(self.get_custom_name())
return N

def __reduce__(self):
"""
Save the matroid for later reloading.
Expand Down
73 changes: 14 additions & 59 deletions src/sage/matroids/circuit_closures_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ Note that the class does not implement custom minor and dual operations::
AUTHORS:

- Rudi Pendavingh, Stefan van Zwam (2013-04-01): initial version

TESTS::

sage: from sage.matroids.advanced import *
sage: M = CircuitClosuresMatroid(matroids.catalog.Fano())
sage: TestSuite(M).run()

Methods
=======
"""
# ****************************************************************************
# Copyright (C) 2013 Rudi Pendavingh <[email protected]>
Expand Down Expand Up @@ -131,7 +122,8 @@ cdef class CircuitClosuresMatroid(Matroid):
True
"""

# NECESSARY
# necessary

def __init__(self, M=None, groundset=None, circuit_closures=None):
"""
Initialization of the matroid. See class docstring for full
Expand All @@ -154,6 +146,12 @@ cdef class CircuitClosuresMatroid(Matroid):
....: 4: ['abcdefgh']})
sage: M.equals(matroids.catalog.P8())
True

TESTS::

sage: from sage.matroids.advanced import *
sage: M = CircuitClosuresMatroid(matroids.catalog.Fano())
sage: TestSuite(M).run()
"""
if M is not None:
self._groundset = M.groundset()
Expand Down Expand Up @@ -206,7 +204,8 @@ cdef class CircuitClosuresMatroid(Matroid):
"""
return len(self._max_independent(X))

# OPTIONAL, OPTIMIZED FOR THIS CLASS
# optional

cpdef full_rank(self) noexcept:
r"""
Return the rank of the matroid.
Expand Down Expand Up @@ -413,7 +412,8 @@ cdef class CircuitClosuresMatroid(Matroid):
SN.append(C)
return SM._isomorphism(SN) is not None

# REPRESENTATION
# representation

def _repr_(self):
"""
Return a string representation of the matroid.
Expand All @@ -430,7 +430,7 @@ cdef class CircuitClosuresMatroid(Matroid):
"""
return Matroid._repr_(self) + " with circuit-closures\n" + setprint_s(self._circuit_closures)

# COMPARISON
# comparison

def __hash__(self):
r"""
Expand Down Expand Up @@ -490,52 +490,7 @@ cdef class CircuitClosuresMatroid(Matroid):
return rich_to_bool(op, 1)
return richcmp(lt._circuit_closures, rt._circuit_closures, op)

# COPYING, LOADING, SAVING

def __copy__(self):
"""
Create a shallow copy.

EXAMPLES::

sage: M = matroids.catalog.Vamos()
sage: N = copy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
True
"""
N = CircuitClosuresMatroid(groundset=[], circuit_closures={})
N._groundset = self._groundset
N._circuit_closures = self._circuit_closures
N._matroid_rank = self._matroid_rank
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo=None):
"""
Create a deep copy.

.. NOTE::

Since matroids are immutable, a shallow copy normally suffices.

EXAMPLES::

sage: M = matroids.catalog.Vamos()
sage: N = deepcopy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
False
"""
if memo is None:
memo = {}
from copy import deepcopy
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
N = CircuitClosuresMatroid(groundset=deepcopy(self._groundset, memo), circuit_closures=deepcopy(self._circuit_closures, memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N
# copying, loading, saving

def __reduce__(self):
"""
Expand Down
49 changes: 0 additions & 49 deletions src/sage/matroids/circuits_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -337,55 +337,6 @@ cdef class CircuitsMatroid(Matroid):

# copying, loading, saving

def __copy__(self):
"""
Create a shallow copy.

EXAMPLES::

sage: from sage.matroids.circuits_matroid import CircuitsMatroid
sage: M = CircuitsMatroid(matroids.catalog.Vamos())
sage: N = copy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
True
"""
N = CircuitsMatroid(groundset=[], circuits=[])
N._groundset = self._groundset
N._C = self._C
N._k_C = self._k_C
N._nsc_defined = self._nsc_defined
N._matroid_rank = self._matroid_rank
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo=None):
"""
Create a deep copy.

.. NOTE::

Since matroids are immutable, a shallow copy normally suffices.

EXAMPLES::

sage: from sage.matroids.circuits_matroid import CircuitsMatroid
sage: M = CircuitsMatroid(matroids.catalog.Vamos())
sage: N = deepcopy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
False
"""
if memo is None:
memo = {}
from copy import deepcopy
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
N = CircuitsMatroid(groundset=deepcopy(self._groundset, memo), circuits=deepcopy(frozenset(self._C), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
"""
Save the matroid for later reloading.
Expand Down
46 changes: 4 additions & 42 deletions src/sage/matroids/dual_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def dual(self):
"""
return self._matroid

# REPRESENTATION
# representation

def _repr_(self):
"""
Return a string representation of the matroid.
Expand All @@ -404,7 +405,7 @@ def _repr_(self):
"""
return "Dual of '" + repr(self._matroid) + "'"

# COMPARISON
# comparison

def __hash__(self):
r"""
Expand Down Expand Up @@ -494,46 +495,7 @@ def __ne__(self, other):
"""
return not self == other

# COPYING, LOADING, SAVING

def __copy__(self):
"""
Create a shallow copy.

EXAMPLES::

sage: M = matroids.catalog.Vamos()
sage: N = copy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
True
"""
N = DualMatroid(self._matroid)
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
"""
Create a deep copy.

.. NOTE::

Since matroids are immutable, a shallow copy normally suffices.

EXAMPLES::

sage: M = matroids.catalog.Vamos().dual()
sage: N = deepcopy(M) # indirect doctest
sage: M == N
True
sage: M.groundset() is N.groundset()
False
"""
from copy import deepcopy
N = DualMatroid(deepcopy(self._matroid, memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N
# copying, loading, saving

def __reduce__(self):
"""
Expand Down
40 changes: 0 additions & 40 deletions src/sage/matroids/graphic_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,46 +450,6 @@ def __ne__(self, other):

# Copying, loading, saving:

def __copy__(self):
"""
Create a shallow copy.

Creating a ``GraphicMatroid`` instance will build a new graph, so
the copies have no attributes in common.

EXAMPLES::

sage: M = Matroid(graphs.PappusGraph())
sage: N = copy(M)
sage: M == N
True
sage: M._G is N._G
False
"""
N = GraphicMatroid(self._G)
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
"""
Create a deep copy.

.. NOTE::

Since matroids are immutable, a shallow copy normally suffices.

EXAMPLES::

sage: M = Matroid(graphs.PetersenGraph())
sage: N = deepcopy(M)
sage: N == M
True
"""
# The only real difference between this and __copy__() is the memo
N = GraphicMatroid(deepcopy(self._G, memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
"""
Save the matroid for later reloading.
Expand Down
26 changes: 0 additions & 26 deletions src/sage/matroids/lean_matrix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -476,32 +476,6 @@ cdef class LeanMatrix:

# Copying, loading, saving:

def __copy__(self):
"""
Return a copy of ``self``.

EXAMPLES::

sage: from sage.matroids.lean_matrix import *
sage: A = GenericMatrix(2, 5, Matrix(GF(5), [[1, 0, 1, 1, 1], [0, 1, 1, 2, 3]]))
sage: A == copy(A) # indirect doctest
True
"""
return self.copy()

def __deepcopy__(self, memo=None):
"""
Return a deep copy of ``self``.

EXAMPLES::

sage: from sage.matroids.lean_matrix import *
sage: A = GenericMatrix(2, 5, Matrix(GF(5), [[1, 0, 1, 1, 1], [0, 1, 1, 2, 3]]))
sage: A == deepcopy(A) # indirect doctest
True
"""
return self.copy()

def __reduce__(self):
"""
Save the object.
Expand Down
Loading
Loading