I have this matrix which has the size of 4192*4192 ,now I want to have this matrix squared.But no matter I squared it in jupyter notebook or in julia pro,it does not to have an end(over 5 minutes).
Is it possible that matrix mulplication of such size can't be resoleved? In python it will be done in a minute.
Your matrix has type Array{Any, 2}
which means that Julia has no idea what might be inside your matrix cells. More than likely you have initialised an empty array with type Any
and then filled it up one by one.
Any
To show you what you're missing out on, here's a comparison of creating an array with Any
types vs Floats
.
using BenchmarkTools
slow_matrix = Array{Any, 2}(undef, 400, 400)
for i in 1:400
for j in 1:400
slow_matrix[i, j] = rand()
end
end
@benchmark slow_matrix * slow_matrix
This has a median running time of 3.879s on my machine. Now, if you array had a Float type you could get this:
fast_matrix = rand(400, 400)
@benchmark fast_matrix * fast_matrix
This runs in 800 μs (speedup of 4000x) because fast_matrix
has type Matrix{Float64}
and Julia will know exactly what each cell holds.
If you need an empty array with 0s that's similar to another array, you zero
:
julia> A = rand(4, 4)
4×4 Matrix{Float64}:
0.447232 0.951705 0.214183 0.97295
0.0605242 0.348032 0.882958 0.00489095
0.320847 0.993347 0.0280731 0.341089
0.14846 0.448864 0.626297 0.269931
julia> zero(A)
4×4 Matrix{Float64}:
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
This will keep the type of the original object.
If you want a completely new array of specific size, initalise it like this:
julia> A = Array{Float64, 2}(undef, 2, 2)
2×2 Matrix{Float64}:
2.32289e-314 2.28075e-314
2.2895e-314 2.28075e-314
You can set arbitrary size or dimensions this way, but you must set the cell type, in this case Float64
.
Just to show that you can indeed multiply such matrices, here are the benchmarks for a 4192x4192 matrix:
big_boy = rand(4192, 4192)
@benchmark big_boy * big_boy
Median time of 726.261ms.