From dd208b42a64b8fa9f05604eb33b8469e6c6a6a70 Mon Sep 17 00:00:00 2001 From: Jutho Haegeman Date: Thu, 21 Jun 2018 23:29:33 +0200 Subject: [PATCH] first attempt to (exclusive) v0.7 compatibility --- REQUIRE | 3 +- src/WignerSymbols.jl | 17 ++---- src/mpz.jl | 116 -------------------------------------- src/primefactorization.jl | 8 +-- test/runtests.jl | 19 +++---- 5 files changed, 16 insertions(+), 147 deletions(-) delete mode 100644 src/mpz.jl diff --git a/REQUIRE b/REQUIRE index 6eb57c1..f6eb0e5 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,3 +1,2 @@ -julia 0.6 +julia 0.7.0-alpha Primes -Compat diff --git a/src/WignerSymbols.jl b/src/WignerSymbols.jl index 57e7595..6db64dc 100644 --- a/src/WignerSymbols.jl +++ b/src/WignerSymbols.jl @@ -2,14 +2,7 @@ __precompile__(true) module WignerSymbols export δ, Δ, clebschgordan, wigner3j, wigner6j, racahV, racahW -if VERSION <= v"0.7.0-DEV.262" - include("mpz.jl") - using .MPZ -else - using Base.GMP.MPZ -end - -using Compat +using Base.GMP.MPZ include("halfinteger.jl") include("primefactorization.jl") @@ -293,8 +286,8 @@ function compute3jseries(β₁, β₂, β₃, α₁, α₂) krange = max(α₁, α₂, zero(α₁)):min(β₁, β₂, β₃) T = PrimeFactorization{eltype(eltype(factorialtable))} - nums = Vector{T}(uninitialized, length(krange)) - dens = Vector{T}(uninitialized, length(krange)) + nums = Vector{T}(undef, length(krange)) + dens = Vector{T}(undef, length(krange)) for (i, k) in enumerate(krange) num = iseven(k) ? one(T) : -one(T) den = primefactorial(k)*primefactorial(k-α₁)*primefactorial(k-α₂)* @@ -312,8 +305,8 @@ function compute6jseries(β₁, β₂, β₃, α₁, α₂, α₃, α₄) krange = max(α₁, α₂, α₃, α₄):min(β₁, β₂, β₃) T = PrimeFactorization{eltype(eltype(factorialtable))} - nums = Vector{T}(uninitialized, length(krange)) - dens = Vector{T}(uninitialized, length(krange)) + nums = Vector{T}(undef, length(krange)) + dens = Vector{T}(undef, length(krange)) for (i, k) in enumerate(krange) num = iseven(k) ? primefactorial(k+1) : -primefactorial(k+1) den = primefactorial(k-α₁)*primefactorial(k-α₂)*primefactorial(k-α₃)* diff --git a/src/mpz.jl b/src/mpz.jl deleted file mode 100644 index a448819..0000000 --- a/src/mpz.jl +++ /dev/null @@ -1,116 +0,0 @@ -# taken from Julia 0.7-Dev base library - -module MPZ -# wrapping of libgmp functions -# - "output parameters" are labeled x, y, z, and are returned when appropriate -# - constant input parameters are labeled a, b, c -# - a method modifying its input has a "!" appendend to its name, according to Julia's conventions -# - some convenient methods are added (in addition to the pure MPZ ones), e.g. `add(a, b) = add!(BigInt(), a, b)` -# and `add!(x, a) = add!(x, x, a)`. -using Base.GMP: BigInt, Limb - -const mpz_t = Ref{BigInt} -const bitcnt_t = Culong - -gmpz(op::Symbol) = (Symbol(:__gmpz_, op), :libgmp) - -init!(x::BigInt) = (ccall((:__gmpz_init, :libgmp), Void, (mpz_t,), x); x) -init2!(x::BigInt, a) = (ccall((:__gmpz_init2, :libgmp), Void, (mpz_t, bitcnt_t), x, a); x) - -realloc2!(x, a) = (ccall((:__gmpz_realloc2, :libgmp), Void, (mpz_t, bitcnt_t), x, a); x) -realloc2(a) = realloc2!(BigInt(), a) - -sizeinbase(a::BigInt, b) = Int(ccall((:__gmpz_sizeinbase, :libgmp), Csize_t, (mpz_t, Cint), a, b)) - -for op in (:add, :sub, :mul, :fdiv_q, :tdiv_q, :fdiv_r, :tdiv_r, :gcd, :lcm, :and, :ior, :xor) - op! = Symbol(op, :!) - @eval begin - $op!(x::BigInt, a::BigInt, b::BigInt) = (ccall($(gmpz(op)), Void, (mpz_t, mpz_t, mpz_t), x, a, b); x) - $op(a::BigInt, b::BigInt) = $op!(BigInt(), a, b) - $op!(x::BigInt, b::BigInt) = $op!(x, x, b) - end -end - -invert!(x::BigInt, a::BigInt, b::BigInt) = - ccall((:__gmpz_invert, :libgmp), Cint, (mpz_t, mpz_t, mpz_t), x, a, b) -invert(a::BigInt, b::BigInt) = invert!(BigInt(), a, b) -invert!(x::BigInt, b::BigInt) = invert!(x, x, b) - -for op in (:add_ui, :sub_ui, :mul_ui, :mul_2exp, :fdiv_q_2exp, :pow_ui, :bin_ui) - op! = Symbol(op, :!) - @eval begin - $op!(x::BigInt, a::BigInt, b) = (ccall($(gmpz(op)), Void, (mpz_t, mpz_t, Culong), x, a, b); x) - $op(a::BigInt, b) = $op!(BigInt(), a, b) - $op!(x::BigInt, b) = $op!(x, x, b) - end -end - -ui_sub!(x::BigInt, a, b::BigInt) = (ccall((:__gmpz_ui_sub, :libgmp), Void, (mpz_t, Culong, mpz_t), x, a, b); x) -ui_sub(a, b::BigInt) = ui_sub!(BigInt(), a, b) - -for op in (:scan1, :scan0) - @eval $op(a::BigInt, b) = Int(ccall($(gmpz(op)), Culong, (mpz_t, Culong), a, b)) -end - -mul_si!(x::BigInt, a::BigInt, b) = (ccall((:__gmpz_mul_si, :libgmp), Void, (mpz_t, mpz_t, Clong), x, a, b); x) -mul_si(a::BigInt, b) = mul_si!(BigInt(), a, b) -mul_si!(x::BigInt, b) = mul_si!(x, x, b) - -for op in (:neg, :com, :sqrt, :set) - op! = Symbol(op, :!) - @eval begin - $op!(x::BigInt, a::BigInt) = (ccall($(gmpz(op)), Void, (mpz_t, mpz_t), x, a); x) - $op(a::BigInt) = $op!(BigInt(), a) - end - op == :set && continue # MPZ.set!(x) would make no sense - @eval $op!(x::BigInt) = $op!(x, x) -end - -for (op, T) in ((:fac_ui, Culong), (:set_ui, Culong), (:set_si, Clong), (:set_d, Cdouble)) - op! = Symbol(op, :!) - @eval begin - $op!(x::BigInt, a) = (ccall($(gmpz(op)), Void, (mpz_t, $T), x, a); x) - $op(a) = $op!(BigInt(), a) - end -end - -popcount(a::BigInt) = Int(ccall((:__gmpz_popcount, :libgmp), Culong, (mpz_t,), a)) - -mpn_popcount(d::Ptr{Limb}, s::Integer) = Int(ccall((:__gmpn_popcount, :libgmp), Culong, (Ptr{Limb}, Csize_t), d, s)) -mpn_popcount(a::BigInt) = mpn_popcount(a.d, abs(a.size)) - -function tdiv_qr!(x::BigInt, y::BigInt, a::BigInt, b::BigInt) - ccall((:__gmpz_tdiv_qr, :libgmp), Void, (mpz_t, mpz_t, mpz_t, mpz_t), x, y, a, b) - x, y -end -tdiv_qr(a::BigInt, b::BigInt) = tdiv_qr!(BigInt(), BigInt(), a, b) - -powm!(x::BigInt, a::BigInt, b::BigInt, c::BigInt) = - (ccall((:__gmpz_powm, :libgmp), Void, (mpz_t, mpz_t, mpz_t, mpz_t), x, a, b, c); x) -powm(a::BigInt, b::BigInt, c::BigInt) = powm!(BigInt(), a, b, c) -powm!(x::BigInt, b::BigInt, c::BigInt) = powm!(x, x, b, c) - -function gcdext!(x::BigInt, y::BigInt, z::BigInt, a::BigInt, b::BigInt) - ccall((:__gmpz_gcdext, :libgmp), Void, (mpz_t, mpz_t, mpz_t, mpz_t, mpz_t), x, y, z, a, b) - x, y, z -end -gcdext(a::BigInt, b::BigInt) = gcdext!(BigInt(), BigInt(), BigInt(), a, b) - -cmp(a::BigInt, b::BigInt) = Int(ccall((:__gmpz_cmp, :libgmp), Cint, (mpz_t, mpz_t), a, b)) -cmp_si(a::BigInt, b) = Int(ccall((:__gmpz_cmp_si, :libgmp), Cint, (mpz_t, Clong), a, b)) -cmp_ui(a::BigInt, b) = Int(ccall((:__gmpz_cmp_ui, :libgmp), Cint, (mpz_t, Culong), a, b)) -cmp_d(a::BigInt, b) = Int(ccall((:__gmpz_cmp_d, :libgmp), Cint, (mpz_t, Cdouble), a, b)) - -mpn_cmp(a::Ptr{Limb}, b::Ptr{Limb}, c) = ccall((:__gmpn_cmp, :libgmp), Cint, (Ptr{Limb}, Ptr{Limb}, Clong), a, b, c) -mpn_cmp(a::BigInt, b::BigInt, c) = mpn_cmp(a.d, b.d, c) - -get_str!(x, a, b::BigInt) = (ccall((:__gmpz_get_str,:libgmp), Ptr{Cchar}, (Ptr{Cchar}, Cint, mpz_t), x, a, b); x) -set_str!(x::BigInt, a, b) = Int(ccall((:__gmpz_set_str, :libgmp), Cint, (mpz_t, Ptr{UInt8}, Cint), x, a, b)) -get_d(a::BigInt) = ccall((:__gmpz_get_d, :libgmp), Cdouble, (mpz_t,), a) - -limbs_write!(x::BigInt, a) = ccall((:__gmpz_limbs_write, :libgmp), Ptr{Limb}, (mpz_t, Clong), x, a) -limbs_finish!(x::BigInt, a) = ccall((:__gmpz_limbs_finish, :libgmp), Void, (mpz_t, Clong), x, a) -import!(x::BigInt, a, b, c, d, e, f) = ccall((:__gmpz_import, :libgmp), Void, - (mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Void}), x, a, b, c, d, e, f) - -end # module MPZ diff --git a/src/primefactorization.jl b/src/primefactorization.jl index 6edba28..a534858 100644 --- a/src/primefactorization.jl +++ b/src/primefactorization.jl @@ -12,8 +12,8 @@ struct PrimeIterator end primes() = PrimeIterator() -Compat.IteratorSize(::Type{PrimeIterator}) = Base.IsInfinite() -Compat.IteratorEltype(::Type{PrimeIterator}) = Base.HasEltype() +Base.IteratorSize(::Type{PrimeIterator}) = Base.IsInfinite() +Base.IteratorEltype(::Type{PrimeIterator}) = Base.HasEltype() Base.eltype(::PrimeIterator) = Int # Get the `n`th prime; store all primes up to the `n`th if not yet available @@ -30,9 +30,7 @@ function prime(n::Int) @inbounds return primetable[n] end -Base.start(::PrimeIterator) = 1 -Base.next(::PrimeIterator, n) = prime(n), n+1 -Base.done(::PrimeIterator, n) = false +Base.iterate(::PrimeIterator, n = 1) = prime(n), n+1 # get primes and their powers as `BigInt`, also cache all results function bigprime(n::Integer, e::Integer=1) diff --git a/test/runtests.jl b/test/runtests.jl index cb65ab5..88e9fc4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,11 +1,6 @@ -if VERSION < v"0.7.0-DEV.2005" - const Test = Base.Test -end - using Test using WignerSymbols -using Compat -using Compat.LinearAlgebra +using LinearAlgebra smalljlist = 0:1//2:10 largejlist = 0:1//2:1000 @@ -13,8 +8,8 @@ largejlist = 0:1//2:1000 @testset "triangle coefficient" begin for j1 in smalljlist, j2 in smalljlist for j3 = abs(j1-j2):(j1+j2) - @test Δ(j1,j2,j3) ≈ sqrt(factorial(float(j1+j2-j3))*factorial(float(j1-j2+j3))* - factorial(float(j2+j3-j1))/factorial(float(j1+j2+j3+1))) + @test Δ(j1,j2,j3) ≈ sqrt(factorial(big(Int(j1+j2-j3)))*factorial(big(Int(j1-j2+j3)))* + factorial(big(Int(j2+j3-j1)))/factorial(big(Int(j1+j2+j3+1)))) end end end @@ -25,7 +20,7 @@ end for j1 in smalljlist, j2 in smalljlist d1 = 2*j1+1 d2 = 2*j2+1 - M = zeros(d1*d2, d1*d2) + M = zeros(Float64, (d1*d2, d1*d2)) ind1 = 1 for m1 in -j1:j1, m2 in -j2:j2 ind2 = 1 @@ -67,7 +62,7 @@ end j6range = max(abs(j2-j4),abs(j1-j5)):min((j2+j4),(j1+j5)) j3range = max(abs(j1-j2),abs(j4-j5)):min((j1+j2),(j4+j5)) @test length(j6range) == length(j3range) - M = zeros(length(j3range),length(j6range)) + M = zeros(Float64, (length(j3range), length(j6range))) for (k2,j6) in enumerate(j6range) for (k1,j3) in enumerate(j3range) M[k1,k2] = sqrt(2*j3+1)*sqrt(2*j6+1)*wigner6j(j1,j2,j3,j4,j5,j6) @@ -115,8 +110,8 @@ end m1range = -j1:j1 m2range = -j2:j2 m3range = -j3:j3 - V1 = Array{Float64}(uninitialized, length(m1range),length(m2range),length(m3range)) - V2 = Array{Float64}(uninitialized, length(m1range),length(m2range),length(m3range)) + V1 = Array{Float64}(undef, length(m1range),length(m2range),length(m3range)) + V2 = Array{Float64}(undef, length(m1range),length(m2range),length(m3range)) for J in max(abs(j1-j2-j3),abs(j1-j2+j3),abs(j1+j2-j3)):(j1+j2+j3) J12range = max(abs(j1-j2),abs(J-j3)):min((j1+j2),(J+j3)) J23range = max(abs(j2-j3),abs(j1-J)):min((j2+j3),(j1+J))