Skip to content

Commit

Permalink
Add rtol and norm to implement isapprox
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswaudby committed Jul 9, 2023
1 parent 3b1e83a commit 82ea33e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Multicomplex numbers are a generalisation of complex numbers, recursively define
## TODO

* FFT
* The comparison `exp(0im1) == 1.` throws a TypeError


## useful links (notes to self for development)
Expand Down
18 changes: 18 additions & 0 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ Base.widen(::Type{Multicomplex{T,N,C}}) where {T,N,C} = Multicomplex{widen(T),N,
Base.float(::Type{Multicomplex{T,N,C}}) where {T<:AbstractFloat,N,C} = Multicomplex{T,N,C}
Base.float(::Type{Multicomplex{T,N,C}}) where {T,N,C} = Multicomplex{float(T),N,C}

Base.rtoldefault(::Type{Multicomplex{T,N,C}}) where {T,N,C} = Base.rtoldefault(T)
Base.rtoldefault(::Multicomplex{T,N,C}) where {T,N,C} = Base.rtoldefault(T)
function Base.rtoldefault(x::Union{T,Type{T}}, y::Union{S,Type{S}}, atol::Real) where {T<:Multicomplex,S<:Number}
rtol = max(Base.rtoldefault(T),Base.rtoldefault(real(S)))
return atol > 0 ? zero(rtol) : rtol
end
function Base.rtoldefault(x::Union{T,Type{T}}, y::Union{S,Type{S}}, atol::Real) where {T<:Number,S<:Multicomplex}
rtol = max(Base.rtoldefault(real(T)),Base.rtoldefault(S))
return atol > 0 ? zero(rtol) : rtol
end
function Base.rtoldefault(x::Union{T,Type{T}}, y::Union{S,Type{S}}, atol::Real) where {T<:Multicomplex,S<:Multicomplex}
rtol = max(Base.rtoldefault(T),Base.rtoldefault(S))
return atol > 0 ? zero(rtol) : rtol
end

##############
# Components #
Expand Down Expand Up @@ -158,6 +172,10 @@ Base.iszero(m::Multicomplex) = all(iszero.(m.value))
Base.isone(m::Multicomplex) = isreal(m) && isone(m.value[1])


"""Norm is sum of squares of components"""
LinearAlgebra.norm(m::Multicomplex) = norm(m.value)


##########
# Traits #
##########
Expand Down

0 comments on commit 82ea33e

Please sign in to comment.