Skip to content

Commit

Permalink
Added conversion methods for some special cases
Browse files Browse the repository at this point in the history
  • Loading branch information
brainandforce committed Feb 27, 2024
1 parent ddd4df9 commit e882a7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 5 additions & 0 deletions test/conversion.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e882a7b

Please sign in to comment.