Skip to content

Commit

Permalink
Merge pull request #4 from fkastner/nice_linalg
Browse files Browse the repository at this point in the history
enable and test matrix factorizations
  • Loading branch information
fkastner committed Feb 17, 2021
2 parents 17f9945 + 7aa7b1f commit 2e9782d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/NiceLinAlg.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import LinearAlgebra: Hermitian, Symmetric, UpperTriangular, LowerTriangular
import LinearAlgebra: cholesky!, _chol!, Cholesky, checkpositivedefinite


function cholesky!(A::Union{Hermitian{NiceNumber,S}, Symmetric{NiceNumber,S}} where S, ::Val{false}=Val(false); check::Bool = true)
C, info = _chol!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular)
check && checkpositivedefinite(info)
return Cholesky(C.data, A.uplo, info)
end
7 changes: 6 additions & 1 deletion src/NiceNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import Base: //
import Base: isless, <, <=, ==, hash
import Base: one, zero, isinteger, isfinite
import Base: promote_rule
import Base: isreal, real, imag, conj, abs
import Base: isreal, real, imag, conj, abs, abs2
import LinearAlgebra: norm, norm2
import Base: copysign

export NiceNumber, nice, @nice
export isrational
Expand Down Expand Up @@ -161,8 +162,10 @@ hash(n::NiceNumber, h::UInt) = hash(n.a, hash(n.coeff, hash(n.radicand, hash(:Ni

conj(n::NiceNumber) = isreal(n) ? n : NiceNumber(n.a, -n.coeff, n.radicand)
abs(n::NiceNumber) = isreal(n) ? float(n)>0 ? n : -n : sqrt(n*conj(n))
abs2(n::NiceNumber) = n*conj(n)
norm(n::NiceNumber) = abs(n)
norm2(v::AbstractArray{NiceNumber,1}) = sqrt(v'v)
copysign(n::NiceNumber, m::NiceNumber) = sign(n) == sign(m) ? n : -n

## macro stuff
"""
Expand Down Expand Up @@ -191,4 +194,6 @@ macro nice(code)
return esc(nice(code))
end

include("NiceLinAlg.jl")

end # module
28 changes: 28 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,32 @@ using Test, LinearAlgebra
@test sprint(io -> show(io, NiceNumber(0, -5, 5))) == "-5⋅√5"
@test sprint(io -> show(io, NiceNumber(0, 1 // 2, 5))) == "1//2⋅√5"
end

@testset "Factorizations" begin
@testset "Cholesky" begin
@nice L = [2 0 0 0;0 1 0 0;4 6*sqrt(-1) 9 0;0 4 0 2]
@test L == cholesky(L*L').L
end

@testset "LU" begin
@nice L = [1 0 0;im 1 0; 1/2 -3im 1]
@nice U = [1 1 1;0 1 1; 0 0 1]
LUL, LUU = lu(L*U, Val(false))
@test LUL == L
@test LUU == U

@nice A = [2 2 0;2 2 1; 2 3 5]
L, U, p = lu(A, Val(true))
@test L == @nice [1 0 0;1 1 0;1 0 1]
@test U == @nice [2 2 0;0 1 5;0 0 1]
@test p == [1,3,2]
end

@testset "QR" begin
@nice A = [4 2;0 3//5;0 -4//5;0 0]
Q, R = qr(A)
@test Q == @nice [-1 0 0 0;0 -3/5 4/5 0;0 4/5 3/5 0;0 0 0 1]
@test R == @nice [-4 -2;0 -1]
end
end
end

0 comments on commit 2e9782d

Please sign in to comment.