-
Notifications
You must be signed in to change notification settings - Fork 52
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
sparse arrays with algebraic, non-numerical data #46
Comments
Happy to accept a PR and see this work. |
Tried, with mixed results ;-) Doing so will forbid creating sparse vectors or matrices of types
That now fail in the creation of the vector. I don't know what to think, is |
I would be ok with forbidding creation of sparse matrices where |
See also JuliaLang/julia#19561, JuliaLang/julia#11408. |
Thanks @stevengj. I was unaware of JuliaLang/julia#11408. If there is no consensus regarding the drop of support for types T without zero(T), then maybe it is better close this (and the PR)? |
I'm in favor of restricting |
Yes, but I admit that I found the example after I ran into the "bug" in the code. The current code uses |
D4M sometimes uses sparse arrays to store non-numeric data. I found this issue after being tripped up by the fact that broadcast non-numeric data didn't work, and was a little surprised that you couldn't broadcast an I wonder whether Base should maybe define |
I get your point, but at least from the mathematical point of view it seems an awkward stretch IMHO: |
This should absolutely not be defined. The empty string is the multiplicative unit of the string semigroup which is why we have |
Right, yeah, I realized later that I was confused. Returning regular expressions seems mathematically correct but not very nice for performance (or type-stability). |
I ran into the following error today when trying to use spare arrays with non-numeric data types. I could create a SparseVector{Symbol} but couldn't use it as intended
If these types do not really support working with non-numeric values, then I would also suggest to forbid their creation |
As far as I know, the |
I wonder if we could not simply add two new parameters |
I would prefer not to add functions as type parameters. |
Is there a particular reason why functions as type parameters are to be avoided? (honest question) |
The reason that made me feel uncomfortable with this was, that it introduced a breaking change into the type system.
Not, if we defined default implementation for |
For every type? This doesn't feel right to me, as in a sense would be making |
It looks like we must either commit a "breaking change" or provoke a "type piracy" by the user, if we want to extend the element types of Thinking more about adding type parameters, and why it disgusts me adding two functions as replacements of
That would allow the use with different concepts to define
and inject this behavior into the sparse matrix using constructor |
Hi! I was redirected to this issue form JuliaLang/julia#42536 since I'm using Is there any consensus as to whether the |
An alternative solution, with other benefits is here: https://github.com/JuliaLang/julia/issues/41036 As far as I can tell https://github.com/JuliaLang/julia/issues/41036 involves no breakage, inefficiency, or pirating. In fact it clarifies the role of the unstored element. |
This is similar I think to the |
I think that although the core idea of sparse arrays for linear algebra and storage is the same, they are quite different on the semantics. Under the actual implementation, for linear algebra you want to have a additive "zero" element under a ring. But for storage sparse array, all you want to have is a "nullable" (unstored) element with no other properties attached to it. Under this circumstances the sparse array idea for linear algebra is a subset of the more general idea of a storage sparse array. One solution that come to my mind that allow the current implementation to continue working is to define a Then base The basic idea is to have something as the following abstract type AbstractStorageSparseArray{Tv, Ti, N, Unstored, IsUnstored} <: AbstractArray{Tv, N} end
abstract type AbstractSparseArray{Tv, Ti, N} <: AbstractStorageSparseArray{Tv, Ti, N, zero, iszero} end
const AbstractStorageSparseVector{Tv, Ti, Unstored, IsUnstored} = AbstractStorageSparseArray{Tv, Ti, 1, Unstored, IsUnstored}
const AbstractSparseVector{Tv, Ti} = AbstractSparseArray{Tv, Ti, 1}
struct SparseVector{Tv, Ti<:Integer} <: AbstractSparseVector{Tv, Ti}
# fields...
end And base the basic interface for sparse arrays on the new This also avoids type piracy, allowing for example to do Maybe I'm missing things, I've tried to go to read all the places where this came up, but it is really hard to find them all and remember all the points made. |
I agree with almost everything (I proposed something similar some comments above), except that I think that having a generic unstored would be useful also for algebraic elements (e.g. |
I agree with that, but the problem remains on that we cannot change type for the current What I think is that this allows to solve most of the problems without breaking anything. Also allows to be extensible, for example, someone could define a Again, this would be more elegant (and general) if we didn't make the distintion of the |
this has been fixed julia> nnz(sparse(Matrix(s)))
0 |
Still not working with Symbolics. I think it should not be closed yet. |
can you provide an MWE? |
Sorry for the pollution, I just noticed that although I had |
This is just an example, there are other methods that don't work quite as they should (e.g.
setindex!
) The problem seems to be that the code for sparse matrices is riddled with!= 0
checks instead of!iszero
checks. This seems related to JuliaLang/julia#19561, but in that case the problem was for types withoutzero(T)
for which sparse does not make real sense anyway. Maybe it would be reasonable to enforce the value type<: Number
forSparseMatrixCSC
? Are there people using it for non-number types? In that case one could fix all the!= 0
instances and see if things work...The text was updated successfully, but these errors were encountered: