Partially revamp the HalfInteger type (#4)

* Call the HalfInteger field twofold

Makes it more immediately obvious what the meaning of the value stored
in the field is.

* Introduce new constructors for HalfInteger

The primary inner constructor mirrors the two-argument constructor of
the Rational type, where the user provides the numerator and denominator
values.

There is also a single argument outer constructor that makes HalfInteger
behave like a normal numeric type such that HalfInteger(n) == n.

* Move HalfInteger tests to a separate file

The using statements in halfinteger.jl are there so that it would be
possible to run the file separately from the other tests.

* Test the single-argument HalfInteger constructor

* Organize halfinteger.jl a bit

Prioritise the convert methods.

* Add multiplication with integer to HalfInteger

* Implement parsing and printing for HalfInteger

* parse(::HalfInteger, x) method
* Overload show to pretty-print HalfInteger

* Overload Base.numerator/denominator

And add tests for the other supplementary functions and methods as
well.

* Add HalfIntegerRange type

Can be constructed using the range operator :. Currently only supports
unit steps in the positive direction.

* Address feedback

* Rename .twofold -> .numerator
* Consistent variable names
* Remove unnecessary methods for HalfIntegerRange

* Allow constructing HalfIntegerRange with non-integer difference

* Add docs and ceil(::HalfInteger)
This commit is contained in:
Morten Piibeleht 2019-01-11 09:50:46 +13:00 committed by Jutho
parent 80038db6a3
commit 8ebb2c791b
4 changed files with 341 additions and 46 deletions

View file

@ -2,37 +2,11 @@ using Test
using WignerSymbols
using LinearAlgebra
using WignerSymbols: HalfInteger
include("halfinteger.jl")
smalljlist = 0:1//2:10
largejlist = 0:1//2:1000
@testset "HalfInteger" begin
@test convert(HalfInteger, 2) == HalfInteger(4)
@test convert(HalfInteger, 1//2) == HalfInteger(1)
@test convert(HalfInteger, 1.5) == HalfInteger(3)
@test_throws InexactError convert(HalfInteger, 1//3)
@test_throws InexactError convert(HalfInteger, 0.6)
@test convert(HalfInteger, 2) == 2
@test convert(HalfInteger, 1//2) == 1//2
@test convert(HalfInteger, 1.5) == 1.5
@test_throws InexactError convert(Integer, HalfInteger(1))
a = HalfInteger(4)
b = HalfInteger(3)
@test a + b == 2 + 3//2
@test a - b == 2 - 3//2
@test zero(a) == 0
@test one(a) == 1
@test a > b
@test b < a
@test b <= a
@test a >= b
@test a == a
@test a != b
@test hash(a) == hash(2)
@test hash(b) == hash(1.5)
@test hash(b) == hash(3//2)
end
@testset "triangle coefficient" begin
for j1 in smalljlist, j2 in smalljlist
for j3 = abs(j1-j2):(j1+j2)