Skip to content

Commit

Permalink
Use LowRankMatrices.jl for LowRankMatrix (#53)
Browse files Browse the repository at this point in the history
* Use LowRankMatrices.jl for LowRankMatrix

* Remove LowRankMatrix test

* Remove dep on FillArrays

* Bump version to v0.5.5

* Remove FillArrays import comment
  • Loading branch information
jishnub authored Jul 31, 2023
1 parent 300e6d9 commit 0c9c7db
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 269 deletions.
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name = "LowRankApprox"
uuid = "898213cb-b102-5a47-900c-97e73b919f73"
version = "0.5.4"
version = "0.5.5"

[deps]
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LowRankMatrices = "e65ccdef-c354-471a-8090-89bec1c20ec3"
Nullables = "4d1e1d77-625e-5b40-9113-a560ec7a8ecd"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[compat]
Aqua = "0.5"
Aqua = "0.6"
FFTW = "0.3, 1"
FillArrays = "0.6, 0.7, 0.8, 0.9, 0.10, 0.11, 0.12, 0.13, 1"
LowRankMatrices = "1"
Nullables = "0.0.8, 1.0"
julia = "1"

Expand Down
5 changes: 3 additions & 2 deletions src/LowRankApprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
=#
__precompile__()
module LowRankApprox
using FillArrays
using LinearAlgebra, SparseArrays, Random

using LowRankMatrices
using LowRankMatrices: _LowRankMatrix

import Base: convert,
eltype, size, getindex, setindex!, copy,
isreal, real, imag, Nothing, copyto!,
Expand All @@ -21,7 +23,6 @@ import SparseArrays: sparse
ind2sub(dims, ind) = CartesianIndices(dims)[ind]
qrfact!(A) = qr!(A)

import FillArrays: AbstractFill

export

Expand Down
177 changes: 0 additions & 177 deletions src/lowrankmatrix.jl
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@


##
# Represent an m x n rank-r matrix
# A = U*Vᵗ
##
function _LowRankMatrix end

mutable struct LowRankMatrix{T} <: AbstractMatrix{T}
U::Matrix{T} # m x r Matrix
V::Matrix{T} # n x r Matrix

global function _LowRankMatrix(U::AbstractMatrix{T}, V::AbstractMatrix{T}) where T
m,r = size(U)
n,rv = size(V)
if r rv throw(ArgumentError("U and V must have same number of columns")) end
new{T}(Matrix{T}(U), Matrix{T}(V))
end
end

LowRankMatrix(U::AbstractMatrix, V::AbstractMatrix) = _LowRankMatrix(promote(U,V)...)
LowRankMatrix(U::AbstractVector, V::AbstractMatrix) = LowRankMatrix(reshape(U,length(U),1),V)
LowRankMatrix(U::AbstractMatrix, V::AbstractVector) = LowRankMatrix(U,reshape(V,length(V),1))
LowRankMatrix(U::AbstractVector, V::AbstractVector) =
_LowRankMatrix(reshape(U,length(U),1), reshape(V,length(V),1))

LowRankMatrix{T}(::UndefInitializer, mn::NTuple{2,Int}, r::Int) where {T} =
LowRankMatrix(Matrix{T}(undef,mn[1],r),Matrix{T}(undef,mn[2],r))
LowRankMatrix{T}(Z::Zeros, r::Int=0) where {T<:Number} =
LowRankMatrix(zeros(T,size(Z,1),r), zeros(T,size(Z,2),r))
LowRankMatrix{T}(Z::Zeros, r::Int=0) where {T} =
LowRankMatrix(zeros(T,size(Z,1),r), zeros(T,size(Z,2),r))

LowRankMatrix(Z::Zeros, r::Int=0) = LowRankMatrix{eltype(Z)}(Z, r)
function LowRankMatrix{T}(F::AbstractFill) where T
v = T(FillArrays.getindex_value(F))
m,n = size(F)
LowRankMatrix(fill(v,m,1), fill(one(T),n,1))
end
LowRankMatrix(F::AbstractFill{T}) where T = LowRankMatrix{T}(F)


similar(L::LowRankMatrix, ::Type{T}, dims::Dims{2}) where {T} = LowRankMatrix{T}(undef, dims, rank(L))
similar(L::LowRankMatrix{T}) where {T} = LowRankMatrix{T}(undef, size(L), rank(L))
similar(L::LowRankMatrix{T}, dims::Dims{2}) where {T} = LowRankMatrix(undef, dims, rank(L))
similar(L::LowRankMatrix{T}, m::Int) where {T} = Vector{T}(undef, m)
similar(L::LowRankMatrix{T}, ::Type{S}) where {S,T} = LowRankMatrix{S}(undef, size(L), rank(L))

function LowRankMatrix{T}(A::AbstractMatrix{T}) where T
U,Σ,V = svd(A)
r = refactorsvd!(U,Σ,V)
LowRankMatrix(U[:,1:r], V[:,1:r])
end

LowRankMatrix{T}(A::AbstractMatrix) where T = LowRankMatrix{T}(AbstractMatrix{T}(A))
LowRankMatrix(A::AbstractMatrix{T}) where T = LowRankMatrix{T}(A)

balance!(U::Matrix{T}, V::Matrix{T}, m::Int, n::Int, r::Int) where {T<:Union{Integer,Rational}} = U,V
function balance!(U::Matrix{T}, V::Matrix{T}, m::Int, n::Int, r::Int) where T
for k=1:r
Expand All @@ -80,123 +23,3 @@ function balance!(U::Matrix{T}, V::Matrix{T}, m::Int, n::Int, r::Int) where T
end
U,V
end

# Moves Σ into U and V
function refactorsvd!(U::AbstractMatrix{S}, Σ::AbstractVector{T}, V::AbstractMatrix{S}) where {S,T}
conj!(V)
σmax = Σ[1]
r = count(s->s>10σmax*eps(T),Σ)
m,n = size(U,1),size(V,1)
for k=1:r
σk = sqrt(Σ[k])
for i=1:m
@inbounds U[i,k] *= σk
end
for j=1:n
@inbounds V[j,k] *= σk
end
end
r
end

for MAT in (:LowRankMatrix, :AbstractMatrix, :AbstractArray)
@eval convert(::Type{$MAT{T}}, L::LowRankMatrix) where {T} =
LowRankMatrix(convert(Matrix{T}, L.U), convert(Matrix{T}, L.V))
end
convert(::Type{Matrix{T}}, L::LowRankMatrix) where {T} = convert(Matrix{T}, Matrix(L))
promote_rule(::Type{LowRankMatrix{T}}, ::Type{LowRankMatrix{V}}) where {T,V} = LowRankMatrix{promote_type(T,V)}
promote_rule(::Type{LowRankMatrix{T}}, ::Type{Matrix{V}}) where {T,V} = Matrix{promote_type(T,V)}

size(L::LowRankMatrix) = size(L.U,1),size(L.V,1)
rank(L::LowRankMatrix) = size(L.U,2)
transpose(L::LowRankMatrix) = LowRankMatrix(L.V,L.U) # TODO: change for 0.7
adjoint(L::LowRankMatrix{T}) where {T<:Real} = LowRankMatrix(L.V,L.U)
adjoint(L::LowRankMatrix) = LowRankMatrix(conj(L.V),conj(L.U))
fill!(L::LowRankMatrix{T}, x::T) where {T} = (fill!(L.U, sqrt(abs(x)/rank(L))); fill!(L.V,sqrt(abs(x)/rank(L))/sign(x)); L)

function unsafe_getindex(L::LowRankMatrix, i::Int, j::Int)
ret = zero(eltype(L))
@inbounds for k=1:rank(L)
ret = muladd(L.U[i,k],L.V[j,k],ret)
end
return ret
end

function getindex(L::LowRankMatrix, i::Int, j::Int)
m,n = size(L)
if 1 i m && 1 j n
unsafe_getindex(L,i,j)
else
throw(BoundsError())
end
end
getindex(L::LowRankMatrix, i::Int, jr::AbstractRange) = transpose(eltype(L)[L[i,j] for j=jr])
getindex(L::LowRankMatrix, ir::AbstractRange, j::Int) = eltype(L)[L[i,j] for i=ir]
getindex(L::LowRankMatrix, ir::AbstractRange, jr::AbstractRange) = eltype(L)[L[i,j] for i=ir,j=jr]
Matrix(L::LowRankMatrix) = L[1:size(L,1),1:size(L,2)]

# constructors

copy(L::LowRankMatrix) = LowRankMatrix(copy(L.U),copy(L.V))
copyto!(L::LowRankMatrix, N::LowRankMatrix) = (copyto!(L.U,N.U); copyto!(L.V,N.V);L)


# algebra

for op in (:+,:-)
@eval begin
$op(L::LowRankMatrix) = LowRankMatrix($op(L.U),L.V)

$op(a::Bool, L::LowRankMatrix{Bool}) = error("Not callable")
$op(L::LowRankMatrix{Bool}, a::Bool) = error("Not callable")
$op(a::Number,L::LowRankMatrix) = $op(LowRankMatrix(Fill(a,size(L))), L)
$op(L::LowRankMatrix,a::Number) = $op(L, LowRankMatrix(Fill(a,size(L))))

function $op(L::LowRankMatrix, M::LowRankMatrix)
size(L) == size(M) || throw(DimensionMismatch("A has dimensions $(size(L)) but B has dimensions $(size(M))"))
LowRankMatrix(hcat(L.U,$op(M.U)), hcat(L.V,M.V))
end
$op(L::LowRankMatrix,A::Matrix) = $op(promote(L,A)...)
$op(A::Matrix,L::LowRankMatrix) = $op(promote(A,L)...)
end
end

*(a::Number, L::LowRankMatrix) = LowRankMatrix(a*L.U,L.V)
*(L::LowRankMatrix, a::Number) = LowRankMatrix(L.U,L.V*a)

# override default:

*(A::LowRankMatrix, B::Adjoint{T,LowRankMatrix{T}}) where T = A*adjoint(B)

function mul!(b::AbstractVector, L::LowRankMatrix, x::AbstractVector)
temp = zeros(promote_type(eltype(L),eltype(x)), rank(L))
mul!(temp, transpose(L.V), x)
mul!(b, L.U, temp)
b
end
function *(L::LowRankMatrix, M::LowRankMatrix)
T = promote_type(eltype(L),eltype(M))
temp = zeros(T,rank(L),rank(M))
mul!(temp, transpose(L.V), M.U)
V = zeros(T,size(M,2),rank(L))
mul!(V, M.V, transpose(temp))
LowRankMatrix(copy(L.U),V)
end



function *(L::LowRankMatrix, A::Matrix)
V = zeros(promote_type(eltype(L),eltype(A)),size(A,2),rank(L))
mul!(V, transpose(A), L.V)
LowRankMatrix(copy(L.U),V)
end



function *(A::Matrix, L::LowRankMatrix)
U = zeros(promote_type(eltype(A),eltype(L)),size(A,1),rank(L))
mul!(U,A,L.U)
LowRankMatrix(U,copy(L.V))
end

\(L::LowRankMatrix, b::AbstractVecOrMat) = transpose(L.V) \ (L.U \ b)
85 changes: 0 additions & 85 deletions test/lowrankmatrix.jl

This file was deleted.

1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ include("pqr.jl")
include("prange.jl")
include("psvd.jl")
include("cur.jl")
include("lowrankmatrix.jl")

4 comments on commit 0c9c7db

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unexpected error occurred during registration.

@jishnub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/88700

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.5 -m "<description of version>" 0c9c7db4110f4942462fe5165a23d0f15d803fbf
git push origin v0.5.5

Please sign in to comment.