Skip to content

Commit

Permalink
ilog3 - fast convolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC committed Jan 31, 2024
1 parent 2629941 commit 653052a
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 37 deletions.
14 changes: 11 additions & 3 deletions src/determinant.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,21 @@ function rowdivgcd!(b::AbstractMatrix{D}, i, k, ij, s) where {Z,D<:QuotientRing{
end

"""
_crt(x, y, p, q)
crt(x, y, p, q)
Chinese remainder theorem.
Given `x, y, p, q` with `gcd(p, q) == 1`,
return `0 <= z < lcm(p, q)` with 'mod(z - x, p) == 0` and `mod(z - y, q) == 0`.
The result type is widened to avoid overflows.
"""
function _crt(x, y, p, q)
function crt(x, y, p, q)
g, c = _crt2(widen(x), widen(y), p, q)
mod(c, div(widen(p) * widen(q), g))
end
_crt(x, y, p, q) = _crt2(x, y, p, q)[2]
function _crt2(x, y, p, q)
g, u, v = gcdx(p, q)
y * u * p + x * v * q
g, y * u * p + x * v * q
end
"""
v = splitmod(a, m)
Expand Down
31 changes: 29 additions & 2 deletions src/enumerations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ function _ofindex(
::IsInfinite,
) where {S,P<:UnivariatePolynomial{S}}
if dim < 0
dim = Int(ceil(log2(a+1) / log2(10))) + 2
dim = Int(ceil(log10(a + 1))) + 2
end
P(hypercube(a, dim, EnumPolynomial()))
end

function ofindex(a::Integer, m::Monic{R,X}) where {R,X}
dim = m.n
P = R[X]
ofindex(a, P, dim-1) + monom(P, dim)
ofindex(a, P, dim - 1) + monom(P, dim)
end

struct Factors{T<:Integer,P}
Expand Down Expand Up @@ -620,6 +620,33 @@ function ilog2(a::AbstractFloat)
Integer(floor(log2(a)))
end

"""
ilog3(a::Integer)::Int
For nonzero integers `a > 0` return Int(floor(log(abs(a)) / log(3))) exact arithmethic.
For zero return `-1`
"""
ilog3(a::Integer) = ilog(3, a)

"""
ilog(base::Integer, a::Integer)::Int
For nonzero integers `a > 0` return Int(floor(log(abs(a)) / log(base))) exact arithmethic.
For zero return `-1`
"""
function ilog(base::Integer, a::Integer)
a < base && return a <= 0 ? -one(a) : zero(a)
b = oftype(a, base)
a = div(a, b)
f = max(Integer(floor(log(b, a) + 0.5)), 0)
f3 = b^f
f3 > a ? f : f + 1
end

function ilog(base::Real, a::Real)
Integer(floor(log(base, a)))
end

"""
inv_hypercube(v::AbstractVector{<:Integer}, EnumCube(), [EnumFull(), EnumHalf()])
Expand Down
Loading

0 comments on commit 653052a

Please sign in to comment.