Skip to content

Commit

Permalink
Merge pull request #25148 from Sacha0/despecial
Browse files Browse the repository at this point in the history
update context-independent and remove context-dependent lowering of '
  • Loading branch information
Sacha0 authored Dec 19, 2017
2 parents ea3639e + 6e3c1ce commit 1319567
Show file tree
Hide file tree
Showing 49 changed files with 141 additions and 168 deletions.
2 changes: 2 additions & 0 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ macro _noinline_meta()
Expr(:meta, :noinline)
end

function postfixapostrophize end

struct BoundsError <: Exception
a::Any
i::Any
Expand Down
4 changes: 2 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,7 @@ end
```jldoctest
julia> v = [1; im];
julia> vc = v';
julia> vc = RowVector(v);
julia> norm(vc, 1)
1.0
Expand Down Expand Up @@ -3103,7 +3103,7 @@ end
*(transA::Transpose{<:Any,<:AbstractTriangular}, transrowvec::Transpose{<:Any,<:RowVector}) = transA * rvtranspose(transrowvec.parent)
*(rowvec::RowVector, adjA::Adjoint{<:Any,<:AbstractTriangular}) = rvadjoint(adjA.parent * rvadjoint(rowvec))
*(A::AbstractTriangular, adjrowvec::Adjoint{<:Any,<:RowVector}) = A * rvadjoint(adjrowvec.parent)
*(adjA::Adjoint{<:Any,<:AbstractTriangular}, adjrowvec::Adjoint{<:Any,<:RowVector}) = adjA.parent' * rvadjoint(adjrowvec.parent)
*(adjA::Adjoint{<:Any,<:AbstractTriangular}, adjrowvec::Adjoint{<:Any,<:RowVector}) = adjA * rvadjoint(adjrowvec.parent)
\(::Union{UpperTriangular,LowerTriangular}, ::RowVector) = throw(DimensionMismatch("Cannot left-divide matrix by transposed vector"))
\(::Union{UnitUpperTriangular,UnitLowerTriangular}, ::RowVector) = throw(DimensionMismatch("Cannot left-divide matrix by transposed vector"))
\(::Adjoint{<:Any,<:Union{UpperTriangular,LowerTriangular}}, ::RowVector) = throw(DimensionMismatch("Cannot left-divide matrix by transposed vector"))
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function ldiv!(adjA::Adjoint{<:Any,<:Union{Bidiagonal,AbstractTriangular}}, B::A
tmp = similar(B,size(B,1))
n = size(B, 1)
if mA != n
throw(DimensionMismatch("size of A' is ($mA,$nA), corresponding dimension of B is $n"))
throw(DimensionMismatch("size of adjoint of A is ($mA,$nA), corresponding dimension of B is $n"))
end
for i = 1:size(B,2)
copyto!(tmp, 1, B, (i - 1)*n + 1, n)
Expand All @@ -537,7 +537,7 @@ function ldiv!(transA::Transpose{<:Any,<:Union{Bidiagonal,AbstractTriangular}},
tmp = similar(B,size(B,1))
n = size(B, 1)
if mA != n
throw(DimensionMismatch("size of A' is ($mA,$nA), corresponding dimension of B is $n"))
throw(DimensionMismatch("size of transpose of A is ($mA,$nA), corresponding dimension of B is $n"))
end
for i = 1:size(B,2)
copyto!(tmp, 1, B, (i - 1)*n + 1, n)
Expand Down
10 changes: 5 additions & 5 deletions base/linalg/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function _chol!(A::AbstractMatrix, ::Type{UpperTriangular})
return UpperTriangular(A), info
end
A[k,k] = Akk
AkkInv = inv(Akk')
AkkInv = inv(adjoint(Akk))
for j = k + 1:n
for i = 1:k - 1
A[k,j] -= A[i,k]'A[i,j]
Expand Down Expand Up @@ -381,14 +381,14 @@ size(C::Union{Cholesky, CholeskyPivoted}) = size(C.factors)
size(C::Union{Cholesky, CholeskyPivoted}, d::Integer) = size(C.factors, d)

function getindex(C::Cholesky, d::Symbol)
d == :U && return UpperTriangular(Symbol(C.uplo) == d ? C.factors : C.factors')
d == :L && return LowerTriangular(Symbol(C.uplo) == d ? C.factors : C.factors')
d == :U && return UpperTriangular(Symbol(C.uplo) == d ? C.factors : adjoint(C.factors))
d == :L && return LowerTriangular(Symbol(C.uplo) == d ? C.factors : adjoint(C.factors))
d == :UL && return Symbol(C.uplo) == :U ? UpperTriangular(C.factors) : LowerTriangular(C.factors)
throw(KeyError(d))
end
function getindex(C::CholeskyPivoted{T}, d::Symbol) where T<:BlasFloat
d == :U && return UpperTriangular(Symbol(C.uplo) == d ? C.factors : C.factors')
d == :L && return LowerTriangular(Symbol(C.uplo) == d ? C.factors : C.factors')
d == :U && return UpperTriangular(Symbol(C.uplo) == d ? C.factors : adjoint(C.factors))
d == :L && return LowerTriangular(Symbol(C.uplo) == d ? C.factors : adjoint(C.factors))
d == :p && return C.piv
if d == :P
n = size(C, 1)
Expand Down
4 changes: 0 additions & 4 deletions base/linalg/conjarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ other arrays, the `ConjArray` constructor can be used directly.
# Examples
```jldoctest
julia> [1+im, 1-im]'
1×2 RowVector{Complex{Int64},ConjArray{Complex{Int64},1,Array{Complex{Int64},1}}}:
1-1im 1+1im
julia> ConjArray([1+im 0; 0 1-im])
2×2 ConjArray{Complex{Int64},2,Array{Complex{Int64},2}}:
1-1im 0+0im
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ function nullspace(A::StridedMatrix{T}) where T
(m == 0 || n == 0) && return Matrix{T}(I, n, n)
SVD = svdfact(A, full = true)
indstart = sum(SVD.S .> max(m,n)*maximum(SVD.S)*eps(eltype(SVD.S))) + 1
return SVD.Vt[indstart:end,:]'
return adjoint(SVD.Vt[indstart:end,:])
end
nullspace(a::StridedVector) = nullspace(reshape(a, length(a), 1))

