Skip to content

Commit

Permalink
Merge pull request kalmarek#11 from kalmarek/fix/broadcasting_1.6
Browse files Browse the repository at this point in the history
Fix/broadcasting 1.6
  • Loading branch information
kalmarek committed Jan 17, 2021
2 parents 706ae68 + dd55d53 commit 58f0f16
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Cyclotomics"
uuid = "da8f5974-afbb-4dc8-91d8-516d5257c83b"
authors = ["Marek Kaluba <kalmar@amu.edu.pl>"]
version = "0.1.6"
version = "0.2.0"

[deps]
LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"
Expand Down
8 changes: 4 additions & 4 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function mul!(
end
zero!(out)

for (αe, αc) in α
for (βe, βc) in β
for (αe, αc) in exps_coeffs(α)
for (βe, βc) in exps_coeffs(β)
out[αe+βe] += αc * βc
end
end
Expand All @@ -98,8 +98,8 @@ end

function Base.conj!(out::Cyclotomic, α::Cyclotomic, n::Integer = -1)
zero!(out)
for (exp, c) in α
out[n*exp] = c
for (e, c) in exps_coeffs(α)
out[n*e] = c
end
return out
end
Expand Down
28 changes: 13 additions & 15 deletions src/cycl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,35 @@ Base.@propagate_inbounds function Base.setindex!(α::Cyclotomic, val, exp::Integ
return val
end

# Base.@propagate_inbounds function Base.setindex!(α::Cyclotomic, val, itr)
# for exp in itr
# α[exp] = val
# end
# return itr
# end
struct ExpCoeffItr{C<:Cyclotomic}
α::C
end

# general definitions for iteration
function Base.iterate(α::Cyclotomic, state = 0)
idx = findnext(!iszero, coeffs(α), state + 1)
function Base.iterate(itr::ExpCoeffItr, state = 0)
idx = findnext(!iszero, coeffs(itr.α), state + 1)
idx === nothing && return nothing
return (idx - 1, coeffs(α)[idx]), idx
return (idx - 1, coeffs(itr.α)[idx]), idx
end

Base.IteratorSize(::Type{<:Cyclotomic}) = Base.HasLength()
Base.length(α::Cyclotomic) = count(!iszero, coeffs(α))
Base.eltype(::Type{<:Cyclotomic{T}}) where {T} = Tuple{Int,T}
Base.IteratorSize(::Type{<:ExpCoeffItr}) = Base.HasLength()
Base.length(itr::ExpCoeffItr) = count(!iszero, coeffs(itr.α))
Base.eltype(::Type{<:ExpCoeffItr{<:Cyclotomic{T}}}) where {T} = Tuple{Int,T}

exps_coeffs::Cyclotomic) = ExpCoeffItr(α)
"""
exponents(α::Cyclotomic)
Return an iterator over non-zero exponents of `α`, beginning at `0`-th one.
Matched iterator over coefficients is provided by @ref(values).
"""
exponents::Cyclotomic) = (first(i) for i in α)
exponents::Cyclotomic) = (e for (e, c) in exps_coeffs(α))

"""
values(α::Cyclotomic)
Return an iterator over non-zero coefficients of `α`, beginning at `0`-th one.
Matched iterator over exponents is provided by @ref(exponents).
"""
Base.values::Cyclotomic) = (last(i) for i in α)
Base.values::Cyclotomic) = (c for (e, c) in exps_coeffs(α))
Base.valtype(::Cyclotomic{T}) where {T} = T

Base.similar::Cyclotomic, T::Type = valtype(α)) = similar(α, T, conductor(α))
Expand All @@ -133,7 +131,7 @@ function Base.Complex{T}(α::Cyclotomic) where {T<:AbstractFloat}
z = zero(Complex{T})
= reduced_embedding(α)
n = conductor(rα)
for (e, c) in
for (e, c) in exps_coeffs(rα)
γ = 2 * T(π) * T(e) / n
z += c * (cos(γ) + im * sin(γ))
end
Expand Down
4 changes: 2 additions & 2 deletions src/normalform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function _tmp_for_reduced_embedding(α::Cyclotomic{T}) where {T}
if k > 1
# the trivial reduction E(n)^e → E(n÷k)^(e÷k)
@debug "Performing trivial reduction from ℚ(ζ$(subscriptify(conductor(α)))) → ℚ(ζ$(subscriptify(conductor(tmp))))"
@inbounds for (e, v) in α
tmp[div(e, k)] = v
@inbounds for (e, c) in exps_coeffs(α)
tmp[div(e, k)] = c
end
else
@debug "No trivial reduction is possible"
Expand Down
14 changes: 9 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ using Cyclotomics
@test x[-3] == 3
@test x[7] == 3

@test collect(E(6)) == [(1,1)]
@test collect(Cyclotomics.normalform!(E(6))) == [(4,-1)]
@test collect(Cyclotomics.exps_coeffs(E(6))) == [(1,1)]
@test collect(Cyclotomics.exps_coeffs(Cyclotomics.normalform!(E(6)))) == [(4,-1)]
end

@testset "aritmetic: +, -, module: *, //" begin
Expand Down Expand Up @@ -150,6 +150,11 @@ using Cyclotomics
Cyclotomics.normalform!(x)
@test isone(div(x-2, 3))

# broadcasting on 1.6 is broken

@test E(3) .* [1,2] == [E(3), 2E(3)]
@test eltype(E(3) .* [1.0,2.0]) <: Cyclotomic{Float64}
@test eltype(1//1*E(3) .* [1,2]) <: Cyclotomic{Rational{Int}}
end

@testset "*, powering" begin
Expand Down Expand Up @@ -281,12 +286,11 @@ using Cyclotomics
@test_throws AssertionError Cyclotomics.galois_conj(x, 5)
end

for x = [E(45)^5 + E(45)^10,
for x in [E(45)^5 + E(45)^10,
E(45) - E(45)^5,
rand1(E(45), -5:5, 3),
rand1(E(45), -1:1, 5)]

@test isone(x*inv(x//big(1)))
iszero(x) || @test isone(x*inv(x//big(1)))
end

for x in [E(45)^5 + big(1)*E(45)^10, (E(45)^5)//1 + big(1)*E(45)^10]
Expand Down

0 comments on commit 58f0f16

Please sign in to comment.