On Tue, Mar 13, 2018 at 12:43 PM, Nils Bruin <
nbr...@sfu.ca> wrote:
> On Tuesday, March 13, 2018 at 11:14:41 AM UTC, Erik Bray wrote:
>>
>>
>> Doesn't Sage allow creating mutable matrices? In a case like that you
>> should mutate the object rather than go through expensive object
>> creation over and over.
>
>
> I think the scenario would be one where the discriminant of a polynomial
> gets computed by something along the lines of:
>
> f.sylvester_matrix(f.derivative()).determinant()
>
> (and now imagine that in a loop for various f; possibly of varying degrees)
That makes sense. Cases like this *could* still be optimized
somewhat, by keeping around a pool of pre-allocated matrix objects (as
long as they're of the same dimensions, base ring, etc.) But it
sounds like that's more or less what Jeroen is proposing (as long as
it's not caching matrices by value?)
CPython itself uses this in several (simpler) areas to avoid object
allocation/initialization overhead for types that are used heavily,
for example it does this for method objects and frame objects using a
"free list". This requires at the least a little hacking around with
tp_alloc, but it can be done from Cython. For something like matrices
maybe it could be done at least for small matrices of common bases.