-
-
Notifications
You must be signed in to change notification settings - Fork 655
Description
Problem description:
Given a vector v
with base ring R
, the constructions
w = vector(R, v)
w = vector(v, R)
w = vector(v)
all return the original vectorv
. As a consequence, all changes applied tow
are also applied tov
and vice versa.
sage: v = vector([1,2,3])
sage: w = vector(v, ZZ)
sage: w is v
True
sage: w[1] = 7
sage: v
(1, 7, 3)
Proposal:
The __call__
method of a parent object serves several purposes: In one-argument calls, (1) it is the identity (in the strong sense of Python's id
) on elements of its parent and (2) in general, a convert map, with (3) input 0
as a permissive special case, which is also the default argument of Parent.__call__
. (4) In multiple-argument calls, it is the element constructor.
Proposal: Define a general protocol for element classes with mutability which does NOT change the above but only refines it for mutable classes as follows:
-
In element classes with mutability,
_element_constructor_(x, ...)
MUST support optional argumentscopy
andmutable
. -
These arguments are allowed to have various defaulting behavior that is the most appropriate for the specific class, but there are some restrictions:
-
copy
MUST NOT default toTrue
for mutable inputsx
because (as discussed) this is not compatible with (1). -
mutable=False
andcopy=False
MUST be an error for mutable inputx
.
Moreover, arithmetic operations need to be consistent: Either always return a new element (even for trivial operations), or always return an immutable element.
We add _test_...
methods that check this protocol.
Related:
- https://groups.google.com/g/sage-devel/c/DNrbtItMVmQ
- Guide for parents with immutable elements #32353 Guide for parents with immutable elements
CC: @kliem @mjungmath @mkoeppe @tscrim @nbruin @dcoudert @mwageringel @pjbruin @kwankyu @williamstein
Component: misc
Keywords: vector, constructor, copy
Branch/Commit: u/mkoeppe/refined_protocol_for__element_constructor__in_element_classes_with_mutability @ 501e7ef
Issue created by migration from https://trac.sagemath.org/ticket/29101