Skip to content

Add Aqua test #79

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
Aqua = "0.8"
LinearAlgebra = "1"
Random = "1"
SparseArrays = "1"
StaticArrays = "1"
julia = "1"
Test = "1"
julia = "1.10"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"

[targets]
test = ["Test"]
test = ["Test", "Aqua"]
4 changes: 2 additions & 2 deletions src/IMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ end
LinearAlgebra.ishermitian(D::IMatrix) = true

####### sparse matrix ######
nnz(M::IMatrix) = M.n
findnz(M::IMatrix{T}) where {T} = (collect(1:M.n), collect(1:M.n), ones(T, M.n))
SparseArrays.nnz(M::IMatrix) = M.n
SparseArrays.findnz(M::IMatrix{T}) where {T} = (collect(1:M.n), collect(1:M.n), ones(T, M.n))
2 changes: 1 addition & 1 deletion src/LuxurySparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using SparseArrays: SparseMatrixCSC
using SparseArrays.HigherOrderFns
using Base: @propagate_inbounds
using LinearAlgebra
import SparseArrays: findnz, nnz
import LinearAlgebra: Diagonal
using LinearAlgebra: StructuredMatrixStyle
using Base.Broadcast:
BroadcastStyle, AbstractArrayStyle, Broadcasted, DefaultArrayStyle, materialize!
Expand Down
6 changes: 3 additions & 3 deletions src/PermMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ end
Base.hash(pm::AbstractPermMatrix) = hash((pm.perm, pm.vals))

