-
-
Notifications
You must be signed in to change notification settings - Fork 189
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
How to handle sparse matrices when f(0) returns not zero? #2597
Comments
Yes, you need to treat the matrix as having 0 entries everywhere there isn't an explicit non-zero value. That means if we apply I think the bigger question is whether the result should be a sparse matrix or a dense matrix. It feels like sparse matrix makes more sense in that the vectorized return type is the same as the input type. But dense would be more efficient. My inclination is to have the result be sparse. The right thing to do for efficiency here is compute I don't think we should try to get fancy and allow a non-zero value as the default for unspecified values. |
imo I'd like a dense matrix, not only because it will be more efficient but it makes the API very explicit that zeroed values will become nonzero. |
AFAIK sparse matrices are most useful when dense representation would inefficient. Automatically converting to a dense matrix seems like a footgun to me. What's the purpose of supporting such "sparsity destroying" operations in the first place?
|
@nhuurre: The problem is that when you apply
because
This inconsistency is what worries me about returning a dense matrix from density inducing functions like |
The problem is that when you apply exp() to a sparse matrix, the result is dense.
And my question is: if applying exp() to a sparse matrix defeats the point of using sparse matrices, why would you want to apply exp() to a sparse matrix?
|
I have no idea where this would come up in practice, but if it did, it would be more efficient to have the argument be sparse even if the result is dense. My main objection is that it's confusing to have varying return types for elementwise operations applied to sparse matrices. |
Maybe this would be a good thing to bring up in the Stan meeting. I agree it's weird, but I think it's weird only because the scheme itself is kind of weird. Like the alt here is that we return back a |
I understand the reasons to return a dense matrix. Another alternative we should consider is to simply not allow functions where f(0) != 0 to apply elementwise to sparse matrices. |
Another alternative we should consider is to simply not allow functions where f(0) != 0 to apply elementwise to sparse matrices.
Yes, come to think of it, that would make most sense.
|
I actually posted this Q to the Eigen discorse server (a chat server Eigen devs and users talk on). They are also saying no no to my dense matrix idea as it sounds too weird so I'm fine with tossing that out.
I'm okay with this, though feel a little weird to have like |
A dense return type is definitely wrong here! The constraining transform must apply only to nonzero elements. Another question is do we allow |
Good points, @nhuurre. I agree about All of our unconstrained types are dense vectors and they'll presumably stay that way. They eventually have to concatenate into the argument to the log density function. I don't think |
I think for now in #2599 I'm going to go with Bob's route and return back a sparse matrix with full NxM size.
Maybe just not having constraints? The only one that makes sense to me is |
With no constraints, there's no problem with accidentally removing sparsity. Other constraints are consistent with sparsity, like We'll eventually want to have something like a sparse Cholesky factor or sparse covariance matrix and then implement log determinants, and multi-normal on the precision scale (where there's some hope to preserve sparsity). |
Description
I'm working on some of the testing stuff for sparse matrices and had a quick Q. For a sparse matrix, how should we handle functions where an input of 0 would return back a dense matrix? i.e. for a sparse matrix
X
callingacos(X)
where 0 returns1.57~
. Should the return be dense or should we treat the non-filled cells as just empty and ignore them, only iterating on the cells with values in them before the operation?Current Version:
v4.1.0
The text was updated successfully, but these errors were encountered: