10

I have defined the following variable

julia> X = (1:10) * ones(1,10)

which defines a matrix with each row equals to the same number and the numbers in the column increasing from 1 to 10 by 1. I want to know which method used Julia for the function *. How can I ask that to Julia?

Gary
  • 13,303
  • 18
  • 49
  • 71
dapias
  • 2,512
  • 3
  • 15
  • 23
  • That's [matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication) – Daniel Feb 01 '16 at 02:01
  • I know it @Daniel, just I want that Julia tells me that I am using the method that is defined like `*(UnitRange{Int64}, Array{Float64,2})` – dapias Feb 01 '16 at 02:19
  • 1
    I'm not very good at interpreting the output, but `@code_warntype (1:10) * ones(1,10)` might tell you what you need to know. As near as I can tell (and I emphasize this is not something I know much about), ultimately `*` appears to be calling `Base.LinAlg.generic_matmatmul!` on two input `Array{Float64, 2}`. Hopefully someone a bit more knowledgeable can chip in. – Colin T Bowers Feb 01 '16 at 03:13

1 Answers1

12

@which is what you are looking for:

@which (1:10) * ones(1, 10)
# *(A::AbstractArray{T,1}, B::AbstractArray{T,2}) at linalg/matmul.jl:89

in Jupyter it will also hyperlink to the corresponding line of code where the method is defined in Julia's GitHub.

amrods
  • 2,029
  • 1
  • 17
  • 28
  • 1
    Interesting. Is there a way to iteratively dig deeper? ie, the line referenced in your answer calls `*` again, but on two abstract matrices. Is there a way to get `@which` to also tell us which method was called next? (presumably in this case it was `*` for two `Matrix{Float64}`?) – Colin T Bowers Feb 01 '16 at 03:19
  • 1
    @colin No that I know of but I'm sure there is a metaprogramming way of doing it. – amrods Feb 01 '16 at 03:32
  • 3
    @ColinTBowers if you got that line of code, you can check the next-`*` by `which(*, (Matrix{Float64},Matrix{Float64}))` manually. – Gnimuc Feb 01 '16 at 03:37
  • is this tracing nightmare a drawback of multiple dispatch? :p – amrods Feb 01 '16 at 03:43
  • @amrods i guess what Colin want is just the `Jump to Definition` functionality in most of modern IDEs which can be used to dig out more info about `*` iteratively in this specific case. – Gnimuc Feb 01 '16 at 03:44
  • 1
    @GnimucK. "`which(*, (Matrix{Float64},Matrix{Float64}))`" -> yep, that does it. As amrods suggests, there's probably a neat little function that can be written here to iterate up the chain. Thanks for responding (both of you). – Colin T Bowers Feb 01 '16 at 03:58
  • When tracing code this way, I assign new names to my variables that match the arguments of the function found by `@which`. Then I copy/paste lines of code from that function into the REPL until I get to the next line I want to trace, and use `@which` again. Saves you from having to be sure about the types. – tholy Feb 01 '16 at 14:29