-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different behavior of .* in sparse and non-sparse matrices and problem in sparse lufact() #142
Comments
Not positive, but I think you actually want to be using |
This is a bit awkward, as well as the existence of a 1-D Array type which after double transposition (should return the original object) becomes a different type. The code below shows that the "black sheep" is the original vector, g; if g was created as a 5x1 Array{Float64,2} (which g becomes in, after double transposition), instead of as a 5-element Array{Float64,1}, the inconsistency wouldn't exist. But that change (vector becomes matrix) probably should have some impact (in time of execution) in many of vector uses... :-) julia> g=[1;2;3;4;5.0] 5-element Array{Float64,1}: 1.0 2.0 3.0 4.0 5.0 julia> g==g'' false julia> sparse(g)==sparse(g'') true julia> g' 1x5 Array{Float64,2}: 1.0 2.0 3.0 4.0 5.0 julia> g'' 5x1 Array{Float64,2}: 1.0 2.0 3.0 4.0 5.0 julia> sparse(g) 5x1 sparse matrix with 5 Float64 entries: [1, 1] = 1.0 [2, 1] = 2.0 [3, 1] = 3.0 [4, 1] = 4.0 [5, 1] = 5.0 julia> sparse(g'') 5x1 sparse matrix with 5 Float64 entries: [1, 1] = 1.0 [2, 1] = 2.0 [3, 1] = 3.0 [4, 1] = 4.0 [5, 1] = 5.0 julia> (sparse(g))'==sparse(g') true julia> full(sparse(g)')==g' true I apologize if these questions have already been discussed and settled down previously (as probably they have...) but I couldn't find when searching. |
Thanks @jiahao. The funny thing is that the sparse() and full() functions have eliminated the inconsistency, that is, sparse objects are "mathematically" consistent with transposition :-) More funny stuff :-) (we can always use multiple dispatch to tackle both types...) julia> function f(v::Array{Float64,1}) println(v) end f (generic function with 1 method) julia> g 5-element Array{Float64,1}: 1.0 2.0 3.0 4.0 5.0 julia> f(g) [1.0,2.0,3.0,4.0,5.0] julia> f(g'') ERROR: `f` has no method matching f(::Array{Float64,2}) julia> f(full(sparse(g))) ERROR: `f` has no method matching f(::Array{Float64,2}) julia> function f(v::Array{Float64,2}) println(v) end f (generic function with 2 methods) julia> f(g'') [1.0 2.0 3.0 4.0 5.0] But i'm diverging... the original issue was that the v .* Msparse operation doesn't work, while v .* M does... and I don't know if it is seen as a "bug" or as a "feature" in Julia :-). |
Only because sparse vectors are currently implemented in a more Matlab-ish manner than the Julia way dense vectors work.
Bug. Sparse vectors should probably be the 1-D case of general N-D COO sparse, rather than shoehorned in as 1-column CSC matrices like they are now. The inconsistency of dense |
Oh, and the
There are cases where this is what you want, if
Line 634 of sparse/sparsematrix.jl is |
OK, thanks @tkelman. Unfortunately I don't know too much (i.e. near zero...) of Julia's innards. I browsed umfpack.jl in the sources and saw that C functions from the package are being called (with ccall()). I cannot yet juggle with Julia's types and type system.Could not go to a spot and propose a patch :-( I'm in the (spare time) process of building simulation tools for handling (hopefully) very large electric circuits, which is only viable in a sparse matrix landscape. Once upon a time I did those things in C, but now I was hoping to benefit from sparse implementation in Julia to avoid the malloc() - free() tango in C :-), use PCRE to parse input files, and escape from turtle velocity offered by Perl, Python, Ruby, etc... :-) |
@jasax I think the best place to look is around line 634 of sparse/sparsematrix,jl. That file should be mostly pure-Julia code. I think unconditionally converting |
I'm fiddling with sparse matrices and have found a few quirks :-)
What I pretend to do is use LU factorization in sparse matrices (for now in real matrices, but I hope to get it done also in sparse complex matrices)
First, in the manual, in the Linear Algebra section (http://docs.julialang.org/en/release-0.3/stdlib/linalg/) there is the relation between the L and U factors (in the case of F=lufact(A) operation they are named/extracted as F[:L] and F[:U] ) and a line and column permutation of the factorized sparse matrix A that reads as
F[:L] * F[:U] == Rs .* A[F[:p], F[:q]]
It seems to me (according to the explanation in that section) that it should read as
F[:L] * F[:U] == F[:Rs] .* A[F[:p], F[:q]]
because Rs doesn't exist per se (it has to be extracted from the object F, and so is F[:Rs]).
Aside that typo, additionally the RHS of that expression when evaluated gives an error due to a mismatch in dimensions. Indeed there is not a perfect equivalence/behavior of the .* operator (this doesn't happen with the .+ operator, for example) in real and in complex matrices, as the session below clearly shows. I end that session with an example of the failure, by applying lufact() and exemplifying the error.
I checked the code around "sparse/sparsematrix.jl:531" where the error is triggered and there is a check of conformant dimensions for matrix dot product .* (and of other operators)
(where A and B are the operands) which means that the .* operator only accepts operands with the same dimensions. I think that it is what triggers the error.
But then this configures a different behavior for .* in sparse and non sparse matrix representations.
Below is a session with julia 0.3.0 running in windows 7 in AMD 8-core Piledriver processor where the above cases are visible, with added comments. I experimented in julia 0.2.1 in Linux-Ubuntu (over Virtualbox) and the behavior of .* is the same as in windows 7; however, in 0.2.1 lufact() is not implemented for sparses, so I could not check it.
The text was updated successfully, but these errors were encountered: