block vector

48 views
Skip to first unread message

Clemens Heuberger

unread,
Jul 9, 2021, 12:31:46 AM7/9/21
to sage-...@googlegroups.com

Is there any recommendation on how to construct "block vectors" (in analogy to
`sage.matrix.special.block_matrix`) in readable way?

We probably do not have subdivisions of vectors in the same way as we have
subdivisions of matrices, but still: the options I see so far are

sage: v1 = vector([1, 2])
sage: v2 = vector([3, 4])
sage: iterable = [v1, v2]
sage: vector(sum((tuple(v) for v in iterable), tuple()))
(1, 2, 3, 4)

or

sage: v1 = vector([1, 2])
sage: v2 = vector([3, 4])
sage: iterable = [v1, v2]
sage: vector(block_matrix([[matrix(v) for v in iterable]]))
(1, 2, 3, 4)

None of this options seem to be very readable.

Any objections to introduce something like

def block_vector(iterable):
return vector(sum((tuple(v) for v in iterable), tuple()))

in `sage.modules.free_module_element.pyx` or other suggestions?

I am currently reviewing #21235 and such "block vectors" are constructed
repeatedly to work together with block matrices.

Thierry

unread,
Jul 9, 2021, 6:00:23 AM7/9/21
to sage-...@googlegroups.com
Hi,
You provided a wrong ticket number.

Ciao,
Thierry


>
> --
> 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+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/71a2cb48-cff8-8538-4492-714724f43e2a%40aau.at.


Clemens Heuberger

unread,
Jul 9, 2021, 6:10:56 AM7/9/21
to sage-...@googlegroups.com, Thierry
Hi,

Am 09.07.21 um 12:00 schrieb Thierry:
> On Fri, Jul 09, 2021 at 06:30:57AM +0200, Clemens Heuberger wrote:
>>
>> Is there any recommendation on how to construct "block vectors" (in analogy
>> to `sage.matrix.special.block_matrix`) in readable way?
>>
>> We probably do not have subdivisions of vectors in the same way as we have
>> subdivisions of matrices, but still: the options I see so far are
>>
>> sage: v1 = vector([1, 2])
>> sage: v2 = vector([3, 4])
>> sage: iterable = [v1, v2]
>> sage: vector(sum((tuple(v) for v in iterable), tuple()))
>> (1, 2, 3, 4)
>>
>> or
>>
>> sage: v1 = vector([1, 2])
>> sage: v2 = vector([3, 4])
>> sage: iterable = [v1, v2]
>> sage: vector(block_matrix([[matrix(v) for v in iterable]]))
>> (1, 2, 3, 4)
>>
>> None of this options seem to be very readable.
>>
>> Any objections to introduce something like
>>
>> def block_vector(iterable):
>> return vector(sum((tuple(v) for v in iterable), tuple()))
>>
>> in `sage.modules.free_module_element.pyx` or other suggestions?
>>
>> I am currently reviewing #21235 and such "block vectors" are constructed
>> repeatedly to work together with block matrices.
>
> You provided a wrong ticket number.

Sorry, I always mistype it for some reason. It is #21325.

TB

unread,
Jul 10, 2021, 8:34:42 PM7/10/21
to sage-...@googlegroups.com
On 09/07/2021 13:10, Clemens Heuberger wrote:
> Hi,
>
> Am 09.07.21 um 12:00 schrieb Thierry:
>> On Fri, Jul 09, 2021 at 06:30:57AM +0200, Clemens Heuberger wrote:
>>>
>>> Is there any recommendation on how to construct "block vectors" (in
>>> analogy
>>> to `sage.matrix.special.block_matrix`) in readable way?

You might want to use itertools' chain [1] or chain.from_iterable, for
example:

sage: from itertools import chain

sage: vector(chain([1,2,3], [4,5,6]))

(1, 2, 3, 4, 5, 6)
>>>
>>> Any objections to introduce something like
>>>
>>> def block_vector(iterable):
>>>      return vector(sum((tuple(v) for v in iterable), tuple()))
>>>
>>> in `sage.modules.free_module_element.pyx` or other suggestions?

It might be convenient to have such a function, but if it is part of the
public API, then I think it should play nice with block_matrix. That is,
will a user be surprised if they call it with "subdivide=True" argument,
and so on. Otherwise, maybe finding a different name or recommend using
chain as above, might be better.
>>>
>>> I am currently reviewing #21235 and such "block vectors" are constructed
>>> repeatedly to work together with block matrices.
>
> Sorry, I always mistype it for some reason. It is #21325.
>
I will try to comment there.

Regards,
TB

[1] - https://docs.python.org/3/library/itertools.html#itertools.chain

Clemens Heuberger

unread,
Jul 11, 2021, 3:43:28 AM7/11/21
to sage-...@googlegroups.com, TB
Hi,

Am 11.07.21 um 02:34 schrieb TB:
> On 09/07/2021 13:10, Clemens Heuberger wrote:
>> Am 09.07.21 um 12:00 schrieb Thierry:
>>> On Fri, Jul 09, 2021 at 06:30:57AM +0200, Clemens Heuberger wrote:
>>>>
>>>> Is there any recommendation on how to construct "block vectors" (in analogy
>>>> to `sage.matrix.special.block_matrix`) in readable way?
>
> You might want to use itertools' chain [1] or chain.from_iterable, for example:
>
> sage: from itertools import chain
> sage: vector(chain([1,2,3], [4,5,6]))
> (1, 2, 3, 4, 5, 6)

ok, thank you, using this seems to be a reasonable compromise between
readability and introducing another public function (with the drawback you
mentioned).

Regards,

Clemens

Reply all
Reply to author
Forward
0 new messages