JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45k stars 5.42k forks source link

Tensor Contraction Multiplication #22914

Closed clintonTE closed 6 years ago

clintonTE commented 6 years ago

The multiplication behavior is unexpected relative to other languages, particularly R and Matlab. I know #3250 was closed. But I feel like this should be called out in the noteworthy differences between Julia and other languages documentation.

Also, I think the comment "If A and B are matrices, then A B denotes a matrix multiplication in Julia, equivalent to R's A %% B" in the "Noteworthy-Differences" section isn't correct in the contraction scenario.

StefanKarpinski commented 6 years ago

What are you saying is different here? Perhaps an example would help explain what you mean.

yuyichao commented 6 years ago

I think he's talking about the difference between * and .*.

StefanKarpinski commented 6 years ago

That behavior is exactly like Matlab...

clintonTE commented 6 years ago

Nevermind, works as expected:

  rect3x2::Matrix{Int} = [0 1; 2 3; 4 5]
  try
    println("Works as expected: ", rect3x2*rect3x2')
  catch err
    println(err)
  end
  println("Also works as expected: ",
    kron(rect3x2[:,1],rect3x2[:,1]') .+
    kron(rect3x2[:,2],rect3x2[:,2]'))

OUT:
Works as expected: [1 3 5; 3 13 23; 5 23 41]
Also works as expected: [1 3 5; 3 13 23; 5 23 41]

Edit: Edited code and response to show that the behavior is as expected, hence closing the issue.