https://kotlinlang.org logo
#mathematics
Title
# mathematics
j

Jason Thomas

03/10/2024, 2:01 AM
I'm trying to do matrix multiplication in multik (https://github.com/Kotlin/multik). It seems like I should get a 2 X 3 matrix, but I get an error message that says "Array shapes don't match: [2, 1] != [1, 3]". Can anyone say if this is a bug or am I doing something horribly wrong?
Copy code
val a = mk.ndarray(floatArrayOf(1.0f, 2.0f), 2, 1)
val b = mk.ndarray(floatArrayOf(1.0f, 2.0f, 3.0f), 1, 3)

val result = a * b
println(result)
a

altavir

03/10/2024, 6:52 AM
They are not matrices, they are nd arrays. Multik works in numpy paradigm where multiplication is element by element. You need to use
dot
operation to dot operation for matrix product.
There are three different ways to separate element-by-element behavior from matrix behavior: 1. Use different operators on the same structures. 2. Use different types (for example wrap 2d ndarray in a Matrix type). 3. Use different context for those operations. Multik uses variant 1.
j

Jason Thomas

03/10/2024, 11:44 AM
That makes sense, thanks a lot for the help.