Skip to content

Commit

Permalink
Make pinv(::AbstractVector) return a RowVector
Browse files Browse the repository at this point in the history
Also broaden from StridedVector to AbstractVector while at it and don't
employ full matrix SVD.
  • Loading branch information
martinholters committed Aug 1, 2017
1 parent 1a43098 commit 7926b11
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 0 additions & 1 deletion base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,6 @@ function pinv(A::StridedMatrix{T}) where T
tol = eps(real(float(one(T))))*maximum(size(A))
return pinv(A, tol)
end
pinv(a::StridedVector) = pinv(reshape(a, length(a), 1))
function pinv(x::Number)
xi = inv(x)
return ifelse(isfinite(xi), xi, zero(xi))
Expand Down
11 changes: 11 additions & 0 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,17 @@ function inv(A::AbstractMatrix{T}) where T
A_ldiv_B!(factorize(convert(AbstractMatrix{S}, A)), eye(S0, checksquare(A)))
end

function pinv(v::AbstractVector{T}, tol::Real=real(zero(T))) where T
res = similar(v, typeof(zero(T) / (abs2(one(T)) + abs2(one(T)))))'
den = sum(abs2, v)
if den <= tol^2
fill!(res, zero(eltype(res)))
else
res .= v' ./ den
end
return res
end

"""
\\(A, B)
Expand Down

0 comments on commit 7926b11

Please sign in to comment.