Skip to content

Commit

Permalink
square decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
schillic committed Jan 27, 2020
1 parent 38f9826 commit d6ce981
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/IntervalMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include("matrix.jl")
# Arithmetic for interval matrices
# =================================
include("arithmetic.jl")
include("power.jl")

# =======================================================
# Methods to handle the exponential of an interval matrix
Expand Down
55 changes: 55 additions & 0 deletions src/power.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Base: show

# helper types representing factors

abstract type AbstractFactor{T<:Integer} end

struct NormalFactor{T} <: AbstractFactor{T}
a::T
end

show(io::IO, nf::NormalFactor) = print(io, nf.a)

struct SquaredFactor{T} <: AbstractFactor{T}
a::T
end

show(io::IO, sf::SquaredFactor) = print(io, sf.a, "²")

# square decomposition

function square_decomposition(k::T) where {T<:Integer}
factor2source = Dict{T, Vector{AbstractFactor{T}}}()
square_decomposition!(factor2source, k)
return factor2source
end

function square_decomposition!(factor2source, k::T) where {T<:Integer}
factors = Vector{AbstractFactor{T}}()
@assert !haskey(factor2source, k)
factor2source[k] = factors
a = k
while a > 3
b = floor(Int, sqrt(a))
push!(factors, SquaredFactor(b))
if !haskey(factor2source, b)
square_decomposition!(factor2source, b)
end
a -= b^2
end
if a == 3
push!(factors, SquaredFactor(1))
if !haskey(factor2source, 2)
square_decomposition!(factor2source, 2)
end
push!(factors, NormalFactor(1))
elseif a == 2
push!(factors, SquaredFactor(1))
if !haskey(factor2source, 2)
square_decomposition!(factor2source, 2)
end
elseif a == 1
push!(factors, NormalFactor(1))
end
return factor2source
end
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ end
f = IntervalMatrices.correction_hull(m, 1e-3, 5)
f2 = IntervalMatrices.input_correction(m, 1e-3, 5)
end

@testset "Square decomposition" begin
sd = IntervalMatrices.square_decomposition
SF = IntervalMatrices.SquaredFactor
NF = IntervalMatrices.NormalFactor
@test Dict(1 => [NF(1)]) sd(1)
@test Dict(2 => [SF(1)]) sd(2)
@test Dict(3 => [SF(1), NF(1)], 2 => [SF(1)]) sd(3)
@test Dict(9 => [SF(3)], 3 => [SF(1), NF(1)]) sd(9)
@test Dict(65 => [SF(8), NF(1)], 8 => [SF(2), SF(2)], 2 => [SF(1)]) sd(65)
end

0 comments on commit d6ce981

Please sign in to comment.