Skip to content
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

Fix of bug in gelsd! for complex arrays and some more. #1427

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ isequal(x::Real, z::Complex) = real_valued(z) && isequal(real(z),x)

hash(z::Complex) = (r = hash(real(z)); real_valued(z) ? r : bitmix(r,hash(imag(z))))

eps(z::Complex) = eps(abs(z))
eps(z::Complex) = eps(real(z))
eps(::Type{Complex64}) = eps(Float32)
eps(::Type{Complex128}) = eps(Float64)

conj(z::Complex) = complex(real(z),-imag(z))
abs(z::Complex) = hypot(real(z), imag(z))
Expand Down
53 changes: 37 additions & 16 deletions base/lapack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,30 +383,51 @@ for (gels, gelsd, gesv, getrs, getri, elty) in
# * .. Array Arguments ..
# INTEGER IWORK( * )
# DOUBLE PRECISION A( LDA, * ), B( LDB, * ), S( * ), WORK( * )
function gelsd!(A::StridedMatrix{$elty}, B::StridedVecOrMat{$elty})
function gelsd!(A::StridedMatrix{$elty}, B::StridedVecOrMat{$elty}, rcond::FloatingPoint)
Lapack.chkstride1(A, B)
m, n = size(A)
m, n = size(A)
if size(B,1) != m; throw(Lapack.LapackDimMisMatch("gelsd!")); end
s = Array($elty, min(m, n))
rcond = eps(Float32)
rnk = Array(Int32, 1)
info = Array(Int32, 1)
work = Array($elty, 1)
lwork = int32(-1)
iwork = Array(Int32, 1)
Rtyp = typeof(real(A[1]))
s = Array(Rtyp, min(m, n))
cmplx = iscomplex(A)
if cmplx
rwork = Array(Rtyp, 1)
end
rnk = Array(Int32, 1)
info = Array(Int32, 1)
work = Array($elty, 1)
lwork = int32(-1)
iwork = Array(Int32, 1)
for i in 1:2
ccall(dlsym(Base.liblapack, $(string(gelsd))), Void,
(Ptr{Int32}, Ptr{Int32}, Ptr{Int32},
Ptr{$elty}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
Ptr{$elty}, Ptr{$elty}, Ptr{Int32}, Ptr{$elty},
Ptr{Int32}, Ptr{Int32}, Ptr{Int32}),
&m, &n, &size(B,2), A, &max(1,stride(A,2)),
B, &max(1,stride(B,2)), s, &rcond, rnk, work, &lwork, iwork, info)
if cmplx
ccall(dlsym(Base.liblapack, $(string(gelsd))), Void,
(Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{$elty},
Ptr{Int32}, Ptr{$elty}, Ptr{Int32}, Ptr{Rtyp},
Ptr{Rtyp}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
Ptr{Rtyp}, Ptr{Int32}, Ptr{Int32}),
&m, &n, &size(B,2), A,
&max(1,stride(A,2)), B, &max(1,stride(B,2)), s,
&rcond, rnk, work, &lwork,
rwork, iwork, info)
else
ccall(dlsym(Base.liblapack, $(string(gelsd))), Void,
(Ptr{Int32}, Ptr{Int32}, Ptr{Int32}, Ptr{$elty},
Ptr{Int32}, Ptr{$elty}, Ptr{Int32}, Ptr{$elty},
Ptr{$elty}, Ptr{Int32}, Ptr{$elty}, Ptr{Int32},
Ptr{Int32}, Ptr{Int32}),
&m, &n, &size(B,2), A,
&max(1,stride(A,2)), B, &max(1,stride(B,2)), s,
&rcond, rnk, work, &lwork,
iwork, info)
end
if info[1] != 0 throw(LapackException(info[1])) end
if lwork < 0
lwork = int32(real(work[1]))
work = Array($elty, lwork)
iwork = Array(Int32, iwork[1])
if cmplx
rwork = Array(Rtyp, int(rwork[1]))
end
end
end
isa(B, Vector) ? B[1:n] : B[1:n,:], rnk[1]
Expand Down
6 changes: 4 additions & 2 deletions base/linalg_dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,14 @@ function (\){T<:LapackType}(A::StridedMatrix{T}, B::StridedVecOrMat{T})
if ishermitian(A) return Lapack.sysv!('U', Acopy, X)[1] end
return Lapack.gesv!(Acopy, X)[3]
end
Lapack.gelsd!(Acopy, X)[1]
Lapack.gelsd!(Acopy, X, -1.0)[1]
end

(\){T1<:LapackType, T2<:Real}(A::StridedMatrix{T1}, B::StridedVecOrMat{T2}) = (\)(A, convert(Array{T1}, B))
(\){T1<:Real, T2<:LapackType}(A::StridedMatrix{T1}, B::StridedVecOrMat{T2}) = (\)(convert(Array{T2}, A), B)
(\){T1<:Real, T2<:Real}(A::StridedMatrix{T1}, B::StridedVecOrMat{T2}) = (\)(float64(A), float64(B))
(\){T1<:Complex, T2<:Complex}(A::StridedMatrix{T1}, B::StridedVecOrMat{T2}) = (\)(convert(Array{Complex128}, A), convert(Array{Complex128}, B))

# TODO: use *gels transpose argument
(/)(A::StridedVecOrMat, B::StridedVecOrMat) = (B' \ A')'

##TODO: Add methods for rank(A::QRP{T}) and adjust the (\) method accordingly
Expand Down
22 changes: 11 additions & 11 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ isequal(x,y) = is(x,y)
> (x,y) = y < x
<=(x,y) = !(y < x)
>=(x,y) = (y <= x)
.> (x,y) = y.<x
.>=(x,y) = y.<=x

# these definitions allow Number types to implement
# == and < instead of isequal and isless, which is more idiomatic:
Expand Down Expand Up @@ -52,22 +54,20 @@ for op = (:+, :*, :&, :|, :$, :min, :max)
end
end

\(x,y) = y/x
\(x::Number,y::Number) = y/x

# .<op> defaults to <op>
./(x,y) = x/y
.\(x,y) = y./x
.*(x,y) = x*y
.^(x,y) = x^y
./(x::Number,y::Number) = x/y
.\(x::Number,y::Number) = y./x
.*(x::Number,y::Number) = x*y
.^(x::Number,y::Number) = x^y
.+(x,y) = x+y
.-(x,y) = x-y

.==(x,y) = x==y
.!=(x,y) = x!=y
.< (x,y) = x<y
.> (x,y) = y.<x
.<=(x,y) = x<=y
.>=(x,y) = y.<=x
.==(x::Number,y::Number) = x==y
.!=(x::Number,y::Number) = x!=y
.< (x::Real,y::Real) = x<y
.<=(x::Real,y::Real) = x<=y

# core << >> and >>> takes Int32 as second arg
<<(x,y::Integer) = x << convert(Int32,y)
Expand Down