From e882a7b84264f690a7072635c813f5a80e6cc7ed Mon Sep 17 00:00:00 2001 From: Brandon Flores Date: Mon, 26 Feb 2024 21:30:41 -0600 Subject: [PATCH] Added conversion methods for some special cases --- src/convert.jl | 13 +++++++++++++ test/conversion.jl | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/convert.jl b/src/convert.jl index 9ccd010..0ae8b9d 100644 --- a/src/convert.jl +++ b/src/convert.jl @@ -9,3 +9,16 @@ end function convert(::Type{T}, x::AbstractCliffordNumber) where T<:BaseNumber return isscalar(x) ? T(x[BitIndex{QuadraticForm(x)}()]) : throw(InexactError(:convert, T, x)) end + +#---Specialized conversion methods for certain representations and signatures----------------------# + +function Base.convert(::Type{T}, x::AbstractCliffordNumber{QFComplex,<:Real}) where T<:BaseNumber + return convert(T, x[BitIndex{QFComplex}()] + x[BitIndex{QFComplex}(1)] * im) +end + +function Base.convert(::Type{T}, z::Complex) where T<:AbstractCliffordNumber{QFComplex,<:Real} + return convert(T, CliffordNumber{QFComplex}(real(z), imag(z))) +end + +# k-vectors of grade 0 are scalars +convert(::Type{T}, k::KVector{0}) where T<:BaseNumber = convert(T, only(k.data)) diff --git a/test/conversion.jl b/test/conversion.jl index dd07405..3223bb1 100644 --- a/test/conversion.jl +++ b/test/conversion.jl @@ -1,7 +1,12 @@ @testset "Conversion" begin + import CliffordNumbers: QFComplex # Conversion of scalar CliffordNumbers to Real subtypes @test convert(Int, CliffordNumber{APS,Float64}(1)) === Int(1) @test_throws InexactError convert(Int, CliffordNumber{APS}(1.5)) + # K-vectors of grade zero are scalars + @test convert(Float64, KVector{0,APS}(3)) === Float64(3) + @test convert(Complex{Float64}, CliffordNumber{QFComplex}(1, 2)) === 1.0 + 2.0im + @test convert(CliffordNumber{QFComplex,Float64}, 1+2im) === CliffordNumber{QFComplex}(1.0, 2.0) end @testset "Promotion" begin