######### sparse array interfaces #########
nnz(M::AbstractPermMatrix) = length(M.vals)
findnz(M::PermMatrix) = (collect(1:size(M, 1)), M.perm, M.vals)
findnz(M::PermMatrixCSC) = (M.perm, collect(1:size(M, 1)), M.vals)
SparseArrays.nnz(M::AbstractPermMatrix) = length(M.vals)
SparseArrays.findnz(M::PermMatrix) = (collect(1:size(M, 1)), M.perm, M.vals)
SparseArrays.findnz(M::PermMatrixCSC) = (M.perm, collect(1:size(M, 1)), M.vals)
24 changes: 12 additions & 12 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ Broadcast.broadcasted(::AbstractArrayStyle{2}, ::typeof(*), A::Diagonal, B::Abst

# TODO: commit this upstream
# specialize Diagonal .* SparseMatrixCSC
Broadcast.broadcasted(
::AbstractArrayStyle{2},
::typeof(*),
A::Diagonal,
B::SparseMatrixCSC,
) = Broadcast.broadcasted(*, A, Diagonal(B))
# Broadcast.broadcasted(
# ::AbstractArrayStyle{2},
# ::typeof(*),
# A::Diagonal,
# B::SparseMatrixCSC,
# ) = Broadcast.broadcasted(*, A, Diagonal(B))

Broadcast.broadcasted(
::AbstractArrayStyle{2},
::typeof(*),
A::SparseMatrixCSC,
B::Diagonal,
) = Broadcast.broadcasted(*, Diagonal(A), B)
# Broadcast.broadcasted(
# ::AbstractArrayStyle{2},
# ::typeof(*),
# A::SparseMatrixCSC,
# B::Diagonal,
# ) = Broadcast.broadcasted(*, Diagonal(A), B)

Broadcast.broadcasted(
::AbstractArrayStyle{2},
Expand Down
2 changes: 1 addition & 1 deletion src/iterate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct IterNz{MT}
end

Base.length(nz::IterNz{<:AbstractMatrix}) = length(nz.A)
Base.length(nz::IterNz{<:AbstractSparseMatrix}) = nnz(nz.A)
Base.length(nz::IterNz{<:AbstractSparseMatrix}) = SparseArrays.nnz(nz.A)
Base.length(nz::IterNz{<:Adjoint}) = length(IterNz(nz.A.parent))
Base.length(nz::IterNz{<:Transpose}) = length(IterNz(nz.A.parent))
Base.length(nz::IterNz{<:Diagonal}) = size(nz.A, 1)
Expand Down
13 changes: 6 additions & 7 deletions src/kronecker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ function orepeat(v::AbstractVector, n::Int)
end


# TODO: since 0.7 transpose is different, we don't take transpose serious here.
####### kronecker product ###########
fastkron(a, b) = kron(a, b)
fastkron(A::Diagonal{<:Number}, B::SparseMatrixCSC{<:Number}) = kron(PermMatrixCSC(A), B)
fastkron(A::SparseMatrixCSC{<:Number}, B::Diagonal{<:Number}) = kron(A, PermMatrixCSC(B))
fastkron(A::Diagonal{<:Number}, B::StridedMatrix{<:Number}) = kron(PermMatrixCSC(A), B)
fastkron(A::StridedMatrix{<:Number}, B::Diagonal{<:Number}) = kron(A, PermMatrixCSC(B))

LinearAlgebra.kron(A::IMatrix{Ta}, B::IMatrix{Tb}) where {Ta<:Number,Tb<:Number} =
IMatrix{promote_type(Ta, Tb)}(A.n * B.n)
LinearAlgebra.kron(A::IMatrix{<:Number}, B::Diagonal{<:Number}) = A.n == 1 ? B : Diagonal(orepeat(B.diag, A.n))
LinearAlgebra.kron(B::Diagonal{<:Number}, A::IMatrix) = A.n == 1 ? B : Diagonal(irepeat(B.diag, A.n))

####### diagonal kron ########
LinearAlgebra.kron(A::StridedMatrix{<:Number}, B::Diagonal{<:Number}) = kron(A, PermMatrixCSC(B))
LinearAlgebra.kron(A::Diagonal{<:Number}, B::StridedMatrix{<:Number}) = kron(PermMatrixCSC(A), B)
LinearAlgebra.kron(A::Diagonal{<:Number}, B::SparseMatrixCSC{<:Number}) = kron(PermMatrixCSC(A), B)
LinearAlgebra.kron(A::SparseMatrixCSC{<:Number}, B::Diagonal{<:Number}) = kron(A, PermMatrixCSC(B))

function LinearAlgebra.kron(A::AbstractMatrix{Tv}, B::IMatrix) where {Tv<:Number}
B.n == 1 && return A
mA, nA = size(A)
Expand Down
35 changes: 21 additions & 14 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ function LinearAlgebra.mul!(C::AbstractMatrix, X::AbstractMatrix, A::AbstractPer
AC = PermMatrixCSC(A)
C .= C .* beta .+ reshape(AC.vals, 1, :) .* view(X, :, AC.perm) .* alpha
end
function LinearAlgebra.mul!(C::AbstractMatrix, A::AbstractPermMatrix, B::AbstractPermMatrix, alpha::Number, beta::Number)
size(C, 1) == size(C, 2) == size(A, 1) == size(B, 1) || throw(DimensionMismatch())
res = A * B
for (i, j, v) in IterNz(res)
C[i, j] = v * alpha + beta * C[i, j]
end
return C
end

# NOTE: this is just a temperory fix for v0.7. We should overload mul! in
# the future (when we start to drop v0.6) to enable buildin lazy evaluation.
Expand Down Expand Up @@ -187,19 +195,18 @@ Base.:/(A::SparseMatrixCOO, B::Int) = rdiv!(copy(A), B)
Base.:-(ii::IMatrix) = (-1) * ii
Base.:-(pm::AbstractPermMatrix) = (-1) * pm

for FUNC in [:randn!, :rand!]
@eval function Random.$FUNC(m::Diagonal)
$FUNC(m.diag)
return m
end

@eval function Random.$FUNC(m::SparseMatrixCSC)
$FUNC(m.nzval)
return m
end
randomize!(m::AbstractArray) = randn!(m)
function randomize!(m::Diagonal)
randomize!(m.diag)
return m
end

@eval function Random.$FUNC(m::AbstractPermMatrix)
$FUNC(m.vals)
return m
end
function randomize!(m::SparseMatrixCSC)
randomize!(m.nzval)
return m
end

function randomize!(m::AbstractPermMatrix)
randomize!(m.vals)
return m
end
4 changes: 2 additions & 2 deletions src/promotions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SparseMatrixCSC
Base.promote_rule(::Type{SparseMatrixCSC{Tv,Ti}}, ::Type{Matrix{T}}) where {Tv,Ti,T} =
Matrix{promote_type(T, Tv)}
#Base.promote_rule(::Type{SparseMatrixCSC{Tv,Ti}}, ::Type{Matrix{T}}) where {Tv,Ti,T} =
# Matrix{promote_type(T, Tv)}

# IMatrix
for MT in [:PermMatrix, :PermMatrixCSC]
Expand Down
11 changes: 1 addition & 10 deletions src/staticize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,4 @@ dynamicize(A::PermMatrix) = PermMatrix(Vector(A.perm), Vector(A.vals))
dynamicize(A::PermMatrixCSC) = PermMatrixCSC(Vector(A.perm), Vector(A.vals))
function dynamicize(A::SSparseMatrixCSC)
SparseMatrixCSC(A.m, A.n, Vector(A.colptr), Vector(A.rowval), Vector(A.nzval))
end

function findnz(M::SDMatrix)
cis = CartesianIndices(size(M))
vec(getindex.(cis, 1)), vec(getindex.(cis, 2)), vec(M)
end
nnz(M::SDMatrix) = length(M)

findnz(sp::AbstractSparseMatrix) = SparseArrays.findnz(sp)
nnz(M::AbstractSparseMatrix) = SparseArrays.nnz(M)
end
5 changes: 1 addition & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ function fast_invperm(order)
v[order[i]] = i
end
v
end

findnz(M::Diagonal) = (collect(1:size(M, 1)), collect(1:size(M, 1)), M.diag)
nnz(M::Diagonal) = length(M.diag)
end
3 changes: 1 addition & 2 deletions test/IMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ end

@testset "sparse" begin
@show p1
@test LuxurySparse.nnz(p1) == 4
@test length(LuxurySparse.findnz(p1)[3]) == 4
@test LuxurySparse.length(IterNz(p1)) == 4
end

@testset "linalg" begin
Expand Down
4 changes: 2 additions & 2 deletions test/PermMatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ end

@testset "identity sparse" begin
p1 = Diagonal(randn(10))
@test LuxurySparse.nnz(p1) == 10
@test LuxurySparse.findnz(p1)[3] == p1.diag
@test length(LuxurySparse.IterNz(p1)) == 10
@test map(x -> x[3], LuxurySparse.IterNz(p1)) == p1.diag
end

@testset "setindex" begin
Expand Down
4 changes: 2 additions & 2 deletions test/PermMatrixCSC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ end

@testset "identity sparse" begin
p1 = Diagonal(randn(10))
@test LuxurySparse.nnz(p1) == 10
@test LuxurySparse.findnz(p1)[3] == p1.diag
@test length(LuxurySparse.IterNz(p1)) == 10
@test map(x -> x[3], LuxurySparse.IterNz(p1)) == p1.diag
end

@testset "setindex" begin
Expand Down
2 changes: 1 addition & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using SparseArrays
d = Diagonal(rand(3))
sp = sprand(3, 3, 0.5)
@test d .* sp ≈ Matrix(d) .* Matrix(sp)
@test typeof(d .* sp) <: Diagonal
@test_broken typeof(d .* sp) <: Diagonal

@testset "Number .* $(nameof(typeof(M)))" for M in Any[pmrand(3), Diagonal(randn(3))]
@test 3.0 .* M ≈ 3.0 .* Matrix(M) && M .* 3.0 ≈ Matrix(M) .* 3.0
Expand Down
10 changes: 5 additions & 5 deletions test/kronecker.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Test, Random, SparseArrays, LinearAlgebra
import LuxurySparse: IMatrix, PermMatrix, PermMatrixCSC, basetype, AbstractPermMatrix
import LuxurySparse: IMatrix, PermMatrix, PermMatrixCSC, basetype, AbstractPermMatrix, fastkron

@testset "kron" begin
Random.seed!(2)
Expand All @@ -17,10 +17,10 @@ import LuxurySparse: IMatrix, PermMatrix, PermMatrixCSC, basetype, AbstractPermM
if source isa AbstractPermMatrix && target isa AbstractPermMatrix && basetype(source) != basetype(target)
continue
end
lres = kron(source, target)
rres = kron(target, source)
flres = kron(Matrix(source), Matrix(target))
frres = kron(Matrix(target), Matrix(source))
lres = fastkron(source, target)
rres = fastkron(target, source)
flres = fastkron(Matrix(source), Matrix(target))
frres = fastkron(Matrix(target), Matrix(source))
@test lres == flres
@test rres == frres
@test eltype(lres) == eltype(flres)
Expand Down
14 changes: 11 additions & 3 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ end
for m in Any[pmrand(T, 5), Diagonal(randn(T, 5))]
zm = zero(m)
@test zm ≈ zeros(T, 5, 5)
rand!(zm)
LuxurySparse.randomize!(zm)
@test !(zm ≈ zeros(T, 5, 5))
zm = zero(m)
randn!(zm)
LuxurySparse.randomize!(zm)
@test !(zm ≈ zeros(T, 5, 5))
end
end
Expand All @@ -111,7 +111,7 @@ end
for m in Any[p1, sp, ds, dv, pm]
for _m in Any[m, staticize(m)]
out = zeros(eltype(m), size(m)...)
for (i, j, v) in zip(LuxurySparse.findnz(_m)...)
for (i, j, v) in LuxurySparse.IterNz(_m)
out[i, j] = v
end
@test out ≈ m
Expand All @@ -129,4 +129,12 @@ end
pop = sparse([2, 3], [4, 5], [1, √2], 5, 5)
zop = Diagonal([0, 1 / 2, 1, -1 / 2, 0])
@test (pop*zop)[2, 4] == -0.5
end

@testset "extra" begin
out = zeros(ComplexF64, 4, 4)
a = pmrand(4)
b = pmrand(4)
mul!(out, a, b, 1, 0)
@test out ≈ Matrix(a) * Matrix(b)
end
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Test
using LinearAlgebra, SparseArrays
using Aqua
using LuxurySparse

@testset "Aqua" begin
Aqua.test_all(LuxurySparse)
end

@testset "IMatrix" begin
include("IMatrix.jl")
end
Expand Down
Loading