On Monday, August 9, 2021 at 5:07:09 PM UTC-7 Nils Bruin wrote:

> On Monday, 9 August 2021 at 15:54:03 UTC-7 Michael Jung wrote:
>
>> I like the idea proposed in https://trac.sagemath.org/ticket/29101 (as 
>> posted by Matthias). Namely, introducing the option mutable=False for 
>> the element constructor. Then your code could be rewritten to
>>
>> sum( D[V(v0+w, mutable=False)] for w in W)
>>
>> which should be fine, I guess.
>>
>
> No, that is not an improvement at all. imm(v0+w) is shorter than that.
>

"imm(v0+w)" is only shorter *after* one defines Nils' local workaround 
function "imm" and doesn't count the definition toward the length.

Another spelling would be (v0+w).copy(mutable=False), by the way.

Overall my suggestion would be to work out the intended semantics for the 
element constructor and element methods (copy and arithmetic), as I started 
in https://trac.sagemath.org/ticket/29101 -- which has not received very 
substantial comments yet. 

The discussion of semantic innovations for the parents seems like a 
distraction to me. Note that in Python, the distinction between the mutable 
and immutable version of a type is usually expressed by different types: 
list/tuple, set/frozenset; and in libraries like SymPy, 
Matrix/ImmutableMatrix, ... Changing from mutable to immutable is done by 
creating a new object of the immutable version of the type. So the very 
operation "set_immutable" of Sage elements (mutating the mutability...) is 
already out of line with Python conventions; and in fact, this is what 
makes workaround functions like Nils' "imm" necessary if one wants to use 
functional notation in generator expressions etc.

For large vectors, one may of course be concerned with the overhead of 
making the immutable copy. Standard object ownership management techniques 
would come to the rescue. For example: Make the immutability flag 3-way: 
immutable/mutable/copy-on-write. Then mutable_element.copy(mutable=False) 
makes a shallow copy only in O(1) and sets mutable_element to 
copy-on-write. Mutating a copy-on-write element would make the actual copy 
of the vector.



-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/d995bb74-3718-4962-94f6-e09bc1888bdcn%40googlegroups.com.

Reply via email to