From 4e776a1841d09c3ac199f50cafbaaef5bae95a05 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sat, 25 May 2024 10:56:48 -0700 Subject: [PATCH 1/4] LPDictionary: Make it safe to copy dictionaries --- src/sage/numerical/interactive_simplex_method.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/sage/numerical/interactive_simplex_method.py b/src/sage/numerical/interactive_simplex_method.py index 2da9fa85057..10156fe5b60 100644 --- a/src/sage/numerical/interactive_simplex_method.py +++ b/src/sage/numerical/interactive_simplex_method.py @@ -3910,8 +3910,17 @@ def __init__(self, A, b, c, objective_value, c = copy(c) B = vector(basic_variables) N = vector(nonbasic_variables) + # Sadly, vector does not guarantee that the result is freshly allocated + # if the input was already a vector: #29101 + if B is basic_variables: + B = copy(B) + if N is nonbasic_variables: + N = copy(N) self._AbcvBNz = [A, b, c, objective_value, B, N, polygen(ZZ, objective_name)] + def __copy__(self): + return type(self)(*self._AbcvBNz) + @staticmethod def random_element(m, n, bound=5, special_probability=0.2): r""" From d5057d6b361b05eb4b6ce8e3329bdb82a343afe6 Mon Sep 17 00:00:00 2001 From: ComboProblem <102884863+ComboProblem@users.noreply.github.com> Date: Wed, 8 May 2024 14:45:40 -0700 Subject: [PATCH 2/4] added tests for copy method and verified correct functionality --- src/sage/numerical/interactive_simplex_method.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/sage/numerical/interactive_simplex_method.py b/src/sage/numerical/interactive_simplex_method.py index 10156fe5b60..aacc75f4b98 100644 --- a/src/sage/numerical/interactive_simplex_method.py +++ b/src/sage/numerical/interactive_simplex_method.py @@ -3919,6 +3919,22 @@ def __init__(self, A, b, c, objective_value, self._AbcvBNz = [A, b, c, objective_value, B, N, polygen(ZZ, objective_name)] def __copy__(self): + r""" + TESTS:: + sage: A = ([1, 1], [3, 1]) + sage: b = (1000, 1500) + sage: c = (10, 5) + sage: P = InteractiveLPProblemStandardForm(A, b, c) + sage: D = P.initial_dictionary() + sage: D_2 = copy(D) + sage: D is D_2 + False + sage: D.enter('x1') + sage: D.leave('x3') + sage: D.update() + sage: D_2 == D + False + """ return type(self)(*self._AbcvBNz) @staticmethod From 67f9300e3cd058a825994f8175a445802b6d5530 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Thu, 9 May 2024 14:06:36 -0700 Subject: [PATCH 3/4] LPDictionary.__copy__: Add explanation, blank line before tests --- src/sage/numerical/interactive_simplex_method.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sage/numerical/interactive_simplex_method.py b/src/sage/numerical/interactive_simplex_method.py index aacc75f4b98..8884e42be14 100644 --- a/src/sage/numerical/interactive_simplex_method.py +++ b/src/sage/numerical/interactive_simplex_method.py @@ -3920,7 +3920,10 @@ def __init__(self, A, b, c, objective_value, def __copy__(self): r""" - TESTS:: + TESTS: + + Test that copies do not share state with the original:: + sage: A = ([1, 1], [3, 1]) sage: b = (1000, 1500) sage: c = (10, 5) From 2e80f9aaee1f4993104f00c8c13a5da802883a31 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Mon, 20 May 2024 20:57:20 -0700 Subject: [PATCH 4/4] src/sage/numerical/interactive_simplex_method.py: Rephrase comment --- src/sage/numerical/interactive_simplex_method.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/numerical/interactive_simplex_method.py b/src/sage/numerical/interactive_simplex_method.py index 8884e42be14..a4ce0a7a9c5 100644 --- a/src/sage/numerical/interactive_simplex_method.py +++ b/src/sage/numerical/interactive_simplex_method.py @@ -3910,8 +3910,8 @@ def __init__(self, A, b, c, objective_value, c = copy(c) B = vector(basic_variables) N = vector(nonbasic_variables) - # Sadly, vector does not guarantee that the result is freshly allocated - # if the input was already a vector: #29101 + # Issue #29101: vector does not guarantee that the result is freshly allocated + # if the input was already a vector if B is basic_variables: B = copy(B) if N is nonbasic_variables: