Skip to content
Merged
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
3 changes: 2 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MatrixFactorizations = "a3b82374-2e81-5b9e-98ce-41277c0e4c87"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand All @@ -16,3 +16,4 @@ ArrayLayouts = "1.12.2"
BenchmarkTools = "1.6.3"
MatrixFactorizations = "3.1.3"
SafeTestsets = "0.1.0"
SciMLTesting = "1"
101 changes: 101 additions & 0 deletions test/core_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using SafeTestsets

@safetestset "Constructors" begin
using FastAlmostBandedMatrices

A = AlmostBandedMatrix{Float64}(undef, (10, 11), (2, 1), 2)
A[1, 1] = 2
@test A[1, 1] == 2.0
A[4, 1] = 0
@test A[4, 1] == 0.0
@test_throws BandError A[4, 1] = 2
A[1, 3] = 5
@test A[1, 3] == 5.0

@test almostbandwidths(A) == (2, 1)
@test almostbandedrank(A) == 2
end

@safetestset "similar" begin
using FastAlmostBandedMatrices

A = AlmostBandedMatrix(brand(Float64, 10, 10, 2, 1), rand(Float64, 2, 10))

@test similar(A) isa AlmostBandedMatrix
@test similar(A, Float32) isa AlmostBandedMatrix{Float32}

fallback = similar(A, Float32, 10, 10)
@test fallback isa Matrix{Float32}
@test size(fallback) == size(A)
end

@safetestset "Copy" begin
using FastAlmostBandedMatrices

n = 5
m = 2

A1 = AlmostBandedMatrix(brand(Float64, n, n, m + 1, m), rand(Float64, m, n))
A2 = copy(A1)

@test !(A2 isa Matrix)
@test A1 == A2

A2 = deepcopy(A1)

@test !(A2 isa Matrix)
@test A1 == A2
end

@safetestset "QR" begin
using LinearAlgebra, FastAlmostBandedMatrices
import MatrixFactorizations: QRPackedQ

n = 80
A = AlmostBandedMatrix(BandedMatrix(fill(2.0, n, n), (1, 1)), fill(3.0, 2, n))
A[band(0)] .+= 1:n
à = deepcopy(A)
B, L = bandpart(A), fillpart(A)

F = qr(A)
@test F.Q isa LinearAlgebra.QRPackedQ{Float64, <:BandedMatrix}
@test F.R isa UpperTriangular{Float64, <:SubArray{Float64, 2, <:AlmostBandedMatrix}}
@test F.Q' * A ≈ F.R
@test A == Ã

@inferred qr(A)