Expand Down
2 changes: 1 addition & 1 deletion base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ function svd(D::Diagonal{<:Number})
end
function svdfact(D::Diagonal)
U, s, V = svd(D)
SVD(U, s, V')
SVD(U, s, adjoint(V))
end

# dismabiguation methods: * of Diagonal and Adj/Trans AbsVec
Expand Down
13 changes: 8 additions & 5 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,18 @@ function inv(A::AbstractMatrix{T}) where T
ldiv!(factorize(convert(AbstractMatrix{S}, A)), dest)
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)))))'
pinv(v::AbstractVector{T}, tol::Real = real(zero(T))) where {T<:Real} = _vectorpinv(Transpose, v, tol)
pinv(v::AbstractVector{T}, tol::Real = real(zero(T))) where {T<:Complex} = _vectorpinv(Adjoint, v, tol)
pinv(v::AbstractVector{T}, tol::Real = real(zero(T))) where {T} = _vectorpinv(Adjoint, v, tol)
function _vectorpinv(dualfn::Tf, v::AbstractVector{Tv}, tol) where {Tv,Tf}
res = dualfn(similar(v, typeof(zero(Tv) / (abs2(one(Tv)) + abs2(one(Tv))))))
den = sum(abs2, v)
# as tol is the threshold relative to the maximum singular value, for a vector with
# single singular value σ=√den, σ ≦ tol*σ is equivalent to den=0 ∨ tol≥1
if iszero(den) || tol >= one(tol)
fill!(res, zero(eltype(res)))
else
res .= v' ./ den
res .= dualfn(v) ./ den
end
return res
end
Expand Down Expand Up @@ -882,7 +885,7 @@ function (\)(A::AbstractMatrix, B::AbstractVecOrMat)
end

(\)(a::AbstractVector, b::AbstractArray) = pinv(a) * b
(/)(A::AbstractVecOrMat, B::AbstractVecOrMat) = (B' \ A')'
(/)(A::AbstractVecOrMat, B::AbstractVecOrMat) = adjoint(Adjoint(B) \ Adjoint(A))
# \(A::StridedMatrix,x::Number) = inv(A)*x Should be added at some point when the old elementwise version has been deprecated long enough
# /(x::Number,A::StridedMatrix) = x*inv(A)
/(x::Number, v::AbstractVector) = x*pinv(v)
Expand Down Expand Up @@ -1290,7 +1293,7 @@ end
vAj += x[i]'*A[i, j]
end

vAj = τ'*vAj
vAj = conj(τ)*vAj

