Vector power operator not implemented

44 views
Skip to first unread message

erentar2002

unread,
Jun 11, 2022, 2:09:45 PM6/11/22
to sage-...@googlegroups.com
sage -c "vector([1,2,3])^2" returns with a NotImplementedError, when in
other mathematics software, the same expression will operate element-wise.

python:
>>> import numpy; numpy.array([1,2,3])**2
array([1, 4, 9])

octave:
octave:1> [1,2,3].^2
ans = 1   4   9

mathematica:
In[1]:= {1,2,3}^2
Out[1]= {1, 4, 9}

Is there a reason this is not the way sage also implements it?

John H Palmieri

unread,
Jun 11, 2022, 5:04:35 PM6/11/22
to sage-devel
In my opinion it's not very mathematical. If we implement powers for vectors like this (and products, too, as some of these other packages do), then for consistency, perhaps products and powers of matrices should behave the same way? Ordinary matrix multiplication is much more common in mathematics than element-wise multiplication, and I think it would be awkward to have a separate operation for ordinary matrix multiplication. Anyway, it is easy to accomplish the same task:

    sage: v = vector([1,2,3])
    sage: vector([a**2 for a in v])
    (1, 4, 9)

or

    sage: v.apply_map(lambda x: x^2)
    (1, 4, 9)

Nils Bruin

unread,
Jun 12, 2022, 10:55:36 AM6/12/22
to sage-devel
On Saturday, 11 June 2022 at 11:09:45 UTC-7 erent...@gmail.com wrote:
sage -c "vector([1,2,3])^2" returns with a NotImplementedError, when in
other mathematics software, the same expression will operate element-wise.

python:
>>> import numpy; numpy.array([1,2,3])**2
array([1, 4, 9])

This perhaps illustrates it best:
   numpy.matrix([1,2,3])**2
doesn't work either. A sage "vector" is a lot more like a 1-dimensional matrix, where multiplication is reserved for ... matrix multiplication! Since exponentiation is strongly tied to iterated multiplication, you end up only being able to take a power of a square matrix.

If you want array semantics, use arrays (there multiplication is element-wise, so element-wise exponentiation makes sense as well).
Reply all
Reply to author
Forward
0 new messages