Skip to content

Bug when add a numpy.array from the left #23

Description

@armando-palacio

Something strange happens when I try to sum a numpy array (as a first summand) with an object of type electrical_signal or optical_signal, like this:

x = np.array([1,2,3])
y = electrical_signal([1,2,3])
x+y

Raise an exception

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,) (3,2)

It seems that numpy's implementation of the __add__ operand somehow manages to interpret the object and translate it into an array of shape (3,2).

This problem don't appear when the first summand is an object other than an np.ndarray. For example:

from opticomlib import *
y = electrical_signal([1,2,3])

for x in [[1,2,3], (1,2,3), '1,2,3']:
    print((x+y).__repr__())

It return:

electrical_signal([2 4 6])
electrical_signal([2 4 6])
electrical_signal([2 4 6])

When electrical_signal or optical_signal objects are first summand, the addition works well in all cases (because __add__ of these objects is executed).

from opticomlib import *
import numpy as np
y = electrical_signal([1,2,3])

for x in [np.array([1,2,3]), [1,2,3], (1,2,3), '1,2,3']:
    print((y+x).__repr__())
electrical_signal([2 4 6])
electrical_signal([2 4 6])
electrical_signal([2 4 6])
electrical_signal([2 4 6])

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions