Groups 68 of 99+ julia-users › Array comprehension cannot detect type? 8 posts by 4 authors rizal zaini 11/9/15 Hi all I want to create matrix containing dot products julia> X = rand(5,5) 5x5 Array{Float64,2}: 0.274799 0.564619 0.915595 0.341059 0.57361 0.0806822 0.293939 0.379279 0.608582 0.652441 0.264678 0.532751 0.289088 0.457759 0.492777 0.33856 0.495587 0.499099 0.548237 0.402534 0.686305 0.168385 0.8322 0.605532 0.518693 julia> K = [dot(X[:,i], X[:,j]) for i=1:5, j=1:5] 5x5 Array{Any,2}: 0.737716 0.603229 1.09884 0.865174 0.832958 0.603229 0.962978 1.16994 0.988988 1.06501 1.09884 1.16994 2.00739 1.45298 1.54767 0.865174 0.988988 1.45298 1.36347 1.35304 0.832958 1.06501 1.54767 1.35304 1.42861 I expect to get an Array{Float64,2}, but what I got is Array{Any,2}. How to get Array{Float64,2} using Array comprehension? Thanks before Rizal rizal zaini 11/9/15 My concern is also on parallel code: julia> K = @parallel [dot(X[:,i], X[:,j]) for i=1:5, j=1:5] 5x5 DArray{Any,2,Array{Any,2}}: 1.35134 1.36439 1.28474 1.13181 1.07863 1.36439 1.40077 1.25708 1.22365 1.08332 1.28474 1.25708 1.54494 0.95311 1.15779 1.13181 1.22365 0.95311 1.70348 1.30076 1.07863 1.08332 1.15779 1.30076 1.34577 I expect to get a DArray{Float64,2,Array{Float64,2}} rather than DArray{Any,2,Array{Any,2}} Please help. Rizal - show quoted text - Kristoffer Carlsson 11/9/15 Don't run the code in global scope or alternatively be explicit about the type you want, for example Float64[....] - show quoted text - Glen O 11/9/15 As far as I'm aware, there are two nice ways to tell the comprehension the type that is going to be returned. Option 1: Tell it the type of the comprehension: K = Float64[dot(X[:,i], X[:,j]) for i=1:5, j=1:5] Option 2: Assert the type of the return of "dot" K = [dot(X[:,i],X[:,j])::Float64 for i=1:5, j=1:5] As far as I can tell, they'll do the same thing. - show quoted text - rizal zaini 11/9/15 When I put that code inside a function, I got the same behavior. For single process, K = Float64[dot(X[:,i], X[:,j]) for i=1:5, j=1:5] does the job. But for parallel I got error: "ERROR: malformed @parallel loop" Rizal - show quoted text - John Gibson 11/9/15 I'm no Julia expert, but I'm pretty certain this fall under the category "If you Julia to optimize, put your code in a function." gibson@timaeus$ ~/packages/julia-0.4.0/bin/julia _ _ _ _(_)_ | A fresh approach to technical computing (_) | (_) (_) | Documentation: http://docs.julialang.org _ _ _| |_ __ _ | Type "?help" for help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 0.4.0 (2015-10-08 06:20 UTC) _/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release |__/ | x86_64-unknown-linux-gnu julia> X = rand(2,2) 2x2 Array{Float64,2}: 0.713809 0.0650767 0.916874 0.111741 julia> K = [dot(X[:,i], X[:,j]) for i=1:2, j=1:2] 2x2 Array{Any,2}: 1.35018 0.148905 0.148905 0.0167211 julia> f(X) = [dot(X[:,i], X[:,j]) for i=1:2, j=1:2] f (generic function with 1 method) julia> f(X) 2x2 Array{Float64,2}: 1.35018 0.148905 0.148905 0.0167211 John - show quoted text - Glen O 11/9/15 Does the assertion form work any better? K=[dot(X[:,i],X[:,j])::Float64 for i=1:5, j=1:5] - show quoted text - rizal zaini 11/9/15 It does work. Thank you so much Glen. Rizal - show quoted text -