b = randn(n)
@test A \ b ≈ Matrix(A) \ b
@test all(A \ b .=== F \ b)
@test all(A \ b .=== F.R \ (F.Q' * b))
Q̃ = QRPackedQ(F.factors, F.τ)
@test Matrix(Q̃) ≈ Matrix(F.Q)
@test lmul!(Q̃, copy(b)) ≈ lmul!(F.Q, copy(b)) ≈ Matrix(F.Q) * b
@test lmul!(Q̃', copy(b)) ≈ lmul!(F.Q', copy(b)) ≈ Matrix(F.Q)' * b
end

@safetestset "Triangular" begin
using LinearAlgebra, ArrayLayouts, FastAlmostBandedMatrices
import FastAlmostBandedMatrices: AlmostBandedLayout

n = 80
A = AlmostBandedMatrix(BandedMatrix(fill(2.0, n, n), (1, 1)), fill(3.0, 1, n))
b = randn(n)
@test MemoryLayout(UpperTriangular(A)) ==
TriangularLayout{'U', 'N', AlmostBandedLayout}()
@test_broken UpperTriangular(Matrix(A)) \ b ≈ UpperTriangular(A) \ b
@test_broken UnitUpperTriangular(Matrix(A)) \ b ≈ UnitUpperTriangular(A) \ b
@test LowerTriangular(Matrix(A)) \ b ≈ LowerTriangular(A) \ b
@test UnitLowerTriangular(Matrix(A)) \ b ≈ UnitLowerTriangular(A) \ b
end

# https://github.com/SciML/FastAlmostBandedMatrices.jl/issues/19
@safetestset "fill! on sparse array with BigFloat" begin
using FastAlmostBandedMatrices, SparseArrays

A = sparse([1, 2], [1, 5], big.([1.0, 1.0]))
A1 = AlmostBandedMatrix(brand(BigFloat, 5, 5, 1, 1), A)
fill!(A1, BigFloat(0.0))
@test length(A1.fill.nzval) == 2
end
4 changes: 4 additions & 0 deletions test/qa/Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
FastAlmostBandedMatrices = "9d29842c-ecb8-4973-b1e9-a27b1157504e"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[sources]
FastAlmostBandedMatrices = {path = "../.."}

[compat]
Aqua = "0.8.14"
SafeTestsets = "0.0.1, 0.1"
SciMLTesting = "1"
Test = "1"
julia = "1.10"
120 changes: 2 additions & 118 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,118 +1,2 @@
using Pkg
using SafeTestsets, Test

const GROUP = get(ENV, "GROUP", "All")

if GROUP == "QA"
Pkg.activate(joinpath(@__DIR__, "qa"))
Pkg.develop(PackageSpec(path = joinpath(@__DIR__, "..")))
Pkg.instantiate()
include(joinpath(@__DIR__, "qa", "qa.jl"))
end

if GROUP == "Core" || GROUP == "All"
@testset "FastAlmostBandedMatrices" begin
@safetestset "Constructors" begin
using FastAlmostBandedMatrices

A = AlmostBandedMatrix{Float64}(undef, (10, 11), (2, 1), 2)
A[1, 1] = 2
@test A[1, 1] == 2.0
A[4, 1] = 0
@test A[4, 1] == 0.0
@test_throws BandError A[4, 1] = 2
A[1, 3] = 5
@test A[1, 3] == 5.0

@test almostbandwidths(A) == (2, 1)
@test almostbandedrank(A) == 2
end

@safetestset "similar" begin
using FastAlmostBandedMatrices

A = AlmostBandedMatrix(brand(Float64, 10, 10, 2, 1), rand(Float64, 2, 10))

@test similar(A) isa AlmostBandedMatrix
@test similar(A, Float32) isa AlmostBandedMatrix{Float32}

fallback = similar(A, Float32, 10, 10)
@test fallback isa Matrix{Float32}
@test size(fallback) == size(A)
end

@safetestset "Copy" begin
using FastAlmostBandedMatrices

n = 5
m = 2

A1 = AlmostBandedMatrix(brand(Float64, n, n, m + 1, m), rand(Float64, m, n))
A2 = copy(A1)

@test !(A2 isa Matrix)
@test A1 == A2

A2 = deepcopy(A1)

@test !(A2 isa Matrix)
@test A1 == A2
end

@safetestset "QR" begin
using LinearAlgebra, FastAlmostBandedMatrices
import MatrixFactorizations: QRPackedQ

n = 80
A = AlmostBandedMatrix(BandedMatrix(fill(2.0, n, n), (1, 1)), fill(3.0, 2, n))
A[band(0)] .+= 1:n
à = deepcopy(A)
B, L = bandpart(A), fillpart(A)

F = qr(A)
@test F.Q isa LinearAlgebra.QRPackedQ{Float64, <:BandedMatrix}
@test F.R isa UpperTriangular{Float64, <:SubArray{Float64, 2, <:AlmostBandedMatrix}}
@test F.Q' * A ≈ F.R
@test A == Ã

@inferred qr(A)

b = randn(n)
@test A \ b ≈ Matrix(A) \ b
@test all(A \ b .=== F \ b)
@test all(A \ b .=== F.R \ (F.Q' * b))
Q̃ = QRPackedQ(F.factors, F.τ)
@test Matrix(Q̃) ≈ Matrix(F.Q)
@test lmul!(Q̃, copy(b)) ≈ lmul!(F.Q, copy(b)) ≈ Matrix(F.Q) * b
@test lmul!(Q̃', copy(b)) ≈ lmul!(F.Q', copy(b)) ≈ Matrix(F.Q)' * b
end

@safetestset "Triangular" begin
using LinearAlgebra, ArrayLayouts, FastAlmostBandedMatrices
import FastAlmostBandedMatrices: AlmostBandedLayout

n = 80
A = AlmostBandedMatrix(BandedMatrix(fill(2.0, n, n), (1, 1)), fill(3.0, 1, n))
b = randn(n)
@test MemoryLayout(UpperTriangular(A)) ==
TriangularLayout{'U', 'N', AlmostBandedLayout}()
@test_broken UpperTriangular(Matrix(A)) \ b ≈ UpperTriangular(A) \ b
@test_broken UnitUpperTriangular(Matrix(A)) \ b ≈ UnitUpperTriangular(A) \ b
@test LowerTriangular(Matrix(A)) \ b ≈ LowerTriangular(A) \ b
@test UnitLowerTriangular(Matrix(A)) \ b ≈ UnitLowerTriangular(A) \ b
end

# https://github.com/SciML/FastAlmostBandedMatrices.jl/issues/19
@safetestset "fill! on sparse array with BigFloat" begin
using FastAlmostBandedMatrices, SparseArrays

A = sparse([1, 2], [1, 5], big.([1.0, 1.0]))
A1 = AlmostBandedMatrix(brand(BigFloat, 5, 5, 1, 1), A)
fill!(A1, BigFloat(0.0))
@test length(A1.fill.nzval) == 2
end
end

# Allocation tests run separately to avoid precompilation interference
include("alloc_tests.jl")
end
using SciMLTesting
run_tests()
Loading