On Thursday, 5 August 2021 at 18:42:55 UTC-7 Kwankyu Lee wrote:

> I didn't know that a fresh new vector is mutable. I have never used this 
> feature. Why does it make sense that a new vector starts as mutable? Why a 
> mutable vector is necessary?
>
It can't start out immutable and then changed to mutable since ... well ... 
an immutable vector is immutable. There's a promise made that it won't 
change its content in its lifetime. We can't break that promise. So once 
you think mutable vectors should be an *option*, having them start out as 
mutable is an easy way of making them available.

Mutable vectors and matrices are very important for numpy-style efficient 
algorithms: modifying matrices in-place (and I guess vectors as well) saves 
a lot of memory allocation churn for consecutively-allocated "float" and 
"int64" arrays. For our RealField and Integer/Rational based vectors this 
is already much less the case, because generally basic arithmetic *already* 
generates memory churn. I suspect in those cases a bit of pointer copying 
may not actually be so costly in comparison, so the overhead of newly 
allocating memory for results might actually not be so significant. In 
numpy and Pandas (which, oddly DOES copy a lot), it can be essential to do 
a lot of in-place modification, because the data structures can be huge, if 
you're working with high-dimensional vectors or matrices.

In general, I am for that every output in Sage should be hashable and hence 
> immutable by default, with exceptions explicitly declared as such.
>

So ... I suspect there's a lot of code in sage where immutable vectors 
wouldn't hurt, because a lot of operations will rely on the default 
arithmetic, which creates new vectors for results. However, there are some 
places where it would be a problem. Indeed, I think it would be quite 
reasonable to ask people to construct a vector explicitly as mutable if 
they need it. Perhaps an optional argument to _element_constructor_ 
"mutable" that is by default False?

This could break a LOT of code. It would fail with sensible error messages 
(hopefully), but it might be a great pain to trace down *where* a vector 
gets created from the location where a mutation attempt is made. To avoid 
this breakage, we could make the default behaviour a property of the 
parent. Then we can maintain the old behaviour by default, while allowing 
to get "default immutable" behaviour at the cost of having to specify it 
explicitly on the parent.

In terms of history: https://trac.sagemath.org/ticket/25509 introduced a 
"immutable" argument to "vector" (with a False default of course). I think 
there may be other tickets concerning this issue.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/f7359ad2-bfe9-491a-a63c-08fc4831066en%40googlegroups.com.

Reply via email to