On Mon, May 30, 2022 at 02:31:35PM -0000, fjwillemsen--- via Python-ideas wrote:

> In Python, array multiplication is quite useful to repeat an existing 
> array, e.g. [1,2,3] * 2 becomes [1,2,3,4,5,6].

It certainly does not do that.

    >>> [1, 2, 3]*2
    [1, 2, 3, 1, 2, 3]

Also, that's a list, not an array. Python does have arrays, from the 
`array` module. And of course there are numpy arrays, which behave 
completely differently, performing scalar multiplication rather than 
sequence replication:

    >>> from numpy import array
    >>> array([1, 2, 3])*2
    array([2, 4, 6])


> However, operations such as [numpy array] * -1 are very common to get 
> the inverse of an array of numbers.

If that is common, there's a lot of buggy code out there! *wink*

Multiplying a numpy array by the scalar -1 performs scalar 
multiplication, same as any other scalar. To get the inverse of a numpy 
array, you need to use numpy.linalg.inv:

    >>> import numpy.linalg
    >>> arr = array([[1, 2], [3, 4]])
    >>> numpy.linalg.inv(arr)
    array([[-2. ,  1. ],
           [ 1.5, -0.5]])


> The confusion here stems from the lack of type checking: while the 
> programmer should check whether the array is a NumPy array or a Python 
> array, this is not always done, giving rise to difficult to trace 
> cases where [1,2,3] * -1 yields [] instead of [-1,-2,-3].

This confusion has nothing to do with multiplication by -1. As the 
earlier example above shows, scalar multiplication on a numpy array 
and sequence replication on a list always give different results, not 
just for -1. (The only exception is multiplication by 1.)

I am afraid that this invalidates your argument from Numpy arrays. It 
simply isn't credible that people are accidentally passing lists instead 
of numpy arrays, and then getting surprised by the result **only** when 
multiplying by a negative value. Its not just negatives that are 
different.


> I can not think of good reasons why Python array multiplication should 
> not throw an error for negative multipliers, because it is meaningless 
> to have array multiplication by negative value in the way it is 
> intended in Python.

Its not meaningless, it is far more *useful* than an unnecessary and 
annoying exception would be. For example, here is how I might pad a list 
to some minimum length with zeroes:

    mylist.extend([0]*(minlength - len(mylist)))


If this was 1991 and Python was brand new, then the behaviour of 
sequence replication for negative values would be up for debate. But 
Python is 31 years old and there is 31 years worth of code that relies 
on this behaviour, so we would need **extraordinarily strong** reasons 
to break all that code.

Not an extraordinarily weak argument based on confusion between numpy 
array scalar multiplication and list replication. Sorry to be blunt.


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4OVPWC35WCGLNKSACH5PWWM77XR7425P/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to