-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
check lengths in covector-vector products #36679
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
Changes from all commits
08f2f2a
2fc81f7
8639f6b
6a8ce50
5a6b631
f840da9
27a09c8
f95c89b
5851198
3052e44
ed9186d
5a3c14d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -267,10 +267,23 @@ Broadcast.broadcast_preserving_zero_d(f, tvs::Union{Number,TransposeAbsVec}...) | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## multiplication * | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
lu = length(u) | ||||||||||||||||||||||||||||||
if lu != length(v) | ||||||||||||||||||||||||||||||
throw(DimensionMismatch("first array has length $(lu) which does not match the length of the second, $(length(v)).")) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
if lu == 0 | ||||||||||||||||||||||||||||||
zero(eltype(u)) * zero(eltype(v)) | ||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||
sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
Comment on lines
+275
to
+279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are manually computing the zero length case anyway, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so this is actually problematic because it makes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to undo the suggested change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, this reads basically exactly (up to the julia/stdlib/LinearAlgebra/src/generic.jl Lines 886 to 899 in 971e769
which I think is good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Actually I feel like I did a similar suggestion before here that was ended up useless exactly due to this... I should have learned from a mistake. |
||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Adjoint/Transpose-vector * vector | ||||||||||||||||||||||||||||||
*(u::AdjointAbsVec{T}, v::AbstractVector{T}) where {T<:Number} = dot(u.parent, v) | ||||||||||||||||||||||||||||||
*(u::AdjointAbsVec{<:Number}, v::AbstractVector{<:Number}) = dot(u.parent, v) | ||||||||||||||||||||||||||||||
*(u::TransposeAbsVec{T}, v::AbstractVector{T}) where {T<:Real} = dot(u.parent, v) | ||||||||||||||||||||||||||||||
Comment on lines
+283
to
284
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding that change in. Any reason not to do the equivalent thing in 284 as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I merged them, and now it looks very clean, actually. 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's causing method ambiguities that I'm not in the mood now of resolving. I've reverted that. I think the |
||||||||||||||||||||||||||||||
*(u::AdjOrTransAbsVec, v::AbstractVector) = sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
*(u::AdjOrTransAbsVec, v::AbstractVector) = _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# vector * Adjoint/Transpose-vector | ||||||||||||||||||||||||||||||
*(u::AbstractVector, v::AdjOrTransAbsVec) = broadcast(*, u, v) | ||||||||||||||||||||||||||||||
|
@@ -281,14 +294,10 @@ Broadcast.broadcast_preserving_zero_d(f, tvs::Union{Number,TransposeAbsVec}...) | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# AdjOrTransAbsVec{<:Any,<:AdjOrTransAbsVec} is a lazy conj vectors | ||||||||||||||||||||||||||||||
# We need to expand the combinations to avoid ambiguities | ||||||||||||||||||||||||||||||
(*)(u::TransposeAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) = | ||||||||||||||||||||||||||||||
sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
(*)(u::AdjointAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) = | ||||||||||||||||||||||||||||||
sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
(*)(u::TransposeAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) = | ||||||||||||||||||||||||||||||
sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
(*)(u::AdjointAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) = | ||||||||||||||||||||||||||||||
sum(uu*vv for (uu, vv) in zip(u, v)) | ||||||||||||||||||||||||||||||
(*)(u::TransposeAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) = _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
(*)(u::AdjointAbsVec, v::AdjointAbsVec{<:Any,<:TransposeAbsVec}) = _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
(*)(u::TransposeAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) = _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
(*)(u::AdjointAbsVec, v::TransposeAbsVec{<:Any,<:AdjointAbsVec}) = _dot_nonrecursive(u, v) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## pseudoinversion | ||||||||||||||||||||||||||||||
pinv(v::AdjointAbsVec, tol::Real = 0) = pinv(v.parent, tol).parent | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re OffsetArrays support, I think it's enough to check
axes
like this?I think this is a kind of error that would have thrown if we use here
promote_shape(u', v)
(with a better error message). Since something likeeachindex(a, b)
already throws an error with something likeeachindex(1:3, OffsetArray(1:3, -2:0))
simply based onaxes
, maybe it makes sense to checkaxes
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we could just do
However, I'm still not actually convinced it's even desirable to throw an error here. I'm also not convinced we shouldn't throw an error, but I will point out that that in the current state of affairs
transpose(u::OffsetVector) * v::Vector
currently has the same behaviour as the implementation in this PR (i.e. no error).I feel like it'd be good at least for 1.5 since it's so near, to just follow the lead of
transpose(u) * v
rather than being over eager about throwing errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm rather inclined towards throwing an error, as in the matrix-vector case. If the user really wants to contract an
o::OffsetArray
with av::Vector
, it is not complicated to doOffsetArrays.no_offset_view(o)' * v
. Perhapsno_offset_view
should be exported.