# ger
A[1, j] -= vAj
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ convert(::Type{AbstractArray}, A::LQ) = convert(AbstractMatrix, A)
convert(::Type{Matrix}, A::LQ) = convert(Array, convert(AbstractArray, A))
convert(::Type{Array}, A::LQ) = convert(Matrix, A)

adjoint(A::LQ{T}) where {T} = QR{T,typeof(A.factors)}(A.factors', A.τ)
adjoint(A::LQ{T}) where {T} = QR{T,typeof(A.factors)}(adjoint(A.factors), A.τ)

function getindex(A::LQ, d::Symbol)
m, n = size(A)
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ function ldiv!(A::QR{T}, B::StridedMatrix{T}) where T
for k = m:-1:1 # Trapezoid to triangular by elementary operation
x = view(R, k, [k; m + 1:n])
τk = reflector!(x)
τ[k] = τk'
τ[k] = adjoint(τk)
for i = 1:k - 1
vRi = R[i,k]
for j = m + 1:n
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/rowvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ IndexStyle(::Type{<:RowVector}) = IndexLinear()

# inner product -> dot product specializations
@inline *(rowvec::RowVector{T}, vec::AbstractVector{T}) where {T<:Real} = dot(parent(rowvec), vec)
@inline *(rowvec::ConjRowVector{T}, vec::AbstractVector{T}) where {T<:Real} = dot(rowvec', vec)
@inline *(rowvec::ConjRowVector, vec::AbstractVector) = dot(rowvec', vec)
@inline *(rowvec::ConjRowVector{T}, vec::AbstractVector{T}) where {T<:Real} = dot(rvadjoint(rowvec), vec)
@inline *(rowvec::ConjRowVector, vec::AbstractVector) = dot(rvadjoint(rowvec), vec)

# Generic behavior
@inline function *(rowvec::RowVector, vec::AbstractVector)
Expand Down Expand Up @@ -261,7 +261,7 @@ end
*(adjvec::Adjoint{<:Any,<:AbstractVector}, adjrowvec::Adjoint{<:Any,<:RowVector}) =
adjoint(adjvec.parent)*rvadjoint(adjrowvec.parent)
*(adjmat::Adjoint{<:Any,<:AbstractMatrix}, adjrowvec::Adjoint{<:Any,<:RowVector}) =
(adjmat.parent)' * rvadjoint(adjrowvec.parent)
adjoint(adjmat.parent) * rvadjoint(adjrowvec.parent)

*(::Adjoint{<:Any,<:RowVector}, ::AbstractVector) = throw(DimensionMismatch("Cannot multiply two vectors"))
*(adjrowvec1::Adjoint{<:Any,<:RowVector}, rowvec2::RowVector) = rvadjoint(adjrowvec1.parent) * rowvec2
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function svd(A::AbstractArray; full::Bool = false, thin::Union{Bool,Void} = noth
full::Bool = !thin
end
F = svdfact(A, full = full)
F.U, F.S, F.Vt'
F.U, F.S, adjoint(F.Vt)
end
function svd(x::Number; full::Bool = false, thin::Union{Bool,Void} = nothing)
# DEPRECATION TODO: remove deprecated thin argument and associated logic after 0.7
Expand All @@ -186,7 +186,7 @@ function getindex(F::SVD, d::Symbol)
elseif d == :Vt
return F.Vt
elseif d == :V
return F.Vt'
return adjoint(F.Vt)
else
throw(KeyError(d))
end
Expand Down
22 changes: 11 additions & 11 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ julia> Hlower = Hermitian(A, :L)
2+2im 0+0im 3-3im 0+0im 4+0im
```
Note that `Hupper` will not be equal to `Hlower` unless `A` is itself Hermitian (e.g. if `A == A'`).
Note that `Hupper` will not be equal to `Hlower` unless `A` is itself Hermitian (e.g. if `A == adjoint(A)`).
All non-real parts of the diagonal will be ignored.
Expand Down Expand Up @@ -264,13 +264,13 @@ Base.conj!(A::HermOrSym) = typeof(A)(conj!(A.data), A.uplo)
# tril/triu
function tril(A::Hermitian, k::Integer=0)
if A.uplo == 'U' && k <= 0
return tril!(A.data',k)
return tril!(adjoint(A.data),k)
elseif A.uplo == 'U' && k > 0
return tril!(A.data',-1) + tril!(triu(A.data),k)
return tril!(adjoint(A.data),-1) + tril!(triu(A.data),k)
elseif A.uplo == 'L' && k <= 0
return tril(A.data,k)
else
return tril(A.data,-1) + tril!(triu!(A.data'),k)
return tril(A.data,-1) + tril!(triu!(adjoint(A.data)),k)
end
end

Expand All @@ -290,11 +290,11 @@ function triu(A::Hermitian, k::Integer=0)
if A.uplo == 'U' && k >= 0
return triu(A.data,k)
elseif A.uplo == 'U' && k < 0
return triu(A.data,1) + triu!(tril!(A.data'),k)
return triu(A.data,1) + triu!(tril!(adjoint(A.data)),k)
elseif A.uplo == 'L' && k >= 0
return triu!(A.data',k)
return triu!(adjoint(A.data),k)
else
return triu!(A.data',1) + triu!(tril(A.data),k)
return triu!(adjoint(A.data),1) + triu!(tril(A.data),k)
end
end

Expand Down Expand Up @@ -551,18 +551,18 @@ eigmax(A::RealHermSymComplexHerm{<:Real,<:StridedMatrix}) = eigvals(A, size(A, 1
eigmin(A::RealHermSymComplexHerm{<:Real,<:StridedMatrix}) = eigvals(A, 1:1)[1]

function eigfact!(A::HermOrSym{T,S}, B::HermOrSym{T,S}) where {T<:BlasReal,S<:StridedMatrix}
vals, vecs, _ = LAPACK.sygvd!(1, 'V', A.uplo, A.data, B.uplo == A.uplo ? B.data : B.data')
vals, vecs, _ = LAPACK.sygvd!(1, 'V', A.uplo, A.data, B.uplo == A.uplo ? B.data : adjoint(B.data))
GeneralizedEigen(vals, vecs)
end
function eigfact!(A::Hermitian{T,S}, B::Hermitian{T,S}) where {T<:BlasComplex,S<:StridedMatrix}
vals, vecs, _ = LAPACK.sygvd!(1, 'V', A.uplo, A.data, B.uplo == A.uplo ? B.data : B.data')
vals, vecs, _ = LAPACK.sygvd!(1, 'V', A.uplo, A.data, B.uplo == A.uplo ? B.data : adjoint(B.data))
GeneralizedEigen(vals, vecs)
end

eigvals!(A::HermOrSym{T,S}, B::HermOrSym{T,S}) where {T<:BlasReal,S<:StridedMatrix} =
LAPACK.sygvd!(1, 'N', A.uplo, A.data, B.uplo == A.uplo ? B.data : B.data')[1]
LAPACK.sygvd!(1, 'N', A.uplo, A.data, B.uplo == A.uplo ? B.data : adjoint(B.data))[1]
eigvals!(A::Hermitian{T,S}, B::Hermitian{T,S}) where {T<:BlasComplex,S<:StridedMatrix} =
LAPACK.sygvd!(1, 'N', A.uplo, A.data, B.uplo == A.uplo ? B.data : B.data')[1]
LAPACK.sygvd!(1, 'N', A.uplo, A.data, B.uplo == A.uplo ? B.data : adjoint(B.data))[1]

eigvecs(A::HermOrSym) = eigvecs(eigfact(A))

Expand Down
4 changes: 2 additions & 2 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,13 @@ function eigvecs(A::UnitUpperTriangular{<:BlasFloat,<:StridedMatrix})
LAPACK.trevc!('R', 'A', BlasInt[], triu!(A.data))
end
function eigvecs(A::LowerTriangular{<:BlasFloat,<:StridedMatrix})
LAPACK.trevc!('L', 'A', BlasInt[], tril!(A.data)')
LAPACK.trevc!('L', 'A', BlasInt[], adjoint(tril!(A.data)))
end
function eigvecs(A::UnitLowerTriangular{<:BlasFloat,<:StridedMatrix})
for i = 1:size(A, 1)
A.data[i,i] = 1
end
LAPACK.trevc!('L', 'A', BlasInt[], tril!(A.data)')
LAPACK.trevc!('L', 'A', BlasInt[], adjoint(tril!(A.data)))
end

####################
Expand Down
5 changes: 3 additions & 2 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ julia> inv(A) * x
4.5
```
"""
\(x,y) = (y'/x')'
\(x,y) = adjoint(Adjoint(y)/Adjoint(x))

# Core <<, >>, and >>> take either Int or UInt as second arg. Signed shift
# counts can shift in either direction, and are translated here to unsigned
Expand Down Expand Up @@ -739,7 +739,8 @@ fldmod1(x::T, y::T) where {T<:Real} = (fld1(x,y), mod1(x,y))
# efficient version for integers
fldmod1(x::T, y::T) where {T<:Integer} = (fld1(x,y), mod1(x,y))

# transpose
# postfix apostophre
Core.postfixapostrophize(x) = Adjoint(x)

"""
adjoint(A)
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/resolve/maxsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mutable struct Graph
adjdict[p0][p1] = j1

bm = trues(spp[p1], spp[p0])
bmt = bm'
bmt = adjoint(bm)

push!(gmsk[p0], bm)
push!(gmsk[p1], bmt)
Expand Down
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function //(x::Rational, y::Rational)
end

//(x::Complex, y::Real) = complex(real(x)//y,imag(x)//y)
//(x::Number, y::Complex) = x*y'//abs2(y)
//(x::Number, y::Complex) = x*conj(y)//abs2(y)


//(X::AbstractArray, y::Number) = X .// y
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ function cond(A::SparseMatrixCSC, p::Real=2)
normA = norm(A, 1)
return normA * normAinv
elseif p == Inf
normAinv = normestinv(A')
normAinv = normestinv(adjoint(A))
normA = norm(A, Inf)
return normA * normAinv
elseif p == 2
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ function LinAlg.lowrankupdate!(A::StridedMatrix, x::StridedVector, y::SparseVect
nzi = nonzeroinds(y)
nzv = nonzeros(y)
@inbounds for (j,v) in zip(nzi,nzv)
αv = α*v'
αv = α*conj(v)
for i in axes(x, 1)
A[i,j] += x[i]*αv
end
Expand Down
2 changes: 1 addition & 1 deletion base/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function cov2cor!(C::AbstractMatrix{T}, xsd::AbstractArray) where T
size(C) == (nx, nx) || throw(DimensionMismatch("inconsistent dimensions"))
for j = 1:nx
for i = 1:j-1
C[i,j] = C[j,i]'
C[i,j] = adjoint(C[j,i])
end
C[j,j] = oneunit(T)
for i = j+1:nx
Expand Down
31 changes: 1 addition & 30 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1515,22 +1515,6 @@
(kwcall-unless-empty f pa kw-container kw-container)
`(call (call (core kwfunc) ,f) ,kw-container ,f ,@pa)))))

;; convert e.g. A'*B to Ac_mul_B(A,B)
(define (expand-transposed-op e ops)
(let ((a (caddr e))
(b (cadddr e)))
(cond ((ctrans? a)
(if (ctrans? b)
`(call ,(aref ops 0) #;Ac_mul_Bc ,(expand-forms (cadr a))
,(expand-forms (cadr b)))
`(call ,(aref ops 1) #;Ac_mul_B ,(expand-forms (cadr a))
,(expand-forms b))))
((ctrans? b)
`(call ,(aref ops 2) #;A_mul_Bc ,(expand-forms a)
,(expand-forms (cadr b))))
(else
`(call ,(cadr e) ,(expand-forms a) ,(expand-forms b))))))

;; convert `a+=b` to `a=a+b`
(define (expand-update-operator- op op= lhs rhs declT)
(let ((e (remove-argument-side-effects lhs)))
Expand Down Expand Up @@ -2212,19 +2196,6 @@
((and (eq? f '^) (length= e 4) (integer? (cadddr e)))
(expand-forms
`(call (top literal_pow) ^ ,(caddr e) (call (call (core apply_type) (top Val) ,(cadddr e))))))

((and (eq? f '*) (length= e 4))
(expand-transposed-op
e
#(Ac_mul_Bc Ac_mul_B A_mul_Bc)))
((and (eq? f '/) (length= e 4))
(expand-transposed-op
e
#(Ac_rdiv_Bc Ac_rdiv_B A_rdiv_Bc)))
((and (eq? f '\\) (length= e 4))
(expand-transposed-op
e
#(Ac_ldiv_Bc Ac_ldiv_B A_ldiv_Bc)))
(else
(map expand-forms e))))
(map expand-forms e)))
Expand Down Expand Up @@ -2396,7 +2367,7 @@
,.(apply append rows)))
`(call (top typed_vcat) ,t ,@a)))))

'|'| (lambda (e) (expand-forms `(call adjoint ,(cadr e))))
'|'| (lambda (e) (expand-forms `(call (core postfixapostrophize) ,(cadr e))))
'|.'| (lambda (e) (begin (deprecation-message (string "The syntax `.'` for transposition is deprecated, "
"and the special lowering of `.'` in multiplication "
"(`*`), left-division (`\\`), and right-division (`/`) "
Expand Down
Loading

0 comments on commit 1319567

Please sign in to comment.