Skip to content

Commit

Permalink
fix a complex edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
fkastner committed Feb 10, 2021
1 parent f130b7c commit fcdb52e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/NiceNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Base: <, <=, ==, hash
import Base: one, zero, isinteger, isfinite
import Base: promote_rule
import LinearAlgebra: norm, norm2
import Base: conj

export NiceNumber, nice, @nice
export isrational
Expand Down Expand Up @@ -35,6 +36,7 @@ NiceNumber(a, coeff, radicand::Rational) =
NiceNumber(a, coeff // denominator(radicand), numerator(radicand) * denominator(radicand))
NiceNumber(x::T) where {T<:Union{Integer,Rational}} = NiceNumber(x, 0, 0)
NiceNumber(x::AbstractFloat) = NiceNumber(rationalize(Int, x), 0, 0)
NiceNumber(z::Complex{T}) where T<:Real = NiceNumber(z.re) + NiceNumber(0,z.im,-1)
NiceNumber(n::NiceNumber) = n

"""
Expand Down Expand Up @@ -82,8 +84,8 @@ function Base.show(io::IO, n::NiceNumber)
print(
io,
iszero(n.a) ? "" : pretty(n.a),
iszero(n.a) ? "" : " + ",
isone(n.coeff) ? "" : pretty(n.coeff),
iszero(n.a) ? "" : n.coeff>0 ? " + " : " - ",
isone(n.coeff) ? "" : pretty(abs(n.coeff)),
isone(n.coeff) ? "" : "",
"",
n.radicand,
Expand All @@ -107,9 +109,16 @@ end
-(n::NiceNumber, m::NiceNumber) = n + (-m)

function *(n::NiceNumber, m::NiceNumber)
return NiceNumber(m.a * n.a, m.a * n.coeff, n.radicand) +
NiceNumber(0, m.coeff * n.a, m.radicand) +
NiceNumber(0, n.coeff * m.coeff, n.radicand * m.radicand)
# if both have a complex part we need a minus in the last term
if n.radicand < 0 && m.radicand < 0
return NiceNumber(m.a * n.a, m.a * n.coeff, n.radicand) +
NiceNumber(0, m.coeff * n.a, m.radicand) +
NiceNumber(0, -n.coeff * m.coeff, n.radicand * m.radicand)
else
return NiceNumber(m.a * n.a, m.a * n.coeff, n.radicand) +
NiceNumber(0, m.coeff * n.a, m.radicand) +
NiceNumber(0, n.coeff * m.coeff, n.radicand * m.radicand)
end
end

inv(n::NiceNumber) = NiceNumber(n.a, -n.coeff, n.radicand) * inv(n.a^2 - n.coeff^2 * n.radicand)
Expand Down Expand Up @@ -144,6 +153,7 @@ hash(n::NiceNumber, h::UInt) = hash(n.a, hash(n.coeff, hash(n.radicand, hash(:Ni

norm(n::NiceNumber) = n > 0 ? n : -n
norm2(v::AbstractArray{NiceNumber,1}) = sqrt(v'v)
conj(n::NiceNumber) = n.radicand >= 0 ? n : NiceNumber(n.a, -n.coeff, n.radicand)

## macro stuff
"""
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using Test, LinearAlgebra
n = NiceNumber(2, 3, 5)
m = NiceNumber(5, -3, 5)
k = NiceNumber(8, 4, 3)
c = NiceNumber(0, 1, -5)

@testset "Basic Arithmetic" begin
@test n + m === NiceNumber(7)
Expand All @@ -27,6 +28,7 @@ using Test, LinearAlgebra
@test_broken n * k
@test n / m === NiceNumber(-11 // 4, -21 // 20, 5)
@test_broken n / k
@test c * c === NiceNumber(-5)
end

@testset "Scalar Arithmetic" begin
Expand All @@ -42,7 +44,6 @@ using Test, LinearAlgebra
@test NiceNumbers.nthroot(NiceNumber(81), 8) === NiceNumber(0, 1, 3)
@test NiceNumber(4)^(3 // 2) === NiceNumber(8)
@test 4^NiceNumber(3 // 2) === NiceNumber(8)

end

@testset "Rational Arithmetic" begin
Expand Down

0 comments on commit fcdb52e

Please sign in to comment.