On Saturday, 7 August 2021 at 01:58:42 UTC-7 Markus Wageringel wrote:

> Generally, I would be in favor of having an option for immutable vectors 
> by default, as I had similar uses before. Usually, I just convert 
> everything to tuples.
>
> However, I am not sure if having an additional flag on the parent is worth 
> the extra burden on maintenance. Already the sparsity flag is not always 
> handled correctly (#29359, #29360), immutability may be even easier to miss.
>

It's also less important. As long as the usual arithmetic within the parent 
respects it, it's fine. 
 

> In my own code, I tend to construct vectors by `vector(ring, [..])`, which 
> relies on the uniqueness of the parent and will not preserve immutability 
> of vectors. Though, it is fair to say that this code is already broken as 
> it does not preserve sparsity either.
>

And it's inefficient if you have the parent lying around anyway! The globar 
constructor has to mangle the construction parameters, use them as a key to 
look up in the global cache, and then get you the parent. If you have the 
desired parent already in hand, none of that is necessary! So I'd say 
that's just a bad habit.

Whether "vector" should by default choose a vector space with default 
mutable or immutable vectors is another matter (it's safer to have them 
immutable unless the user really wants them mutable, so that would be the 
sensible default, but it would go against legacy, so may be harder).

Another point is coercion: if we have

V = VectorSpace(GF(5),3,immutable_vectors = True)
W = VectorSpace(GF(5),3,immutable_vectors = False)

what should V.gen(1)+W.gen(1) be? Does V get priority?

Otherwise, I think having the flag will be a life-saver in various 
interactive scenarios and low-impact outside. So I think it should be 
fairly uncontroversial to implement it.

Regarding the performance discussion, (although this was not the question) 
using vectors also has the potential of being much slower than using numpy 
arrays directly.
>
>
>     sage: a = vector(CDF, [CDF(1, i/(i+1)) for i in range(10^6)])
>     sage: %time c1 = a.conjugate()
>     Wall time: 4.39 s
>     sage: %time c2 = a.parent()(a.numpy().conj())
>     Wall time: 11.8 ms
>

Yes, while there are some valid vector-mutating algorithms, I think they 
would usually arise within linear algebra modules. On user level, a mutable 
vector is usually just an array and would be served perfectly by a numpy 
(or python) array. 

-- 
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/ab3a82dc-4150-446c-8934-ec8a32d885ben%40googlegroups.com.

Reply via email to