jjjkkkjjj / Matft

Numpy-like library in swift. (Multi-dimensional Array, ndarray, matrix and vector library)
BSD 3-Clause "New" or "Revised" License
134 stars 21 forks source link

Invalid broadcasting #12

Closed alwc closed 4 years ago

alwc commented 4 years ago

Hi @jjjkkkjjj

I'm trying to do the following broadcasting, but it is considered as error in Matft:

let a = Matft.arange(start: 1, to: 7, by: 1, shape: [3, 2])
let b = Matft.arange(start: 1, to: 5, by: 1, shape: [2, 1, 2])
print(a - b)

Error message:

Fatal error: could not broadcast from shape 3, [2, 1, 2] into shape 3, [1, 3, 2]: file /Users/alwc/Library/Developer/Xcode/DerivedData/testnpy-afxfserzcwmbfdfgfaszjulxzrwa/SourcePackages/checkouts/Matft/Sources/Matft/core/function/conversion.swift, line 290

2020-07-21 19:05:02.261713+0800 testnpy[21523:5245927] Fatal error: could not broadcast from shape 3, [2, 1, 2] into shape 3, [1, 3, 2]: file /Users/alwc/Library/Developer/Xcode/DerivedData/testnpy-afxfserzcwmbfdfgfaszjulxzrwa/SourcePackages/checkouts/Matft/Sources/Matft/core/function/conversion.swift, line 290

In Python this is legal. For example,

x = np.random.randint(10, size=(3, 2))
y = np.random.randint(10, size=(2, 1, 2))

# Returns a shape (2, 3, 2) nd-array
print(x - y)      
jjjkkkjjj commented 4 years ago

@alwc Good catch! I misunderstood broadcasting rule... I'll modify this bug ASAP, please wait for a moment!